Skip to content

Commit da4d087

Browse files
committed
fix: config via environment
Fix the ability to set the configuration of testcontainers using an environment variable set by a caller before the use of testcontainers. This removes the warning about disabling the reaper which is actually safe when done correctly. Optimise verbose check. Improve Config struct and field documentation. Fixes: #2701 #2636
1 parent 5024e26 commit da4d087

File tree

2 files changed

+66
-27
lines changed

2 files changed

+66
-27
lines changed

internal/config/config.go

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,71 @@ var (
2020

2121
// testcontainersConfig {
2222

23-
// Config represents the configuration for Testcontainers
23+
// Config represents the configuration for Testcontainers.
24+
// User values are read from ~/.testcontainers.properties file which can be overridden
25+
// using the specified environment variables. For more information, see [Custom Configuration].
26+
//
27+
// The Ryuk prefixed fields controls the [Garbage Collector] feature, which ensures that
28+
// resources are cleaned up after the test execution.
29+
//
30+
// [Garbage Collector]: https://golang.testcontainers.org/features/garbage_collector/
31+
// [Custom Configuration]: https://golang.testcontainers.org/features/configuration/
2432
type Config struct {
25-
Host string `properties:"docker.host,default="`
26-
TLSVerify int `properties:"docker.tls.verify,default=0"`
27-
CertPath string `properties:"docker.cert.path,default="`
28-
HubImageNamePrefix string `properties:"hub.image.name.prefix,default="`
29-
RyukDisabled bool `properties:"ryuk.disabled,default=false"`
30-
RyukPrivileged bool `properties:"ryuk.container.privileged,default=false"`
33+
// Host is the address of the Docker daemon.
34+
//
35+
// Environment variable: DOCKER_HOST
36+
Host string `properties:"docker.host,default="`
37+
38+
// TLSVerify is a flag to enable or disable TLS verification when connecting to a Docker daemon.
39+
//
40+
// Environment variable: DOCKER_TLS_VERIFY
41+
TLSVerify int `properties:"docker.tls.verify,default=0"`
42+
43+
// CertPath is the path to the directory containing the Docker certificates.
44+
// This is used when connecting to a Docker daemon over TLS.
45+
//
46+
// Environment variable: DOCKER_CERT_PATH
47+
CertPath string `properties:"docker.cert.path,default="`
48+
49+
// HubImageNamePrefix is the prefix used for the images pulled from the Docker Hub.
50+
// This is useful when running tests in environments with restricted internet access.
51+
//
52+
// Environment variable: TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX
53+
HubImageNamePrefix string `properties:"hub.image.name.prefix,default="`
54+
55+
// RyukDisabled is a flag to enable or disable the Garbage Collector.
56+
// Setting this to true will prevent testcontainers from automatically cleaning up
57+
// resources, which is particularly important in tests which timeout as they
58+
// don't run test clean up.
59+
//
60+
// Environment variable: TESTCONTAINERS_RYUK_DISABLED
61+
RyukDisabled bool `properties:"ryuk.disabled,default=false"`
62+
63+
// RyukPrivileged is a flag to enable or disable the privileged mode for the Garbage Collector container.
64+
// Setting this to true will run the Garbage Collector container in privileged mode.
65+
//
66+
// Environment variable: TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED
67+
RyukPrivileged bool `properties:"ryuk.container.privileged,default=false"`
68+
69+
// RyukReconnectionTimeout is the time to wait before attempting to reconnect to the Garbage Collector container.
70+
//
71+
// Environment variable: TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT
3172
RyukReconnectionTimeout time.Duration `properties:"ryuk.reconnection.timeout,default=10s"`
32-
RyukConnectionTimeout time.Duration `properties:"ryuk.connection.timeout,default=1m"`
33-
RyukVerbose bool `properties:"ryuk.verbose,default=false"`
34-
TestcontainersHost string `properties:"tc.host,default="`
73+
74+
// RyukConnectionTimeout is the time to wait before timing out when connecting to the Garbage Collector container.
75+
//
76+
// Environment variable: TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT
77+
RyukConnectionTimeout time.Duration `properties:"ryuk.connection.timeout,default=1m"`
78+
79+
// RyukVerbose is a flag to enable or disable verbose logging for the Garbage Collector.
80+
//
81+
// Environment variable: TESTCONTAINERS_RYUK_VERBOSE
82+
RyukVerbose bool `properties:"ryuk.verbose,default=false"`
83+
84+
// TestcontainersHost is the address of the Testcontainers host.
85+
//
86+
// Environment variable: TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE
87+
TestcontainersHost string `properties:"tc.host,default="`
3588
}
3689

3790
// }

logger.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,20 @@ import (
88
"testing"
99

1010
"github.com/docker/docker/client"
11-
12-
"github.com/testcontainers/testcontainers-go/internal/config"
1311
)
1412

1513
// Logger is the default log instance
1614
var Logger Logging = log.New(os.Stderr, "", log.LstdFlags)
1715

1816
func init() {
19-
verbose := false
2017
for _, arg := range os.Args {
2118
if strings.EqualFold(arg, "-test.v=true") || strings.EqualFold(arg, "-v") {
22-
verbose = true
23-
break
19+
return
2420
}
2521
}
2622

27-
if !verbose {
28-
Logger = &noopLogger{}
29-
}
30-
31-
if config.Read().RyukDisabled {
32-
ryukDisabledMessage := `
33-
**********************************************************************************************
34-
Ryuk has been disabled for the current execution. This can cause unexpected behavior in your environment.
35-
More on this: https://golang.testcontainers.org/features/garbage_collector/
36-
**********************************************************************************************`
37-
Logger.Printf(ryukDisabledMessage)
38-
}
23+
// If we are not running in verbose mode, we configure a noop logger by default.
24+
Logger = &noopLogger{}
3925
}
4026

4127
// Validate our types implement the required interfaces.

0 commit comments

Comments
 (0)