Skip to content

Commit 6da7433

Browse files
authored
Remove ImportImage dependency from ACR tests (Azure#33039)
* rearranging for clarity * Updates to Registry and Repository live tests * fix cleanup * Update tests to use new blobs instead of image import * begin to update samples * revert samples changes and update async methods with Async suffix * re-record one of the failing tests * Don't do clean-up if in playback, and don't record it
1 parent 1d25136 commit 6da7433

19 files changed

+2527
-1888
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using Azure.Containers.ContainerRegistry.Specialized;
8+
9+
namespace Azure.Containers.ContainerRegistry.Tests
10+
{
11+
/// <summary>
12+
/// Extensions to simplify test authoring for ACR tests.
13+
/// </summary>
14+
internal static class ContainerRegistryBlobClientExtensions
15+
{
16+
private static Random _random = new Random();
17+
18+
public static async Task UploadTestImageAsync(this ContainerRegistryBlobClient client, string tag = default)
19+
{
20+
OciManifest manifest = new()
21+
{
22+
SchemaVersion = 2
23+
};
24+
25+
// Upload a config file
26+
using Stream config = BinaryData.FromString("Sample config").ToStream();
27+
var uploadConfigResult = await client.UploadBlobAsync(config);
28+
29+
manifest.Config = new OciBlobDescriptor()
30+
{
31+
Digest = uploadConfigResult.Value.Digest,
32+
Size = uploadConfigResult.Value.Size,
33+
MediaType = "application/vnd.oci.image.config.v1+json"
34+
};
35+
36+
// Upload a layer file
37+
using Stream layer = BinaryData.FromString($"Sample layer {_random.Next()}").ToStream();
38+
var uploadLayerResult = await client.UploadBlobAsync(layer);
39+
40+
manifest.Layers.Add(new OciBlobDescriptor()
41+
{
42+
Digest = uploadLayerResult.Value.Digest,
43+
Size = uploadLayerResult.Value.Size,
44+
MediaType = "application/vnd.oci.image.layer.v1.tar"
45+
});
46+
47+
// Finally, upload the manifest file
48+
var options = tag != null ? new UploadManifestOptions(tag) : null;
49+
await client.UploadManifestAsync(manifest, options);
50+
}
51+
52+
public static async Task AddTagAsync(this ContainerRegistryBlobClient client, string reference, string tag)
53+
{
54+
// Get the image manifest
55+
var manifestResult = await client.DownloadManifestAsync(new DownloadManifestOptions(reference));
56+
var stream = manifestResult.Value.ManifestStream;
57+
stream.Seek(0, SeekOrigin.Begin);
58+
59+
// Upload the manifest with the new tag
60+
await client.UploadManifestAsync(stream, new UploadManifestOptions(tag));
61+
}
62+
}
63+
}

sdk/containerregistry/Azure.Containers.ContainerRegistry/tests/ContainerRegistryClientLiveTests.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using System.Collections.Generic;
4+
using System.Linq;
55
using Azure.Core.TestFramework;
66
using NUnit.Framework;
77
using Task = System.Threading.Tasks.Task;
@@ -97,48 +97,35 @@ public async Task CanStartPagingMidCollection(bool anonymous)
9797
Assert.IsTrue(pageCount >= minExpectedPages);
9898
}
9999

100-
[RecordedTest, NonParallelizable]
100+
[RecordedTest]
101101
public async Task CanDeleteRepository()
102102
{
103103
// Arrange
104-
string registry = TestEnvironment.Registry;
105-
string repository = $"library/hello-world";
106-
List<string> tags = new List<string>()
107-
{
108-
"latest",
109-
"v1",
110-
"v2",
111-
"v3",
112-
"v4",
113-
};
114104
var client = CreateClient();
105+
var repositoryId = Recording.Random.NewGuid().ToString();
115106

116107
try
117108
{
118109
if (Mode != RecordedTestMode.Playback)
119110
{
120-
await ImportImageAsync(registry, repository, tags);
111+
await CreateRepositoryAsync(repositoryId);
121112
}
122113

123-
// Act
124-
await client.DeleteRepositoryAsync(repository);
125-
126114
var repositories = client.GetRepositoryNamesAsync();
115+
Assert.IsTrue(await repositories.ContainsAsync(repositoryId), $"Test set-up failed: Repository {repositoryId} was not created.");
127116

128-
await foreach (var item in repositories)
129-
{
130-
if (item.Contains(repository))
131-
{
132-
Assert.Fail($"Repository {repository} was not deleted.");
133-
}
134-
}
117+
// Act
118+
await client.DeleteRepositoryAsync(repositoryId);
119+
120+
// Assert
121+
repositories = client.GetRepositoryNamesAsync();
122+
Assert.IsFalse(await repositories.ContainsAsync(repositoryId), $"Repository {repositoryId} was not deleted.");
135123
}
136124
finally
137125
{
138-
// Clean up - put the repository with tags back.
139126
if (Mode != RecordedTestMode.Playback)
140127
{
141-
await ImportImageAsync(registry, repository, tags);
128+
await DeleteRepositoryAsync(repositoryId);
142129
}
143130
}
144131
}

0 commit comments

Comments
 (0)