From 369f58ce290920f16c604bdc11707fd5e63734e3 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Sat, 7 Dec 2024 11:14:25 +0100 Subject: [PATCH] feat: Set ryuk.container.privileged default value to true --- docs/custom_configuration/index.md | 2 +- .../DockerDesktopEndpointAuthenticationProvider.cs | 2 +- .../TestcontainersEndpointAuthenticationProvider.cs | 2 +- .../Configurations/CustomConfiguration.cs | 10 ++++++---- .../Configurations/EnvironmentConfiguration.cs | 2 +- .../Configurations/ICustomConfiguration.cs | 3 ++- .../Configurations/PropertiesFileConfiguration.cs | 2 +- .../Configurations/TestcontainersSettings.cs | 2 +- .../Unit/Configurations/CustomConfigurationTest.cs | 12 ++++++------ 9 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/custom_configuration/index.md b/docs/custom_configuration/index.md index f44be6c6d..32dda6c64 100644 --- a/docs/custom_configuration/index.md +++ b/docs/custom_configuration/index.md @@ -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` | diff --git a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs index c7eddad4a..f2363f89a 100644 --- a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs +++ b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs @@ -91,7 +91,7 @@ public bool GetRyukDisabled() } /// - public bool GetRyukContainerPrivileged() + public bool? GetRyukContainerPrivileged() { return false; } diff --git a/src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs b/src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs index 93a42976d..49cedf99c 100644 --- a/src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs +++ b/src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs @@ -118,7 +118,7 @@ public bool GetRyukDisabled() } /// - public bool GetRyukContainerPrivileged() + public bool? GetRyukContainerPrivileged() { return _customConfiguration.GetRyukContainerPrivileged(); } diff --git a/src/Testcontainers/Configurations/CustomConfiguration.cs b/src/Testcontainers/Configurations/CustomConfiguration.cs index c757b7f26..8f2166844 100644 --- a/src/Testcontainers/Configurations/CustomConfiguration.cs +++ b/src/Testcontainers/Configurations/CustomConfiguration.cs @@ -79,9 +79,9 @@ protected virtual bool GetRyukDisabled(string propertyName) return GetPropertyValue(propertyName); } - protected virtual bool GetRyukContainerPrivileged(string propertyName) + protected virtual bool? GetRyukContainerPrivileged(string propertyName) { - return GetPropertyValue(propertyName); + return GetPropertyValue(propertyName); } protected virtual IImage GetRyukContainerImage(string propertyName) @@ -127,16 +127,18 @@ private T GetPropertyValue(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: diff --git a/src/Testcontainers/Configurations/EnvironmentConfiguration.cs b/src/Testcontainers/Configurations/EnvironmentConfiguration.cs index 2d5706944..cdacdc50a 100644 --- a/src/Testcontainers/Configurations/EnvironmentConfiguration.cs +++ b/src/Testcontainers/Configurations/EnvironmentConfiguration.cs @@ -140,7 +140,7 @@ public bool GetRyukDisabled() } /// - public bool GetRyukContainerPrivileged() + public bool? GetRyukContainerPrivileged() { return GetRyukContainerPrivileged(RyukContainerPrivileged); } diff --git a/src/Testcontainers/Configurations/ICustomConfiguration.cs b/src/Testcontainers/Configurations/ICustomConfiguration.cs index 7f0b173c2..f0a205d8c 100644 --- a/src/Testcontainers/Configurations/ICustomConfiguration.cs +++ b/src/Testcontainers/Configurations/ICustomConfiguration.cs @@ -92,7 +92,8 @@ internal interface ICustomConfiguration /// /// The Ryuk container privileged custom configuration. /// https://dotnet.testcontainers.org/custom_configuration/. - bool GetRyukContainerPrivileged(); + [CanBeNull] + bool? GetRyukContainerPrivileged(); /// /// Gets the Ryuk container image custom configuration. diff --git a/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs b/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs index 7e303f33e..2870e5449 100644 --- a/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs +++ b/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs @@ -127,7 +127,7 @@ public bool GetRyukDisabled() } /// - public bool GetRyukContainerPrivileged() + public bool? GetRyukContainerPrivileged() { const string propertyName = "ryuk.container.privileged"; return GetRyukContainerPrivileged(propertyName); diff --git a/src/Testcontainers/Configurations/TestcontainersSettings.cs b/src/Testcontainers/Configurations/TestcontainersSettings.cs index efb611a91..3f7e3d57f 100644 --- a/src/Testcontainers/Configurations/TestcontainersSettings.cs +++ b/src/Testcontainers/Configurations/TestcontainersSettings.cs @@ -67,7 +67,7 @@ static TestcontainersSettings() /// Gets or sets a value indicating whether the privileged mode is enabled or not. /// public static bool ResourceReaperPrivilegedModeEnabled { get; set; } - = EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() || PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged(); + = EnvironmentConfiguration.Instance.GetRyukContainerPrivileged() ?? PropertiesFileConfiguration.Instance.GetRyukContainerPrivileged() ?? true; /// /// Gets or sets the image. diff --git a/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs b/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs index 260dfa067..6b4276cbc 100644 --- a/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs +++ b/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs @@ -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(); @@ -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());