Skip to content

Commit

Permalink
update code based on comments
Browse files Browse the repository at this point in the history
Signed-off-by: nhu1997 <[email protected]>
  • Loading branch information
nhu1997 committed Nov 12, 2024
1 parent 0b76879 commit ce21442
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 47 deletions.
11 changes: 8 additions & 3 deletions src/OrasProject.Oras/Oci/Descriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ public static Descriptor Create(Span<byte> data, string mediaType)
return new Descriptor
{
MediaType = mediaType,
Data = byteData,
Digest = OrasProject.Oras.Content.Digest.ComputeSHA256(byteData),
Digest = Content.Digest.ComputeSHA256(byteData),
Size = byteData.Length
};
}

public static Descriptor Empty => Descriptor.Create(new byte[] { 0x7B, 0x7D }, OrasProject.Oras.Oci.MediaType.EmptyJson);
public static Descriptor Empty => new()
{
MediaType = Oci.MediaType.EmptyJson,
Digest = "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
Size = 2,
Data = [0x7B, 0x7D]
};

internal BasicDescriptor BasicDescriptor => new BasicDescriptor(MediaType, Digest, Size);
}
2 changes: 1 addition & 1 deletion src/OrasProject.Oras/PackManifestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct PackManifestOptions
/// Config references a configuration object for a container, by digest
/// For more details: https://github.com/opencontainers/image-spec/blob/v1.1.0/manifest.md#image-manifest-property-descriptions.
/// </summary>
public Descriptor Config { get; set; }
public Descriptor? Config { get; set; }

/// <summary>
/// Layers is an array of objects, and each object id a Content Descriptor (or simply Descriptor)
Expand Down
12 changes: 7 additions & 5 deletions src/OrasProject.Oras/Packer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
using OrasProject.Oras.Oci;
using OrasProject.Oras.Content;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Utils;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Threading;
Expand All @@ -45,6 +43,10 @@ public static class Packer
/// </summary>
private const string _errMissingArtifactType = "missing artifact type";

public const string UnknownConfig = "application/vnd.unknown.config.v1+json";

public const string UnknownArtifact = "application/vnd.unknown.artifact.v1";

/// <summary>
/// ManifestVersion represents the manifest version used for PackManifest
/// </summary>
Expand Down Expand Up @@ -141,7 +143,7 @@ private static async Task<Descriptor> PackManifestV1_0Async(IPushable pusher, st
{
if (string.IsNullOrEmpty(artifactType))
{
artifactType = Utils.MediaType.UnknownConfig;
artifactType = UnknownConfig;
}
ValidateMediaType(artifactType);
configDescriptor = await PushCustomEmptyConfigAsync(pusher, artifactType, options.ConfigAnnotations, cancellationToken);
Expand Down Expand Up @@ -171,7 +173,7 @@ private static async Task<Descriptor> PackManifestV1_0Async(IPushable pusher, st
/// <exception cref="MissingArtifactTypeException"></exception>
private static async Task<Descriptor> PackManifestV1_1Async(IPushable pusher, string? artifactType, PackManifestOptions options = default, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(artifactType) && (options.Config == null || options.Config.MediaType == Oci.MediaType.EmptyJson))
if (string.IsNullOrEmpty(artifactType) && (options.Config == null || options.Config.MediaType == MediaType.EmptyJson))
{
throw new MissingArtifactTypeException(_errMissingArtifactType);
} else if (!string.IsNullOrEmpty(artifactType)) {
Expand Down Expand Up @@ -205,7 +207,7 @@ private static async Task<Descriptor> PackManifestV1_1Async(IPushable pusher, st
var manifest = new Manifest
{
SchemaVersion = 2,
MediaType = Oci.MediaType.ImageManifest,
MediaType = MediaType.ImageManifest,
ArtifactType = artifactType,
Subject = options.Subject,
Config = options.Config,
Expand Down
21 changes: 0 additions & 21 deletions src/OrasProject.Oras/Utils/MediaType.cs

This file was deleted.

26 changes: 9 additions & 17 deletions tests/OrasProject.Oras.Tests/PackerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Text;
using System.Text.Json;
using Xunit;
using OrasProject.Oras.Utils;

namespace OrasProject.Oras.Tests;

Expand Down Expand Up @@ -54,8 +53,7 @@ public async Task TestPackManifestImageV1_0()
{
MediaType = artifactType,
Digest = Digest.ComputeSHA256(expectedConfigData),
Size = expectedConfigData.Length,
Data = expectedConfigData
Size = expectedConfigData.Length
};
var expectedConfigBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(expectedConfig));
var incomingConfig = manifest?.Config;
Expand Down Expand Up @@ -202,8 +200,7 @@ public async Task TestPackManifestImageV1_0_WithOptions()
{
MediaType = expectedManifest.MediaType,
Digest = Digest.ComputeSHA256(expectedManifestBytes),
Size = expectedManifestBytes.Length,
Data = expectedManifestBytes
Size = expectedManifestBytes.Length
};
expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType;
expectedManifestDesc.Annotations = expectedManifest.Annotations;
Expand All @@ -224,9 +221,8 @@ public async Task TestPackManifestImageV1_0_WithOptions()
{
MediaType = artifactType,
Digest = Digest.ComputeSHA256(configBytes),
Size = configBytes.Length,
Annotations = configAnnotations,
Data = configBytes
Size = configBytes.Length
};
expectedManifest = new Manifest
{
Expand All @@ -249,8 +245,7 @@ public async Task TestPackManifestImageV1_0_WithOptions()
{
MediaType = expectedManifest.MediaType,
Digest = Digest.ComputeSHA256(expectedManifestBytes),
Size = expectedManifestBytes.Length,
Data = expectedManifestBytes
Size = expectedManifestBytes.Length
};
expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType;
expectedManifestDesc.Annotations = expectedManifest.Annotations;
Expand Down Expand Up @@ -302,8 +297,8 @@ public async Task TestPackManifestImageV1_0_NoArtifactType()

// Verify artifact type and config media type

Assert.Equal(Utils.MediaType.UnknownConfig, manifestDesc.ArtifactType);
Assert.Equal(Utils.MediaType.UnknownConfig, manifest!.Config.MediaType);
Assert.Equal(Packer.UnknownConfig, manifestDesc.ArtifactType);
Assert.Equal(Packer.UnknownConfig, manifest!.Config.MediaType);
}

[Fact]
Expand Down Expand Up @@ -521,8 +516,7 @@ public async Task TestPackManifestImageV1_1_WithOptions()
{
MediaType = expectedManifest.MediaType,
Digest = Digest.ComputeSHA256(expectedManifestBytes),
Size = expectedManifestBytes.Length,
Data = expectedManifestBytes
Size = expectedManifestBytes.Length
};
expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType;
expectedManifestDesc.Annotations = expectedManifest.Annotations;
Expand Down Expand Up @@ -552,8 +546,7 @@ public async Task TestPackManifestImageV1_1_WithOptions()
{
MediaType = expectedManifest.MediaType,
Digest = Digest.ComputeSHA256(expectedManifestBytes),
Size = expectedManifestBytes.Length,
Data = expectedManifestBytes
Size = expectedManifestBytes.Length
};
expectedManifestDesc.Annotations = expectedManifest.Annotations;
Assert.Equal(JsonSerializer.SerializeToUtf8Bytes(expectedManifestDesc), JsonSerializer.SerializeToUtf8Bytes(manifestDesc));
Expand Down Expand Up @@ -589,8 +582,7 @@ public async Task TestPackManifestImageV1_1_WithOptions()
{
MediaType = expectedManifest.MediaType,
Digest = Digest.ComputeSHA256(expectedManifestBytes),
Size = expectedManifestBytes.Length,
Data = expectedManifestBytes
Size = expectedManifestBytes.Length
};
expectedManifestDesc.ArtifactType = artifactType;
expectedManifestDesc.Annotations = expectedManifest.Annotations;
Expand Down

0 comments on commit ce21442

Please sign in to comment.