示例#1
0
            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.");
                    }
                }
            }
示例#2
0
            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());
                    }
                }
            }
示例#3
0
            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}"));
            }
示例#4
0
            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());
                    }
                }
            }
示例#5
0
 public OrderInChallengeServerFixture()
 {
     this.webApplicationFactory = new WebApplicationFactory <Startup>();
     this.mongoDbContainer      = new TestcontainersBuilder <TestcontainersContainer>()
                                  .WithImage("mongo")
                                  .WithName("orderInTest")
                                  .WithPortBinding(27017)
                                  .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(27017))
                                  .Build();
 }
 public DevChallengeServerFixture()
 {
     this.webApplicationFactory = new WebApplicationFactory <Startup>();
     this.sqlServerContainer    = new TestcontainersBuilder <TestcontainersContainer>()
                                  .WithImage("mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04")
                                  .WithEnvironment("ACCEPT_EULA", "Y")
                                  .WithEnvironment("SA_PASSWORD", "Your_password123")
                                  .WithPortBinding(1433)
                                  .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))
                                  .Build();
 }
示例#7
0
            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();
                }
            }
示例#8
0
            public async Task SafeDisposable()
            {
                // Given
                var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                            .WithImage("alpine");

                // When
                // Then
                await using (IDockerContainer testcontainer = testcontainersBuilder.Build())
                {
                    await testcontainer.StartAsync();
                }
            }
示例#9
0
            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();
                }
            }
示例#10
0
            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();
                }
            }
示例#11
0
            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 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();
                }
            }
示例#14
0
            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);
                }
            }
示例#15
0
            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));
                }
            }
示例#16
0
            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());
                }
            }
示例#17
0
            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 QueryContainerInformationOfNotCreatedContainer()
            {
                // Given
                var testcontainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                            .WithImage("nginx");

                // When
                // Then
                await using (IDockerContainer testcontainer = testcontainersBuilder.Build())
                {
                    Assert.Throws <InvalidOperationException>(() => testcontainer.Name);
                    Assert.Throws <InvalidOperationException>(() => testcontainer.IpAddress);
                    Assert.Throws <InvalidOperationException>(() => testcontainer.MacAddress);
                    Assert.Throws <InvalidOperationException>(() => testcontainer.GetMappedPublicPort(0));
                    await Assert.ThrowsAsync <InvalidOperationException>(() => testcontainer.StopAsync());
                }
            }
示例#19
0
            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());
                }
            }
示例#20
0
            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 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);
                }
            }
示例#22
0
            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.");
            }
示例#23
0
            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());
                    }
                }
            }