Skip to content

Commit ae0c648

Browse files
authored
chore(couchbase|etcd|firestore|mcpgateway|eventhubs|servicebus): apply consistent pattern for options (#3447)
* chore(couchbase): apply consistent patterns for options * chore(etcd): apply consistent patterns for options * chore(gcloud/firestore): apply consistent patterns for options * chore(dockermcpgateway): apply consistent patterns for options * chore(azure/eventhubs): apply consistent patterns for options * chore(azure/servicebus): apply consistent patterns for options * fix: proper error message
1 parent d7bf834 commit ae0c648

File tree

6 files changed

+82
-67
lines changed

6 files changed

+82
-67
lines changed

modules/azure/eventhubs/eventhubs.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,7 @@ func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.Termin
6868

6969
// Run creates an instance of the Azure Event Hubs container type
7070
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
71-
moduleOpts := []testcontainers.ContainerCustomizer{
72-
testcontainers.WithExposedPorts(defaultAMPQPort, defaultHTTPPort),
73-
testcontainers.WithWaitStrategy(wait.ForAll(
74-
wait.ForListeningPort(defaultAMPQPort),
75-
wait.ForListeningPort(defaultHTTPPort),
76-
wait.ForHTTP("/health").WithPort(defaultHTTPPort).WithStatusCodeMatcher(func(status int) bool {
77-
return status == http.StatusOK
78-
}),
79-
)),
80-
}
81-
71+
// Process custom options first to extract settings
8272
defaultOptions := defaultOptions()
8373
for _, opt := range opts {
8474
if o, ok := opt.(Option); ok {
@@ -90,6 +80,18 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
9080

9181
c := &Container{azuriteOptions: &defaultOptions}
9282

83+
// Build moduleOpts with defaults
84+
moduleOpts := []testcontainers.ContainerCustomizer{
85+
testcontainers.WithExposedPorts(defaultAMPQPort, defaultHTTPPort),
86+
testcontainers.WithWaitStrategy(wait.ForAll(
87+
wait.ForListeningPort(defaultAMPQPort),
88+
wait.ForListeningPort(defaultHTTPPort),
89+
wait.ForHTTP("/health").WithPort(defaultHTTPPort).WithStatusCodeMatcher(func(status int) bool {
90+
return status == http.StatusOK
91+
}),
92+
)),
93+
}
94+
9395
if defaultOptions.azuriteContainer == nil {
9496
azuriteNetwork, err := network.New(ctx)
9597
if err != nil {

modules/azure/servicebus/servicebus.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.Termin
7373

7474
// Run creates an instance of the Azure ServiceBus container type
7575
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
76+
// Process custom options first to extract settings
77+
defaultOptions := defaultOptions()
78+
for _, opt := range opts {
79+
if o, ok := opt.(Option); ok {
80+
if err := o(&defaultOptions); err != nil {
81+
return nil, fmt.Errorf("servicebus option: %w", err)
82+
}
83+
}
84+
}
85+
86+
c := &Container{mssqlOptions: &defaultOptions}
87+
88+
// Build moduleOpts with defaults
7689
moduleOpts := []testcontainers.ContainerCustomizer{
7790
testcontainers.WithExposedPorts(defaultPort, defaultHTTPPort),
7891
testcontainers.WithEnv(map[string]string{
@@ -87,17 +100,6 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
87100
)),
88101
}
89102

90-
defaultOptions := defaultOptions()
91-
for _, opt := range opts {
92-
if o, ok := opt.(Option); ok {
93-
if err := o(&defaultOptions); err != nil {
94-
return nil, fmt.Errorf("servicebus option: %w", err)
95-
}
96-
}
97-
}
98-
99-
c := &Container{mssqlOptions: &defaultOptions}
100-
101103
if defaultOptions.mssqlContainer == nil {
102104
mssqlNetwork, err := network.New(ctx)
103105
if err != nil {

modules/couchbase/couchbase.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,16 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7272
indexStorageMode: MemoryOptimized,
7373
}
7474

75-
moduleOpts := []testcontainers.ContainerCustomizer{
76-
testcontainers.WithExposedPorts(MGMT_PORT+"/tcp", MGMT_SSL_PORT+"/tcp"),
77-
}
78-
79-
serviceCustomizers := make([]testcontainers.ContainerCustomizer, 0, len(initialServices))
75+
// Process custom options first to extract config
76+
// Start with initial services
77+
allCustomizers := make([]testcontainers.ContainerCustomizer, 0, len(initialServices)+len(opts))
8078
for _, srv := range initialServices {
81-
serviceCustomizers = append(serviceCustomizers, withService(srv))
79+
allCustomizers = append(allCustomizers, withService(srv))
8280
}
81+
allCustomizers = append(allCustomizers, opts...)
8382

84-
serviceCustomizers = append(serviceCustomizers, opts...)
85-
86-
// transfer options to the config
87-
for _, opt := range serviceCustomizers {
83+
// Transfer custom options to the config
84+
for _, opt := range allCustomizers {
8885
if bucketCustomizer, ok := opt.(bucketCustomizer); ok {
8986
// If the option is a bucketCustomizer, we need to add the buckets to the request
9087
config.buckets = append(config.buckets, bucketCustomizer.buckets...)
@@ -100,12 +97,18 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
10097
config.password = credentialsCustomizer.password
10198

10299
if len(credentialsCustomizer.password) < 6 {
103-
return nil, errors.New("admin password must be at most 6 characters long")
100+
return nil, errors.New("admin password must be at least 6 characters long")
104101
}
105102
}
106103
}
107104

108-
moduleOpts = append(moduleOpts, serviceCustomizers...)
105+
// Build moduleOpts with defaults
106+
moduleOpts := []testcontainers.ContainerCustomizer{
107+
testcontainers.WithExposedPorts(MGMT_PORT+"/tcp", MGMT_SSL_PORT+"/tcp"),
108+
}
109+
110+
// Append all customizers (initial services + user opts)
111+
moduleOpts = append(moduleOpts, allCustomizers...)
109112

110113
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
111114
var couchbaseContainer *CouchbaseContainer

modules/dockermcpgateway/dockermcpgateway.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,7 @@ type Container struct {
2727
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
2828
dockerHostMount := core.MustExtractDockerSocket(ctx)
2929

30-
moduleOpts := []testcontainers.ContainerCustomizer{
31-
testcontainers.WithExposedPorts(defaultPort),
32-
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
33-
hc.Binds = []string{
34-
dockerHostMount + ":/var/run/docker.sock",
35-
}
36-
}),
37-
testcontainers.WithWaitStrategy(wait.ForAll(
38-
wait.ForListeningPort(defaultPort),
39-
wait.ForLog(".*Start sse server on port.*").AsRegexp(),
40-
)),
41-
}
42-
30+
// Process custom options first to extract settings
4331
settings := defaultOptions()
4432
for _, opt := range opts {
4533
if apply, ok := opt.(Option); ok {
@@ -56,6 +44,21 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5644
cmds = append(cmds, "--tools="+tool)
5745
}
5846
}
47+
48+
// Build moduleOpts with defaults
49+
moduleOpts := []testcontainers.ContainerCustomizer{
50+
testcontainers.WithExposedPorts(defaultPort),
51+
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
52+
hc.Binds = []string{
53+
dockerHostMount + ":/var/run/docker.sock",
54+
}
55+
}),
56+
testcontainers.WithWaitStrategy(wait.ForAll(
57+
wait.ForListeningPort(defaultPort),
58+
wait.ForLog(".*Start sse server on port.*").AsRegexp(),
59+
)),
60+
}
61+
5962
if len(settings.secrets) > 0 {
6063
cmds = append(cmds, "--secrets="+secretsPath)
6164

@@ -73,7 +76,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7376

7477
moduleOpts = append(moduleOpts, testcontainers.WithCmd(cmds...))
7578

76-
// append user-defined options
79+
// Append user options
7780
moduleOpts = append(moduleOpts, opts...)
7881

7982
container, err := testcontainers.Run(ctx, img, moduleOpts...)

modules/etcd/etcd.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ func (c *EtcdContainer) Terminate(ctx context.Context, opts ...testcontainers.Te
6060

6161
// Run creates an instance of the etcd container type
6262
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*EtcdContainer, error) {
63-
moduleOpts := []testcontainers.ContainerCustomizer{
64-
testcontainers.WithExposedPorts(clientPort, peerPort),
65-
}
66-
67-
moduleOpts = append(moduleOpts, opts...)
68-
63+
// Process custom options first to extract settings
6964
settings := defaultOptions()
7065
for _, opt := range opts {
7166
if apply, ok := opt.(Option); ok {
@@ -80,6 +75,14 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
8075
return nil, fmt.Errorf("configure cluster: %w", err)
8176
}
8277

78+
// Build moduleOpts with defaults
79+
moduleOpts := []testcontainers.ContainerCustomizer{
80+
testcontainers.WithExposedPorts(clientPort, peerPort),
81+
}
82+
83+
// Append user options
84+
moduleOpts = append(moduleOpts, opts...)
85+
8386
// configure CMD with the nodes
8487
moduleOpts = append(moduleOpts, testcontainers.WithCmd(configureCMD(settings)...))
8588

modules/gcloud/firestore/firestore.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ func (c *Container) URI() string {
3434
// Run creates an instance of the Firestore GCloud container type.
3535
// The URI uses the empty string as the protocol.
3636
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
37-
moduleOpts := []testcontainers.ContainerCustomizer{
38-
testcontainers.WithExposedPorts(defaultPort),
39-
testcontainers.WithWaitStrategy(wait.ForAll(
40-
wait.ForListeningPort(defaultPort),
41-
wait.ForLog("running"),
42-
)),
43-
}
44-
37+
// Process custom options first to extract settings
4538
settings := defaultOptions()
4639
for _, opt := range opts {
4740
if apply, ok := opt.(Option); ok {
@@ -56,12 +49,21 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5649
gcloudParameters += " --database-mode=datastore-mode"
5750
}
5851

59-
moduleOpts = append(moduleOpts, testcontainers.WithCmd(
60-
"/bin/sh",
61-
"-c",
62-
"gcloud beta emulators firestore start --host-port 0.0.0.0:"+defaultPortNumber+" "+gcloudParameters,
63-
))
52+
// Build moduleOpts with defaults
53+
moduleOpts := []testcontainers.ContainerCustomizer{
54+
testcontainers.WithExposedPorts(defaultPort),
55+
testcontainers.WithWaitStrategy(wait.ForAll(
56+
wait.ForListeningPort(defaultPort),
57+
wait.ForLog("running"),
58+
)),
59+
testcontainers.WithCmd(
60+
"/bin/sh",
61+
"-c",
62+
"gcloud beta emulators firestore start --host-port 0.0.0.0:"+defaultPortNumber+" "+gcloudParameters,
63+
),
64+
}
6465

66+
// Append user options
6567
moduleOpts = append(moduleOpts, opts...)
6668

6769
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)

0 commit comments

Comments
 (0)