Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
        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 TestableJobStatusReportingHelper(vstsContext, new TraceLogger(), mockTaskHttpClient, mockReleaseClient, mockBuildClient, timeLineRecordName);

            // 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);
                }
            }
        }