public async Task WaitForDropArtifactBuildFailedTest() { // given var buildClient = new MockBuildClient() { MockBuild = new Build() { Status = BuildStatus.Completed, Result = BuildResult.Failed, Repository = new BuildRepository() { Name = "someRepo" } }, }; var gitClient = new MockGitClient(); var releaseClient = new MockReleaseClient(); var artifactHelper = new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo"); // when var throws = false; try { await artifactHelper.WaitForDropArtifact("someArtifact", 123, default(CancellationToken)); } catch (VstsArtifactsNotFoundException) { throws = true; } // then Assert.IsTrue(throws); }
public TestableJobStatusReportingHelper(VstsMessage vstsMessage, ILogger traceLogger, ITaskClient mockTaskHttpClient, IReleaseClient releaseClient, MockBuildClient mockBuildClient, string timeLineRecordName = null) : base(vstsMessage, traceLogger, timeLineRecordName) { this.mockTaskHttpClient = mockTaskHttpClient; this.releaseClient = releaseClient; this.mockBuildClient = mockBuildClient; }
public async Task ExtractFileContainerArtifactFailsIfVstsDropJsonNotFoundTest() { // given var expectedContent = "somestuff"; var entryName = string.Format("{0}/RANDOMNAME", Guid.NewGuid()); var buildClient = new MockBuildClient() { ContentStream = GetZippedStream(expectedContent, entryName) }; var gitClient = new MockGitClient(); var releaseClient = new MockReleaseClient(); var artifactHelper = new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo"); // when var throws = false; try { await artifactHelper.ExtractFileContainerArtifact("someArtifact", 123, default(CancellationToken)); } catch (VstsArtifactsNotFoundException ex) { if (ex.Message.Contains("not found ")) { throws = true; } } // then Assert.IsTrue(throws); }
public async Task TryGetArtifactBuildIdFromRelease_NoMatchingArtifactSourceAliasTest() { // given var mockArtifactDefinitions = new List <AgentArtifactDefinition>() { new AgentArtifactDefinition() { Alias = "someAlias", Version = "123", } }; var releaseClient = new MockReleaseClient() { MockArtifactDefinitions = mockArtifactDefinitions, }; var gitClient = new MockGitClient(); var buildClient = new MockBuildClient(); var artifactHelper = new VstsArtifactsHelper(buildClient, releaseClient, gitClient, Guid.NewGuid(), "someRepo"); // when var throws = false; try { await artifactHelper.TryGetArtifactBuildIdFromRelease(123, "otherAlias", default(CancellationToken)); } catch (VstsArtifactsNotFoundException) { throws = true; } // then Assert.IsTrue(throws); }
public async Task ExecuteSkipsDeletedBuildTest() { // given - a message with a handler that fails var mockMessage = CreateMockMessage(CreateValidTestVstsMessage()); var mockVstsHandler = new MockVstsHandler() { MockExecuteFunc = msg => { Assert.Fail("should not be called"); throw new NotImplementedException("should not be called"); }, }; var mockBuildClient = new MockBuildClient() { ReturnNullBuild = true }; var mockMessageListener = new MockServiceBusQueueMessageListener(); // when await ProcessTestMessage(mockMessage, mockMessageListener, new MockTaskClient(), mockVstsHandler, mockBuildClient : mockBuildClient); // then - logs are written to existing timeline Assert.IsTrue(mockMessageListener.IsCompleted); }
public async Task TryGetArtifactBuildIdFromRelease_MatchingArtifactSourceAliasTest() { // given var mockArtifactDefinitions = new List <AgentArtifactDefinition>() { new AgentArtifactDefinition() { Alias = "someAlias", Version = "123", }, new AgentArtifactDefinition() { Alias = "matchingAlias", Version = "456", }, }; var releaseClient = new MockReleaseClient() { MockArtifactDefinitions = mockArtifactDefinitions, }; var gitClient = new MockGitClient(); var buildClient = new MockBuildClient(); var artifactHelper = new VstsArtifactsHelper(buildClient, releaseClient, gitClient, Guid.NewGuid(), "someRepo"); // when var result = await artifactHelper.TryGetArtifactBuildIdFromRelease(123, "matchingAlias", default(CancellationToken)); // then Assert.IsNotNull(result); Assert.AreEqual(false, result.Failed); Assert.AreEqual(456, result.DropSourceBuildId); }
public async Task WaitForDropArtifactBuildNotFoundTest() { // given var buildClient = new MockBuildClient() { ReturnNullBuild = true, }; var releaseClient = new MockReleaseClient(); var gitClient = new MockGitClient(); var artifactHelper = new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo"); // when var throws = false; try { await artifactHelper.WaitForDropArtifact("someArtifact", 123, default(CancellationToken)); } catch (ArgumentException) { throws = true; } // then Assert.IsTrue(throws); }
private VstsArtifactsHelper FromArtifactResource(ArtifactResource r) { var gitClient = new MockGitClient(); var releaseClient = new MockReleaseClient(); var buildClient = new MockBuildClient() { MockBuildArtifact = new BuildArtifact() { Resource = r } }; return(new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo")); }
private static async Task TestReportJobStarted(ReleaseStatus releaseStatus, bool returnNullRelease, int expectedEventCount) { // given VstsMessage vstsContext = new TestVstsMessage { VstsHub = HubType.Release, VstsUri = new Uri("http://vstsUri"), VstsPlanUri = new Uri("http://vstsPlanUri"), ReleaseProperties = new VstsReleaseProperties(), }; var mockBuildClient = new MockBuildClient() { MockBuild = new Build() { Status = BuildStatus.None }, ReturnNullBuild = false, }; var mockReleaseClient = new MockReleaseClient() { MockRelease = new Release() { Status = releaseStatus }, ReturnNullRelease = returnNullRelease, }; var mockTaskClient = new MockTaskClient(); var reportingHelper = new VstsReportingHelper(vstsContext, new TraceBrokerInstrumentation(), new Dictionary <string, string>()) { CreateReleaseClient = (uri, s) => ReturnMockReleaseClientIfUrlValid(uri, vstsContext, mockReleaseClient), CreateBuildClient = (uri, s) => mockBuildClient, CreateTaskHttpClient = (uri, s, i, r) => mockTaskClient }; // when await reportingHelper.ReportJobStarted(DateTime.UtcNow, "test message", default(CancellationToken)); // then Assert.AreEqual(expectedEventCount, mockTaskClient.EventsReceived.Count); if (expectedEventCount != 0) { var taskEvent = mockTaskClient.EventsReceived[0] as JobStartedEvent; Assert.IsNotNull(taskEvent); } }
public async Task ExtractFileContainerArtifactTest() { // given var expectedContent = "somestuff"; var entryName = string.Format("{0}/{1}", Guid.NewGuid(), VstsArtifactsHelper.VstsDropJsonFileName); var buildClient = new MockBuildClient() { ContentStream = GetZippedStream(expectedContent, entryName) }; var releaseClient = new MockReleaseClient(); var gitClient = new MockGitClient(); var artifactHelper = new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo"); // when var contentString = await artifactHelper.ExtractFileContainerArtifact("someArtifact", 123, default(CancellationToken)); // then Assert.AreEqual(expectedContent, contentString); }
private static async Task TestReportJobStarted(ReleaseStatus releaseStatus, bool returnNullRelease, int expectedEventCount) { // given VstsMessage vstsContext = new TestVstsMessage { VstsHub = HubType.Release, VstsUri = new Uri("http://vstsUri"), VstsPlanUri = new Uri("http://vstsPlanUri"), ReleaseProperties = new VstsReleaseProperties(), }; var mockBuildClient = new MockBuildClient() { MockBuild = new Build() { Status = BuildStatus.None }, ReturnNullBuild = false, }; var mockReleaseClient = new MockReleaseClient() { MockRelease = new Release() { Status = releaseStatus }, ReturnNullRelease = returnNullRelease, }; var mockTaskClient = new MockTaskClient(); Assert.AreNotEqual(vstsContext.VstsUri, vstsContext.VstsPlanUri, "need to be different to ensure we can test correct one is used"); var reportingHelper = new TestableJobStatusReportingHelper(vstsContext, new TraceLogger(), mockTaskClient, mockReleaseClient, mockBuildClient); // when await reportingHelper.ReportJobStarted(DateTime.UtcNow, "test message", default(CancellationToken)); // then Assert.AreEqual(expectedEventCount, mockTaskClient.EventsReceived.Count); if (expectedEventCount != 0) { var taskEvent = mockTaskClient.EventsReceived[0] as JobStartedEvent; Assert.IsNotNull(taskEvent); } }
private static async Task TestReportJobStarted(BuildStatus buildStatus, bool returnNullBuild, int expectedEventCount) { // given VstsMessage vstsContext = new TestVstsMessage { VstsHub = HubType.Build, VstsUri = new Uri("http://vstsUri"), VstsPlanUri = new Uri("http://vstsPlanUri"), BuildProperties = new VstsBuildProperties(), }; var mockBuildClient = new MockBuildClient() { MockBuild = new Build() { Status = buildStatus }, ReturnNullBuild = returnNullBuild, }; var mockTaskClient = new MockTaskClient(); var mockReleaseClient = new MockReleaseClient(); var reportingHelper = new TestableJobStatusReportingHelper(vstsContext, new TraceLogger(), mockTaskClient, mockReleaseClient, mockBuildClient) { //CreateBuildClient = (uri, s) => ReportingBrokerJobCompletedTests.ReturnMockBuildClientIfUrlValid(uri, vstsContext, mockBuildClient), //CreateReleaseClient = (uri, t) => mockReleaseClient }; // when await reportingHelper.ReportJobStarted(DateTime.UtcNow, "test message", default(CancellationToken)); // then Assert.AreEqual(expectedEventCount, mockTaskClient.EventsReceived.Count); if (expectedEventCount != 0) { var taskEvent = mockTaskClient.EventsReceived[0] as JobStartedEvent; Assert.IsNotNull(taskEvent); } }
public async Task WaitForDropArtifactGoldenPathTest() { // given var expectedContent = "{\"VstsDropBuildArtifact\":{\"VstsDropUrl\":\"https://someartifacturl\"}}"; var entryName = string.Format("{0}/{1}", Guid.NewGuid(), VstsArtifactsHelper.VstsDropJsonFileName); var buildClient = new MockBuildClient() { MockBuild = new Build() { Status = BuildStatus.Completed, Result = BuildResult.Succeeded }, MockBuildArtifact = new BuildArtifact(), ContentStream = GetZippedStream(expectedContent, entryName), }; var gitClient = new MockGitClient(); var releaseClient = new MockReleaseClient(); var artifactHelper = new VstsArtifactsHelper(buildClient: buildClient, releaseClient: releaseClient, gitClient: gitClient, projectId: Guid.NewGuid(), repoName: "someRepo"); // when var artifactUrl = await artifactHelper.WaitForDropArtifact("someArtifact", 123, default(CancellationToken)); // then Assert.AreEqual("https://someartifacturl", artifactUrl); }
internal static MockBuildClient ReturnMockBuildClientIfUrlValid(Uri uri, VstsMessage vstsMessage, MockBuildClient mockBuildClient) { Assert.IsNotNull(uri, "require uri to validate correct one is used"); Assert.AreNotEqual(vstsMessage.VstsUri, vstsMessage.VstsPlanUri, "need to be different to ensure we can test correct one is used"); Assert.AreEqual(vstsMessage.VstsUri, uri, "wrong url passed to create bulid client"); return(mockBuildClient); }
private async Task TestReportJobCompleted(BuildStatus buildStatus, bool returnNullBuild, bool isPassed, int expectedEventCount, TaskResult expectedResult, int expectedRecordCount = 0, string timeLineRecordName = null) { // given Guid parentId = Guid.NewGuid(); Guid childId = Guid.NewGuid(); VstsMessage vstsContext = new TestVstsMessage { JobId = parentId, VstsHub = HubType.Build, VstsUri = new Uri("http://vstsUri"), VstsPlanUri = new Uri("http://vstsPlanUri"), BuildProperties = new VstsBuildProperties(), }; var mockBuildClient = new MockBuildClient() { MockBuild = new Build() { Status = buildStatus }, ReturnNullBuild = returnNullBuild, }; var mockReleaseClient = new MockReleaseClient() { MockRelease = new Release() { Status = ReleaseStatus.Undefined }, ReturnNullRelease = false, }; var timelineRecords = new List <Microsoft.TeamFoundation.DistributedTask.WebApi.TimelineRecord> { new Microsoft.TeamFoundation.DistributedTask.WebApi.TimelineRecord() { Id = parentId }, new Microsoft.TeamFoundation.DistributedTask.WebApi.TimelineRecord() { Id = childId, ParentId = parentId }, new Microsoft.TeamFoundation.DistributedTask.WebApi.TimelineRecord() { // Should be ignored Id = Guid.NewGuid() } }; if (!string.IsNullOrEmpty(timeLineRecordName)) { timelineRecords.Add(new Microsoft.TeamFoundation.DistributedTask.WebApi.TimelineRecord() { Id = Guid.NewGuid(), Name = timeLineRecordName }); } var mockTaskHttpClient = new MockTaskClient() { GetRecordsReturnCollection = timelineRecords }; var reportingHelper = new VstsReportingHelper(vstsContext, new TraceBrokerInstrumentation(), new Dictionary <string, string>(), timeLineRecordName) { CreateBuildClient = (uri, s) => ReturnMockBuildClientIfUrlValid(uri, vstsContext, mockBuildClient), CreateReleaseClient = (uri, s) => mockReleaseClient, CreateTaskHttpClient = (uri, s, i, r) => mockTaskHttpClient }; // when await reportingHelper.ReportJobCompleted(DateTime.UtcNow, "test message", isPassed, default(CancellationToken)); // then Assert.AreEqual(expectedEventCount, mockTaskHttpClient.EventsReceived.Count); if (expectedEventCount != 0) { var taskEvent = mockTaskHttpClient.EventsReceived[0] as JobCompletedEvent; Assert.IsNotNull(taskEvent); Assert.AreEqual(taskEvent.Result, expectedResult); } Assert.AreEqual(expectedRecordCount, mockTaskHttpClient.TimelineRecordsUpdated.Count); if (expectedRecordCount != 0) { var records = string.IsNullOrEmpty(timeLineRecordName) ? mockTaskHttpClient.TimelineRecordsUpdated.Where(rec => rec.Id == parentId || rec.Id == childId).ToList() : mockTaskHttpClient.TimelineRecordsUpdated.Where(rec => rec.Name != null && rec.Name.Equals(timeLineRecordName, StringComparison.OrdinalIgnoreCase)).ToList(); Assert.AreEqual(expectedRecordCount, records.Count); foreach (var record in records) { Assert.IsNotNull(record); Assert.AreEqual(expectedResult, record.Result); } } }