Skip to content

Commit 2150dba

Browse files
committed
Merge branch 'main' into catinapoke/main
* main: chore: use a much smaller image for testing (testcontainers#2795) fix: parallel containers clean race (testcontainers#2790) fix(registry): wait for (testcontainers#2793) fix: container timeout test (testcontainers#2792) docs: document redpanda options (testcontainers#2789) feat: support databend module (testcontainers#2779) chore: golangci-lint 1.61.0 (testcontainers#2787) fix(mssql): bump Docker image version (testcontainers#2786) fix: handle 127 error code for podman compatibility (testcontainers#2778) fix: do not override ImageBuildOptions.Labels when building from a Dockerfile (testcontainers#2775) feat(mongodb): Wait for mongodb module with a replicaset to finish (testcontainers#2777) fix(postgres): Apply default snapshot name if no name specified (testcontainers#2783) fix: resource clean up for tests and examples (testcontainers#2738) ci: add generate for mocks (testcontainers#2774) fix: docker config error handling when config file does not exist (testcontainers#2772)
2 parents b51be12 + 738e8fc commit 2150dba

File tree

280 files changed

+5228
-4473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

280 files changed

+5228
-4473
lines changed

.github/workflows/ci-test-go.yml

+15-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ jobs:
7373

7474
- name: golangci-lint
7575
if: ${{ inputs.platform == 'ubuntu-latest' }}
76-
uses: golangci/golangci-lint-action@9d1e0624a798bb64f6c3cea93db47765312263dc # v5
76+
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0
7777
with:
7878
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
79-
version: v1.59.1
79+
version: v1.61.0
8080
# Optional: working directory, useful for monorepos
8181
working-directory: ${{ inputs.project-directory }}
8282
# Optional: golangci-lint command line arguments.
@@ -85,13 +85,25 @@ jobs:
8585
# takes precedence over all other caching options.
8686
skip-cache: true
8787

88+
- name: generate
89+
if: ${{ inputs.platform == 'ubuntu-latest' }}
90+
working-directory: ./${{ inputs.project-directory }}
91+
shell: bash
92+
run: |
93+
make generate
94+
git --no-pager diff && [[ 0 -eq $(git status --porcelain | wc -l) ]]
95+
8896
- name: modVerify
8997
working-directory: ./${{ inputs.project-directory }}
9098
run: go mod verify
9199

92100
- name: modTidy
101+
if: ${{ inputs.platform == 'ubuntu-latest' }}
93102
working-directory: ./${{ inputs.project-directory }}
94-
run: make tidy
103+
shell: bash
104+
run: |
105+
make tidy
106+
git --no-pager diff && [[ 0 -eq $(git status --porcelain | wc -l) ]]
95107
96108
- name: ensure compilation
97109
working-directory: ./${{ inputs.project-directory }}

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
matrix:
9595
go-version: [1.22.x, 1.x]
9696
platform: [ubuntu-latest]
97-
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, dolt, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
97+
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, databend, dolt, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
9898
uses: ./.github/workflows/ci-test-go.yml
9999
with:
100100
go-version: ${{ matrix.go-version }}

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ TEST-*.xml
1414

1515
tcvenv
1616

17-
**/go.work
17+
**/go.work
18+
19+
# VS Code settings
20+
.vscode

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ linters:
88
- nonamedreturns
99
- testifylint
1010
- errcheck
11+
- nolintlint
1112

1213
linters-settings:
1314
errorlint:

.vscode/.testcontainers-go.code-workspace

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
"name": "module / couchbase",
5050
"path": "../modules/couchbase"
5151
},
52+
{
53+
"name": "module / databend",
54+
"path": "../modules/databend"
55+
},
5256
{
5357
"name": "module / dolt",
5458
"path": "../modules/dolt"

cleanup.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package testcontainers
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"reflect"
8+
"time"
9+
)
10+
11+
// terminateOptions is a type that holds the options for terminating a container.
12+
type terminateOptions struct {
13+
ctx context.Context
14+
timeout *time.Duration
15+
volumes []string
16+
}
17+
18+
// TerminateOption is a type that represents an option for terminating a container.
19+
type TerminateOption func(*terminateOptions)
20+
21+
// StopContext returns a TerminateOption that sets the context.
22+
// Default: context.Background().
23+
func StopContext(ctx context.Context) TerminateOption {
24+
return func(c *terminateOptions) {
25+
c.ctx = ctx
26+
}
27+
}
28+
29+
// StopTimeout returns a TerminateOption that sets the timeout.
30+
// Default: See [Container.Stop].
31+
func StopTimeout(timeout time.Duration) TerminateOption {
32+
return func(c *terminateOptions) {
33+
c.timeout = &timeout
34+
}
35+
}
36+
37+
// RemoveVolumes returns a TerminateOption that sets additional volumes to remove.
38+
// This is useful when the container creates named volumes that should be removed
39+
// which are not removed by default.
40+
// Default: nil.
41+
func RemoveVolumes(volumes ...string) TerminateOption {
42+
return func(c *terminateOptions) {
43+
c.volumes = volumes
44+
}
45+
}
46+
47+
// TerminateContainer calls [Container.Terminate] on the container if it is not nil.
48+
//
49+
// This should be called as a defer directly after [GenericContainer](...)
50+
// or a modules Run(...) to ensure the container is terminated when the
51+
// function ends.
52+
func TerminateContainer(container Container, options ...TerminateOption) error {
53+
if isNil(container) {
54+
return nil
55+
}
56+
57+
c := &terminateOptions{
58+
ctx: context.Background(),
59+
}
60+
61+
for _, opt := range options {
62+
opt(c)
63+
}
64+
65+
// TODO: Add a timeout when terminate supports it.
66+
err := container.Terminate(c.ctx)
67+
if !isCleanupSafe(err) {
68+
return fmt.Errorf("terminate: %w", err)
69+
}
70+
71+
// Remove additional volumes if any.
72+
if len(c.volumes) == 0 {
73+
return nil
74+
}
75+
76+
client, err := NewDockerClientWithOpts(c.ctx)
77+
if err != nil {
78+
return fmt.Errorf("docker client: %w", err)
79+
}
80+
81+
defer client.Close()
82+
83+
// Best effort to remove all volumes.
84+
var errs []error
85+
for _, volume := range c.volumes {
86+
if errRemove := client.VolumeRemove(c.ctx, volume, true); errRemove != nil {
87+
errs = append(errs, fmt.Errorf("volume remove %q: %w", volume, errRemove))
88+
}
89+
}
90+
91+
return errors.Join(errs...)
92+
}
93+
94+
// isNil returns true if val is nil or an nil instance false otherwise.
95+
func isNil(val any) bool {
96+
if val == nil {
97+
return true
98+
}
99+
100+
valueOf := reflect.ValueOf(val)
101+
switch valueOf.Kind() {
102+
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
103+
return valueOf.IsNil()
104+
default:
105+
return false
106+
}
107+
}

commons-test.mk

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ define go_install
66
endef
77

88
$(GOBIN)/golangci-lint:
9-
$(call go_install,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1)
9+
$(call go_install,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0)
1010

1111
$(GOBIN)/gotestsum:
1212
$(call go_install,gotest.tools/gotestsum@latest)
1313

14+
$(GOBIN)/mockery:
15+
$(call go_install,github.com/vektra/mockery/v2@v2.45)
16+
1417
.PHONY: install
15-
install: $(GOBIN)/golangci-lint $(GOBIN)/gotestsum
18+
install: $(GOBIN)/golangci-lint $(GOBIN)/gotestsum $(GOBIN)/mockery
1619

1720
.PHONY: clean
1821
clean:
1922
rm $(GOBIN)/golangci-lint
2023
rm $(GOBIN)/gotestsum
24+
rm $(GOBIN)/mockery
2125

2226
.PHONY: dependencies-scan
2327
dependencies-scan:
@@ -26,7 +30,11 @@ dependencies-scan:
2630

2731
.PHONY: lint
2832
lint: $(GOBIN)/golangci-lint
29-
golangci-lint run --out-format=github-actions --path-prefix=. --verbose -c $(ROOT_DIR)/.golangci.yml --fix
33+
golangci-lint run --out-format=colored-line-number --path-prefix=. --verbose -c $(ROOT_DIR)/.golangci.yml --fix
34+
35+
.PHONY: generate
36+
generate: $(GOBIN)/mockery
37+
go generate ./...
3038

3139
.PHONY: test-%
3240
test-%: $(GOBIN)/gotestsum
@@ -51,3 +59,6 @@ test-tools: $(GOBIN)/gotestsum
5159
.PHONY: tidy
5260
tidy:
5361
go mod tidy
62+
63+
.PHONY: pre-commit
64+
pre-commit: generate tidy lint

container.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,14 @@ func (c *ContainerRequest) BuildOptions() (types.ImageBuildOptions, error) {
460460
}
461461

462462
if !c.ShouldKeepBuiltImage() {
463-
buildOptions.Labels = core.DefaultLabels(core.SessionID())
463+
dst := GenericLabels()
464+
if err = core.MergeCustomLabels(dst, c.Labels); err != nil {
465+
return types.ImageBuildOptions{}, err
466+
}
467+
if err = core.MergeCustomLabels(dst, buildOptions.Labels); err != nil {
468+
return types.ImageBuildOptions{}, err
469+
}
470+
buildOptions.Labels = dst
464471
}
465472

466473
// Do this as late as possible to ensure we don't leak the context on error/panic.
@@ -513,7 +520,7 @@ func (c *ContainerRequest) validateMounts() error {
513520

514521
c.HostConfigModifier(&hostConfig)
515522

516-
if hostConfig.Binds != nil && len(hostConfig.Binds) > 0 {
523+
if len(hostConfig.Binds) > 0 {
517524
for _, bind := range hostConfig.Binds {
518525
parts := strings.Split(bind, ":")
519526
if len(parts) != 2 {

0 commit comments

Comments
 (0)