-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extend the "wait until file exists" API to distinguish between …
…the test host and container filesystem (#1009) Co-authored-by: Max Sevon <[email protected]> Co-authored-by: Andre Hofmeister <[email protected]>
- Loading branch information
1 parent
5d5cb2d
commit 871a9cd
Showing
6 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace DotNet.Testcontainers.Configurations | ||
{ | ||
using JetBrains.Annotations; | ||
|
||
/// <summary> | ||
/// Indicates the file system for file operations. | ||
/// </summary> | ||
[PublicAPI] | ||
public enum FileSystem | ||
{ | ||
/// <summary> | ||
/// The test host file system. | ||
/// </summary> | ||
Host = 0, | ||
|
||
/// <summary> | ||
/// The container file system. | ||
/// </summary> | ||
Container = 1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Testcontainers/Configurations/WaitStrategies/UntilFileExistsInContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace DotNet.Testcontainers.Configurations | ||
{ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Containers; | ||
|
||
internal class UntilFileExistsInContainer : IWaitUntil | ||
{ | ||
private readonly string _file; | ||
|
||
public UntilFileExistsInContainer(string file) | ||
{ | ||
_file = file; | ||
} | ||
|
||
public async Task<bool> UntilAsync(IContainer container) | ||
{ | ||
try | ||
{ | ||
_ = await container.ReadFileAsync(_file) | ||
.ConfigureAwait(false); | ||
|
||
return true; | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/Testcontainers.Tests/Unit/Configurations/WaitUntilFileExistsInContainerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit | ||
{ | ||
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 : IAsyncLifetime, IDisposable | ||
{ | ||
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() | ||
{ | ||
_cts.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public async Task ContainerIsRunning() | ||
{ | ||
var execResult = await _container.ExecAsync(new List<string> { "test", "-f", ContainerFilePath }) | ||
.ConfigureAwait(false); | ||
|
||
Assert.Equal(0, execResult.ExitCode); | ||
} | ||
} | ||
} |