|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using Moq; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | +using OctoshiftCLI.Extensions; |
| 8 | +using OctoshiftCLI.Services; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace OctoshiftCLI.Tests.Octoshift.Services |
| 12 | +{ |
| 13 | + public class AdoPipelineTriggerService_ErrorHandlingTests |
| 14 | + { |
| 15 | + private const string ADO_ORG = "foo-org"; |
| 16 | + private const string TEAM_PROJECT = "foo-project"; |
| 17 | + private const string REPO_NAME = "foo-repo"; |
| 18 | + private const string PIPELINE_NAME = "CI Pipeline"; |
| 19 | + private const int PIPELINE_ID = 123; |
| 20 | + private const string ADO_SERVICE_URL = "https://dev.azure.com"; |
| 21 | + |
| 22 | + private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); |
| 23 | + private readonly Mock<AdoApi> _mockAdoApi = TestHelpers.CreateMock<AdoApi>(); |
| 24 | + private readonly AdoPipelineTriggerService _triggerService; |
| 25 | + |
| 26 | + public AdoPipelineTriggerService_ErrorHandlingTests() |
| 27 | + { |
| 28 | + _triggerService = new AdoPipelineTriggerService(_mockAdoApi.Object, _mockOctoLogger.Object, ADO_SERVICE_URL); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task RewirePipelineToGitHub_Should_Skip_When_Pipeline_Not_Found_404() |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + var githubOrg = "github-org"; |
| 36 | + var githubRepo = "github-repo"; |
| 37 | + var serviceConnectionId = Guid.NewGuid().ToString(); |
| 38 | + var defaultBranch = "main"; |
| 39 | + var clean = "true"; |
| 40 | + var checkoutSubmodules = "false"; |
| 41 | + |
| 42 | + var pipelineUrl = $"{ADO_SERVICE_URL}/{ADO_ORG.EscapeDataString()}/{TEAM_PROJECT.EscapeDataString()}/_apis/build/definitions/{PIPELINE_ID}?api-version=6.0"; |
| 43 | + |
| 44 | + // Mock 404 error when trying to get pipeline definition |
| 45 | + _mockAdoApi.Setup(x => x.GetAsync(pipelineUrl)) |
| 46 | + .ThrowsAsync(new HttpRequestException("Response status code does not indicate success: 404 (Not Found).")); |
| 47 | + |
| 48 | + // Act & Assert - Should not throw exception, should handle gracefully |
| 49 | + await _triggerService.Invoking(x => x.RewirePipelineToGitHub( |
| 50 | + ADO_ORG, TEAM_PROJECT, PIPELINE_ID, defaultBranch, clean, checkoutSubmodules, |
| 51 | + githubOrg, githubRepo, serviceConnectionId, null, null)) |
| 52 | + .Should().NotThrowAsync(); |
| 53 | + |
| 54 | + // Verify that warning was logged |
| 55 | + _mockOctoLogger.Verify(x => x.LogWarning(It.Is<string>(s => |
| 56 | + s.Contains("Pipeline 123 not found") && |
| 57 | + s.Contains("Skipping pipeline rewiring"))), Times.Once); |
| 58 | + |
| 59 | + // Verify that PutAsync was never called since we should skip the operation |
| 60 | + _mockAdoApi.Verify(x => x.PutAsync(It.IsAny<string>(), It.IsAny<object>()), Times.Never); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task RewirePipelineToGitHub_Should_Skip_When_Pipeline_HTTP_Error() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var githubOrg = "github-org"; |
| 68 | + var githubRepo = "github-repo"; |
| 69 | + var serviceConnectionId = Guid.NewGuid().ToString(); |
| 70 | + var defaultBranch = "main"; |
| 71 | + var clean = "true"; |
| 72 | + var checkoutSubmodules = "false"; |
| 73 | + |
| 74 | + var pipelineUrl = $"{ADO_SERVICE_URL}/{ADO_ORG.EscapeDataString()}/{TEAM_PROJECT.EscapeDataString()}/_apis/build/definitions/{PIPELINE_ID}?api-version=6.0"; |
| 75 | + |
| 76 | + // Mock HTTP error (not 404) when trying to get pipeline definition |
| 77 | + _mockAdoApi.Setup(x => x.GetAsync(pipelineUrl)) |
| 78 | + .ThrowsAsync(new HttpRequestException("Response status code does not indicate success: 500 (Internal Server Error).")); |
| 79 | + |
| 80 | + // Act & Assert - Should not throw exception, should handle gracefully |
| 81 | + await _triggerService.Invoking(x => x.RewirePipelineToGitHub( |
| 82 | + ADO_ORG, TEAM_PROJECT, PIPELINE_ID, defaultBranch, clean, checkoutSubmodules, |
| 83 | + githubOrg, githubRepo, serviceConnectionId, null, null)) |
| 84 | + .Should().NotThrowAsync(); |
| 85 | + |
| 86 | + // Verify that warning was logged |
| 87 | + _mockOctoLogger.Verify(x => x.LogWarning(It.Is<string>(s => |
| 88 | + s.Contains("HTTP error retrieving pipeline 123") && |
| 89 | + s.Contains("Skipping pipeline rewiring"))), Times.Once); |
| 90 | + |
| 91 | + // Verify that PutAsync was never called since we should skip the operation |
| 92 | + _mockAdoApi.Verify(x => x.PutAsync(It.IsAny<string>(), It.IsAny<object>()), Times.Never); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public async Task RewirePipelineToGitHub_Should_Continue_When_Pipeline_Found() |
| 97 | + { |
| 98 | + // Arrange |
| 99 | + var githubOrg = "github-org"; |
| 100 | + var githubRepo = "github-repo"; |
| 101 | + var serviceConnectionId = Guid.NewGuid().ToString(); |
| 102 | + var defaultBranch = "main"; |
| 103 | + var clean = "true"; |
| 104 | + var checkoutSubmodules = "false"; |
| 105 | + |
| 106 | + var existingPipelineData = new |
| 107 | + { |
| 108 | + name = PIPELINE_NAME, |
| 109 | + repository = new { name = REPO_NAME }, |
| 110 | + triggers = new JArray() |
| 111 | + }; |
| 112 | + |
| 113 | + var pipelineUrl = $"{ADO_SERVICE_URL}/{ADO_ORG.EscapeDataString()}/{TEAM_PROJECT.EscapeDataString()}/_apis/build/definitions/{PIPELINE_ID}?api-version=6.0"; |
| 114 | + var repoUrl = $"{ADO_SERVICE_URL}/{ADO_ORG.EscapeDataString()}/{TEAM_PROJECT.EscapeDataString()}/_apis/git/repositories/{REPO_NAME.EscapeDataString()}?api-version=6.0"; |
| 115 | + |
| 116 | + // Mock successful pipeline retrieval |
| 117 | + _mockAdoApi.Setup(x => x.GetAsync(pipelineUrl)) |
| 118 | + .ReturnsAsync(existingPipelineData.ToJson()); |
| 119 | + |
| 120 | + // Mock repository lookup for branch policy check |
| 121 | + var repositoryId = "repo-123"; |
| 122 | + var repoResponse = new { id = repositoryId, name = REPO_NAME }.ToJson(); |
| 123 | + _mockAdoApi.Setup(x => x.GetAsync(repoUrl)) |
| 124 | + .ReturnsAsync(repoResponse); |
| 125 | + |
| 126 | + // Mock branch policies (empty) |
| 127 | + var policies = new { count = 0, value = Array.Empty<object>() }.ToJson(); |
| 128 | + var policyUrl = $"{ADO_SERVICE_URL}/{ADO_ORG.EscapeDataString()}/{TEAM_PROJECT.EscapeDataString()}/_apis/policy/configurations?repositoryId={repositoryId}&api-version=6.0"; |
| 129 | + _mockAdoApi.Setup(x => x.GetAsync(policyUrl)) |
| 130 | + .ReturnsAsync(policies); |
| 131 | + |
| 132 | + // Act |
| 133 | + await _triggerService.RewirePipelineToGitHub( |
| 134 | + ADO_ORG, TEAM_PROJECT, PIPELINE_ID, defaultBranch, clean, checkoutSubmodules, |
| 135 | + githubOrg, githubRepo, serviceConnectionId, null, null); |
| 136 | + |
| 137 | + // Assert - Verify that PutAsync was called (pipeline was successfully rewired) |
| 138 | + _mockAdoApi.Verify(x => x.PutAsync(pipelineUrl, It.IsAny<object>()), Times.Once); |
| 139 | + |
| 140 | + // Verify that no error warnings were logged |
| 141 | + _mockOctoLogger.Verify(x => x.LogWarning(It.Is<string>(s => |
| 142 | + s.Contains("not found") || s.Contains("HTTP error"))), Times.Never); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments