public async Task BindMountAndEnvironment() { // Given const string target = "tmp"; const string file = "dayOfWeek"; var dayOfWeek = DateTime.UtcNow.DayOfWeek.ToString(); // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithEntrypoint("/bin/sh", "-c", $"printf $dayOfWeek > /{target}/{file} && tail -f /dev/null") .WithEnvironment("dayOfWeek", dayOfWeek) .WithBindMount(TempDir, $"/{target}") .WithWaitStrategy(Wait.ForUnixContainer() .UntilFileExists(Path.Combine(TempDir, file))); await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } // Then Assert.Equal(dayOfWeek, await File.ReadAllTextAsync(Path.Combine(TempDir, file))); }
public async Task Entrypoint() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint("/bin/sh", "-c", "exit 255"); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(255, await testcontainer.GetExitCode()); } }
public async Task GeneratedContainerName() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.NotEmpty(testcontainer.Name); } }
public async Task DockerEndpoint() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithDockerEndpoint(TestcontainersSettings.OS.DockerEndpointAuthConfig) .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { var exception = await Record.ExceptionAsync(() => testcontainer.StartAsync()); Assert.Null(exception); } }
public async Task RandomHostPortBindings() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command) .WithPortBinding(80, true); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.NotEqual(0, testcontainer.GetMappedPublicPort(80)); } }
public async Task CleanUpTrueDoesStartDefaultResourceReaper() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithCleanUp(true); // When await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync() .ConfigureAwait(false); } // Then Assert.True(Docker.Exists(DockerResource.Container, DefaultRyukContainerName)); }
public async Task ExposedPorts() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command) .WithExposedPort(80); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { var exception = await Record.ExceptionAsync(() => testcontainer.StartAsync()); Assert.Null(exception); } }
public async Task WorkingDirectory() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint("/bin/sh", "-c", "test -d /tmp && exit $? || exit $?") .WithWorkingDirectory("/tmp"); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(0, await testcontainer.GetExitCode()); } }
public async Task ExecCommandInRunningContainerWithStdout() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); var execResult = await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", "ping -c 4 google.com" }); Assert.Contains("PING google.com", execResult.Stdout); } }
public async Task ExecCommandInRunningContainer() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); var execResult = await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", "exit 255" }); Assert.Equal(255, execResult.ExitCode); } }
public async Task WaitStrategy() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command) .WithWaitStrategy(Wait.ForUnixContainer() .AddCustomWaitStrategy(new WaitUntilFiveSecondsPassedFixture())); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { var exception = await Record.ExceptionAsync(() => testcontainer.StartAsync()); Assert.Null(exception); } }
public async Task UntilCommandIsCompleted() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("mcr.microsoft.com/windows/servercore:ltsc2019") .WithEntrypoint("PowerShell", "-NoLogo", "-Command", "ping -t localhost | Out-Null") .WithWaitStrategy(Wait.ForWindowsContainer() .UntilCommandIsCompleted("Exit !(Test-Path -Path 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')")); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.True(true); } }
public async Task UntilPortIsAvailable() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("mcr.microsoft.com/windows/servercore:ltsc2019") .WithEntrypoint("PowerShell", "-NoLogo", "-Command", "$tcpListener = [System.Net.Sockets.TcpListener]1337; $tcpListener.Start(); ping -t localhost | Out-Null") .WithWaitStrategy(Wait.ForWindowsContainer() .UntilPortIsAvailable(1337)); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.True(true); } }
public async Task ExecCommandInRunningContainerWithStderr() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); var execResult = await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", "cd missing_directory" }); Assert.Contains("can't cd to missing_directory", execResult.Stderr); } }
public async Task UsingResourceReaperSessionIdDoesNotStartDefaultResourceReaper() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithAutoRemove(true) .WithResourceReaperSessionId(Guid.NewGuid()); // When await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync() .ConfigureAwait(false); } // Then Assert.False(Docker.Exists(DockerResource.Container, DefaultRyukContainerName)); }
public async Task QueryContainerInformationOfCreatedContainer() { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx"); // When // Then await using (ITestcontainersContainer 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 Hostname() { // Given const string hostname = "alpine"; // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint("/bin/sh", "-c", $"hostname | grep '{hostname}' &> /dev/null") .WithHostname(hostname); // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.Equal(0, await testcontainer.GetExitCode()); } }
public async Task SpecifiedContainerName() { // Given var name = Guid.NewGuid().ToString("D"); // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command) .WithName(name); // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); Assert.EndsWith(name, testcontainer.Name); } }
public async Task AutoRemoveTrueShouldRemoveContainer() { // Given string testcontainerId; var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithAutoRemove(true) .WithCleanUp(false) .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); testcontainerId = testcontainer.Id; } // Then Assert.False(Docker.Exists(DockerResource.Container, testcontainerId)); }
public async Task CopyFileToRunningContainer() { // Given const string dayOfWeekFilePath = "/tmp/dayOfWeek"; var dayOfWeek = DateTime.UtcNow.DayOfWeek.ToString(); var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); await testcontainer.CopyFileAsync(dayOfWeekFilePath, Encoding.Default.GetBytes(dayOfWeek)); Assert.Equal(0, (await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", $"test \"$(cat {dayOfWeekFilePath})\" = \"{dayOfWeek}\"" })).ExitCode); Assert.Equal(0, (await testcontainer.ExecAsync(new[] { "/bin/sh", "-c", $"stat {dayOfWeekFilePath} | grep 0600" })).ExitCode); } }
public async Task OutputConsumer() { // Given var unixTimeInMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture); using (var consumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream())) { // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("alpine") .WithEntrypoint("/bin/sh", "-c", $"printf \"{unixTimeInMilliseconds}\" | tee /dev/stderr && tail -f /dev/null") .WithOutputConsumer(consumer) .WithWaitStrategy(Wait.ForUnixContainer() .UntilMessageIsLogged(consumer.Stdout, unixTimeInMilliseconds) .UntilMessageIsLogged(consumer.Stderr, unixTimeInMilliseconds)); await using (ITestcontainersContainer 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()); } } }
public async Task PortBindingsHttpAndHttps(int hostPort, int containerPort) { // Given var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithPortBinding(hostPort, containerPort) .WithWaitStrategy(Wait.ForUnixContainer() .UntilPortIsAvailable(containerPort)); // When // Then await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync($"http://localhost:{hostPort}")) { Assert.True(HttpStatusCode.OK.Equals(response.StatusCode), $"nginx port {hostPort} is not available."); } } } }
public async Task BindMountAndCommand() { // Given const string target = "tmp"; const string file = "hostname"; // When var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>() .WithImage("nginx") .WithEntrypoint("/bin/sh", "-c") .WithCommand($"hostname > /{target}/{file} && tail -f /dev/null") .WithBindMount(TempDir, $"/{target}") .WithWaitStrategy(Wait.ForUnixContainer() .UntilFileExists(Path.Combine(TempDir, file))); await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build()) { await testcontainer.StartAsync(); } // Then Assert.True(File.Exists(Path.Combine(TempDir, file)), $"{file} does not exist."); }