|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/caarlos0/env/v11" |
| 10 | + "github.com/magiconair/properties" |
| 11 | +) |
| 12 | + |
| 13 | +// config represents the configuration for Testcontainers. |
| 14 | +// User values are read from ~/.testcontainers.properties file which can be overridden |
| 15 | +// using the specified environment variables. For more information, see [Custom Configuration]. |
| 16 | +// |
| 17 | +// The Ryuk fields controls the [Garbage Collector] feature, which ensures that resources are |
| 18 | +// cleaned up after the test execution. |
| 19 | +// |
| 20 | +// [Garbage Collector]: https://golang.testcontainers.org/features/garbage_collector/ |
| 21 | +// [Custom Configuration]: https://golang.testcontainers.org/features/configuration/ |
| 22 | +type config struct { // TODO: consider renaming adding default values to the struct fields. |
| 23 | + // Host is the address of the Docker daemon. |
| 24 | + Host string `properties:"docker.host" env:"DOCKER_HOST"` |
| 25 | + |
| 26 | + // TLSVerify is a flag to enable or disable TLS verification when connecting to a Docker daemon. |
| 27 | + TLSVerify bool `properties:"docker.tls.verify" env:"DOCKER_TLS_VERIFY"` |
| 28 | + |
| 29 | + // CertPath is the path to the directory containing the Docker certificates. |
| 30 | + // This is used when connecting to a Docker daemon over TLS. |
| 31 | + CertPath string `properties:"docker.cert.path" env:"DOCKER_CERT_PATH"` |
| 32 | + |
| 33 | + // HubImageNamePrefix is the prefix used for the images pulled from the Docker Hub. |
| 34 | + // This is useful when running tests in environments with restricted internet access. |
| 35 | + HubImageNamePrefix string `properties:"hub.image.name.prefix" env:"TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX"` |
| 36 | + |
| 37 | + // TestcontainersHost is the address of the Testcontainers host. |
| 38 | + TestcontainersHost string `properties:"tc.host" env:"TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE"` |
| 39 | + |
| 40 | + // Ryuk is the configuration for the Garbage Collector. |
| 41 | + Ryuk ryukConfig |
| 42 | +} |
| 43 | + |
| 44 | +type ryukConfig struct { |
| 45 | + // Disabled is a flag to enable or disable the Garbage Collector. |
| 46 | + // Setting this to true will prevent testcontainers from automatically cleaning up |
| 47 | + // resources, which is particularly important in tests which timeout as they |
| 48 | + // don't run test clean up. |
| 49 | + Disabled bool `properties:"ryuk.disabled" env:"TESTCONTAINERS_RYUK_DISABLED"` |
| 50 | + |
| 51 | + // Privileged is a flag to enable or disable the privileged mode for the Garbage Collector container. |
| 52 | + // Setting this to true will run the Garbage Collector container in privileged mode. |
| 53 | + Privileged bool `properties:"ryuk.container.privileged" env:"TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED"` |
| 54 | + |
| 55 | + // ReconnectionTimeout is the time to wait before attempting to reconnect to the Garbage Collector container. |
| 56 | + ReconnectionTimeout time.Duration `properties:"ryuk.reconnection.timeout,default=10s" env:"TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT"` |
| 57 | + |
| 58 | + // ConnectionTimeout is the time to wait before timing out when connecting to the Garbage Collector container. |
| 59 | + ConnectionTimeout time.Duration `properties:"ryuk.connection.timeout,default=1m" env:"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT"` |
| 60 | + |
| 61 | + // Verbose is a flag to enable or disable verbose logging for the Garbage Collector. |
| 62 | + Verbose bool `properties:"ryuk.verbose" env:"TESTCONTAINERS_RYUK_VERBOSE"` |
| 63 | +} |
| 64 | + |
| 65 | +// newConfig returns a new configuration loaded from the properties file |
| 66 | +// located in the user's home directory and overridden by environment variables. |
| 67 | +func newConfig() (*config, error) { |
| 68 | + home, err := os.UserHomeDir() |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("user home dir: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + props, err := properties.LoadFiles([]string{filepath.Join(home, ".testcontainers.properties")}, properties.UTF8, true) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("load properties file: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + var cfg config |
| 79 | + if err := props.Decode(&cfg); err != nil { |
| 80 | + return nil, fmt.Errorf("decode properties: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + if err := env.Parse(cfg); err != nil { |
| 84 | + return nil, fmt.Errorf("parse env: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return &cfg, nil |
| 88 | +} |
0 commit comments