Skip to content

Commit

Permalink
chore: Apply minor improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Nov 1, 2023
1 parent 7daf856 commit 0d3f6cc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers/Configurations/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DotNet.Testcontainers.Configurations
public enum FileSystem
{
/// <summary>
/// The file system of the host machine.
/// The test host file system.
/// </summary>
Host = 0,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public interface IWaitForContainerOS
/// <summary>
/// Waits until the file exists.
/// </summary>
/// <param name="file">The file to be checked.</param>
/// <param name="filePath">The file path to be checked.</param>
/// <param name="fileSystem">The file system to be checked.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilFileExists(string file, FileSystem fileSystem = FileSystem.Host);
IWaitForContainerOS UntilFileExists(string filePath, FileSystem fileSystem = FileSystem.Host);

/// <summary>
/// Waits until the message is logged.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace DotNet.Testcontainers.Configurations
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;

internal class UntilFilesExistsInContainer : IWaitUntil
internal class UntilFileExistsInContainer : IWaitUntil
{
private readonly string _file;

public UntilFilesExistsInContainer(string file)
public UntilFileExistsInContainer(string file)
{
_file = file;
}
Expand All @@ -17,7 +17,9 @@ public async Task<bool> UntilAsync(IContainer container)
{
try
{
await container.ReadFileAsync(_file);
_ = await container.ReadFileAsync(_file)
.ConfigureAwait(false);

return true;
}
catch (FileNotFoundException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace DotNet.Testcontainers.Configurations
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;

internal class UntilFilesExists : IWaitUntil
internal class UntilFileExistsOnHost : IWaitUntil
{
private readonly string _file;

public UntilFilesExists(string file)
public UntilFileExistsOnHost(string file)
{
_file = file;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public virtual IWaitForContainerOS AddCustomWaitStrategy(IWaitUntil waitStrategy
}

/// <inheritdoc />
public virtual IWaitForContainerOS UntilFileExists(string file, FileSystem fileSystem = FileSystem.Host)
public virtual IWaitForContainerOS UntilFileExists(string filePath, FileSystem fileSystem = FileSystem.Host)
{
switch (fileSystem)
{
case FileSystem.Container:
return AddCustomWaitStrategy(new UntilFilesExistsInContainer(file));
return AddCustomWaitStrategy(new UntilFileExistsInContainer(filePath));
case FileSystem.Host:
default:
return AddCustomWaitStrategy(new UntilFilesExists(file));
return AddCustomWaitStrategy(new UntilFileExistsOnHost(filePath));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Commons;
using DotNet.Testcontainers.Configurations;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Commons;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Xunit;

public sealed class WaitUntilFileExistsInContainerTest : IDisposable
public sealed class WaitUntilFileExistsInContainerTest : IAsyncLifetime, IDisposable
{
private readonly CancellationTokenSource _cts = new(TimeSpan.FromMinutes(1));
private const string ContainerFilePath = "/tmp/hostname";

private readonly CancellationTokenSource _cts = new CancellationTokenSource(TimeSpan.FromMinutes(1));

private readonly IContainer _container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand("hostname > " + ContainerFilePath + "; trap : TERM INT; sleep infinity & wait")
.WithWaitStrategy(Wait.ForUnixContainer().UntilFileExists(ContainerFilePath, FileSystem.Container))
.Build();

public Task InitializeAsync()
{
return _container.StartAsync(_cts.Token);
}

public Task DisposeAsync()
{
return _container.DisposeAsync().AsTask();
}

public void Dispose()
{
Expand All @@ -21,25 +41,10 @@ public void Dispose()
[Fact]
public async Task ContainerIsRunning()
{
// Given
const string target = "tmp";

const string file = "hostname";

await using var container = new ContainerBuilder()
.WithImage(CommonImages.Nginx)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand($"hostname > /{target}/{file}; trap : TERM INT; sleep infinity & wait")
.WithWaitStrategy(Wait.ForUnixContainer().UntilFileExists($"/{target}/{file}", FileSystem.Container))
.Build();

// When
await container.StartAsync(_cts.Token)
var execResult = await _container.ExecAsync(new List<string> { "test", "-f", ContainerFilePath })
.ConfigureAwait(false);

// Then
var catResult = await container.ExecAsync(new List<string> { "cat", $"/{target}/{file}" });
Assert.NotEmpty(catResult.Stdout);
Assert.Equal(0, execResult.ExitCode);
}
}
}

0 comments on commit 0d3f6cc

Please sign in to comment.