Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom network for etcd and pulsar #468

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
47 changes: 32 additions & 15 deletions testhelper/docker/resource/pulsar/pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,52 @@ import (
)

type Resource struct {
URL string
AdminURL string
URL string
AdminURL string
URLSameNetwork string
mihir20 marked this conversation as resolved.
Show resolved Hide resolved
fracasula marked this conversation as resolved.
Show resolved Hide resolved
}

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 +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
}
Loading