-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIISContainerTestHelper.cs
62 lines (52 loc) · 2.15 KB
/
IISContainerTestHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NETFRAMEWORK
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using FluentAssertions;
using IntegrationTests.Helpers;
using Xunit.Abstractions;
namespace IntegrationTests;
internal class IISContainerTestHelper
{
public static async Task<IContainer> StartContainerAsync(
string imageName,
int webPort,
Dictionary<string, string> environmentVariables,
ITestOutputHelper testOutputHelper)
{
var networkName = await DockerNetworkHelper.SetupIntegrationTestsNetworkAsync();
var logPath = EnvironmentHelper.IsRunningOnCI()
? Path.Combine(Environment.GetEnvironmentVariable("GITHUB_WORKSPACE"), "test-artifacts", "profiler-logs")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"OpenTelemetry .NET AutoInstrumentation", "logs");
Directory.CreateDirectory(logPath);
testOutputHelper.WriteLine("Collecting docker logs to: " + logPath);
var builder = new ContainerBuilder()
.WithImage(imageName)
.WithCleanUp(cleanUp: true)
.WithName($"{imageName}-{webPort}")
.WithNetwork(networkName)
.WithPortBinding(webPort, 80)
.WithBindMount(logPath, "c:/inetpub/wwwroot/logs");
foreach (var env in environmentVariables)
{
builder = builder.WithEnvironment(env.Key, env.Value);
}
var container = builder.Build();
try
{
var wasStarted = container.StartAsync().Wait(TimeSpan.FromMinutes(5));
wasStarted.Should().BeTrue($"Container based on {imageName} has to be operational for the test.");
testOutputHelper.WriteLine($"Container was started successfully.");
await HealthzHelper.TestAsync($"http://localhost:{webPort}/healthz", testOutputHelper);
testOutputHelper.WriteLine($"IIS WebApp was started successfully.");
}
catch
{
await container.DisposeAsync();
throw;
}
return container;
}
}
#endif