From 5cca7a49264eed2388ea6c13c3023695fb05e4e6 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Fri, 17 May 2024 16:22:45 +0200 Subject: [PATCH 1/5] feat: etcd network support --- testhelper/docker/resource/etcd/etcd.go | 55 ++++++++++++++++++++----- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/testhelper/docker/resource/etcd/etcd.go b/testhelper/docker/resource/etcd/etcd.go index 3a2fe2ea..dd9193ed 100644 --- a/testhelper/docker/resource/etcd/etcd.go +++ b/testhelper/docker/resource/etcd/etcd.go @@ -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" @@ -13,15 +14,41 @@ import ( ) type Resource struct { - Client *etcd.Client - Hosts []string - Port int + Client *etcd.Client + Hosts []string + 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) @@ -59,9 +86,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 } From 064f6667d2fd110d72c69a8e6d9740303be1463b Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Fri, 17 May 2024 16:33:29 +0200 Subject: [PATCH 2/5] feat: pulsar network support --- testhelper/docker/resource/pulsar/config.go | 23 +++++++--- testhelper/docker/resource/pulsar/pulsar.go | 47 ++++++++++++++------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/testhelper/docker/resource/pulsar/config.go b/testhelper/docker/resource/pulsar/config.go index c5c73f0e..622689dd 100644 --- a/testhelper/docker/resource/pulsar/config.go +++ b/testhelper/docker/resource/pulsar/config.go @@ -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 } diff --git a/testhelper/docker/resource/pulsar/pulsar.go b/testhelper/docker/resource/pulsar/pulsar.go index 776232ea..5141bffd 100644 --- a/testhelper/docker/resource/pulsar/pulsar.go +++ b/testhelper/docker/resource/pulsar/pulsar.go @@ -10,42 +10,52 @@ import ( ) type Resource struct { - URL string - AdminURL string + URL string + AdminURL string + URLSameNetwork 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 } @@ -56,8 +66,15 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...Opt) (*Resource, e }); err != nil { return nil, err } + + var urlSameNetwork string + if c.network != nil { + urlSameNetwork = "pulsar://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":6650" + } + return &Resource{ - URL: url, - AdminURL: adminURL, + URL: url, + AdminURL: adminURL, + URLSameNetwork: urlSameNetwork, }, nil } From bfa9120770e109df6a2417dc02bdd38e0d8e13d5 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Fri, 17 May 2024 16:41:57 +0200 Subject: [PATCH 3/5] chore: renaming variables --- testhelper/docker/resource/etcd/etcd.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/testhelper/docker/resource/etcd/etcd.go b/testhelper/docker/resource/etcd/etcd.go index dd9193ed..0270535c 100644 --- a/testhelper/docker/resource/etcd/etcd.go +++ b/testhelper/docker/resource/etcd/etcd.go @@ -14,10 +14,10 @@ import ( ) type Resource struct { - Client *etcd.Client - Hosts []string - HostsInNetwork []string - Port int + Client *etcd.Client + Hosts []string + HostsSameNetwork []string + Port int } type config struct { @@ -86,17 +86,17 @@ func Setup(pool *dockertest.Pool, cln resource.Cleaner, opts ...Option) (*Resour return nil, fmt.Errorf("could not connect to dockerized ETCD: %v", err) } - var hostsInNetwork []string + var hostsSameNetwork []string if c.network != nil { - hostsInNetwork = []string{ + hostsSameNetwork = []string{ "http://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":2379", } } return &Resource{ - Client: etcdClient, - Hosts: etcdHosts, - HostsInNetwork: hostsInNetwork, - Port: etcdPort, + Client: etcdClient, + Hosts: etcdHosts, + HostsSameNetwork: hostsSameNetwork, + Port: etcdPort, }, nil } From 71dc501b9237aabcbcaa804aa32184f0ef635cd4 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Mon, 20 May 2024 10:04:32 +0200 Subject: [PATCH 4/5] chore: rename variable + comment --- testhelper/docker/resource/etcd/etcd.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/testhelper/docker/resource/etcd/etcd.go b/testhelper/docker/resource/etcd/etcd.go index 0270535c..9f3c886a 100644 --- a/testhelper/docker/resource/etcd/etcd.go +++ b/testhelper/docker/resource/etcd/etcd.go @@ -14,10 +14,11 @@ import ( ) type Resource struct { - Client *etcd.Client - Hosts []string - HostsSameNetwork []string - Port int + Client *etcd.Client + Hosts []string + // HostsInNetwork is the list of ETCD hosts accessible from the provided Docker network (if any). + HostsInNetwork []string + Port int } type config struct { @@ -86,17 +87,17 @@ func Setup(pool *dockertest.Pool, cln resource.Cleaner, opts ...Option) (*Resour return nil, fmt.Errorf("could not connect to dockerized ETCD: %v", err) } - var hostsSameNetwork []string + var hostsInNetwork []string if c.network != nil { - hostsSameNetwork = []string{ + hostsInNetwork = []string{ "http://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":2379", } } return &Resource{ - Client: etcdClient, - Hosts: etcdHosts, - HostsSameNetwork: hostsSameNetwork, - Port: etcdPort, + Client: etcdClient, + Hosts: etcdHosts, + HostsInNetwork: hostsInNetwork, + Port: etcdPort, }, nil } From 350ffb029fedc2849664501e67b96ac2677eede6 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Mon, 20 May 2024 10:58:43 +0200 Subject: [PATCH 5/5] chore: rename variable + comment --- testhelper/docker/resource/pulsar/pulsar.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/testhelper/docker/resource/pulsar/pulsar.go b/testhelper/docker/resource/pulsar/pulsar.go index 5141bffd..84719623 100644 --- a/testhelper/docker/resource/pulsar/pulsar.go +++ b/testhelper/docker/resource/pulsar/pulsar.go @@ -10,9 +10,10 @@ import ( ) type Resource struct { - URL string - AdminURL string - URLSameNetwork string + 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 ...Option) (*Resource, error) { @@ -67,14 +68,14 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...Option) (*Resource return nil, err } - var urlSameNetwork string + var urlInNetwork string if c.network != nil { - urlSameNetwork = "pulsar://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":6650" + urlInNetwork = "pulsar://" + container.GetIPInNetwork(&dockertest.Network{Network: c.network}) + ":6650" } return &Resource{ - URL: url, - AdminURL: adminURL, - URLSameNetwork: urlSameNetwork, + URL: url, + AdminURL: adminURL, + URLInNetwork: urlInNetwork, }, nil }