Skip to content

Commit

Permalink
feat: support configuring Ryuk verbose mode at config level (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Dec 21, 2023
1 parent 5faf6f4 commit 9f68760
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ but does not allow starting privileged containers, you can turn off the Ryuk con
`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 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
For more information about Ryuk, see [Garbage Collector](garbage_collector.md).
Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
RyukPrivileged bool `properties:"ryuk.container.privileged,default=false"`
RyukReconnectionTimeout time.Duration `properties:"ryuk.reconnection.timeout,default=10s"`
RyukConnectionTimeout time.Duration `properties:"ryuk.connection.timeout,default=1m"`
RyukVerbose bool `properties:"ryuk.verbose,default=false"`
TestcontainersHost string `properties:"tc.host,default="`
}

Expand Down Expand Up @@ -80,6 +81,11 @@ func read() Config {
config.RyukPrivileged = ryukPrivilegedEnv == "true"
}

ryukVerboseEnv := os.Getenv("TESTCONTAINERS_RYUK_VERBOSE")
if parseBool(ryukVerboseEnv) {
config.RyukVerbose = ryukVerboseEnv == "true"
}

return config
}

Expand Down
53 changes: 53 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func resetTestEnv(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "")
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "")
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "")
t.Setenv("TESTCONTAINERS_RYUK_VERBOSE", "")
}

func TestReadConfig(t *testing.T) {
Expand Down Expand Up @@ -117,12 +118,14 @@ 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_VERBOSE", "true")

config := read()
expected := Config{
HubImageNamePrefix: defaultHubPrefix,
RyukDisabled: true,
RyukPrivileged: true,
RyukVerbose: true,
}

assert.Equal(t, expected, config)
Expand Down Expand Up @@ -260,6 +263,16 @@ func TestReadTCConfig(t *testing.T) {
RyukConnectionTimeout: 12 * time.Second,
},
},
{
"With Ryuk verbose configured using properties",
`ryuk.verbose=true`,
map[string]string{},
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
},
},
{
"With Ryuk disabled using an env var",
``,
Expand Down Expand Up @@ -324,6 +337,46 @@ func TestReadTCConfig(t *testing.T) {
},
defaultConfig,
},
{
"With Ryuk verbose using an env var and properties. Env var wins (0)",
`ryuk.verbose=true`,
map[string]string{
"TESTCONTAINERS_RYUK_VERBOSE": "true",
},
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
},
},
{
"With Ryuk verbose using an env var and properties. Env var wins (1)",
`ryuk.verbose=false`,
map[string]string{
"TESTCONTAINERS_RYUK_VERBOSE": "true",
},
Config{
RyukVerbose: true,
RyukConnectionTimeout: defaultRyukConnectionTimeout,
RyukReconnectionTimeout: defaultRyukReonnectionTimeout,
},
},
{
"With Ryuk verbose using an env var and properties. Env var wins (2)",
`ryuk.verbose=true`,
map[string]string{
"TESTCONTAINERS_RYUK_VERBOSE": "false",
},
defaultConfig,
},
{
"With Ryuk verbose using an env var and properties. Env var wins (3)",
`ryuk.verbose=false`,
map[string]string{
"TESTCONTAINERS_RYUK_VERBOSE": "false",
},
defaultConfig,
},
{
"With Ryuk container privileged using an env var and properties. Env var wins (0)",
`ryuk.container.privileged=true`,
Expand Down
3 changes: 3 additions & 0 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ func newReaper(ctx context.Context, sessionID string, provider ReaperProvider) (
if to := tcConfig.RyukReconnectionTimeout; to > time.Duration(0) {
req.Env["RYUK_RECONNECTION_TIMEOUT"] = to.String()
}
if tcConfig.RyukVerbose {
req.Env["RYUK_VERBOSE"] = "true"
}

// include reaper-specific labels to the reaper container
req.Labels[testcontainersdocker.LabelReaper] = "true"
Expand Down
13 changes: 13 additions & 0 deletions reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,19 @@ func Test_NewReaper(t *testing.T) {
RyukReconnectionTimeout: 10 * time.Minute,
}},
},
{
name: "configured verbose mode",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
req.Env = map[string]string{
"RYUK_VERBOSE": "true",
}
return req
}),
config: TestcontainersConfig{Config: config.Config{
RyukPrivileged: true,
RyukVerbose: true,
}},
},
{
name: "docker-host in context",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
Expand Down

0 comments on commit 9f68760

Please sign in to comment.