|
| 1 | +namespace DotNet.Testcontainers.Tests.Unit |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Threading; |
| 6 | + using System.Threading.Tasks; |
| 7 | + using DotNet.Testcontainers.Builders; |
| 8 | + using DotNet.Testcontainers.Commons; |
| 9 | + using DotNet.Testcontainers.Configurations; |
| 10 | + using DotNet.Testcontainers.Containers; |
| 11 | + using Xunit; |
| 12 | + |
| 13 | + public sealed class WaitUntilFileExistsInContainerTest : IAsyncLifetime, IDisposable |
| 14 | + { |
| 15 | + private const string ContainerFilePath = "/tmp/hostname"; |
| 16 | + |
| 17 | + private readonly CancellationTokenSource _cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); |
| 18 | + |
| 19 | + private readonly IContainer _container = new ContainerBuilder() |
| 20 | + .WithImage(CommonImages.Alpine) |
| 21 | + .WithEntrypoint("/bin/sh", "-c") |
| 22 | + .WithCommand("hostname > " + ContainerFilePath + "; trap : TERM INT; sleep infinity & wait") |
| 23 | + .WithWaitStrategy(Wait.ForUnixContainer().UntilFileExists(ContainerFilePath, FileSystem.Container)) |
| 24 | + .Build(); |
| 25 | + |
| 26 | + public Task InitializeAsync() |
| 27 | + { |
| 28 | + return _container.StartAsync(_cts.Token); |
| 29 | + } |
| 30 | + |
| 31 | + public Task DisposeAsync() |
| 32 | + { |
| 33 | + return _container.DisposeAsync().AsTask(); |
| 34 | + } |
| 35 | + |
| 36 | + public void Dispose() |
| 37 | + { |
| 38 | + _cts.Dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public async Task ContainerIsRunning() |
| 43 | + { |
| 44 | + var execResult = await _container.ExecAsync(new List<string> { "test", "-f", ContainerFilePath }) |
| 45 | + .ConfigureAwait(false); |
| 46 | + |
| 47 | + Assert.Equal(0, execResult.ExitCode); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments