Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Runtime.InteropServices;
using Azure.Core.TestFramework;
using NUnit.Framework;
using Task = System.Threading.Tasks.Task;
Expand Down Expand Up @@ -98,53 +99,40 @@ public async Task CanStartPagingMidCollection(bool anonymous)
}

[RecordedTest, NonParallelizable]
public async Task CanDeleteRepostitory()
public async Task CanDeleteRepository()
{
// Arrange
string registry = TestEnvironment.Registry;
string repository = $"library/hello-world";
string sourceRepository = $"library/hello-world";
string targetRepository = $"hello-world-1{GetPlatformSuffix()}";
List<string> tags = new List<string>()
{
"latest",
"v1",
"v2",
"v3",
"v4",
"test-delete-repo"
};

var client = CreateClient();

try
if (Mode != RecordedTestMode.Playback)
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImageAsync(registry, repository, tags);
}

// Act
await client.DeleteRepositoryAsync(repository);
await ImportImageAsync(registry, sourceRepository, tags, targetRepository);
}

var repositories = client.GetRepositoryNamesAsync();
// Act
await client.DeleteRepositoryAsync(targetRepository);

await foreach (var item in repositories)
{
if (item.Contains(repository))
{
Assert.Fail($"Repository {repository} was not deleted.");
}
}
}
finally
// Assert
var repositories = client.GetRepositoryNamesAsync();
await foreach (var item in repositories)
{
// Clean up - put the repository with tags back.
if (Mode != RecordedTestMode.Playback)
if (item.Contains(targetRepository))
{
await ImportImageAsync(registry, repository, tags);
Assert.Fail($"Repository {targetRepository} was not deleted.");
}
}
}

[RecordedTest, NonParallelizable]
public void CanDeleteRepostitory_Anonymous()
public void CanDeleteRepository_Anonymous()
{
// Arrange
string repository = $"library/hello-world";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NUnit.Framework;
using System.Linq;
using Task = System.Threading.Tasks.Task;
using System.Runtime.InteropServices;

namespace Azure.Containers.ContainerRegistry.Tests
{
Expand Down Expand Up @@ -42,12 +43,36 @@ public ContainerRegistryClient CreateClient(bool anonymousAccess = false, string
));
}

public async Task ImportImageAsync(string registry, string repository, string tag)
protected string GetPlatformSuffix()
{
await ImportImageAsync(registry, repository, new List<string>() { tag });
var os = FormatIdentifier(RuntimeInformation.OSDescription);
var dotnetVersion = FormatIdentifier(RuntimeInformation.FrameworkDescription);
return $"-{os}-{dotnetVersion}";
}

public async Task ImportImageAsync(string registry, string repository, List<string> tags)
private string FormatIdentifier(string value)
{
List<string> invalidCharacters = new List<string> { " ", ".", "#", "~", ":", ";", "/", "\\" };
foreach (var invalid in invalidCharacters)
{
value = value.Replace(invalid, string.Empty);
}

int maxLength = 25;
if (value.Length > maxLength)
{
value = value.Substring(0, maxLength);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If a SubString ends up being needed, it'd be better to do that before replacing values.

}

return value.ToLower();
}

public async Task ImportImageAsync(string registry, string repository, string tag, string targetRepository = default)
{
await ImportImageAsync(registry, repository, new List<string>() { tag }, targetRepository);
}

public async Task ImportImageAsync(string registry, string repository, List<string> tags, string targetRepository = default)
{
var credential = new AzureCredentials(
new ServicePrincipalLoginInformation
Expand All @@ -67,7 +92,8 @@ public async Task ImportImageAsync(string registry, string repository, List<stri
RegistryUri = "registry.hub.docker.com"
};

var targetTags = tags.Select(tag => $"{repository}:{tag}");
var target = targetRepository ?? repository;
var targetTags = tags.Select(tag => $"{target}:{tag}");

await managementClient.Registries.ImportImageAsync(
resourceGroupName: TestEnvironment.ResourceGroup,
Expand All @@ -80,5 +106,40 @@ await managementClient.Registries.ImportImageAsync(
TargetTags = targetTags.ToList()
});
}

public async Task ImportImageByDigestAsync(string registry, string repository, string digest, string targetRepository, string targetTag)
{
var credential = new AzureCredentials(
new ServicePrincipalLoginInformation
{
ClientId = TestEnvironment.ClientId,
ClientSecret = TestEnvironment.ClientSecret,
},
TestEnvironment.TenantId,
AzureEnvironment.AzureGlobalCloud);

var managementClient = new ContainerRegistryManagementClient(credential.WithDefaultSubscription(TestEnvironment.SubscriptionId));
managementClient.SubscriptionId = TestEnvironment.SubscriptionId;

var importSource = new ImportSource
{
SourceImage = $"{repository}@{digest}",
RegistryUri = "registry.hub.docker.com"
};

var targetImage = $"{targetRepository}:{targetTag}";

await managementClient.Registries.ImportImageAsync(
resourceGroupName: TestEnvironment.ResourceGroup,
registryName: registry,
parameters:
new ImportImageParameters
{
Mode = ImportMode.Force,
Source = importSource,
TargetTags = new List<string>() { targetImage }
});
;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,61 @@ public async Task CanGetRepositoryProperties()
public async Task CanSetRepositoryProperties()
{
// Arrange
var client = CreateClient();
var repository = client.GetRepository(_repositoryName);
string registry = TestEnvironment.Registry;
string sourceRepository = $"library/hello-world";
string targetRepository = $"hello-world-3{GetPlatformSuffix()}";
List<string> tags = new List<string>()
{
"test-set-repo-properties"
};

ContainerRepositoryProperties repositoryProperties = await repository.GetPropertiesAsync();
ContainerRepositoryProperties originalProperties = repositoryProperties;
var client = CreateClient();
var repository = client.GetRepository(targetRepository);

// Act
ContainerRepositoryProperties properties = await repository.UpdatePropertiesAsync(
new ContainerRepositoryProperties()
try
{
if (Mode != RecordedTestMode.Playback)
{
CanList = false,
CanRead = false,
CanWrite = false,
CanDelete = false,
});
await ImportImageAsync(registry, sourceRepository, tags, targetRepository);
}

// Assert
Assert.IsFalse(properties.CanList);
Assert.IsFalse(properties.CanRead);
Assert.IsFalse(properties.CanWrite);
Assert.IsFalse(properties.CanDelete);
// Act
ContainerRepositoryProperties properties = await repository.UpdatePropertiesAsync(
new ContainerRepositoryProperties()
{
CanList = false,
CanRead = false,
CanWrite = false,
CanDelete = false,
});

// Assert
Assert.IsFalse(properties.CanList);
Assert.IsFalse(properties.CanRead);
Assert.IsFalse(properties.CanWrite);
Assert.IsFalse(properties.CanDelete);

ContainerRepositoryProperties updatedProperties = await repository.GetPropertiesAsync();
ContainerRepositoryProperties updatedProperties = await repository.GetPropertiesAsync();

Assert.IsFalse(updatedProperties.CanList);
Assert.IsFalse(updatedProperties.CanRead);
Assert.IsFalse(updatedProperties.CanWrite);
Assert.IsFalse(updatedProperties.CanDelete);
Assert.IsFalse(updatedProperties.CanList);
Assert.IsFalse(updatedProperties.CanRead);
Assert.IsFalse(updatedProperties.CanWrite);
Assert.IsFalse(updatedProperties.CanDelete);
}
finally
{
// Clean up
ContainerRepositoryProperties properties = await repository.UpdatePropertiesAsync(
new ContainerRepositoryProperties()
{
CanList = true,
CanRead = true,
CanWrite = true,
CanDelete = true,
});

// Cleanup
await repository.UpdatePropertiesAsync(originalProperties);
await repository.DeleteAsync();
}
}

[RecordedTest, NonParallelizable]
Expand All @@ -92,39 +116,27 @@ public void CanSetRepositoryProperties_Anonymous()
public async Task CanDeleteRepository()
{
// Arrange
string registry = TestEnvironment.Registry;
string sourceRepository = $"library/hello-world";
string targetRepository = $"hello-world-2{GetPlatformSuffix()}";
List<string> tags = new List<string>()
{
"latest",
"v1",
"v2",
"v3",
"v4",
"test-delete-repo"
};

var client = CreateClient();
var repository = client.GetRepository(_repositoryName);
var repository = client.GetRepository(targetRepository);

try
if (Mode != RecordedTestMode.Playback)
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImageAsync(TestEnvironment.Registry, _repositoryName, tags);
}
await ImportImageAsync(registry, sourceRepository, tags, targetRepository);
}

// Act
await repository.DeleteAsync();
// Act
await repository.DeleteAsync();

// Assert
Assert.ThrowsAsync<RequestFailedException>(async () => { await repository.GetPropertiesAsync(); });
}
finally
{
// Clean up - put the repository with tags back.
if (Mode != RecordedTestMode.Playback)
{
await ImportImageAsync(TestEnvironment.Registry, _repositoryName, tags);
}
}
// Assert
Assert.ThrowsAsync<RequestFailedException>(async () => { await repository.GetPropertiesAsync(); });
}

[RecordedTest]
Expand Down Expand Up @@ -232,40 +244,43 @@ public async Task CanGetArtifactsStartingMidCollection(bool anonymous)
public async Task CanGetManifestsOrdered()
{
// Arrange
string repositoryName = $"library/node";
string tag = "newest";
string registry = TestEnvironment.Registry;
string sourceRepository = $"library/node";
string targetRepository = $"node-1{GetPlatformSuffix()}";

var client = CreateClient();
var repository = client.GetRepository(repositoryName);
var artifact = client.GetArtifact(repositoryName, tag);
var repository = client.GetRepository(targetRepository);

string oldDigest = "sha256:b2e85fe0e037a625d601a81ce962d196bec948cab3d68278ab4a5dd177da59e2";
string newDigest = "sha256:5e5d07de2101ee559c51656ddfe9f78b8ee5f02932979a6b60343dc1e3abeebb";

try
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImageAsync(TestEnvironment.Registry, repositoryName, tag);
await ImportImageByDigestAsync(registry, sourceRepository, oldDigest, targetRepository, "oldest");

await Task.Delay(1000);

await ImportImageByDigestAsync(registry, sourceRepository, newDigest, targetRepository, "newest");
}

// Act
AsyncPageable<ArtifactManifestProperties> manifests = repository.GetManifestPropertiesCollectionAsync(ArtifactManifestOrderBy.LastUpdatedOnDescending);

// Assert
string digest = null;
await foreach (ArtifactManifestProperties manifest in manifests)
{
// Make sure we're looking at a manifest list, which has the tag
if (manifest.RelatedArtifacts != null && manifest.RelatedArtifacts.Count > 0)
{
digest = manifest.Digest;
Assert.That(manifest.RepositoryName.Contains(repositoryName));
Assert.That(manifest.Tags.Contains(tag));
break;
}
// The newer manifest should appear first given the sort order we specified
Assert.AreEqual(targetRepository, manifest.RepositoryName);
Assert.AreEqual(newDigest, manifest.Digest);
break;
}
}
finally
{
// Clean up
await artifact.DeleteAsync();
await repository.DeleteAsync();
}
}
}
Expand Down
Loading