public async Task CompareLocalPackageToFeedPackageShouldCorrectlyInterpretFeedResponse( string feedResponseContentName, HttpStatusCode feedResponseStatusCode, PackageFeedStatus expectedResult) { var localPackagePath = TestInputs.GetFullPath(Path.Combine("Nupkgs", "test-package-a.zip")); var packageContentUrl = $"https://fakefeed.azure.com/nuget/v3/{feedResponseContentName}.nupkg"; var taskLoggingHelper = new Microsoft.Build.Utilities.TaskLoggingHelper(new StubTask()); var retryHandler = new MockRetryHandler(); var response = new HttpResponseMessage(feedResponseStatusCode); if (!string.IsNullOrEmpty(feedResponseContentName)) { var content = TestInputs.ReadAllBytes(Path.Combine("Nupkgs", $"{feedResponseContentName}.zip")); response.Content = new ByteArrayContent(content); } ; var httpClient = FakeHttpClient.WithResponses(response); var result = await GeneralUtils.CompareLocalPackageToFeedPackage( localPackagePath, packageContentUrl, httpClient, taskLoggingHelper, retryHandler); result.Should().Be(expectedResult); }
[InlineData(HttpStatusCode.InternalServerError, 2)] // retry on 5xx public async Task CompareLocalPackageToFeedPackageShouldRetryFailedRequests( HttpStatusCode initialResponseStatusCode, int expectedAttemptCount) { var testPackageName = Path.Combine("Nupkgs", "test-package-a.zip"); var localPackagePath = TestInputs.GetFullPath(testPackageName); var packageContentUrl = "https://fakefeed.azure.com/nuget/v3/test-package-a.zip"; var taskLoggingHelper = new Microsoft.Build.Utilities.TaskLoggingHelper(new StubTask()); var retryHandler = new MockRetryHandler(maxAttempts: 2); var responseContent = TestInputs.ReadAllBytes(testPackageName); var responses = new[] { new HttpResponseMessage(initialResponseStatusCode) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(responseContent) } }; var httpClient = FakeHttpClient.WithResponses(responses); await GeneralUtils.CompareLocalPackageToFeedPackage( localPackagePath, packageContentUrl, httpClient, taskLoggingHelper, retryHandler); retryHandler.ActualAttempts.Should().Be(expectedAttemptCount); }
public void DownloadFileAsyncSucceedsForValidUrl() { var buildEngine = new MockBuildEngine(); var publishTask = new PublishArtifactsInManifestV3 { BuildEngine = buildEngine, }; var testFile = Path.Combine("Symbols", "test.txt"); var responseContent = TestInputs.ReadAllBytes(testFile); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(responseContent) }; using HttpClient client = FakeHttpClient.WithResponses(response); var path = TestInputs.GetFullPath(Guid.NewGuid().ToString()); var test = publishTask.DownloadFileAsync( client, PublishArtifactsInManifestBase.ArtifactName.BlobArtifacts, "1234", "test.txt", path); Assert.True(File.Exists(path)); publishTask.DeleteTemporaryFiles(path); publishTask.DeleteTemporaryDirectory(path); }
public async Task DownloadFailureWhenStatusCodeIsInvalid(HttpStatusCode httpStatus) { var buildEngine = new MockBuildEngine(); var publishTask = new PublishArtifactsInManifestV3 { BuildEngine = buildEngine, }; var testFile = Path.Combine("Symbols", "test.txt"); var responseContent = TestInputs.ReadAllBytes(testFile); publishTask.RetryHandler = new ExponentialRetry() { MaxAttempts = 3, DelayBase = 1 }; var responses = new[] { new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) } }; using HttpClient client = FakeHttpClient.WithResponses(responses); var path = TestInputs.GetFullPath(Guid.NewGuid().ToString()); var actualError = await Assert.ThrowsAsync <Exception>(() => publishTask.DownloadFileAsync( client, PublishArtifactsInManifestBase.ArtifactName.BlobArtifacts, "1234", "test.txt", path)); Assert.Contains($"Failed to download local file '{path}' after {publishTask.RetryHandler.MaxAttempts} attempts. See inner exception for details.", actualError.Message); }
public async Task ErrorAfterMaxRetriesToGetContainerId(HttpStatusCode httpStatus) { var buildEngine = new MockBuildEngine(); var publishTask = new PublishArtifactsInManifestV3 { BuildEngine = buildEngine, }; publishTask.BuildId = "1243456"; publishTask.RetryHandler = new ExponentialRetry() { MaxAttempts = 3, DelayBase = 1 }; var testPackageName = Path.Combine("Symbols", "test.txt"); var responseContent = TestInputs.ReadAllBytes(testPackageName); var responses = new[] { new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) } }; using HttpClient client = FakeHttpClient.WithResponses(responses); var actualError = await Assert.ThrowsAsync <Exception>(() => publishTask.GetContainerIdAsync( client, PublishArtifactsInManifestBase.ArtifactName.BlobArtifacts)); Assert.Contains($"Failed to get container id after {publishTask.RetryHandler.MaxAttempts} attempts. See inner exception for details,", actualError.Message); }
public async Task DownloadFileSuccessfulAfterRetryTest(HttpStatusCode httpStatus) { var buildEngine = new MockBuildEngine(); var publishTask = new PublishArtifactsInManifestV3 { BuildEngine = buildEngine, }; var testFile = Path.Combine("Symbols", "test.txt"); var responseContent = TestInputs.ReadAllBytes(testFile); publishTask.RetryHandler = new ExponentialRetry() { MaxAttempts = 2, DelayBase = 1 }; var responses = new[] { new HttpResponseMessage(httpStatus) { Content = new ByteArrayContent(responseContent) }, new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(responseContent) } }; using HttpClient client = FakeHttpClient.WithResponses(responses); var path = TestInputs.GetFullPath(Guid.NewGuid().ToString()); await publishTask.DownloadFileAsync( client, PublishArtifactsInManifestBase.ArtifactName.BlobArtifacts, "1234", "test.txt", path); Assert.True(File.Exists(path)); publishTask.DeleteTemporaryFiles(path); publishTask.DeleteTemporaryDirectory(path); }
public async Task GetContainerIdToDownloadArtifactAsync(PublishArtifactsInManifestBase.ArtifactName artifactName, string containerId) { var buildEngine = new MockBuildEngine(); var publishTask = new PublishArtifactsInManifestV3 { BuildEngine = buildEngine, }; publishTask.BuildId = "1243456"; var testPackageName = Path.Combine("Symbols", "test.txt"); var responseContent = TestInputs.ReadAllBytes(testPackageName); var responses = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(responseContent) }; using HttpClient client = FakeHttpClient.WithResponses(responses); var test = await publishTask.GetContainerIdAsync( client, artifactName); Assert.Equal(containerId, test); }