Skip to content

Commit

Permalink
feat(ryuk): allow to configure ryuk timeouts using env variables (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcx-mrogez authored May 20, 2024
1 parent 7eb1c07 commit 741d438
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Please read more about customizing images in the [Image name substitution](image
1. If your environment already implements automatic cleanup of containers after the execution,
but does not allow starting privileged containers, you can turn off the Ryuk container by setting
`TESTCONTAINERS_RYUK_DISABLED` **environment variable** to `true`.
1. You can specify the connection timeout for Ryuk by setting the `ryuk.connection.timeout` **property**. The default value is 1 minute.
1. You can specify the reconnection timeout for Ryuk by setting the `ryuk.reconnection.timeout` **property**. The default value is 10 seconds.
1. You can specify the connection timeout for Ryuk by setting the `TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT` **environment variable**, or the `ryuk.connection.timeout` **property**. The default value is 1 minute.
1. You can specify the reconnection timeout for Ryuk by setting the `TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT` **environment variable**, or the `ryuk.reconnection.timeout` **property**. The default value is 10 seconds.
1. You can configure Ryuk to run in verbose mode by setting any of the `ryuk.verbose` **property** or the `TESTCONTAINERS_RYUK_VERBOSE` **environment variable**. The default value is `false`.

!!!info
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func read() Config {
config.RyukVerbose = ryukVerboseEnv == "true"
}

ryukReconnectionTimeoutEnv := os.Getenv("TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT")
if timeout, err := time.ParseDuration(ryukReconnectionTimeoutEnv); err == nil {
config.RyukReconnectionTimeout = timeout
}

ryukConnectionTimeoutEnv := os.Getenv("TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT")
if timeout, err := time.ParseDuration(ryukConnectionTimeoutEnv); err == nil {
config.RyukConnectionTimeout = timeout
}

return config
}

Expand Down
97 changes: 66 additions & 31 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func resetTestEnv(t *testing.T) {
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "")
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "")
t.Setenv("TESTCONTAINERS_RYUK_VERBOSE", "")
t.Setenv("TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT", "")
t.Setenv("TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT", "")
}

func TestReadConfig(t *testing.T) {
Expand Down Expand Up @@ -74,14 +76,18 @@ func TestReadTCConfig(t *testing.T) {
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", defaultHubPrefix)
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")
t.Setenv("TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT", "13s")
t.Setenv("TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT", "12s")

config := read()

expected := Config{
HubImageNamePrefix: defaultHubPrefix,
RyukDisabled: true,
RyukPrivileged: true,
Host: "", // docker socket is empty at the properties file
HubImageNamePrefix: defaultHubPrefix,
RyukDisabled: true,
RyukPrivileged: true,
Host: "", // docker socket is empty at the properties file
RyukReconnectionTimeout: 13 * time.Second,
RyukConnectionTimeout: 12 * time.Second,
}

assert.Equal(t, expected, config)
Expand Down Expand Up @@ -119,24 +125,28 @@ func TestReadTCConfig(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", defaultHubPrefix)
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")
t.Setenv("TESTCONTAINERS_RYUK_VERBOSE", "true")
t.Setenv("TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT", "13s")
t.Setenv("TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT", "12s")

config := read()
expected := Config{
HubImageNamePrefix: defaultHubPrefix,
RyukDisabled: true,
RyukPrivileged: true,
RyukVerbose: true,
HubImageNamePrefix: defaultHubPrefix,
RyukDisabled: true,
RyukPrivileged: true,
RyukVerbose: true,
RyukReconnectionTimeout: 13 * time.Second,
RyukConnectionTimeout: 12 * time.Second,
}

assert.Equal(t, expected, config)
})

t.Run("HOME contains TC properties file", func(t *testing.T) {
defaultRyukConnectionTimeout := 60 * time.Second
defaultRyukReonnectionTimeout := 10 * time.Second
defaultRyukReconnectionTimeout := 10 * time.Second
defaultConfig := Config{
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
}

tests := []struct {
Expand All @@ -152,7 +162,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
Host: tcpDockerHost33293,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -164,7 +174,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
Host: tcpDockerHost4711,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -179,7 +189,7 @@ func TestReadTCConfig(t *testing.T) {
Host: tcpDockerHost1234,
TLSVerify: 1,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -188,7 +198,7 @@ func TestReadTCConfig(t *testing.T) {
map[string]string{},
Config{
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -200,7 +210,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
Host: tcpDockerHost1234,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -210,7 +220,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
Host: tcpDockerHost33293,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -230,7 +240,7 @@ func TestReadTCConfig(t *testing.T) {
Host: tcpDockerHost1234,
CertPath: "/tmp/certs",
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -240,7 +250,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukDisabled: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -250,7 +260,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukPrivileged: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -263,14 +273,39 @@ func TestReadTCConfig(t *testing.T) {
RyukConnectionTimeout: 12 * time.Second,
},
},
{
"With Ryuk container timeouts configured using env vars",
``,
map[string]string{
"TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT": "13s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
},
Config{
RyukReconnectionTimeout: 13 * time.Second,
RyukConnectionTimeout: 12 * time.Second,
},
},
{
"With Ryuk container timeouts configured using env vars and properties. Env var wins",
`ryuk.connection.timeout=22s
ryuk.reconnection.timeout=23s`,
map[string]string{
"TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT": "13s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
},
Config{
RyukReconnectionTimeout: 13 * time.Second,
RyukConnectionTimeout: 12 * time.Second,
},
},
{
"With Ryuk verbose configured using properties",
`ryuk.verbose=true`,
map[string]string{},
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -282,7 +317,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukDisabled: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -294,7 +329,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukPrivileged: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -306,7 +341,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukDisabled: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -318,7 +353,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukDisabled: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand Down Expand Up @@ -346,7 +381,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -358,7 +393,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand Down Expand Up @@ -386,7 +421,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukPrivileged: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -398,7 +433,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
RyukPrivileged: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand Down Expand Up @@ -453,7 +488,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
HubImageNamePrefix: defaultHubPrefix + "/props/",
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -465,7 +500,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
HubImageNamePrefix: defaultHubPrefix + "/env/",
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
{
Expand All @@ -477,7 +512,7 @@ func TestReadTCConfig(t *testing.T) {
Config{
HubImageNamePrefix: defaultHubPrefix + "/env/",
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
RyukReconnectionTimeout: defaultRyukReconnectionTimeout,
},
},
}
Expand Down

0 comments on commit 741d438

Please sign in to comment.