Skip to content

Commit 5941323

Browse files
authored
chore(gcloud): use Run function (#3411)
* chore(gcloud): use Run in deprecated constructors * chore(bigquery): use Run function * chore(bigtable): use Run function * chore(datastore): use Run function * chore: extract ports to constants * chore(firestore): use Run function * chore(pubsub): use Run function * chore(spanner): use Run function * fix: missing options after refactor * fix: typo in docs * fix: order
1 parent 46d68aa commit 5941323

File tree

15 files changed

+198
-219
lines changed

15 files changed

+198
-219
lines changed

modules/gcloud/bigquery.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,32 @@ func RunBigQueryContainer(ctx context.Context, opts ...testcontainers.ContainerC
1818
// RunBigQuery creates an instance of the GCloud container type for BigQuery.
1919
// The URI uses http:// as the protocol.
2020
func RunBigQuery(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*GCloudContainer, error) {
21-
req := testcontainers.GenericContainerRequest{
22-
ContainerRequest: testcontainers.ContainerRequest{
23-
Image: img,
24-
ExposedPorts: []string{"9050/tcp", "9060/tcp"},
25-
WaitingFor: wait.ForHTTP("/discovery/v1/apis/bigquery/v2/rest").WithPort("9050/tcp").WithStartupTimeout(time.Second * 5),
26-
},
27-
Started: true,
21+
moduleOpts := []testcontainers.ContainerCustomizer{
22+
testcontainers.WithExposedPorts("9050/tcp", "9060/tcp"),
23+
testcontainers.WithWaitStrategy(wait.ForHTTP("/discovery/v1/apis/bigquery/v2/rest").WithPort("9050/tcp").WithStartupTimeout(time.Second * 5)),
2824
}
2925

30-
settings, err := applyOptions(&req, opts)
26+
moduleOpts = append(moduleOpts, opts...)
27+
28+
settings, err := applyOptions(opts)
3129
if err != nil {
3230
return nil, err
3331
}
3432

35-
req.Cmd = append(req.Cmd, "--project", settings.ProjectID)
33+
moduleOpts = append(moduleOpts, testcontainers.WithCmdArgs("--project", settings.ProjectID))
3634

3735
// Process data yaml file only for the BigQuery container.
3836
if settings.bigQueryDataYaml != nil {
3937
containerPath := "/testcontainers-data.yaml"
4038

41-
req.Cmd = append(req.Cmd, "--data-from-yaml", containerPath)
39+
moduleOpts = append(moduleOpts, testcontainers.WithCmdArgs("--data-from-yaml", containerPath))
4240

43-
req.Files = append(req.Files, testcontainers.ContainerFile{
41+
moduleOpts = append(moduleOpts, testcontainers.WithFiles(testcontainers.ContainerFile{
4442
Reader: settings.bigQueryDataYaml,
4543
ContainerFilePath: containerPath,
4644
FileMode: 0o644,
47-
})
45+
}))
4846
}
4947

50-
return newGCloudContainer(ctx, req, 9050, settings, "http")
48+
return newGCloudContainer(ctx, img, 9050, settings, "http", moduleOpts...)
5149
}

modules/gcloud/bigquery/bigquery.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const (
1515

1616
// bigQueryDataYamlPath is the path to the data yaml file in the container.
1717
bigQueryDataYamlPath = "/testcontainers-data.yaml"
18+
19+
defaultPortNumber9050 = "9050"
20+
defaultPortNumber9060 = "9060"
21+
defaultPort9050 = defaultPortNumber9050 + "/tcp"
22+
defaultPort9060 = defaultPortNumber9060 + "/tcp"
1823
)
1924

2025
// Container represents the BigQuery container type used in the module
@@ -36,44 +41,39 @@ func (c *Container) URI() string {
3641
// Run creates an instance of the BigQuery GCloud container type.
3742
// The URI uses http:// as the protocol.
3843
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
39-
req := testcontainers.GenericContainerRequest{
40-
ContainerRequest: testcontainers.ContainerRequest{
41-
Image: img,
42-
ExposedPorts: []string{"9050/tcp", "9060/tcp"},
43-
WaitingFor: wait.ForAll(
44-
wait.ForListeningPort("9050/tcp"),
45-
wait.ForHTTP("/discovery/v1/apis/bigquery/v2/rest").WithPort("9050/tcp").WithStatusCodeMatcher(func(status int) bool {
46-
return status == 200
47-
}).WithStartupTimeout(time.Second*5),
48-
),
49-
},
50-
Started: true,
44+
moduleOpts := []testcontainers.ContainerCustomizer{
45+
testcontainers.WithExposedPorts(defaultPort9050, defaultPort9060),
46+
testcontainers.WithWaitStrategy(wait.ForAll(
47+
wait.ForListeningPort(defaultPort9050),
48+
wait.ForHTTP("/discovery/v1/apis/bigquery/v2/rest").WithPort(defaultPort9050).WithStatusCodeMatcher(func(status int) bool {
49+
return status == 200
50+
}).WithStartupTimeout(time.Second*5),
51+
)),
5152
}
5253

5354
settings := defaultOptions()
5455
for _, opt := range opts {
5556
if apply, ok := opt.(Option); ok {
5657
if err := apply(&settings); err != nil {
57-
return nil, err
58+
return nil, fmt.Errorf("apply option: %w", err)
5859
}
5960
}
60-
if err := opt.Customize(&req); err != nil {
61-
return nil, err
62-
}
6361
}
6462

65-
req.Cmd = append(req.Cmd, "--project", settings.ProjectID)
63+
moduleOpts = append(moduleOpts, testcontainers.WithCmdArgs("--project", settings.ProjectID))
64+
65+
moduleOpts = append(moduleOpts, opts...)
6666

67-
container, err := testcontainers.GenericContainer(ctx, req)
67+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
6868
var c *Container
69-
if container != nil {
70-
c = &Container{Container: container, settings: settings}
69+
if ctr != nil {
70+
c = &Container{Container: ctr, settings: settings}
7171
}
7272
if err != nil {
73-
return c, fmt.Errorf("generic container: %w", err)
73+
return c, fmt.Errorf("run bigquery: %w", err)
7474
}
7575

76-
portEndpoint, err := c.PortEndpoint(ctx, "9050/tcp", "http")
76+
portEndpoint, err := c.PortEndpoint(ctx, defaultPort9050, "http")
7777
if err != nil {
7878
return c, fmt.Errorf("port endpoint: %w", err)
7979
}

modules/gcloud/bigquery/bigquery_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,12 @@ func TestBigQueryWithDataYAML(t *testing.T) {
7676
tcbigquery.WithDataYAML(bytes.NewReader(dataYaml)),
7777
)
7878
testcontainers.CleanupContainer(t, bigQueryContainer)
79-
require.EqualError(t, err, `data yaml already exists`)
79+
require.ErrorContains(t, err, `data yaml already exists`)
8080
})
8181

8282
t.Run("multi-value-not-set", func(t *testing.T) {
83-
noValueOption := func() testcontainers.CustomizeRequestOption {
84-
return func(req *testcontainers.GenericContainerRequest) error {
85-
req.Cmd = append(req.Cmd, "--data-from-yaml")
86-
return nil
87-
}
83+
noValueOption := func() testcontainers.ContainerCustomizer {
84+
return testcontainers.WithCmdArgs("--data-from-yaml")
8885
}
8986

9087
bigQueryContainer, err := tcbigquery.Run(

modules/gcloud/bigquery/options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func WithDataYAML(r io.Reader) testcontainers.CustomizeRequestOption {
3434
return errors.New("data yaml already exists")
3535
}
3636

37-
req.Cmd = append(req.Cmd, "--data-from-yaml", bigQueryDataYamlPath)
37+
if err := testcontainers.WithCmdArgs("--data-from-yaml", bigQueryDataYamlPath)(req); err != nil {
38+
return err
39+
}
3840

39-
req.Files = append(req.Files, testcontainers.ContainerFile{
41+
return testcontainers.WithFiles(testcontainers.ContainerFile{
4042
Reader: r,
4143
ContainerFilePath: bigQueryDataYamlPath,
4244
FileMode: 0o644,
43-
})
44-
45-
return nil
45+
})(req)
4646
}
4747
}

modules/gcloud/bigtable.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ func RunBigTableContainer(ctx context.Context, opts ...testcontainers.ContainerC
1616
// Deprecated: use [bigtable.Run] instead
1717
// RunBigTable creates an instance of the GCloud container type for BigTable.
1818
func RunBigTable(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*GCloudContainer, error) {
19-
req := testcontainers.GenericContainerRequest{
20-
ContainerRequest: testcontainers.ContainerRequest{
21-
Image: img,
22-
ExposedPorts: []string{"9000/tcp"},
23-
WaitingFor: wait.ForLog("running"),
24-
},
25-
Started: true,
19+
moduleOpts := []testcontainers.ContainerCustomizer{
20+
testcontainers.WithExposedPorts("9000/tcp"),
21+
testcontainers.WithWaitStrategy(wait.ForAll(
22+
wait.ForListeningPort("9000/tcp"),
23+
wait.ForLog("running")),
24+
),
2625
}
2726

28-
settings, err := applyOptions(&req, opts)
27+
moduleOpts = append(moduleOpts, opts...)
28+
29+
settings, err := applyOptions(opts)
2930
if err != nil {
3031
return nil, err
3132
}
3233

33-
req.Cmd = []string{
34+
moduleOpts = append(moduleOpts, testcontainers.WithCmd(
3435
"/bin/sh",
3536
"-c",
36-
"gcloud beta emulators bigtable start --host-port 0.0.0.0:9000 --project=" + settings.ProjectID,
37-
}
37+
"gcloud beta emulators bigtable start --host-port 0.0.0.0:9000 --project="+settings.ProjectID,
38+
))
3839

39-
return newGCloudContainer(ctx, req, 9000, settings, "")
40+
return newGCloudContainer(ctx, img, 9000, settings, "", moduleOpts...)
4041
}

modules/gcloud/bigtable/bigtable.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010

1111
const (
1212
// DefaultProjectID is the default project ID for the BigTable container.
13-
DefaultProjectID = "test-project"
13+
DefaultProjectID = "test-project"
14+
defaultPortNumber = "9000"
15+
defaultPort = defaultPortNumber + "/tcp"
1416
)
1517

1618
// Container represents the BigTable container type used in the module
@@ -32,16 +34,12 @@ func (c *Container) URI() string {
3234
// Run creates an instance of the BigTable GCloud container type.
3335
// The URI uses the empty string as the protocol.
3436
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
35-
req := testcontainers.GenericContainerRequest{
36-
ContainerRequest: testcontainers.ContainerRequest{
37-
Image: img,
38-
ExposedPorts: []string{"9000/tcp"},
39-
WaitingFor: wait.ForAll(
40-
wait.ForListeningPort("9000/tcp"),
41-
wait.ForLog("running"),
42-
),
43-
},
44-
Started: true,
37+
moduleOpts := []testcontainers.ContainerCustomizer{
38+
testcontainers.WithExposedPorts(defaultPort),
39+
testcontainers.WithWaitStrategy(wait.ForAll(
40+
wait.ForListeningPort(defaultPort),
41+
wait.ForLog("running"),
42+
)),
4543
}
4644

4745
settings := defaultOptions()
@@ -51,27 +49,26 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5149
return nil, err
5250
}
5351
}
54-
if err := opt.Customize(&req); err != nil {
55-
return nil, err
56-
}
5752
}
5853

59-
req.Cmd = []string{
54+
moduleOpts = append(moduleOpts, testcontainers.WithCmd(
6055
"/bin/sh",
6156
"-c",
62-
"gcloud beta emulators bigtable start --host-port 0.0.0.0:9000 --project=" + settings.ProjectID,
63-
}
57+
"gcloud beta emulators bigtable start --host-port 0.0.0.0:"+defaultPortNumber+" --project="+settings.ProjectID,
58+
))
59+
60+
moduleOpts = append(moduleOpts, opts...)
6461

65-
container, err := testcontainers.GenericContainer(ctx, req)
62+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
6663
var c *Container
67-
if container != nil {
68-
c = &Container{Container: container, settings: settings}
64+
if ctr != nil {
65+
c = &Container{Container: ctr, settings: settings}
6966
}
7067
if err != nil {
71-
return c, fmt.Errorf("generic container: %w", err)
68+
return c, fmt.Errorf("run bigtable: %w", err)
7269
}
7370

74-
portEndpoint, err := c.PortEndpoint(ctx, "9000/tcp", "")
71+
portEndpoint, err := c.PortEndpoint(ctx, defaultPort, "")
7572
if err != nil {
7673
return c, fmt.Errorf("port endpoint: %w", err)
7774
}

modules/gcloud/datastore.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@ func RunDatastoreContainer(ctx context.Context, opts ...testcontainers.Container
1616
// Deprecated: use [datastore.Run] instead
1717
// RunDatastore creates an instance of the GCloud container type for Datastore.
1818
func RunDatastore(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*GCloudContainer, error) {
19-
req := testcontainers.GenericContainerRequest{
20-
ContainerRequest: testcontainers.ContainerRequest{
21-
Image: img,
22-
ExposedPorts: []string{"8081/tcp"},
23-
WaitingFor: wait.ForHTTP("/").WithPort("8081/tcp"),
24-
},
25-
Started: true,
19+
moduleOpts := []testcontainers.ContainerCustomizer{
20+
testcontainers.WithExposedPorts("8081/tcp"),
21+
testcontainers.WithWaitStrategy(wait.ForHTTP("/").WithPort("8081/tcp")),
2622
}
2723

28-
settings, err := applyOptions(&req, opts)
24+
moduleOpts = append(moduleOpts, opts...)
25+
26+
settings, err := applyOptions(opts)
2927
if err != nil {
3028
return nil, err
3129
}
3230

33-
req.Cmd = []string{
31+
moduleOpts = append(moduleOpts, testcontainers.WithCmd(
3432
"/bin/sh",
3533
"-c",
36-
"gcloud beta emulators datastore start --host-port 0.0.0.0:8081 --project=" + settings.ProjectID,
37-
}
34+
"gcloud beta emulators datastore start --host-port 0.0.0.0:8081 --project="+settings.ProjectID,
35+
))
3836

39-
return newGCloudContainer(ctx, req, 8081, settings, "")
37+
return newGCloudContainer(ctx, img, 8081, settings, "", moduleOpts...)
4038
}

modules/gcloud/datastore/datastore.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010

1111
const (
1212
// DefaultProjectID is the default project ID for the Datastore container.
13-
DefaultProjectID = "test-project"
13+
DefaultProjectID = "test-project"
14+
defaultPortNumber = "8081"
15+
defaultPort = defaultPortNumber + "/tcp"
1416
)
1517

1618
// Container represents the Datastore container type used in the module
@@ -32,16 +34,12 @@ func (c *Container) URI() string {
3234
// Run creates an instance of the Datastore GCloud container type.
3335
// The URI uses the empty string as the protocol.
3436
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
35-
req := testcontainers.GenericContainerRequest{
36-
ContainerRequest: testcontainers.ContainerRequest{
37-
Image: img,
38-
ExposedPorts: []string{"8081/tcp"},
39-
WaitingFor: wait.ForAll(
40-
wait.ForListeningPort("8081/tcp"),
41-
wait.ForHTTP("/").WithPort("8081/tcp"),
42-
),
43-
},
44-
Started: true,
37+
moduleOpts := []testcontainers.ContainerCustomizer{
38+
testcontainers.WithExposedPorts(defaultPort),
39+
testcontainers.WithWaitStrategy(wait.ForAll(
40+
wait.ForListeningPort(defaultPort),
41+
wait.ForHTTP("/").WithPort(defaultPort),
42+
)),
4543
}
4644

4745
settings := defaultOptions()
@@ -51,27 +49,26 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5149
return nil, err
5250
}
5351
}
54-
if err := opt.Customize(&req); err != nil {
55-
return nil, err
56-
}
5752
}
5853

59-
req.Cmd = []string{
54+
moduleOpts = append(moduleOpts, testcontainers.WithCmd(
6055
"/bin/sh",
6156
"-c",
62-
"gcloud beta emulators datastore start --host-port 0.0.0.0:8081 --project=" + settings.ProjectID,
63-
}
57+
"gcloud beta emulators datastore start --host-port 0.0.0.0:"+defaultPortNumber+" --project="+settings.ProjectID,
58+
))
59+
60+
moduleOpts = append(moduleOpts, opts...)
6461

65-
container, err := testcontainers.GenericContainer(ctx, req)
62+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
6663
var c *Container
67-
if container != nil {
68-
c = &Container{Container: container, settings: settings}
64+
if ctr != nil {
65+
c = &Container{Container: ctr, settings: settings}
6966
}
7067
if err != nil {
71-
return c, fmt.Errorf("generic container: %w", err)
68+
return c, fmt.Errorf("run datastore: %w", err)
7269
}
7370

74-
portEndpoint, err := c.PortEndpoint(ctx, "8081/tcp", "")
71+
portEndpoint, err := c.PortEndpoint(ctx, defaultPort, "")
7572
if err != nil {
7673
return c, fmt.Errorf("port endpoint: %w", err)
7774
}

0 commit comments

Comments
 (0)