Skip to content

Commit

Permalink
feat: custom network for etcd and pulsar (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
fracasula authored May 20, 2024
1 parent 816ce79 commit 71f4ea7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
52 changes: 44 additions & 8 deletions testhelper/docker/resource/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
etcd "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"

Expand All @@ -15,13 +16,40 @@ import (
type Resource struct {
Client *etcd.Client
Hosts []string
Port int
// HostsInNetwork is the list of ETCD hosts accessible from the provided Docker network (if any).
HostsInNetwork []string
Port int
}

func Setup(pool *dockertest.Pool, cln resource.Cleaner) (*Resource, error) {
etcdImage := "bitnami/etcd"
container, err := pool.Run(etcdImage, "3.5", []string{
"ALLOW_NONE_AUTHENTICATION=yes",
type config struct {
network *docker.Network
}

type Option func(*config)

func WithNetwork(network *docker.Network) Option {
return func(c *config) {
c.network = network
}
}

func Setup(pool *dockertest.Pool, cln resource.Cleaner, opts ...Option) (*Resource, error) {
var c config
for _, opt := range opts {
opt(&c)
}

var networkID string
if c.network != nil {
networkID = c.network.ID
}
container, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "bitnami/etcd",
Tag: "3.5",
NetworkID: networkID,
Env: []string{
"ALLOW_NONE_AUTHENTICATION=yes",
},
})
if err != nil {
return nil, fmt.Errorf("could not create container: %v", err)
Expand Down Expand Up @@ -59,9 +87,17 @@ func Setup(pool *dockertest.Pool, cln resource.Cleaner) (*Resource, error) {
return nil, fmt.Errorf("could not connect to dockerized ETCD: %v", err)
}

var hostsInNetwork []string
if c.network != nil {
hostsInNetwork = []string{
"http://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":2379",
}
}

return &Resource{
Client: etcdClient,
Hosts: etcdHosts,
Port: etcdPort,
Client: etcdClient,
Hosts: etcdHosts,
HostsInNetwork: hostsInNetwork,
Port: etcdPort,
}, nil
}
23 changes: 17 additions & 6 deletions testhelper/docker/resource/pulsar/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package pulsar

type Opt func(*Config)
import (
"github.com/ory/dockertest/v3/docker"
)

func WithTag(tag string) Opt {
return func(c *Config) {
c.Tag = tag
type Option func(*config)

func WithTag(tag string) Option {
return func(c *config) {
c.tag = tag
}
}

func WithNetwork(network *docker.Network) Option {
return func(c *config) {
c.network = network
}
}

type Config struct {
Tag string
type config struct {
tag string
network *docker.Network
}
44 changes: 31 additions & 13 deletions testhelper/docker/resource/pulsar/pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,51 @@ import (
type Resource struct {
URL string
AdminURL string
// URLInNetwork is the URL accessible from the provided Docker network (if any).
URLInNetwork string
}

func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...Opt) (*Resource, error) {
c := &Config{
Tag: "3.1.2",
func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...Option) (*Resource, error) {
c := &config{
tag: "3.2.2",
}
for _, opt := range opts {
opt(c)
}
cmd := []string{"bin/pulsar", "standalone"}

pulsarContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
var networkID string
if c.network != nil {
networkID = c.network.ID
}
container, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "apachepulsar/pulsar",
Tag: c.Tag,
Tag: c.tag,
Env: []string{},
ExposedPorts: []string{"6650", "8080"},
Cmd: cmd,
Cmd: []string{"bin/pulsar", "standalone"},
NetworkID: networkID,
})
if err != nil {
return nil, err
}

d.Cleanup(func() {
if err := pool.Purge(pulsarContainer); err != nil {
if err := pool.Purge(container); err != nil {
d.Log("Could not purge resource:", err)
}
})

url := fmt.Sprintf("pulsar://localhost:%s", pulsarContainer.GetPort("6650/tcp"))
adminURL := fmt.Sprintf("http://localhost:%s", pulsarContainer.GetPort("8080/tcp"))
url := fmt.Sprintf("pulsar://localhost:%s", container.GetPort("6650/tcp"))
adminURL := fmt.Sprintf("http://localhost:%s", container.GetPort("8080/tcp"))

if err := pool.Retry(func() (err error) {
var w bytes.Buffer
code, err := pulsarContainer.Exec([]string{"sh", "-c", "curl -I http://localhost:8080/admin/v2/namespaces/public/default | grep '200' || exit 1"}, dockertest.ExecOptions{StdOut: &w, StdErr: &w})
code, err := container.Exec(
[]string{
"sh", "-c", "curl -I http://localhost:8080/admin/v2/namespaces/public/default | grep '200' || exit 1",
},
dockertest.ExecOptions{StdOut: &w, StdErr: &w},
)
if err != nil {
return err
}
Expand All @@ -56,8 +67,15 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...Opt) (*Resource, e
}); err != nil {
return nil, err
}

var urlInNetwork string
if c.network != nil {
urlInNetwork = "pulsar://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":6650"
}

return &Resource{
URL: url,
AdminURL: adminURL,
URL: url,
AdminURL: adminURL,
URLInNetwork: urlInNetwork,
}, nil
}

0 comments on commit 71f4ea7

Please sign in to comment.