public async Task DeleteManifestAsync_ValidatesArguments_Async() { var client = new ImageRegistryClient(new HttpClient()); await Assert.ThrowsAsync <ArgumentNullException>(() => client.DeleteManifestAsync(null, string.Empty, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ArgumentNullException>(() => client.DeleteManifestAsync(string.Empty, null, default)).ConfigureAwait(false); }
public async Task GetManifestAsync_Works_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/registry/manifests/my-tag") .With((request) => request.Headers.Accept.Any(h => h.MediaType == "application/vnd.oci.image.manifest.v1+json")) .Respond(HttpStatusCode.OK, new StringContent("{\"schemaVersion\":2,\"config\":{\"mediaType\":\"\",\"digest\":\"sha256:a\",\"size\":1},\"layers\":[{\"mediaType\":\"\",\"digest\":\"sha256:b\",\"size\":2}]}", Encoding.UTF8, "application/json")); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); var manifest = await client.GetManifestAsync("registry", "my-tag", default).ConfigureAwait(false); Assert.Equal(2, manifest.SchemaVersion); Assert.Equal(1, manifest.Config.Size); var layer = Assert.Single(manifest.Layers); Assert.Equal(2, layer.Size); handler.VerifyNoOutstandingExpectation(); }
public async Task PushManifestAsync_Works_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Put, "http://localhost:5000/v2/registry/manifests/my-tag") .With((request) => request.Content is StreamContent) .With((request) => request.Content.Headers.ContentType.MediaType == "application/vnd.oci.image.manifest.v1+json") .Respond( (request) => { var response = new HttpResponseMessage() { StatusCode = HttpStatusCode.Created, }; response.Headers.Location = new Uri("http://localhost:5000/v2/registry/manifests/my-tag"); return(response); }); using (var blobStream = new MemoryStream()) { var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); var result = await client.PushManifestAsync("registry", "my-tag", blobStream, default).ConfigureAwait(false); Assert.Equal(new Uri("http://localhost:5000/v2/registry/manifests/my-tag"), result); } handler.VerifyNoOutstandingExpectation(); }
public async Task Dispose_Works_Async() { var httpClient = new HttpClient(); var client = new ImageRegistryClient(httpClient); Assert.False(client.IsDisposed); client.Dispose(); Assert.Throws <ObjectDisposedException>(() => httpClient.BaseAddress = new Uri("http://localhost:5000")); Assert.True(client.IsDisposed); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.DeleteBlobAsync(null, null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.DeleteManifestAsync(null, null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.GetBlobAsync(null, null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.GetManifestAsync(null, null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.ListTagsAsync(null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.PushBlobAsync(null, null, null, default)).ConfigureAwait(false); await Assert.ThrowsAsync <ObjectDisposedException>(() => client.PushManifestAsync(null, null, null, default)).ConfigureAwait(false); }
public async Task PushBlobAsync_InvalidDigest_Throws_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Post, "http://localhost:5000/v2/registry/blobs/uploads/") .WithContent(string.Empty) .Respond( (request) => { var response = new HttpResponseMessage() { StatusCode = HttpStatusCode.Accepted, }; response.Headers.Location = new Uri("http://localhost:5000/v2/registry/blobs/uploads/65707092-6422-4b33-bba7-c655318616e9?_state=state_string"); return(response); }); handler .Expect(HttpMethod.Put, "http://localhost:5000/v2/registry/blobs/uploads/65707092-6422-4b33-bba7-c655318616e9?_state=state_string&digest=sha256:0") .Respond(HttpStatusCode.BadRequest); using (var blobStream = new MemoryStream()) { var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.PushBlobAsync("registry", blobStream, "sha256:0", default)).ConfigureAwait(false); } handler.VerifyNoOutstandingExpectation(); }
public async Task DeleteManifestAsync_ThrowsOnError_Async() { var handler = new MockHttpMessageHandler(); handler .When(HttpMethod.Delete, "http://localhost:5000/v2/registry/manifests/my-tag") .Respond(HttpStatusCode.NotFound); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.DeleteManifestAsync("registry", "my-tag", default)).ConfigureAwait(false); }
public async Task GetBlobAsync_ThrowsOnError_Async() { var handler = new MockHttpMessageHandler(); handler .When(HttpMethod.Get, "http://localhost:5000/v2/registry/blobs/sha:0") .Respond(HttpStatusCode.NotFound); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.GetBlobAsync("registry", "sha:0", default)).ConfigureAwait(false); handler.VerifyNoOutstandingExpectation(); }
public async Task ListTagsAsync_BadRequest_Throws_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/invalid-registry/tags/list") .Respond(HttpStatusCode.BadRequest); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.ListTagsAsync("invalid-registry", default)).ConfigureAwait(false); handler.VerifyNoOutstandingExpectation(); }
public async Task PushBlobAsync_InvalidRegistry_Throws_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Post, "http://localhost:5000/v2/REGISTRY/blobs/uploads/") .WithContent(string.Empty) .Respond(HttpStatusCode.NotFound); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.PushBlobAsync("REGISTRY", new MemoryStream(), "sha:0", default)).ConfigureAwait(false); handler.VerifyNoOutstandingExpectation(); }
public async Task DeleteManifestAsync_Works_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Delete, "http://localhost:5000/v2/registry/manifests/my-tag") .Respond(HttpStatusCode.OK); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); // Should not throw await client.DeleteManifestAsync("registry", "my-tag", default).ConfigureAwait(false); handler.VerifyNoOutstandingExpectation(); }
public async Task GetManifestAsync_RetursNullWhenNotFound_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/registry/manifests/my-tag") .With((request) => request.Headers.Accept.Any(h => h.MediaType == "application/vnd.oci.image.manifest.v1+json")) .Respond(HttpStatusCode.NotFound); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); Assert.Null(await client.GetManifestAsync("registry", "my-tag", default).ConfigureAwait(false)); handler.VerifyNoOutstandingExpectation(); }
public async Task ListTagsAsync_NotFound_Throws_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/registry/tags/list") .Respond(HttpStatusCode.NotFound, new StringContent("{\"errors\":[{\"code\":\"NAME_UNKNOWN\",\"message\":\"repository name not known to registry\",\"detail\":{\"name\":\"registry\"}}]}\n", Encoding.UTF8, "application/json")); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); var tags = await client.ListTagsAsync("registry", default).ConfigureAwait(false); Assert.Empty(tags); handler.VerifyNoOutstandingExpectation(); }
public async Task GetBlobAsync_Works_Async() { var handler = new MockHttpMessageHandler(); using (MemoryStream content = new MemoryStream()) { handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/registry/blobs/sha:0") .Respond(HttpStatusCode.OK, new StreamContent(content)); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); var result = await client.GetBlobAsync("registry", "sha:0", default).ConfigureAwait(false); Assert.NotNull(result); } handler.VerifyNoOutstandingExpectation(); }
public async Task ListTagsAsync_Works_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Get, "http://localhost:5000/v2/registry/tags/list") .Respond(HttpStatusCode.OK, new StringContent("{\"name\":\"registry\",\"tags\":[\"my-tag\"]}\n", Encoding.UTF8, "application/json")); var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); var tags = await client.ListTagsAsync("registry", default).ConfigureAwait(false); var tag = Assert.Single(tags); Assert.Equal("my-tag", tag); handler.VerifyNoOutstandingExpectation(); }
public async Task PushManifestAsync_Error_Throws_Async() { var handler = new MockHttpMessageHandler(); handler .Expect(HttpMethod.Put, "http://localhost:5000/v2/registry/manifests/my-tag") .With((request) => request.Content is StreamContent) .With((request) => request.Content.Headers.ContentType.MediaType == "application/vnd.oci.image.manifest.v1+json") .Respond(HttpStatusCode.InternalServerError, new StringContent("{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\",\"detail\":{}}]}\n", Encoding.UTF8, "application/json")); using (var blobStream = new MemoryStream()) { var httpClient = handler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var client = new ImageRegistryClient(httpClient); await Assert.ThrowsAsync <ImageRegistryException>(() => client.PushManifestAsync("registry", "my-tag", blobStream, default)).ConfigureAwait(false); } handler.VerifyNoOutstandingExpectation(); }
public async Task ListTagsAsync_ValidatesArguments_Async() { var client = new ImageRegistryClient(new HttpClient()); await Assert.ThrowsAsync <ArgumentNullException>(() => client.ListTagsAsync(null, default)).ConfigureAwait(false); }