Skip to content

Commit c39e369

Browse files
stevenhmdelapenya
andauthored
break: return error from Customize request option (testcontainers#2267)
* chore!: return error from Customize Change ContainerCustomizer.Customize method to return an error so that options can handle errors gracefully instead of relying on panic or just a log entry, neither of which are user friendly. Enable errcheck linter to ensure that errors that aren't handled are reported. Run go mod tidy on k3s and weaviate to allow tests to be run using go 1.22. Run gofumpt on a few files to satisfy golangci-lint. Fix direct comparison with http.ErrServerClosed flagged by errcheck. Fixes testcontainers#2266 BREAKING CHANGE: `ContainerCustomizer.Customize` now returns an error. * fix(mongodb): captured loop variable Fix captured loop variable in mongodb test reported by govet. * fix(k3s): test formatting Fix formatting in test file reported by gci during linting. * chore: add missing Customize error returns Add missing error returns for implementations of CustomizeRequestOption. * chore: update modules with the new error API * fix: use logger * fix: update modulegen tests * chore: fix lint * chore: handle customise errors * chore: update new host port access option * docs: move new error API to the right doc * docs: move customise request to the bottom * docs: sync functional options * chore: avoid panics in localstack * chore: require no error in test * chore: ignore error in copy API, using nolint:errcheck * chore: convert log into error --------- Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
1 parent 838370d commit c39e369

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

mongodb.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
3636
}
3737

3838
for _, opt := range opts {
39-
opt.Customize(&genericContainerReq)
39+
if err := opt.Customize(&genericContainerReq); err != nil {
40+
return nil, err
41+
}
4042
}
4143
username := req.Env["MONGO_INITDB_ROOT_USERNAME"]
4244
password := req.Env["MONGO_INITDB_ROOT_PASSWORD"]
@@ -59,17 +61,21 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
5961
// It is used in conjunction with WithPassword to set a username and its password.
6062
// It will create the specified user with superuser power.
6163
func WithUsername(username string) testcontainers.CustomizeRequestOption {
62-
return func(req *testcontainers.GenericContainerRequest) {
64+
return func(req *testcontainers.GenericContainerRequest) error {
6365
req.Env["MONGO_INITDB_ROOT_USERNAME"] = username
66+
67+
return nil
6468
}
6569
}
6670

6771
// WithPassword sets the initial password of the user to be created when the container starts
6872
// It is used in conjunction with WithUsername to set a username and its password.
6973
// It will set the superuser password for MongoDB.
7074
func WithPassword(password string) testcontainers.CustomizeRequestOption {
71-
return func(req *testcontainers.GenericContainerRequest) {
75+
return func(req *testcontainers.GenericContainerRequest) error {
7276
req.Env["MONGO_INITDB_ROOT_PASSWORD"] = password
77+
78+
return nil
7379
}
7480
}
7581

mongodb_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ func TestMongoDB(t *testing.T) {
3333
}
3434

3535
for _, tc := range testCases {
36-
t.Run(tc.image, func(t *testing.T) {
36+
image := tc.image
37+
t.Run(image, func(t *testing.T) {
3738
t.Parallel()
3839

3940
ctx := context.Background()
4041

41-
mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage(tc.image))
42+
mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage(image))
4243
if err != nil {
4344
t.Fatalf("failed to start container: %s", err)
4445
}

0 commit comments

Comments
 (0)