-
-
Notifications
You must be signed in to change notification settings - Fork 313
Improve error reporting when loading the Docker configuration file #1263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for testcontainers-dotnet ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
f5a5f7a
to
1e95bea
Compare
2e15016
to
49ef7dc
Compare
1e95bea
to
11cf6cc
Compare
11cf6cc
to
a527375
Compare
a527375
to
beaa485
Compare
beaa485
to
43b87e5
Compare
43b87e5
to
6641034
Compare
using (var metaFileStream = File.OpenRead(metaFilePath)) | ||
catch (Exception notFoundException) when (notFoundException is DirectoryNotFoundException or FileNotFoundException) | ||
{ | ||
var meta = JsonSerializer.Deserialize(metaFileStream, SourceGenerationContext.Default.DockerContextMeta); | ||
var host = meta?.Name == dockerContext ? meta.Endpoints?.Docker?.Host : null; | ||
return string.IsNullOrEmpty(host) ? null : new Uri(host.Replace("npipe:////./", "npipe://./")); | ||
throw new DockerConfigurationException($"The Docker context '{dockerContext}' does not exist", notFoundException); | ||
} | ||
catch (Exception exception) when (exception is not DockerConfigurationException) | ||
{ | ||
throw new DockerConfigurationException($"The Docker context '{dockerContext}' failed to load from {metaFilePath}", exception); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to explain my concerns. Maybe this is hypothetical, but let's assume:
- The
~/.docker/config.json
file exists. - The file contains a
currentContext
node. - The meta directory or file does not exist, or the
currentContext
cannot be found. - The user runs a remote container runtime, e.g. configured with
WithDockerEndpoint(Uri)
.
With this implementation, an exception will be thrown when creating the list of Docker endpoints to check for availability.
System.TypeInitializationException
The type initializer for 'DotNet.Testcontainers.Configurations.TestcontainersSettings' threw an exception.
DotNet.Testcontainers.Builders.DockerConfigurationException
The Docker context '...' does not exist
System.IO.DirectoryNotFoundException
Could not find a part of the path '...'.
This prevents users from overriding the builder configuration using the API mentioned above. That said, I don't think WithDockerEndpoint(Uri)
is the real issue here. If a user falls back to this API by default, something else is probably wrong.
My main concern is that this exception could also happen in other configurations. I believe this was one of the reasons we didn't throw exceptions in the initial implementation of Docker context support (IIRC, we removed it on purpose). Until now, we've been failing silently allowing another provider to find an available Docker endpoint.
The
DockerConfigTests
were updated and a newThrowsWhenDockerConfigEndpointNotFound
test was added.
I definitely agree with you on this. It bothers me too. I don't have a strong opinion on this PR. I was considering adding more logging to show which provider is processed and chosen (and which ones aren't). But if you don't see any issues with throwing an exception, we can go ahead and merge it. LMKWYT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it's a bit unfortunate to throw if a valid endpoint is configured through WithDockerEndpoint(Uri)
. On the other hand, not throwing in this case makes it really hard to diagnose a broken Docker Desktop configuration.
Also, this case should really be exceptional and I think it warrants an exception that must be addressed by actually fixing the Docker Desktop configuration.
There's also this important change in the DockerDesktopEndpointAuthenticationProvider:
diff --git b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
index f2363f89..e1352585 100644
--- b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
+++ a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
@@ -15,7 +15,7 @@ namespace DotNet.Testcontainers.Builders
/// Initializes a new instance of the <see cref="DockerDesktopEndpointAuthenticationProvider" /> class.
/// </summary>
public DockerDesktopEndpointAuthenticationProvider()
- : base(DockerConfig.Instance.GetCurrentEndpoint()?.AbsolutePath, GetSocketPathFromHomeDesktopDir(), GetSocketPathFromHomeRunDir())
+ : base(DockerConfig.Instance.GetCurrentEndpoint())
{
}
It means that Testcontainers won't blindly try to connect to potentially valid sockets (from home desktop or home run directories). I've experienced issues doing so in the past where the socket was existing and but connecting to the socket would just stall because the engine was not running. It was very hard to diagnose!
What does this PR do?
This pull request improves error handling while reading the Docker context configuration file. It throws a new
DockerConfigurationException
instead of silently failing.Also, since
GetCurrentEndpoint()
can't returnnull
anymore there's no need to try the defaultGetSocketPathFromHomeDesktopDir()
andGetSocketPathFromHomeRunDir()
socket paths.Why is it important?
Silently returning
null
instead of throwing proper exceptions makes it harder to diagnose issues.Related issues
How to test this PR
The
DockerConfigTests
were updated and a newThrowsWhenDockerConfigEndpointNotFound
test was added.