Skip to content

Commit 5ff4ae1

Browse files
authored
feat: Set ryuk.container.privileged default value to true (#1313)
1 parent e982134 commit 5ff4ae1

File tree

9 files changed

+20
-17
lines changed

9 files changed

+20
-17
lines changed

docs/custom_configuration/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Testcontainers supports various configurations to set up your test environment.
1414
| `host.override` | `TESTCONTAINERS_HOST_OVERRIDE` | The host that exposes Docker's ports. | - |
1515
| `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` |
1616
| `ryuk.disabled` | `TESTCONTAINERS_RYUK_DISABLED` | Disables Ryuk (resource reaper). | `false` |
17-
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `false` |
17+
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `true` |
1818
| `ryuk.container.image` | `TESTCONTAINERS_RYUK_CONTAINER_IMAGE` | The Ryuk (resource reaper) Docker image. | `testcontainers/ryuk:0.5.1` |
1919
| `hub.image.name.prefix` | `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX` | The name to use for substituting the Docker Hub registry part of the image name. | - |
2020
| `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |

src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public bool GetRyukDisabled()
9191
}
9292

9393
/// <inheritdoc />
94-
public bool GetRyukContainerPrivileged()
94+
public bool? GetRyukContainerPrivileged()
9595
{
9696
return false;
9797
}

src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public bool GetRyukDisabled()
118118
}
119119

120120
/// <inheritdoc />
121-
public bool GetRyukContainerPrivileged()
121+
public bool? GetRyukContainerPrivileged()
122122
{
123123
return _customConfiguration.GetRyukContainerPrivileged();
124124
}

src/Testcontainers/Configurations/CustomConfiguration.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ protected virtual bool GetRyukDisabled(string propertyName)
7979
return GetPropertyValue<bool>(propertyName);
8080
}
8181

82-
protected virtual bool GetRyukContainerPrivileged(string propertyName)
82+
protected virtual bool? GetRyukContainerPrivileged(string propertyName)
8383
{
84-
return GetPropertyValue<bool>(propertyName);
84+
return GetPropertyValue<bool?>(propertyName);
8585
}
8686

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

130+
var isNullable = type != typeof(T);
131+
130132
switch (Type.GetTypeCode(type))
131133
{
132134
case TypeCode.Boolean:
133135
{
134-
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ("1".Equals(propertyValue, StringComparison.Ordinal) || (bool.TryParse(propertyValue, out var result) && result)));
136+
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && bool.TryParse(propertyValue, out var result) ? result : isNullable ? null : "1".Equals(propertyValue, StringComparison.Ordinal));
135137
}
136138

137139
case TypeCode.UInt16:
138140
{
139-
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ushort.TryParse(propertyValue, out var result) ? result : null);
141+
return (T)(object)(_properties.TryGetValue(propertyName, out var propertyValue) && ushort.TryParse(propertyValue, out var result) ? result : isNullable ? null : 0);
140142
}
141143

142144
case TypeCode.String:

src/Testcontainers/Configurations/EnvironmentConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public bool GetRyukDisabled()
140140
}
141141

142142
/// <inheritdoc />
143-
public bool GetRyukContainerPrivileged()
143+
public bool? GetRyukContainerPrivileged()
144144
{
145145
return GetRyukContainerPrivileged(RyukContainerPrivileged);
146146
}

src/Testcontainers/Configurations/ICustomConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ internal interface ICustomConfiguration
9292
/// </summary>
9393
/// <returns>The Ryuk container privileged custom configuration.</returns>
9494
/// <remarks>https://dotnet.testcontainers.org/custom_configuration/.</remarks>
95-
bool GetRyukContainerPrivileged();
95+
[CanBeNull]
96+
bool? GetRyukContainerPrivileged();
9697

9798
/// <summary>
9899
/// Gets the Ryuk container image custom configuration.

src/Testcontainers/Configurations/PropertiesFileConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public bool GetRyukDisabled()
127127
}
128128

129129
/// <inheritdoc />
130-
public bool GetRyukContainerPrivileged()
130+
public bool? GetRyukContainerPrivileged()
131131
{
132132
const string propertyName = "ryuk.container.privileged";
133133
return GetRyukContainerPrivileged(propertyName);

src/Testcontainers/Configurations/TestcontainersSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static TestcontainersSettings()
6767
/// Gets or sets a value indicating whether the <see cref="ResourceReaper" /> privileged mode is enabled or not.
6868
/// </summary>
6969
public static bool ResourceReaperPrivilegedModeEnabled { get; set; }
70-
= EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() || PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged();
70+
= EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() ?? PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged() ?? true;
7171

7272
/// <summary>
7373
/// Gets or sets the <see cref="ResourceReaper" /> image.

tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ public void GetRyukDisabledCustomConfiguration(string propertyName, string prope
159159
}
160160

161161
[Theory]
162-
[InlineData("", "", false)]
163-
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "", false)]
162+
[InlineData("", "", null)]
163+
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "", null)]
164164
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "false", false)]
165165
[InlineData("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true", true)]
166-
public void GetRyukContainerPrivilegedCustomConfiguration(string propertyName, string propertyValue, bool expected)
166+
public void GetRyukContainerPrivilegedCustomConfiguration(string propertyName, string propertyValue, bool? expected)
167167
{
168168
SetEnvironmentVariable(propertyName, propertyValue);
169169
ICustomConfiguration customConfiguration = new EnvironmentConfiguration();
@@ -362,11 +362,11 @@ public void GetRyukDisabledCustomConfiguration(string configuration, bool expect
362362
}
363363

364364
[Theory]
365-
[InlineData("", false)]
366-
[InlineData("ryuk.container.privileged=", false)]
365+
[InlineData("", null)]
366+
[InlineData("ryuk.container.privileged=", null)]
367367
[InlineData("ryuk.container.privileged=false", false)]
368368
[InlineData("ryuk.container.privileged=true", true)]
369-
public void GetRyukContainerPrivilegedCustomConfiguration(string configuration, bool expected)
369+
public void GetRyukContainerPrivilegedCustomConfiguration(string configuration, bool? expected)
370370
{
371371
ICustomConfiguration customConfiguration = new PropertiesFileConfiguration(new[] { configuration });
372372
Assert.Equal(expected, customConfiguration.GetRyukContainerPrivileged());

0 commit comments

Comments
 (0)