Skip to content

Commit

Permalink
feat: Set ryuk.container.privileged default value to true (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Dec 7, 2024
1 parent e982134 commit 5ff4ae1
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/custom_configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Testcontainers supports various configurations to set up your test environment.
| `host.override` | `TESTCONTAINERS_HOST_OVERRIDE` | The host that exposes Docker's ports. | - |
| `docker.socket.override` | `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE` | The file path to the Docker daemon socket that is used by Ryuk (resource reaper). | `/var/run/docker.sock` |
| `ryuk.disabled` | `TESTCONTAINERS_RYUK_DISABLED` | Disables Ryuk (resource reaper). | `false` |
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `false` |
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `true` |
| `ryuk.container.image` | `TESTCONTAINERS_RYUK_CONTAINER_IMAGE` | The Ryuk (resource reaper) Docker image. | `testcontainers/ryuk:0.5.1` |
| `hub.image.name.prefix` | `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX` | The name to use for substituting the Docker Hub registry part of the image name. | - |
| `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public bool GetRyukDisabled()
}

/// <inheritdoc />
public bool GetRyukContainerPrivileged()
public bool? GetRyukContainerPrivileged()
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public bool GetRyukDisabled()
}

/// <inheritdoc />
public bool GetRyukContainerPrivileged()
public bool? GetRyukContainerPrivileged()
{
return _customConfiguration.GetRyukContainerPrivileged();
}
Expand Down
10 changes: 6 additions & 4 deletions src/Testcontainers/Configurations/CustomConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ protected virtual bool GetRyukDisabled(string propertyName)
return GetPropertyValue<bool>(propertyName);
}

protected virtual bool GetRyukContainerPrivileged(string propertyName)
protected virtual bool? GetRyukContainerPrivileged(string propertyName)
{
return GetPropertyValue<bool>(propertyName);
return GetPropertyValue<bool?>(propertyName);
}

protected virtual IImage GetRyukContainerImage(string propertyName)
Expand Down Expand Up @@ -127,16 +127,18 @@ private T GetPropertyValue<T>(string propertyName)
{
var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

var isNullable = type != typeof(T);

switch (Type.GetTypeCode(type))
{
case TypeCode.Boolean:
{
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ("1".Equals(propertyValue, StringComparison.Ordinal) || (bool.TryParse(propertyValue, out var result) && result)));
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && bool.TryParse(propertyValue, out var result) ? result : isNullable ? null : "1".Equals(propertyValue, StringComparison.Ordinal));
}

case TypeCode.UInt16:
{
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ushort.TryParse(propertyValue, out var result) ? result : null);
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ushort.TryParse(propertyValue, out var result) ? result : isNullable ? null : 0);
}

case TypeCode.String:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public bool GetRyukDisabled()
}

/// <inheritdoc />
public bool GetRyukContainerPrivileged()
public bool? GetRyukContainerPrivileged()
{
return GetRyukContainerPrivileged(RyukContainerPrivileged);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Testcontainers/Configurations/ICustomConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ internal interface ICustomConfiguration
/// </summary>
/// <returns>The Ryuk container privileged custom configuration.</returns>
/// <remarks>https://dotnet.testcontainers.org/custom_configuration/.</remarks>
bool GetRyukContainerPrivileged();
[CanBeNull]
bool? GetRyukContainerPrivileged();

/// <summary>
/// Gets the Ryuk container image custom configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public bool GetRyukDisabled()
}

/// <inheritdoc />
public bool GetRyukContainerPrivileged()
public bool? GetRyukContainerPrivileged()
{
const string propertyName = "ryuk.container.privileged";
return GetRyukContainerPrivileged(propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static TestcontainersSettings()
/// Gets or sets a value indicating whether the <see cref="ResourceReaper" /> privileged mode is enabled or not.
/// </summary>
public static bool ResourceReaperPrivilegedModeEnabled { get; set; }
= EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() || PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged();
= EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() ?? PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged() ?? true;

/// <summary>
/// Gets or sets the <see cref="ResourceReaper" /> image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ public void GetRyukDisabledCustomConfiguration(string propertyName, string prope
}

[Theory]
[InlineData("", "", false)]
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "", false)]
[InlineData("", "", null)]
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "", null)]
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "false", false)]
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true", true)]
public void GetRyukContainerPrivilegedCustomConfiguration(string propertyName, string propertyValue, bool expected)
public void GetRyukContainerPrivilegedCustomConfiguration(string propertyName, string propertyValue, bool? expected)
{
SetEnvironmentVariable(propertyName, propertyValue);
ICustomConfiguration customConfiguration = new EnvironmentConfiguration();
Expand Down Expand Up @@ -362,11 +362,11 @@ public void GetRyukDisabledCustomConfiguration(string configuration, bool expect
}

[Theory]
[InlineData("", false)]
[InlineData("ryuk.container.privileged=", false)]
[InlineData("", null)]
[InlineData("ryuk.container.privileged=", null)]
[InlineData("ryuk.container.privileged=false", false)]
[InlineData("ryuk.container.privileged=true", true)]
public void GetRyukContainerPrivilegedCustomConfiguration(string configuration, bool expected)
public void GetRyukContainerPrivilegedCustomConfiguration(string configuration, bool? expected)
{
ICustomConfiguration customConfiguration = new PropertiesFileConfiguration(new[] { configuration });
Assert.Equal(expected, customConfiguration.GetRyukContainerPrivileged());
Expand Down

0 comments on commit 5ff4ae1

Please sign in to comment.