public async Task VolumeAndEnvironment() { // Given const string target = "tmp"; const string file = "dayOfWeek"; var dayOfWeek = DateTime.Now.DayOfWeek.ToString(); // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithCommand("/bin/sh", "-c", $"printf $dayOfWeek > /{target}/{file}") .WithEnvironment("dayOfWeek", dayOfWeek) .WithMount(TempDir, $"/{target}") .WithWaitStrategy(Wait.ForUnixContainer() .UntilFileExists($"{TempDir}/{file}")); await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } // Then Assert.Equal(dayOfWeek, await File.ReadAllTextAsync($"{TempDir}/{file}")); }
public async Task PortBindingsHttpAndHttps() { // Given var http = new { From = 80, To = 80 }; var https = new { From = 443, To = 80 }; var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx"); // When // Then foreach (var port in new[] { http, https }) { await using (IDockerContainer testcontainer = testcontainersBuilder .WithPortBinding(port.From, port.To) .WithWaitStrategy(Wait.ForUnixContainer() .UntilPortIsAvailable(port.To)) .Build()) { await testcontainer.StartAsync(); var request = WebRequest.Create($"http://localhost:{port.From}"); var response = (HttpWebResponse)await request.GetResponseAsync(); Assert.True(HttpStatusCode.OK.Equals(response.StatusCode), $"nginx port {port.From} is not available."); } } }
public async Task OutputConsumer() { // Given using (var consumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream())) { // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithCommand("/bin/sh", "-c", "hostname > /dev/stdout && hostname > /dev/stderr") .WithOutputConsumer(consumer); await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } consumer.Stdout.Position = 0; consumer.Stderr.Position = 0; // Then using (var streamReader = new StreamReader(consumer.Stdout, leaveOpen: true)) { Assert.NotEmpty(await streamReader.ReadToEndAsync()); } using (var streamReader = new StreamReader(consumer.Stderr, leaveOpen: true)) { Assert.NotEmpty(await streamReader.ReadToEndAsync()); } } }
public async Task OutputConsumer() { // Given using (var output = new OutputConsumerConsoleFixture()) { // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithOutputConsumer(output) .WithCommand("/bin/sh", "-c", "hostname > /dev/stdout && hostname > /dev/stderr"); await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } output.Stdout.Position = 0; output.Stderr.Position = 0; // Then using (var streamReader = new StreamReader(output.Stdout)) { Assert.NotEmpty(streamReader.ReadToEnd()); } using (var streamReader = new StreamReader(output.Stderr)) { Assert.NotEmpty(streamReader.ReadToEnd()); } } }
public async Task SafeDisposable() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task Disposable() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("mcr.microsoft.com/windows/nanoserver:1809"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task WaitStrategy() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithWaitStrategy(new WaitStrategyDelayForFiveSecondsFixture()); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task DockerEndpoint() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithDockerEndpoint(DockerApiEndpoint.Local.ToString()) .WithImage("alpine"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task ExposedPorts() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithExposedPort(80); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task UntilPortIsAvailable() { var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("mcr.microsoft.com/windows/servercore:1809") .WithEntrypoint("PowerShell", "-Command", "$tcpListener = [System.Net.Sockets.TcpListener]1337; $tcpListener.Start(); Start-Sleep -Seconds 3600") .WithWaitStrategy(Wait.ForWindowsContainer() .UntilPortIsAvailable(1337)); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task UntilCommandIsCompleted() { var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("mcr.microsoft.com/windows/servercore:1809") .WithEntrypoint("PowerShell", "-Command", "Start-Sleep -Seconds 3600") .WithWaitStrategy(Wait.ForWindowsContainer() .UntilCommandIsCompleted("exit !(Test-Path -Path 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')")); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } }
public async Task GeneratedContainerName() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.NotEmpty(testcontainer.Name); } }
public async Task RandomHostPortBindings() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithPortBinding(80, true); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.NotEqual(0, testcontainer.GetMappedPublicPort(80)); } }
public async Task Entrypoint() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint("/bin/sh", "-c", "exit 255"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(255, await testcontainer.GetExitCode()); } }
public async Task ExecCommandInRunningContainer() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithCommand("/bin/sh", "-c", "tail -f /dev/null"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(255, await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", "exit 255" })); } }
public async Task WorkingDirectory() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithCommand("/bin/sh", "-c", "test -d /tmp && exit $? || exit $?") .WithWorkingDirectory("/tmp"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(0, await testcontainer.GetExitCode()); } }
public async Task QueryContainerInformationOfCreatedContainer() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx"); // When // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.NotEmpty(testcontainer.Id); Assert.NotEmpty(testcontainer.Name); Assert.NotEmpty(testcontainer.IpAddress); Assert.NotEmpty(testcontainer.MacAddress); } }
public async Task SpecifiedContainerName() { // Given const string name = "/alpine"; // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithName(name); // Then await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(name, testcontainer.Name); } }
public async Task VolumeAndCommand() { // Given const string target = "tmp"; const string file = "hostname"; // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithMount(TempDir, $"/{target}") .WithWaitStrategy(Wait.UntilFilesExists($"{TempDir}/{file}")) .WithCommand("/bin/sh", "-c", $"hostname > /{target}/{file}"); await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } // Then Assert.True(File.Exists($"{TempDir}/{file}"), $"{file} does not exist."); }
public async Task OutputConsumer() { var unixTimeInMilliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(); // Given using (var consumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream())) { // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithCommand("/bin/sh", "-c", $"printf \"{unixTimeInMilliseconds}\" | tee /dev/stderr") .WithOutputConsumer(consumer) .WithWaitStrategy(Wait.ForUnixContainer() .UntilMessageIsLogged(consumer.Stdout, unixTimeInMilliseconds) .UntilMessageIsLogged(consumer.Stderr, unixTimeInMilliseconds)); await using (IDockerContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } consumer.Stdout.Seek(0, SeekOrigin.Begin); consumer.Stderr.Seek(0, SeekOrigin.Begin); // Then using (var streamReader = new StreamReader(consumer.Stdout, leaveOpen: true)) { Assert.Equal(unixTimeInMilliseconds, await streamReader.ReadToEndAsync()); } using (var streamReader = new StreamReader(consumer.Stderr, leaveOpen: true)) { Assert.Equal(unixTimeInMilliseconds, await streamReader.ReadToEndAsync()); } } }