Skip to content

Commit aa67ae3

Browse files
committed
change tests
1 parent c4f3969 commit aa67ae3

File tree

2 files changed

+77
-92
lines changed

2 files changed

+77
-92
lines changed

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/DockerRegistryManager.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public class DockerRegistryManager
1111
{
1212
public const string RuntimeBaseImage = "dotnet/runtime";
1313
public const string AspNetBaseImage = "dotnet/aspnet";
14-
public const string Nginx = "library/nginx";
1514
public const string BaseImageSource = "mcr.microsoft.com";
16-
public const string LibraryImageSourse = "docker.io";
1715
public const string Net6ImageTag = "6.0";
1816
public const string Net7ImageTag = "7.0";
1917
public const string Net8ImageTag = "8.0";
@@ -51,7 +49,7 @@ public static async Task StartAndPopulateDockerRegistry(ITestOutputHelper testOu
5149
int spawnRegistryDelay = 1000; //ms
5250
StringBuilder failureReasons = new();
5351

54-
var mcrPullRegistry = new Registry(BaseImageSource, logger, RegistryMode.Pull);
52+
var pullRegistry = new Registry(BaseImageSource, logger, RegistryMode.Pull);
5553
var pushRegistry = new Registry(LocalRegistry, logger, RegistryMode.Push);
5654

5755
for (int spawnRegistryAttempt = 1; spawnRegistryAttempt <= spawnRegistryMaxRetry; spawnRegistryAttempt++)
@@ -78,13 +76,14 @@ public static async Task StartAndPopulateDockerRegistry(ITestOutputHelper testOu
7876
string dotnetdll = System.Reflection.Assembly.GetExecutingAssembly().Location;
7977
var ridjson = Path.Combine(Path.GetDirectoryName(dotnetdll)!, "RuntimeIdentifierGraph.json");
8078

81-
await PullFromRemoteRegistryThenPushToLocalRegistry(mcrPullRegistry, pushRegistry, BaseImageSource, RuntimeBaseImage, tag, logger);
79+
var image = await pullRegistry.GetImageManifestAsync(RuntimeBaseImage, tag, "linux-x64", new SameArchManifestPicker(), CancellationToken.None);
80+
var source = new SourceImageReference(pullRegistry, RuntimeBaseImage, tag);
81+
var dest = new DestinationImageReference(pushRegistry, RuntimeBaseImage, [tag]);
82+
logger.LogInformation($"Pushing image for {BaseImageSource}/{RuntimeBaseImage}:{tag}");
83+
await pushRegistry.PushAsync(image.Build(), source, dest, CancellationToken.None);
84+
logger.LogInformation($"Pushed image for {BaseImageSource}/{RuntimeBaseImage}:{tag}");
8285
}
8386

84-
var nginxTag = "latest";
85-
var librPullRegistry = new Registry(LibraryImageSourse, logger, RegistryMode.Pull);
86-
await PullFromRemoteRegistryThenPushToLocalRegistry(librPullRegistry, pushRegistry, LibraryImageSourse, Nginx, nginxTag, logger);
87-
8887
return;
8988
}
9089
catch (Exception ex)
@@ -114,22 +113,6 @@ public static async Task StartAndPopulateDockerRegistry(ITestOutputHelper testOu
114113
throw new InvalidOperationException($"The registry was not loaded after {spawnRegistryMaxRetry} retries. {failureReasons}");
115114
}
116115

117-
private static async Task PullFromRemoteRegistryThenPushToLocalRegistry(
118-
Registry remoteRegistry,
119-
Registry localRegistry,
120-
string imageSource,
121-
string repositoryName,
122-
string tag,
123-
ILogger logger)
124-
{
125-
var image = await remoteRegistry.GetImageManifestAsync(repositoryName, tag, "linux-x64", new SameArchManifestPicker(), CancellationToken.None);
126-
var source = new SourceImageReference(remoteRegistry, repositoryName, tag);
127-
var dest = new DestinationImageReference(localRegistry, repositoryName, [tag]);
128-
logger.LogInformation($"Pushing image for {imageSource}/{repositoryName}:{tag}");
129-
await localRegistry.PushAsync(image.Build(), source, dest, CancellationToken.None);
130-
logger.LogInformation($"Pushed image for {imageSource}/{repositoryName}:{tag}");
131-
}
132-
133116
public static void ShutdownDockerRegistry(ITestOutputHelper testOutput)
134117
{
135118
if (s_registryContainerId != null)

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,35 @@ public async Task ApiEndToEndWithLocalLoad()
130130
[DockerAvailableFact]
131131
public async Task ApiEndToEndWithArchiveWritingAndLoad()
132132
{
133-
ILogger logger = _loggerFactory.CreateLogger(nameof(ApiEndToEndWithArchiveWritingAndLoad));
133+
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory,
134+
nameof(ApiEndToEndWithArchiveWritingAndLoad), "app.tar.gz");
135+
136+
// Build the image
137+
(BuiltImage builtImage, SourceImageReference sourceReference, DestinationImageReference destinationReference) =
138+
await BuildDockerImageWithArciveDestinationAsync(archiveFile, ["latest", "1.0"], nameof(ApiEndToEndWithArchiveWritingAndLoad));
139+
140+
// Write the image to disk
141+
await destinationReference.LocalRegistry!.LoadAsync(builtImage, sourceReference, destinationReference, default).ConfigureAwait(false);
142+
143+
Assert.True(File.Exists(archiveFile), $"File.Exists({archiveFile})");
144+
145+
// Load the archive
146+
ContainerCli.LoadCommand(_testOutput, "--input", archiveFile)
147+
.Execute()
148+
.Should().Pass();
149+
150+
// Run the image
151+
foreach (string tag in destinationReference.Tags)
152+
{
153+
ContainerCli.RunCommand(_testOutput, "--rm", "--tty", $"{NewImageName()}:{tag}")
154+
.Execute()
155+
.Should().Pass();
156+
}
157+
}
158+
159+
private async Task<(BuiltImage image, SourceImageReference sourceReference, DestinationImageReference destinationReference)> BuildDockerImageWithArciveDestinationAsync(string archiveFile, string[] tags, string testName)
160+
{
161+
ILogger logger = _loggerFactory.CreateLogger(testName);
134162
string publishDirectory = BuildLocalApp(tfm: "net8.0");
135163

136164
// Build the image
@@ -154,32 +182,55 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad()
154182
BuiltImage builtImage = imageBuilder.Build();
155183

156184
// Write the image to disk
157-
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory,
158-
nameof(ApiEndToEndWithArchiveWritingAndLoad), "app.tar.gz");
159185
var sourceReference = new SourceImageReference(registry, DockerRegistryManager.RuntimeBaseImage, DockerRegistryManager.Net7ImageTag);
160-
var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), new[] { "latest", "1.0" });
186+
var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), tags);
161187

162-
await destinationReference.LocalRegistry!.LoadAsync(builtImage, sourceReference, destinationReference, default).ConfigureAwait(false);
188+
return (builtImage, sourceReference, destinationReference);
189+
}
190+
191+
[DockerAvailableFact]
192+
public async Task TarballsHaveCorrectStructure()
193+
{
194+
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory,
195+
nameof(TarballsHaveCorrectStructure), "app.tar.gz");
196+
197+
// 1. Create docker image and write it to a tarball
198+
(BuiltImage dockerImage, SourceImageReference sourceReference, DestinationImageReference destinationReference) =
199+
await BuildDockerImageWithArciveDestinationAsync(archiveFile, ["latest"], nameof(TarballsHaveCorrectStructure));
163200

201+
await destinationReference.LocalRegistry!.LoadAsync(dockerImage, sourceReference, destinationReference, default).ConfigureAwait(false);
202+
164203
Assert.True(File.Exists(archiveFile), $"File.Exists({archiveFile})");
165204

166-
// Load the archive
167-
ContainerCli.LoadCommand(_testOutput, "--input", archiveFile)
168-
.Execute()
169-
.Should().Pass();
205+
CheckDockerTarballStructure(archiveFile);
170206

171-
// Run the image
172-
foreach (string tag in destinationReference.Tags)
173-
{
174-
ContainerCli.RunCommand(_testOutput, "--rm", "--tty", $"{NewImageName()}:{tag}")
175-
.Execute()
176-
.Should().Pass();
177-
}
207+
// 2. Convert the docker image to an OCI image and write it to a tarball
208+
BuiltImage ociImage = ConvertToOciImage(dockerImage);
209+
210+
await destinationReference.LocalRegistry!.LoadAsync(ociImage, sourceReference, destinationReference, default).ConfigureAwait(false);
211+
212+
Assert.True(File.Exists(archiveFile), $"File.Exists({archiveFile})");
178213

179-
CheckForDockerTarballStructure(archiveFile);
214+
CheckOciTarballStructure(archiveFile);
180215
}
181216

182-
private void CheckForDockerTarballStructure(string tarball)
217+
private BuiltImage ConvertToOciImage(BuiltImage builtImage)
218+
{
219+
// Convert the image to an OCI image
220+
var ociImage = new BuiltImage
221+
{
222+
Config = builtImage.Config,
223+
ImageDigest = builtImage.ImageDigest,
224+
ImageSha = builtImage.ImageSha,
225+
ImageSize = builtImage.ImageSize,
226+
Manifest = builtImage.Manifest,
227+
ManifestMediaType = SchemaTypes.OciManifestV1,
228+
};
229+
230+
return ociImage;
231+
}
232+
233+
private void CheckDockerTarballStructure(string tarball)
183234
{
184235
var layersCount = 0;
185236
int configJson = 0;
@@ -218,56 +269,7 @@ private void CheckForDockerTarballStructure(string tarball)
218269
Assert.True(layersCount > 0);
219270
}
220271

221-
[DockerAvailableFact]
222-
public async Task ApiEndToEndOciImageWithArchiveWritingAndLoad()
223-
{
224-
ILogger logger = _loggerFactory.CreateLogger(nameof(ApiEndToEndOciImageWithArchiveWritingAndLoad));
225-
226-
// Build the image
227-
228-
Registry registry = new(DockerRegistryManager.LocalRegistry, logger, RegistryMode.Push);
229-
230-
ImageBuilder imageBuilder = await registry.GetImageManifestAsync(
231-
DockerRegistryManager.Nginx,
232-
"latest",
233-
"linux-x64",
234-
ToolsetUtils.RidGraphManifestPicker,
235-
cancellationToken: default).ConfigureAwait(false);
236-
Assert.NotNull(imageBuilder);
237-
238-
BuiltImage builtImage = imageBuilder.Build();
239-
240-
// Write the image to disk
241-
var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory,
242-
nameof(ApiEndToEndWithArchiveWritingAndLoad), "nginx.tar.gz");
243-
var sourceReference = new SourceImageReference(registry, DockerRegistryManager.RuntimeBaseImage, DockerRegistryManager.Net7ImageTag);
244-
var destinationReference = new DestinationImageReference(new ArchiveFileRegistry(archiveFile), NewImageName(), ["latest"]);
245-
246-
await destinationReference.LocalRegistry!.LoadAsync(builtImage, sourceReference, destinationReference, default).ConfigureAwait(false);
247-
248-
Assert.True(File.Exists(archiveFile), $"File.Exists({archiveFile})");
249-
250-
// Docker cannot load an OCI image, so we check for Podman
251-
if (ContainerCli.IsPodman)
252-
{
253-
// Load the archive
254-
ContainerCli.LoadCommand(_testOutput, "--input", archiveFile)
255-
.Execute()
256-
.Should().Pass();
257-
258-
// Run the image
259-
foreach (string tag in destinationReference.Tags)
260-
{
261-
ContainerCli.RunCommand(_testOutput, "--rm", "--tty", $"{NewImageName()}:{tag}")
262-
.Execute()
263-
.Should().Pass();
264-
}
265-
}
266-
267-
CheckForOciTarballStructure(archiveFile);
268-
}
269-
270-
private void CheckForOciTarballStructure(string tarball)
272+
private void CheckOciTarballStructure(string tarball)
271273
{
272274
int blobsCount = 0;
273275
int ociLayoutCount = 0;

0 commit comments

Comments
 (0)