Skip to content

Commit

Permalink
[DEBUG] Add logs to diagnose failing test on GitHub actions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Aug 19, 2024
1 parent 7df5dc8 commit 401d30f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/Testcontainers/Builders/DockerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using DotNet.Testcontainers.Configurations;
using Microsoft.Extensions.Logging;
using JetBrains.Annotations;

internal class DockerConfig
Expand Down Expand Up @@ -45,43 +47,61 @@ public JsonDocument Parse()
}

[CanBeNull]
public Uri GetCurrentEndpoint()
public Uri GetCurrentEndpoint([CanBeNull] ILogger logger = null)
{
try
{
logger?.LogInformation("{Config}", File.ReadAllText(FullName));
using (var config = Parse())
{
if (config.RootElement.TryGetProperty("currentContext", out var currentContextNode) && currentContextNode.ValueKind == JsonValueKind.String)
{
var currentContext = currentContextNode.GetString();
foreach (var metaDirectory in Directory.EnumerateDirectories(UserProfileDockerContextMetaPath, "*", SearchOption.TopDirectoryOnly))
logger?.LogInformation("The current context is {CurrentContext}", currentContext);
var directories = Directory.GetDirectories(UserProfileDockerContextMetaPath, "*", SearchOption.TopDirectoryOnly);
logger?.LogInformation("Searching {DirectoryCount} directories", directories.Length);
foreach (var metaDirectory in directories)
{
var endpoint = GetEndpoint(metaDirectory, currentContext);
logger?.LogInformation("Searching {MetaDirectory}", metaDirectory);
var endpoint = GetEndpoint(metaDirectory, currentContext, logger);
if (endpoint != null)
{
return endpoint;
}
}
}
else
{
logger?.LogInformation("The current context is not found");
}
}
}
catch
catch (Exception exception)
{
logger?.LogError(exception, "GetCurrentEndpoint failed");
return null;
}

return null;
}

[CanBeNull]
private static Uri GetEndpoint(string metaDirectory, string currentContext)
private static Uri GetEndpoint(string metaDirectory, string currentContext, [CanBeNull] ILogger logger)
{
try
{
var metaFilePath = Path.Combine(metaDirectory, "meta.json");
logger?.LogInformation("Reading {MetaFilePath}", metaFilePath);
using (var metaFileStream = new FileStream(metaFilePath, FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(metaFileStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: true))
{
logger?.LogInformation("{MetaContent}", reader.ReadToEnd());
}
metaFileStream.Position = 0;
var meta = JsonSerializer.Deserialize(metaFileStream, SourceGenerationContext.Default.DockerContextMeta);
logger?.LogInformation("Meta name: {MetaName}", meta?.Name);
logger?.LogInformation("Meta host: {MetaHost}", meta?.Endpoints?.Docker?.Host);
if (meta?.Name == currentContext)
{
var host = meta?.Endpoints?.Docker?.Host;
Expand All @@ -92,9 +112,9 @@ private static Uri GetEndpoint(string metaDirectory, string currentContext)
}
}
}
catch
catch (Exception exception)
{
return null;
logger?.LogError(exception, "GetEndpoint failed");
}

return null;
Expand Down
20 changes: 19 additions & 1 deletion tests/Testcontainers.Tests/Unit/Builders/DockerConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Commons;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -11,9 +13,25 @@ public class DockerConfigTest(ITestOutputHelper output)
public void GetCurrentEndpoint()
{
var expectedEndpoint = DockerCli.GetCurrentEndpoint();
var endpoint = DockerConfig.Default.GetCurrentEndpoint();
var endpoint = DockerConfig.Default.GetCurrentEndpoint(new TestOutputLogger(output));
output.WriteLine($"DockerConfig.Default.GetCurrentEndpoint() => {endpoint}");
Assert.Equal(expectedEndpoint, endpoint);

Check failure on line 18 in tests/Testcontainers.Tests/Unit/Builders/DockerConfigTest.cs

View workflow job for this annotation

GitHub Actions / report (ubuntu-22.04)

DotNet.Testcontainers.Tests.Unit.DockerConfigTest ► GetCurrentEndpoint

Failed test found in: Testcontainers.Tests.trx Error: Assert.Equal() Failure: Values differ Expected: unix:///var/run/docker.sock Actual: null
Raw output
Assert.Equal() Failure: Values differ
Expected: unix:///var/run/docker.sock
Actual:   null
   at DotNet.Testcontainers.Tests.Unit.DockerConfigTest.GetCurrentEndpoint() in /home/runner/work/testcontainers-dotnet/testcontainers-dotnet/tests/Testcontainers.Tests/Unit/Builders/DockerConfigTest.cs:line 18
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
}
}

internal class TestOutputLogger(ITestOutputHelper output) : ILogger
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
output.WriteLine(formatter(state, exception));
if (exception != null)
{
output.WriteLine(exception.ToString());
}
}

public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public IDisposable BeginScope<TState>(TState state) => null!;
}
}

0 comments on commit 401d30f

Please sign in to comment.