-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SampleImageTests.cs
203 lines (175 loc) · 8.02 KB
/
SampleImageTests.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Docker.Tests
{
[Trait("Category", "sample")]
public class SampleImageTests
{
private static readonly string s_samplesPath = Path.Combine(Config.SourceRepoRoot, "samples");
public SampleImageTests(ITestOutputHelper outputHelper)
{
OutputHelper = outputHelper;
DockerHelper = new DockerHelper(outputHelper);
}
protected DockerHelper DockerHelper { get; }
protected ITestOutputHelper OutputHelper { get; }
public static IEnumerable<object[]> GetImageData()
{
return TestData.GetSampleImageData()
.Select(imageData => new object[] { imageData });
}
[DotNetTheory]
[MemberData(nameof(GetImageData))]
public async Task VerifyDotnetSample(SampleImageData imageData)
{
if (imageData.DockerfileSuffix == "windowsservercore-iis")
{
return;
}
await VerifySampleAsync(imageData, SampleImageType.Dotnetapp, (image, containerName) =>
{
string output = DockerHelper.Run(image, containerName);
Assert.True(output.Contains("42") || output.StartsWith("Hello"));
ValidateEnvironmentVariables(imageData, image, SampleImageType.Dotnetapp);
return Task.CompletedTask;
});
}
[DotNetTheory]
[MemberData(nameof(GetImageData))]
public async Task VerifyAspnetSample(SampleImageData imageData)
{
if (imageData.OS == OS.Bionic && imageData.DockerfileSuffix != "ubuntu-x64")
{
return;
}
await VerifySampleAsync(imageData, SampleImageType.Aspnetapp, async (image, containerName) =>
{
int port = imageData.DockerfileSuffix == "windowsservercore-iis" ? 80 : imageData.DefaultPort;
try
{
DockerHelper.Run(
image: image,
name: containerName,
detach: true,
optionalRunArgs: $"-p {port}");
if (!Config.IsHttpVerificationDisabled)
{
await ImageScenarioVerifier.VerifyHttpResponseFromContainerAsync(containerName, DockerHelper, OutputHelper, port);
}
ValidateEnvironmentVariables(imageData, image, SampleImageType.Aspnetapp);
}
finally
{
DockerHelper.DeleteContainer(containerName);
}
});
}
[Fact]
public void VerifyComplexAppSample()
{
// complexapp sample doesn't currently support building on Windows.
if (!DockerHelper.IsLinuxContainerModeEnabled)
{
return;
}
string appTag = SampleImageData.GetImageName("complexapp-local-app");
string testTag = SampleImageData.GetImageName("complexapp-local-test");
string sampleFolder = Path.Combine(s_samplesPath, "complexapp");
string dockerfilePath = $"{sampleFolder}/Dockerfile";
string testContainerName = ImageData.GenerateContainerName("sample-complex-test");
string tempDir = null;
try
{
// Test that the app works
DockerHelper.Build(appTag, dockerfilePath, contextDir: sampleFolder, pull: Config.PullImages);
string containerName = ImageData.GenerateContainerName("sample-complex");
string output = DockerHelper.Run(appTag, containerName);
Assert.StartsWith("string: The quick brown fox jumps over the lazy dog", output);
if (!DockerHelper.IsLinuxContainerModeEnabled &&
DockerHelper.DockerArchitecture.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
{
// Skipping run app tests due to a .NET issue: https://github.com/dotnet/runtime/issues/2082
return;
}
// Run the app's tests
DockerHelper.Build(testTag, dockerfilePath, target: "test", contextDir: sampleFolder);
DockerHelper.Run(testTag, testContainerName, skipAutoCleanup: true);
// Copy the test log from the container to the host
tempDir = Directory.CreateDirectory(
Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())).FullName;
DockerHelper.Copy($"{testContainerName}:/source/tests/TestResults", tempDir);
string testLogFile = new DirectoryInfo($"{tempDir}/TestResults").GetFiles("*.trx").First().FullName;
// Open the test log file and verify the tests passed
XDocument doc = XDocument.Load(testLogFile);
XElement summary = doc.Root.Element(XName.Get("ResultSummary", doc.Root.Name.NamespaceName));
Assert.Equal("Completed", summary.Attribute("outcome").Value);
XElement counters = summary.Element(XName.Get("Counters", doc.Root.Name.NamespaceName));
Assert.Equal("3", counters.Attribute("total").Value);
Assert.Equal("3", counters.Attribute("passed").Value);
}
finally
{
if (tempDir != null)
{
Directory.Delete(tempDir, true);
}
DockerHelper.DeleteContainer(testContainerName);
DockerHelper.DeleteImage(testTag);
DockerHelper.DeleteImage(appTag);
}
}
private async Task VerifySampleAsync(
SampleImageData imageData,
SampleImageType sampleImageType,
Func<string, string, Task> verifyImageAsync)
{
string image = imageData.GetImage(sampleImageType, DockerHelper);
string imageType = Enum.GetName(typeof(SampleImageType), sampleImageType).ToLowerInvariant();
try
{
if (!imageData.IsPublished)
{
string sampleFolder = Path.Combine(s_samplesPath, imageType);
string dockerfilePath = $"{sampleFolder}/Dockerfile";
if (!string.IsNullOrEmpty(imageData.DockerfileSuffix))
{
dockerfilePath += $".{imageData.DockerfileSuffix}";
}
DockerHelper.Build(image, dockerfilePath, contextDir: sampleFolder, pull: Config.PullImages, platform: imageData.Platform);
}
string containerName = imageData.GetIdentifier($"sample-{imageType}");
await verifyImageAsync(image, containerName);
}
finally
{
if (!imageData.IsPublished)
{
DockerHelper.DeleteImage(image);
}
}
}
private void ValidateEnvironmentVariables(SampleImageData imageData, string image, SampleImageType imageType)
{
List<EnvironmentVariableInfo> variables = new List<EnvironmentVariableInfo>();
variables.AddRange(ProductImageTests.GetCommonEnvironmentVariables());
if (imageType == SampleImageType.Aspnetapp)
{
variables.Add(new EnvironmentVariableInfo("ASPNETCORE_HTTP_PORTS", imageData.DefaultPort.ToString()));
}
EnvironmentVariableInfo.Validate(
variables,
image,
imageData,
DockerHelper);
}
}
}