testcontainers-dotnet icon indicating copy to clipboard operation
testcontainers-dotnet copied to clipboard

BUG: DotNet.Testcontainers.Tests.Unit.DockerEndpointAuthenticationProviderTest.GetDockerClientConfiguration is failing

Open vlaskal opened this issue 3 years ago • 1 comments

Describe the bug Unit test DotNet.Testcontainers.Tests.Unit.DockerEndpointAuthenticationProviderTest.GetDockerClientConfiguration is failing when all test are executed. I think it is related to change in file src/Testcontainers/Configurations/EnvironmentConfiguration.cs in PR #550:

/// <summary>
/// Gets the <see cref="ICustomConfiguration" /> instance.
/// </summary>
public static ICustomConfiguration Instance { get; }
      = new EnvironmentConfiguration();

and usage in file src/Testcontainers/Builders/EnvironmentEndpointAuthenticationProvider.cs

public EnvironmentEndpointAuthenticationProvider()
{
  this.dockerEngine = PropertiesFileConfiguration.Instance.GetDockerHost() ?? EnvironmentConfiguration.Instance.GetDockerHost();
}

To Reproduce Steps to reproduce the behavior:

  1. There is not testcontainer setting file or empty
  2. There is not environment variable DOCKER_HOST set in the OS
  3. Run all tests (at least one which needs to get DockerClientConfiguration)
  4. GetDockerClientConfiguration test for EnvironmentEndpointAuthenticationProvider test case fail on error System.ArgumentNullException : Value cannot be null. (Parameter 'endpoint')

Expected behavior Test will pass independently on OS setup and order of test execution.

Screenshots image

Desktop (please complete the following information):

  • latest develop branch

Additional context I think what happen is that first test which checks some module alreadz trigger static constructor of EnvironmentConfiguration.cs and data are loaded from OS. When test GetDockerClientConfiguration set environment variable to any value, then it is too late. When the test is executed alone then it works because static constructor is invoked by the first touch of the class. So current implementation of tha test cases of the test is not correct or the implementation should be more flexible. Current implementation:

public AuthConfigTestData()
{
  const string dockerHost = "tcp://127.0.0.1:2375";
  Environment.SetEnvironmentVariable("DOCKER_HOST", dockerHost);
  this.Add(new object[] { new EnvironmentEndpointAuthenticationProvider().GetAuthConfig(), new Uri(dockerHost) });
  this.Add(new object[] { new NpipeEndpointAuthenticationProvider().GetAuthConfig(), new Uri("npipe://./pipe/docker_engine") });
  this.Add(new object[] { new UnixEndpointAuthenticationProvider().GetAuthConfig(), new Uri("unix:/var/run/docker.sock") });
  Environment.SetEnvironmentVariable("DOCKER_HOST", null);
}

I found it more problematic when I wanted to extend this test for PR #548 by following code which does not work completly:

public AuthConfigTestData()
{
  const string dockerHost = "tcp://127.0.0.1:2375";
  const string dockerHostTls = "tcp://127.0.0.1:2376";

  Environment.SetEnvironmentVariable("DOCKER_HOST", dockerHostTls);
  this.Add(new object[] { new TlsEndpointAuthenticationProvider().GetAuthConfig(), new Uri(dockerHostTls) });

  Environment.SetEnvironmentVariable("DOCKER_HOST", dockerHost);
  this.Add(new object[] { new EnvironmentEndpointAuthenticationProvider().GetAuthConfig(), new Uri(dockerHost) });
  
  this.Add(new object[] { new NpipeEndpointAuthenticationProvider().GetAuthConfig(), new Uri("npipe://./pipe/docker_engine") });
  this.Add(new object[] { new UnixEndpointAuthenticationProvider().GetAuthConfig(), new Uri("unix:/var/run/docker.sock") });
  Environment.SetEnvironmentVariable("DOCKER_HOST", null);
}

I would like to ask which way to use as solution. I need to solve it before I will continue on PR #548

vlaskal avatar Sep 06 '22 22:09 vlaskal

I added PR with possible solution #577.

vlaskal avatar Sep 07 '22 11:09 vlaskal