Skip to content

Commit a0c3a56

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
add more ImageIndexGenertorTests
1 parent 51d9cc8 commit a0c3a56

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Containers/Microsoft.NET.Build.Containers/ImageIndexGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ internal static (string, string) GenerateImageIndex(BuiltImage[] images)
4646

4747
internal static string GenerateImageIndex(BuiltImage[] images, string manifestMediaType, string imageIndexMediaType)
4848
{
49+
if (images.Length == 0)
50+
{
51+
throw new ArgumentException(string.Format(Strings.ImagesEmpty));
52+
}
53+
4954
// Here we are using ManifestListV2 struct, but we could use ImageIndexV1 struct as well.
5055
// We are filling the same fields, so we can use the same struct.
5156
var manifests = new PlatformSpecificManifest[images.Length];

src/Tests/Microsoft.NET.Build.Containers.UnitTests/ImageIndexGeneratorTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public void ImagesCannotBeEmpty()
1515
Assert.Equal(Strings.ImagesEmpty, ex.Message);
1616
}
1717

18+
[Fact]
19+
public void ImagesCannotBeEmpty_SpecifiedMediaType()
20+
{
21+
BuiltImage[] images = Array.Empty<BuiltImage>();
22+
var ex = Assert.Throws<ArgumentException>(() => ImageIndexGenerator.GenerateImageIndex(images, "manifestMediaType", "imageIndexMediaType"));
23+
Assert.Equal(Strings.ImagesEmpty, ex.Message);
24+
}
25+
1826
[Fact]
1927
public void UnsupportedMediaTypeThrows()
2028
{
@@ -133,4 +141,11 @@ public void GenerateOciImageIndex()
133141
Assert.Equal("{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1\\u002Bjson\",\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1\\u002Bjson\",\"size\":3,\"digest\":\"sha256:digest1\",\"platform\":{\"architecture\":\"arch1\",\"os\":\"os1\"}},{\"mediaType\":\"application/vnd.oci.image.manifest.v1\\u002Bjson\",\"size\":3,\"digest\":\"sha256:digest2\",\"platform\":{\"architecture\":\"arch2\",\"os\":\"os2\"}}]}", imageIndex);
134142
Assert.Equal(SchemaTypes.OciImageIndexV1, mediaType);
135143
}
144+
145+
[Fact]
146+
public void GenerateImageIndexWithAnnotations()
147+
{
148+
string imageIndex = ImageIndexGenerator.GenerateImageIndexWithAnnotations("mediaType", "sha256:digest", 3, "repository", ["1.0", "2.0"]);
149+
Assert.Equal("{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1\\u002Bjson\",\"manifests\":[{\"mediaType\":\"mediaType\",\"size\":3,\"digest\":\"sha256:digest\",\"platform\":{},\"annotations\":{\"io.containerd.image.name\":\"repository:1.0\",\"org.opencontainers.image.ref.name\":\"1.0\"}},{\"mediaType\":\"mediaType\",\"size\":3,\"digest\":\"sha256:digest\",\"platform\":{},\"annotations\":{\"io.containerd.image.name\":\"repository:2.0\",\"org.opencontainers.image.ref.name\":\"2.0\"}}]}", imageIndex);
150+
}
136151
}

0 commit comments

Comments
 (0)