public ActionResult Get(string name)
        {
            using (TfsClaimsPrincipal claim = (TfsClaimsPrincipal)HttpContext.User)
            {
                CommandHandler handler  = new CommandHandler(claim, null);
                IdentityRef    identity = handler.GetMemberByName(claim.TfsIdentity, name);
                if (identity == null)
                {
                    return(new ContentResult().GetContentUserNotFound(name));
                }
                else
                {
                    IList <WorkItem> items = handler.GetWorkItemResult(name, 1);

                    if (claim.IsReturnJson)
                    {
                        return(new JsonResult(items));
                    }
                    else
                    {
                        return(claim.GetContentResult(items.ToBotString()));
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Downloads the profile picture and returns the url.
        /// </summary>
        /// <param name="user">The user to extend.</param>
        /// <returns>The path of the image.</returns>
        public static string GetProfilePath(this IdentityRef user)
        {
            var imgDir     = HttpContext.Current.Server.MapPath("~/Images");
            var serverPath = Path.Combine(imgDir, $"{user.Id}.png");

            Directory.CreateDirectory(imgDir);
            if (File.Exists(serverPath) && new FileInfo(serverPath).LastWriteTime > DateTime.Now - new TimeSpan(24, 0, 0))
            {
                return($"/Images/{user.Id}.png");
            }

            try
            {
                var apiUri = InfoscreenConfiguration.Instance.Uri.ToString();
                var client = new RestClient(apiUri)
                {
                    Authenticator = new HttpBasicAuthenticator(string.Empty, InfoscreenConfiguration.Instance.AccessToken)
                };
                var request  = new RestRequest($"{user.ImageUrl.Substring(user.ImageUrl.IndexOf(apiUri) + apiUri.Length) }&size=2");
                var response = client.DownloadData(request);
                using (var memoryStream = new MemoryStream(response))
                    using (var image = Image.FromStream(memoryStream))
                    {
                        image.Save(serverPath, ImageFormat.Png);
                    }
            }
            catch (Exception)
            {
                return($"{user.ImageUrl}&size=2");
            }

            return($"/Images/{user.Id}.png");
        }
 public TestCaseDefinition(string automatedTestName, IdentityRef owner, string priority, string storage)
 {
     AutomatedTestName = automatedTestName;
     Storage           = storage;
     Owner             = owner;
     Priority          = priority;
 }
Example #4
0
        static IList <string> fieldsToStringList(int blanksPrefix, IDictionary <String, Object> fields)
        {
            IList <string> strs = new List <string>();

            if (fields == null || fields.Count == 0)
            {
                return(strs);
            }

            strs.Add(createBlanks(blanksPrefix) + "Fields: ");
            foreach (var field in fields)
            {
                if (field.Value is IdentityRef)
                {
                    IdentityRef identityRef = (IdentityRef)field.Value;
                    strs.AddRange(IdentityRefToStringList(blanksPrefix + 4, identityRef, field.Key));
                }
                else
                {
                    strs.Add(createBlanks(blanksPrefix + 4) + field.Key + ": " + field.Value);
                }
            }

            return(strs);
        }
Example #5
0
 private static string?FieldOutput(object workItemField)
 {
     return(workItemField switch
     {
         IdentityRef r => r.DisplayName,
         _ => workItemField?.ToString()
     });
Example #6
0
 public WorkItemStore(EngineContext context, WorkItem workItem) : this(context)
 {
     //initialize tracker with initial work item
     var wrapper = new WorkItemWrapper(_context, workItem);
     //store event initiator identity
     _triggerIdentity = wrapper.ChangedBy;
 }
Example #7
0
        static IList <string> IdentityRefToStringList(int blanksPrefix, IdentityRef identityRef, string keyString)
        {
            IList <string> strs = new List <string>();

            if (identityRef == null)
            {
                return(strs);
            }

            strs.Add(createBlanks(blanksPrefix) + keyString);

            if (!string.IsNullOrWhiteSpace(identityRef.Id))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "Id: " + identityRef.Id);
            }

            if (!string.IsNullOrWhiteSpace(identityRef.DisplayName))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "DisplayName: " + identityRef.DisplayName);
            }

            if (!string.IsNullOrWhiteSpace(identityRef.DirectoryAlias))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "DirectoryAlias: " + identityRef.DirectoryAlias);
            }

            if (!string.IsNullOrWhiteSpace(identityRef.ImageUrl))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "ImageUrl: " + identityRef.ImageUrl);
            }

            strs.Add(createBlanks(blanksPrefix + 4) + "inactive: " + identityRef.Inactive.ToString());
            strs.Add(createBlanks(blanksPrefix + 4) + "AadIdentity: " + identityRef.IsAadIdentity.ToString());
            strs.Add(createBlanks(blanksPrefix + 4) + "container: " + identityRef.IsContainer.ToString());
            strs.Add(createBlanks(blanksPrefix + 4) + "deletedInOrigin: " + identityRef.IsDeletedInOrigin.ToString());

            if (!string.IsNullOrWhiteSpace(identityRef.ProfileUrl))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "ProfileUrl: " + identityRef.ProfileUrl);
            }

            if (!string.IsNullOrWhiteSpace(identityRef.UniqueName))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "UniqueName: " + identityRef.UniqueName);
            }

            if (!string.IsNullOrWhiteSpace(identityRef.Url))
            {
                strs.Add(createBlanks(blanksPrefix + 4) + "Url: " + identityRef.Url);
            }

            strs.AddRange(linksToStringList(blanksPrefix + 4, identityRef.Links));

            strs.AddRange(subjectDescriptorToStringList(blanksPrefix + 4, identityRef.Descriptor));

            return(strs);
        }
        private static string GetAlias(IdentityRef identityRef)
        {
            var email     = identityRef.UniqueName;
            var indexOfAt = email.IndexOf('@');

            return(indexOfAt >= 0
                ? email.Substring(0, indexOfAt)
                : email);
        }
Example #9
0
        private async Task EnsureScheduledBuildFailSubscriptionExists(BuildDefinition pipeline, WebApiTeam team, bool persistChanges)
        {
            const string BuildFailureNotificationTag = "#AutomaticBuildFailureNotification";
            var          subscriptions = await service.GetSubscriptionsAsync(team.Id);

            var hasSubscription = subscriptions.Any(sub => sub.Description.Contains(BuildFailureNotificationTag));

            logger.LogInformation("Team Is Subscribed TeamId = {0} PipelineId = {1} HasSubscription = {2}", team.Id, pipeline.Id, hasSubscription);

            if (!hasSubscription)
            {
                var filterModel = new ExpressionFilterModel
                {
                    Clauses = new ExpressionFilterClause[]
                    {
                        new ExpressionFilterClause {
                            Index = 1, LogicalOperator = "", FieldName = "Status", Operator = "=", Value = "Failed"
                        },
                        new ExpressionFilterClause {
                            Index = 2, LogicalOperator = "And", FieldName = "Definition name", Operator = "=", Value = $"\\{pipeline.Project.Name}\\{pipeline.Name}"
                        },
                        new ExpressionFilterClause {
                            Index = 3, LogicalOperator = "And", FieldName = "Build reason", Operator = "=", Value = "Scheduled"
                        }
                    }
                };
                var filter = new ExpressionFilter("ms.vss-build.build-completed-event", filterModel);

                var identity = new IdentityRef
                {
                    Id  = team.Id.ToString(),
                    Url = team.IdentityUrl
                };

                var newSubscription = new NotificationSubscriptionCreateParameters
                {
                    Channel = new UserSubscriptionChannel {
                        UseCustomAddress = false
                    },
                    Description = $"A build fails {BuildFailureNotificationTag}",
                    Filter      = filter,
                    Scope       = new SubscriptionScope {
                        Type = "none", Id = pipeline.Project.Id
                    },
                    Subscriber = identity,
                };

                logger.LogInformation("Creating Subscription PipelineId = {0}, TeamId = {1}", pipeline.Id, team.Id);
                if (persistChanges)
                {
                    var subscription = await service.CreateSubscriptionAsync(newSubscription);
                }
            }
        }
        private void InitialiseRunUserIdRef(TestRunContext runContext)
        {
            string runUser = "";

            if (runContext?.Owner != null)
            {
                runUser = runContext.Owner;
            }

            _runUserIdRef = new IdentityRef()
            {
                DisplayName = runUser
            };
        }
Example #11
0
        static Release UpdateReleaseWithApprover(ReleaseHttpClient rmClient, Release release)
        {
            var         releaseApproval = new ReleaseDefinitionApprovalStep();
            IdentityRef identifyref     = new IdentityRef();

            identifyref.UniqueName      = "*****@*****.**";
            identifyref.Id              = "8af33462-3b72-4a0f-bde1-815b1c12e64b";
            releaseApproval.Approver    = identifyref;
            releaseApproval.Rank        = 1;
            releaseApproval.IsAutomated = false;

            release.Environments[1].PreApprovalsSnapshot.Approvals.Clear();
            release.Environments[1].PreApprovalsSnapshot.Approvals.Add(releaseApproval);

            var updatedRelease = rmClient.UpdateReleaseAsync(release, projectName, release.Id).Result;

            return(updatedRelease);
        }
Example #12
0
        /// <summary>
        /// Export to a file.
        /// </summary>
        /// <param name="fileName">The output file name.</param>
        /// <param name="cultureName">The culture name for the CSV file format. If not defined, takes the current culture.</param>
        /// <param name="withHeaders">Add column headers in the first line of the file.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task ToFile(string fileName, string?cultureName = null, bool withHeaders = true)
        {
            var data = await this.query.GetData();

            var culture = (cultureName == null) ? CultureInfo.CurrentCulture : new CultureInfo(cultureName);
            var conf    = new CsvConfiguration(culture)
            {
                Encoding = Encoding.Unicode,
            };

            using var writer = new StreamWriter(fileName);
            using var csv    = new CsvWriter(writer, conf);

            if (withHeaders)
            {
                foreach (var field in data.Fields)
                {
                    csv.WriteField(field);
                }

                await csv.NextRecordAsync();
            }

            foreach (var record in data.Records)
            {
                foreach (var value in from field in record
                         let value = field switch
                {
                    IdentityRef identity => identity.DisplayName,
                    _ => field
                }
                         select value)
                {
                    csv.WriteField(value);
                }

                await csv.NextRecordAsync();
            }

            await csv.FlushAsync();
        }
Example #13
0
 public TfsUser(IdentityRef identity)
 {
     DisplayName = identity.DisplayName;
     Id          = identity.Id;
     UniqueName  = identity.Descriptor.Identifier;
 }
        /// <summary>
        /// Reads a trx file from disk, converts it into a TestRunData object.
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <returns>TestRunData</returns>
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext)
        {
            _executionContext = executionContext;

            _definitions = new Dictionary <string, TestCaseDefinition>();

            List <TestCaseResultData> results = new List <TestCaseResultData>();

            string xmlContents = File.ReadAllText(filePath);

            xmlContents = xmlContents.Replace("xmlns", "ns");

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xmlContents);

            string runName = Name + " Test Run";

            if (runContext != null)
            {
                if (!String.IsNullOrWhiteSpace(runContext.RunName))
                {
                    runName = runContext.RunName;
                }
                else
                {
                    runName = StringUtil.Format("{0} {1} {2}", runName, runContext.Configuration, runContext.Platform);
                }
            }


            string runUser = "";

            if (runContext.Owner != null)
            {
                runUser = runContext.Owner;
            }

            _runUserIdRef             = new IdentityRef();
            _runUserIdRef.DisplayName = runUser;

            //Parse the run start and finish times. If either of it is not available, send neither.
            XmlNode  node          = doc.SelectSingleNode("/TestRun/Times");
            DateTime runStartDate  = DateTime.MinValue;
            DateTime runFinishDate = DateTime.MinValue;

            if (node != null && node.Attributes["start"] != null && node.Attributes["finish"] != null)
            {
                if (DateTime.TryParse(node.Attributes["start"].Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out runStartDate))
                {
                    _executionContext.Debug(string.Format(CultureInfo.InvariantCulture, "Setting run start and finish times."));
                    //Only if there is a valid start date.
                    DateTime.TryParse(node.Attributes["finish"].Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out runFinishDate);
                    if (runFinishDate < runStartDate)
                    {
                        runFinishDate = runStartDate = DateTime.MinValue;
                        _executionContext.Debug("Run finish date is less than start date. Resetting to min value.");
                    }
                }
            }

            TestRunData testRunData = new TestRunData(
                name: runName,
                buildId: runContext.BuildId,
                startedDate: runStartDate != DateTime.MinValue ? runStartDate.ToString("o") : null,
                completedDate: runFinishDate != DateTime.MinValue ? runFinishDate.ToString("o") : null,
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                dueDate: string.Empty,
                type: string.Empty,
                buildFlavor: runContext.Configuration,
                buildPlatform: runContext.Platform,
                releaseUri: runContext.ReleaseUri,
                releaseEnvironmentUri: runContext.ReleaseEnvironmentUri
                );

            //Parse the Deployment node for the runDeploymentRoot - this is the attachment root. Required for .NET Core
            XmlNode deploymentNode  = doc.SelectSingleNode("/TestRun/TestSettings/Deployment");
            var     _attachmentRoot = string.Empty;

            if (deploymentNode != null && deploymentNode.Attributes["runDeploymentRoot"] != null)
            {
                _attachmentRoot = deploymentNode.Attributes["runDeploymentRoot"].Value;
            }
            else
            {
                _attachmentRoot = Path.GetFileNameWithoutExtension(filePath); // This for platform v1
            }
            _attachmentLocation = Path.Combine(Path.GetDirectoryName(filePath), _attachmentRoot, "In");
            _executionContext.Debug(string.Format(CultureInfo.InvariantCulture, "Attachment location: {0}", _attachmentLocation));

            AddRunLevelAttachments(filePath, doc, testRunData);

            // Create a dictionary of testcase definitions to be used when iterating over the results
            Dictionary <string, TestCaseDefinition> definitions = new Dictionary <string, TestCaseDefinition>();

            if (doc.SelectNodes("/TestRun/TestDefinitions").Count > 0)
            {
                foreach (XmlNode definitionNode in doc.SelectNodes("/TestRun/TestDefinitions")[0])
                {
                    IdentityRef owner = null;
                    string      priority = null, storage = null;
                    if (definitionNode.Attributes["storage"] != null && definitionNode.Attributes["storage"].Value != null)
                    {
                        storage = Path.GetFileName(definitionNode.Attributes["storage"].Value);
                    }

                    XmlAttribute priorityAttribute = definitionNode.Attributes["priority"];
                    if (priorityAttribute != null)
                    {
                        priority = priorityAttribute.Value;
                    }

                    XmlNode ownerNode = definitionNode.SelectSingleNode("./Owners/Owner");
                    if (ownerNode != null)
                    {
                        IdentityRef ownerIdRef = new IdentityRef();
                        ownerIdRef.DisplayName    = ownerNode.Attributes["name"].Value;
                        ownerIdRef.DirectoryAlias = ownerNode.Attributes["name"].Value;
                        owner = ownerIdRef;
                    }

                    // The automated test name should be FQDN, if we are unable to figure it out like in case of a webtest,
                    // set it as "name" from the parent (where it is always present)
                    XmlNode testResultNode    = definitionNode.SelectSingleNode("./TestMethod");
                    string  automatedTestName = null;
                    if (testResultNode != null && testResultNode.Attributes["className"] != null && testResultNode.Attributes["name"] != null)
                    {
                        // At times the class names are coming as
                        // className="MS.TF.Test.AgileX.VSTests.WiLinking.UI.WiLinkingUIQueryTests"
                        // at other times, they are as
                        // className="UnitTestProject3.UnitTest1, UnitTestProject3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        string className = testResultNode.Attributes["className"].Value.Split(',')[0];
                        automatedTestName = className + "." + testResultNode.Attributes["name"].Value;
                    }
                    else if (definitionNode.Attributes["name"] != null)
                    {
                        automatedTestName = definitionNode.Attributes["name"].Value;
                    }

                    _definitions.Add(definitionNode.Attributes["id"].Value, new TestCaseDefinition(automatedTestName, owner, priority, storage));
                }
            }

            // Read UnitTestResults, WebTestResults and OrderedTestResults
            XmlNodeList resultsNodes           = doc.SelectNodes("/TestRun/Results/UnitTestResult");
            XmlNodeList webTestResultNodes     = doc.SelectNodes("/TestRun/Results/WebTestResult");
            XmlNodeList orderedTestResultNodes = doc.SelectNodes("/TestRun/Results/TestResultAggregation");

            results.AddRange(ReadActualResults(resultsNodes, TestType.UnitTest));
            results.AddRange(ReadActualResults(webTestResultNodes, TestType.WebTest));
            results.AddRange(ReadActualResults(orderedTestResultNodes, TestType.OrderedTest));


            testRunData.Results = results.ToArray <TestCaseResultData>();
            _executionContext.Debug(string.Format(CultureInfo.InvariantCulture, "Total test results: {0}", testRunData.Results.Length));

            return(testRunData);
        }
Example #15
0
        //Based on the XUnit V2 format: http://xunit.github.io/docs/format-xml-v2.html
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            List <TestCaseResultData> results = new List <TestCaseResultData>();

            XmlDocument doc = new XmlDocument();

            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Ignore
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return(null);
            }

            string runName = Name + " Test Run";
            string runUser = "";

            if (runContext != null)
            {
                if (!string.IsNullOrWhiteSpace(runContext.RunName))
                {
                    runName = runContext.RunName;
                }
                else
                {
                    runName = string.Format(CultureInfo.CurrentCulture, "{0} {1} {2}", runName, runContext.Configuration, runContext.Platform);
                }

                if (runContext.Owner != null)
                {
                    runUser = runContext.Owner;
                }
            }

            IdentityRef runUserIdRef = new IdentityRef();

            runUserIdRef.DisplayName = runUser;

            var    minStartTime       = DateTime.MaxValue;
            var    maxCompletedTime   = DateTime.MinValue;
            bool   dateTimeParseError = true;
            bool   assemblyRunDateTimeAttributesNotPresent = false;
            bool   assemblyTimeAttributeNotPresent         = false;
            double assemblyRunDuration = 0;
            double testRunDuration     = 0;

            XmlNodeList assemblyNodes = doc.SelectNodes("/assemblies/assembly");

            foreach (XmlNode assemblyNode in assemblyNodes)
            {
                var assemblyRunStartTimeStamp = DateTime.MinValue;
                if (assemblyNode.Attributes["run-date"] != null && assemblyNode.Attributes["run-time"] != null)
                {
                    string runDate = assemblyNode.Attributes["run-date"].Value;
                    string runTime = assemblyNode.Attributes["run-time"].Value;

                    var startDate = DateTime.Now;
                    var startTime = TimeSpan.Zero;
                    if (DateTime.TryParse(runDate, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out startDate) &&
                        TimeSpan.TryParse(runTime, CultureInfo.InvariantCulture, out startTime))
                    {
                        dateTimeParseError = false;
                    }
                    assemblyRunStartTimeStamp = startDate + startTime;
                    if (minStartTime > assemblyRunStartTimeStamp)
                    {
                        minStartTime = assemblyRunStartTimeStamp;
                    }
                }
                else
                {
                    assemblyRunDateTimeAttributesNotPresent = true;
                }
                if (!assemblyTimeAttributeNotPresent && assemblyNode.Attributes["time"] != null)
                {
                    double assemblyDuration = 0;
                    Double.TryParse(assemblyNode.Attributes["time"].Value, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out assemblyDuration);
                    assemblyRunDuration = assemblyRunDuration + assemblyDuration;
                    var durationFromSeconds = TimeSpan.FromSeconds(assemblyDuration);

                    // no assemblystarttime available so dont calculate assemblycompletedtime
                    if (assemblyRunStartTimeStamp != DateTime.MinValue)
                    {
                        DateTime assemblyRunCompleteTimeStamp =
                            assemblyRunStartTimeStamp.AddTicks(durationFromSeconds.Ticks);

                        //finding maximum comleted time
                        if (maxCompletedTime < assemblyRunCompleteTimeStamp)
                        {
                            maxCompletedTime = assemblyRunCompleteTimeStamp;
                        }
                    }
                }
                else
                {
                    assemblyTimeAttributeNotPresent = true;
                }
                XmlNodeList testCaseNodeList = assemblyNode.SelectNodes("./collection/test");
                foreach (XmlNode testCaseNode in testCaseNodeList)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData()
                    {
                        Priority = TestManagementConstants.UnspecifiedPriority,  //Priority is int type so if no priority set then its 255.
                    };

                    //Test storage.
                    if (assemblyNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestStorage = Path.GetFileName(assemblyNode.Attributes["name"].Value);
                    }

                    //Fully Qualified Name.
                    if (testCaseNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    //Test Method Name.
                    if (testCaseNode.Attributes["method"] != null)
                    {
                        resultCreateModel.TestCaseTitle = testCaseNode.Attributes["method"].Value;
                    }

                    //Test duration.
                    if (testCaseNode.Attributes["time"] != null && testCaseNode.Attributes["time"].Value != null)
                    {
                        double duration = 0;
                        double.TryParse(testCaseNode.Attributes["time"].Value, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out duration);
                        var durationFromSeconds = TimeSpan.FromSeconds(duration);
                        resultCreateModel.DurationInMs = durationFromSeconds.TotalMilliseconds;

                        // no assemblystarttime available so dont set testcase start and completed
                        if (assemblyRunStartTimeStamp != DateTime.MinValue)
                        {
                            resultCreateModel.StartedDate   = assemblyRunStartTimeStamp;
                            resultCreateModel.CompletedDate = assemblyRunStartTimeStamp.AddTicks(durationFromSeconds.Ticks);
                            assemblyRunStartTimeStamp       = assemblyRunStartTimeStamp.AddTicks(1) + durationFromSeconds;
                            //next start time
                        }

                        //Calculate overall run duration.
                        testRunDuration += duration;
                    }


                    //Test outcome.
                    if (testCaseNode.SelectSingleNode("./failure") != null)
                    {
                        resultCreateModel.Outcome = TestOutcome.Failed.ToString();

                        //Error message.
                        XmlNode failureMessageNode = testCaseNode.SelectSingleNode("./failure/message");
                        if (failureMessageNode != null && !string.IsNullOrWhiteSpace(failureMessageNode.InnerText))
                        {
                            resultCreateModel.ErrorMessage = failureMessageNode.InnerText;
                        }

                        //Stack trace.
                        XmlNode failureStackTraceNode = testCaseNode.SelectSingleNode("./failure/stack-trace");
                        if (failureStackTraceNode != null && !string.IsNullOrWhiteSpace(failureStackTraceNode.InnerText))
                        {
                            resultCreateModel.StackTrace = failureStackTraceNode.InnerText;
                        }

                        // Console log
                        resultCreateModel.AttachmentData = new AttachmentData();
                        XmlNode consoleLog = testCaseNode.SelectSingleNode("./output");
                        if (consoleLog != null && !string.IsNullOrWhiteSpace(consoleLog.InnerText))
                        {
                            resultCreateModel.AttachmentData.ConsoleLog = consoleLog.InnerText;
                        }
                    }
                    else if (testCaseNode.Attributes["result"] != null && string.Equals(testCaseNode.Attributes["result"].Value, "pass", StringComparison.OrdinalIgnoreCase))
                    {
                        resultCreateModel.Outcome = TestOutcome.Passed.ToString();
                    }
                    else
                    {
                        resultCreateModel.Outcome = TestOutcome.NotExecuted.ToString();
                    }

                    //Test priority.
                    XmlNode priorityTrait = testCaseNode.SelectSingleNode("./traits/trait[@name='priority']");
                    if (priorityTrait != null && priorityTrait.Attributes["value"] != null)
                    {
                        var priorityValue = priorityTrait.Attributes["value"].Value;
                        resultCreateModel.Priority = !string.IsNullOrEmpty(priorityValue) ? Convert.ToInt32(priorityValue)
                                                        : TestManagementConstants.UnspecifiedPriority;
                    }

                    //Test owner.
                    XmlNode ownerNode = testCaseNode.SelectSingleNode("./traits/trait[@name='owner']");
                    if (ownerNode != null && ownerNode.Attributes["value"] != null && ownerNode.Attributes["value"].Value != null)
                    {
                        IdentityRef ownerIdRef = new IdentityRef();
                        ownerIdRef.DisplayName    = ownerNode.Attributes["value"].Value;
                        ownerIdRef.DirectoryAlias = ownerNode.Attributes["value"].Value;
                        resultCreateModel.Owner   = ownerIdRef;
                    }

                    resultCreateModel.RunBy = runUserIdRef;

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        results.Add(resultCreateModel);
                    }
                }
            }
            if (dateTimeParseError || assemblyRunDateTimeAttributesNotPresent)
            {
                executionContext.Warning("Atleast for one assembly start time was not obtained due to tag not present or parsing issue, total run duration will now be summation of time taken by each assembly");

                if (assemblyTimeAttributeNotPresent)
                {
                    executionContext.Warning("Atleast for one assembly time tag is not present, total run duration will now be summation of time from all test runs");
                }
            }

            //if minimum start time is not available then set it to present time
            minStartTime = minStartTime == DateTime.MaxValue ? DateTime.UtcNow : minStartTime;

            //if start time cannot be obtained even for one assembly then fallback duration to sum of assembly run time
            //if assembly run time cannot be obtained even for one assembly then fallback duration to total test run
            maxCompletedTime = dateTimeParseError || assemblyRunDateTimeAttributesNotPresent || maxCompletedTime == DateTime.MinValue ? minStartTime.Add(assemblyTimeAttributeNotPresent ? TimeSpan.FromSeconds(testRunDuration) : TimeSpan.FromSeconds(assemblyRunDuration)) : maxCompletedTime;

            executionContext.Output(string.Format("Obtained XUnit Test Run Start Date: {0} and Completed Date: {1}", minStartTime.ToString("o"), maxCompletedTime.ToString("o")));
            TestRunData testRunData = new TestRunData(
                name: runName,
                buildId: runContext != null ? runContext.BuildId : 0,
                startedDate: minStartTime != DateTime.MinValue ? minStartTime.ToString("o") : null,
                completedDate: maxCompletedTime != DateTime.MinValue ? maxCompletedTime.ToString("o") : null,
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildFlavor: runContext != null ? runContext.Configuration : null,
                buildPlatform: runContext != null ? runContext.Platform : null,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results     = results.ToArray();
            testRunData.Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0];

            return(testRunData);
        }
Example #16
0
        private List<TestCaseResultData> FindTestCaseNodes(XmlNode startNode, string hostName, IdentityRef runUserIdRef, DateTime assemblyStartTime, string assemblyName = null)
        {
            List<TestCaseResultData> results = new List<TestCaseResultData>();

            string testStorage = assemblyName;
            if (startNode.Attributes["type"] != null && startNode.Attributes["type"].Value != null && startNode.Attributes["type"].Value.Equals("assembly", StringComparison.OrdinalIgnoreCase))
            {
                if (startNode.Attributes["name"] != null && startNode.Attributes["name"].Value != null)
                {
                    testStorage = startNode.Attributes["name"].Value;
                }
            }

            //get each test case result information
            XmlNodeList testCaseNodes = startNode.SelectNodes("results/test-case"); //all test-case nodes under testAssemblyNode
            if (testCaseNodes != null)
            {
                DateTime testCaseStartTime = assemblyStartTime;
                foreach (XmlNode testCaseNode in testCaseNodes)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData();

                    //test case name and type
                    if (testCaseNode.Attributes["name"] != null && testCaseNode.Attributes["name"].Value != null)
                    {
                        resultCreateModel.TestCaseTitle = testCaseNode.Attributes["name"].Value;
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    if (!string.IsNullOrEmpty(testStorage))
                    {
                        resultCreateModel.AutomatedTestStorage = testStorage;
                    }

                    //test case duration, starttime and endtime
                    TimeSpan testCaseDuration = TimeSpan.Zero;
                    if (testCaseNode.Attributes["time"] != null && testCaseNode.Attributes["time"].Value != null)
                    {
                        double duration = 0;
                        double.TryParse(testCaseNode.Attributes["time"].Value, out duration);
                        testCaseDuration = TimeSpan.FromSeconds(duration);
                    }
                    resultCreateModel.DurationInMs = testCaseDuration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);

                    if (assemblyStartTime != DateTime.MinValue)
                    {
                        resultCreateModel.StartedDate = testCaseStartTime.ToString("o");
                        resultCreateModel.CompletedDate =
                            testCaseStartTime.AddTicks(testCaseDuration.Ticks).ToString("o");
                        testCaseStartTime = testCaseStartTime.AddTicks(1) + testCaseDuration; //next start time
                    }

                    //test run outcome
                    if (testCaseNode.SelectSingleNode("./failure") != null)
                    {
                        resultCreateModel.Outcome = "Failed";

                        XmlNode failureMessageNode, failureStackTraceNode;

                        if ((failureMessageNode = testCaseNode.SelectSingleNode("./failure/message")) != null && !string.IsNullOrWhiteSpace(failureMessageNode.InnerText))
                        {
                            resultCreateModel.ErrorMessage = failureMessageNode.InnerText;
                        }
                        // stack trace
                        if ((failureStackTraceNode = testCaseNode.SelectSingleNode("./failure/stack-trace")) != null && !string.IsNullOrWhiteSpace(failureStackTraceNode.InnerText))
                        {
                            resultCreateModel.StackTrace = failureStackTraceNode.InnerText;
                        }
                    }
                    else
                    {
                        resultCreateModel.Outcome = "Passed";
                    }

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    //other properties
                    if (runUserIdRef != null)
                    {
                        resultCreateModel.RunBy = runUserIdRef;
                        resultCreateModel.Owner = runUserIdRef;
                    }

                    resultCreateModel.ComputerName = hostName;

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        results.Add(resultCreateModel);
                    }

                }
            }

            XmlNodeList testSuiteNodes = startNode.SelectNodes("results/test-suite");
            if (testSuiteNodes != null)
            {
                foreach (XmlNode testSuiteNode in testSuiteNodes)
                {
                    results.AddRange(FindTestCaseNodes(testSuiteNode, hostName, runUserIdRef, assemblyStartTime, testStorage));
                }
            }

            return results;
        }
Example #17
0
        /// <summary>
        /// Reads a JUnit results file from disk, converts it into a TestRunData object.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="runContext"></param>
        /// <returns></returns>
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            // http://windyroad.com.au/dl/Open%20Source/JUnit.xsd

            XmlDocument doc = new XmlDocument();

            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Ignore
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return(null);
            }

            //init test run summary information - run name, host name, start time
            TestSuiteSummary runSummary = new TestSuiteSummary(Name);

            IdentityRef runUserIdRef = null;
            string      runUser      = runContext != null ? runContext.Owner : string.Empty;

            if (!string.IsNullOrEmpty(runUser))
            {
                runUserIdRef = new IdentityRef()
                {
                    DisplayName = runUser
                };
            }

            var presentTime = DateTime.UtcNow;

            runSummary.TimeStamp = DateTime.MaxValue;
            var maxCompletedTime = DateTime.MinValue;

            //read data from testsuite nodes

            XmlNode testSuitesNode = doc.SelectSingleNode("testsuites");

            if (testSuitesNode != null)
            {
                //found testsuites node - some plugins generate it like karma junit plugin
                XmlNodeList testSuiteNodeList = doc.SelectNodes("/testsuites/testsuite");
                if (testSuiteNodeList != null)
                {
                    foreach (XmlNode testSuiteNode in testSuiteNodeList)
                    {
                        //for each available suites get all suite details
                        TestSuiteSummary testSuiteSummary = ReadTestSuite(testSuiteNode, runUserIdRef);

                        // sum up testsuite durations and test case durations, decision on what to use will be taken later
                        runSummary.TotalTestCaseDuration   = runSummary.TotalTestCaseDuration.Add(testSuiteSummary.TotalTestCaseDuration);
                        runSummary.TestSuiteDuration       = runSummary.TestSuiteDuration.Add(testSuiteSummary.TestSuiteDuration);
                        runSummary.SuiteTimeDataAvailable  = runSummary.SuiteTimeDataAvailable && testSuiteSummary.SuiteTimeDataAvailable;
                        runSummary.SuiteTimeStampAvailable = runSummary.SuiteTimeStampAvailable && testSuiteSummary.SuiteTimeStampAvailable;
                        runSummary.Host = testSuiteSummary.Host;
                        runSummary.Name = testSuiteSummary.Name;
                        //stop calculating timestamp information, if timestamp data is not avilable for even one test suite
                        if (testSuiteSummary.SuiteTimeStampAvailable)
                        {
                            runSummary.TimeStamp = runSummary.TimeStamp > testSuiteSummary.TimeStamp ? testSuiteSummary.TimeStamp : runSummary.TimeStamp;
                            DateTime completedTime = testSuiteSummary.TimeStamp.AddTicks(testSuiteSummary.TestSuiteDuration.Ticks);
                            maxCompletedTime = maxCompletedTime < completedTime ? completedTime : maxCompletedTime;
                        }
                        runSummary.Results.AddRange(testSuiteSummary.Results);
                    }

                    if (testSuiteNodeList.Count > 1)
                    {
                        runSummary.Name = Name + "_" + Path.GetFileName(filePath);
                    }
                }
            }
            else
            {
                XmlNode testSuiteNode = doc.SelectSingleNode("testsuite");
                if (testSuiteNode != null)
                {
                    runSummary = ReadTestSuite(testSuiteNode, runUserIdRef);
                    //only if start time is available then only we need to calculate completed time
                    if (runSummary.TimeStamp != DateTime.MaxValue)
                    {
                        DateTime completedTime = runSummary.TimeStamp.AddTicks(runSummary.TestSuiteDuration.Ticks);
                        maxCompletedTime = maxCompletedTime < completedTime ? completedTime : maxCompletedTime;
                    }
                    else
                    {
                        runSummary.SuiteTimeStampAvailable = false;
                    }
                }
            }

            if (runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName))
            {
                runSummary.Name = runContext.RunName;
            }

            if (!runSummary.SuiteTimeStampAvailable)
            {
                executionContext.Output("Timestamp is not available for one or more testsuites. Total run duration is being calculated as the sum of time durations of detected testsuites");

                if (!runSummary.SuiteTimeDataAvailable)
                {
                    executionContext.Output("Time is not available for one or more testsuites. Total run duration is being calculated as the sum of time durations of detected testcases");
                }
            }
            //if start time is not calculated then it should be initialized as present time
            runSummary.TimeStamp = runSummary.TimeStamp == DateTime.MaxValue
                ? presentTime
                : runSummary.TimeStamp;
            //if suite timestamp data is not available even for single testsuite, then fallback to testsuite run time
            //if testsuite run time is not available even for single testsuite, then fallback to total test case duration
            maxCompletedTime = !runSummary.SuiteTimeStampAvailable || maxCompletedTime == DateTime.MinValue
                ? runSummary.TimeStamp.Add(runSummary.SuiteTimeDataAvailable ? runSummary.TestSuiteDuration
                : runSummary.TotalTestCaseDuration) : maxCompletedTime;
            //create test run data
            var testRunData = new TestRunData(
                name: runSummary.Name,
                startedDate: runSummary.TimeStamp != DateTime.MinValue ? runSummary.TimeStamp.ToString("o") : null,
                completedDate: maxCompletedTime != DateTime.MinValue ? maxCompletedTime.ToString("o") : null,
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: runContext != null ? runContext.Configuration : string.Empty,
                buildPlatform: runContext != null ? runContext.Platform : string.Empty,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                )
            {
                Results     = runSummary.Results.ToArray(),
                Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0]
            };

            return(testRunData);
        }
Example #18
0
        /// <summary>
        /// Reads a JUnit results file from disk, converts it into a TestRunData object.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="runContext"></param>
        /// <returns></returns>
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            // http://windyroad.com.au/dl/Open%20Source/JUnit.xsd

            XmlDocument doc = new XmlDocument();

            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Ignore
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return(null);
            }

            //init test run summary information - run name, host name, start time
            TestSuiteSummary runSummary = new TestSuiteSummary(Name);

            IdentityRef runUserIdRef = null;
            string      runUser      = runContext != null ? runContext.Owner : string.Empty;

            if (!string.IsNullOrEmpty(runUser))
            {
                runUserIdRef = new IdentityRef()
                {
                    DisplayName = runUser
                };
            }

            //read data from testsuite nodes

            XmlNode testSuitesNode = doc.SelectSingleNode("testsuites");

            if (testSuitesNode != null)
            {
                //found testsuites node - some plugins generate it like karma junit plugin
                XmlNodeList testSuiteNodeList = doc.SelectNodes("/testsuites/testsuite");
                if (testSuiteNodeList != null)
                {
                    foreach (XmlNode testSuiteNode in testSuiteNodeList)
                    {
                        TestSuiteSummary testSuiteSummary = ReadTestSuite(testSuiteNode, runUserIdRef);
                        runSummary.Duration = runSummary.Duration.Add(testSuiteSummary.Duration);
                        runSummary.Results.AddRange(testSuiteSummary.Results);
                        runSummary.Host = testSuiteSummary.Host;
                        runSummary.Name = testSuiteSummary.Name;
                    }

                    if (testSuiteNodeList.Count > 1)
                    {
                        runSummary.Name = Name + "_" + Path.GetFileName(filePath);
                    }
                }
            }
            else
            {
                XmlNode testSuiteNode = doc.SelectSingleNode("testsuite");
                if (testSuiteNode != null)
                {
                    runSummary = ReadTestSuite(testSuiteNode, runUserIdRef);
                }
            }

            if (runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName))
            {
                runSummary.Name = runContext.RunName;
            }

            if (runSummary.Results.Count > 0)
            {
                //first testsuite starteddate is the starteddate of the run
                runSummary.TimeStamp = runSummary.Results[0].StartedDate;
            }

            //create test run data
            var testRunData = new TestRunData(
                name: runSummary.Name,
                startedDate: runSummary.TimeStamp.ToString("o"),
                completedDate: runSummary.TimeStamp.Add(runSummary.Duration).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: runContext != null ? runContext.Configuration : string.Empty,
                buildPlatform: runContext != null ? runContext.Platform : string.Empty,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                )
            {
                Results     = runSummary.Results.ToArray(),
                Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0]
            };

            return(testRunData);
        }
Example #19
0
        public TestRunData GetTestRunData(string filePath, XmlDocument doc, XmlNode testResultsNode, TestRunContext runContext, bool addResultsAsAttachments)
        {
            var testRunNode      = doc.SelectSingleNode("test-run");
            var testRunStartedOn = DateTime.MinValue;
            var testRunEndedOn   = DateTime.MinValue;
            var testCaseResults  = new List <TestCaseResultData>();

            _runUserIdRef = runContext != null ? new IdentityRef()
            {
                DisplayName = runContext.Owner
            } : null;
            _platform = runContext != null ? runContext.Platform : string.Empty;
            if (testRunNode.Attributes["start-time"] != null)
            {
                DateTime.TryParse(testRunNode.Attributes["start-time"]?.Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out testRunStartedOn);
                DateTime.TryParse(testRunNode.Attributes["end-time"]?.Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out testRunEndedOn);
                var testAssemblyNodes = testRunNode.SelectNodes("//test-suite[@type='Assembly']");
                if (testAssemblyNodes != null)
                {
                    foreach (XmlNode testAssemblyNode in testAssemblyNodes)
                    {
                        var environmentNode = testAssemblyNode.SelectSingleNode("environment");
                        var hostname        = String.Empty;
                        var assemblyName    = (testAssemblyNode.Attributes["name"] != null ? testAssemblyNode.Attributes["name"].Value : null);
                        if (environmentNode != null)
                        {
                            if (environmentNode.Attributes["machine-name"] != null)
                            {
                                hostname = environmentNode.Attributes["machine-name"].Value;
                            }
                            if (environmentNode.Attributes["platform"] != null)
                            {
                                // override platform
                                _platform = environmentNode.Attributes["platform"].Value;
                            }
                        }
                        var testCaseNodes = testAssemblyNode.SelectNodes(".//test-case");
                        if (testCaseNodes != null)
                        {
                            foreach (XmlNode testCaseNode in testCaseNodes)
                            {
                                testCaseResults.Add(getTestCaseResultData(testCaseNode, assemblyName, hostname));
                            }
                        }
                    }
                }
            }
            TestRunData testRunData = new TestRunData(
                name: runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName) ? runContext.RunName : _defaultRunName,
                startedDate: (testRunStartedOn == DateTime.MinValue ? string.Empty : testRunStartedOn.ToString("o")),
                completedDate: (testRunEndedOn == DateTime.MinValue ? string.Empty : testRunEndedOn.ToString("o")),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: runContext != null ? runContext.Configuration : string.Empty,
                buildPlatform: _platform,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results     = testCaseResults.ToArray();
            testRunData.Attachments = addResultsAsAttachments ? new string[] { filePath } : new string[0];
            return(testRunData);
        }
Example #20
0
        private List <TestCaseResultData> FindTestCaseNodes(XmlNode startNode, string hostName, IdentityRef runUserIdRef, DateTime assemblyStartTime, string assemblyName = null)
        {
            List <TestCaseResultData> results = new List <TestCaseResultData>();

            string testStorage = assemblyName;

            if (startNode.Attributes["type"] != null && startNode.Attributes["type"].Value != null && startNode.Attributes["type"].Value.Equals("assembly", StringComparison.OrdinalIgnoreCase))
            {
                if (startNode.Attributes["name"] != null && startNode.Attributes["name"].Value != null)
                {
                    testStorage = startNode.Attributes["name"].Value;
                }
            }

            //get each test case result information
            XmlNodeList testCaseNodes = startNode.SelectNodes(TestCaseNodeName); //all test-case nodes under testAssemblyNode

            if (testCaseNodes != null)
            {
                DateTime testCaseStartTime = assemblyStartTime;
                foreach (XmlNode testCaseNode in testCaseNodes)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData();

                    //test case name and type
                    if (testCaseNode.Attributes["name"] != null && testCaseNode.Attributes["name"].Value != null)
                    {
                        resultCreateModel.TestCaseTitle     = testCaseNode.Attributes["name"].Value;
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    if (!string.IsNullOrEmpty(testStorage))
                    {
                        resultCreateModel.AutomatedTestStorage = testStorage;
                    }

                    //test case duration, starttime and endtime
                    TimeSpan testCaseDuration = TimeSpan.Zero;
                    if (testCaseNode.Attributes["time"] != null && testCaseNode.Attributes["time"].Value != null)
                    {
                        double duration = 0;
                        double.TryParse(testCaseNode.Attributes["time"].Value, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out duration);
                        testCaseDuration = TimeSpan.FromSeconds(duration);
                    }
                    resultCreateModel.DurationInMs = testCaseDuration.TotalMilliseconds;

                    if (assemblyStartTime != DateTime.MinValue)
                    {
                        resultCreateModel.StartedDate   = testCaseStartTime;
                        resultCreateModel.CompletedDate = testCaseStartTime.AddTicks(testCaseDuration.Ticks);
                        testCaseStartTime = testCaseStartTime.AddTicks(1) + testCaseDuration; //next start time
                    }

                    //test run outcome
                    if (testCaseNode.SelectSingleNode("./failure") != null)
                    {
                        resultCreateModel.Outcome = "Failed";

                        XmlNode failureMessageNode, failureStackTraceNode;

                        if ((failureMessageNode = testCaseNode.SelectSingleNode("./failure/message")) != null && !string.IsNullOrWhiteSpace(failureMessageNode.InnerText))
                        {
                            resultCreateModel.ErrorMessage = failureMessageNode.InnerText;
                        }
                        // stack trace
                        if ((failureStackTraceNode = testCaseNode.SelectSingleNode("./failure/stack-trace")) != null && !string.IsNullOrWhiteSpace(failureStackTraceNode.InnerText))
                        {
                            resultCreateModel.StackTrace = failureStackTraceNode.InnerText;
                        }
                    }
                    else
                    {
                        if (testCaseNode.Attributes["result"] != null && string.Equals(testCaseNode.Attributes["result"].Value, "Ignored", StringComparison.OrdinalIgnoreCase))
                        {
                            resultCreateModel.Outcome = "NotExecuted";
                        }
                        else
                        {
                            resultCreateModel.Outcome = "Passed";
                        }
                    }

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = "NUnit";

                    //other properties
                    if (runUserIdRef != null)
                    {
                        resultCreateModel.RunBy = runUserIdRef;
                        resultCreateModel.Owner = runUserIdRef;
                    }

                    resultCreateModel.ComputerName = hostName;

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        results.Add(resultCreateModel);
                    }
                }
            }

            XmlNodeList testSuiteNodes = startNode.SelectNodes(InnerTestSuiteNodeName);

            if (testSuiteNodes != null)
            {
                foreach (XmlNode testSuiteNode in testSuiteNodes)
                {
                    results.AddRange(FindTestCaseNodes(testSuiteNode, hostName, runUserIdRef, assemblyStartTime, testStorage));
                }
            }

            return(results);
        }
        private async Task EnsureScheduledBuildFailSubscriptionExists(BuildDefinition pipeline, WebApiTeam team, bool persistChanges)
        {
            const string BuildFailureNotificationTag = "#AutomaticBuildFailureNotification";
            var          subscriptions = await service.GetSubscriptionsAsync(team.Id);

            var subscription = subscriptions.FirstOrDefault(sub => sub.Description.Contains(BuildFailureNotificationTag));

            logger.LogInformation("Team Is Subscribed TeamName = {0} PipelineId = {1}", team.Name, pipeline.Id);

            string definitionName = $"\\{pipeline.Project.Name}\\{pipeline.Name}";

            if (subscription == default)
            {
                var filterModel = new ExpressionFilterModel
                {
                    Clauses = new ExpressionFilterClause[]
                    {
                        new ExpressionFilterClause {
                            Index = 1, LogicalOperator = "", FieldName = "Status", Operator = "=", Value = "Failed"
                        },
                        new ExpressionFilterClause {
                            Index = 2, LogicalOperator = "And", FieldName = "Definition name", Operator = "=", Value = definitionName
                        },
                        new ExpressionFilterClause {
                            Index = 3, LogicalOperator = "And", FieldName = "Build reason", Operator = "=", Value = "Scheduled"
                        }
                    }
                };
                var filter = new ExpressionFilter("ms.vss-build.build-completed-event", filterModel);

                var identity = new IdentityRef
                {
                    Id  = team.Id.ToString(),
                    Url = team.IdentityUrl
                };

                var newSubscription = new NotificationSubscriptionCreateParameters
                {
                    Channel = new UserSubscriptionChannel {
                        UseCustomAddress = false
                    },
                    Description = $"A build fails {BuildFailureNotificationTag}",
                    Filter      = filter,
                    Scope       = new SubscriptionScope {
                        Type = "none", Id = pipeline.Project.Id
                    },
                    Subscriber = identity,
                };

                logger.LogInformation("Creating Subscription PipelineId = {0}, TeamId = {1}", pipeline.Id, team.Id);
                if (persistChanges)
                {
                    subscription = await service.CreateSubscriptionAsync(newSubscription);
                }
            }
            else
            {
                var filter = subscription.Filter as ExpressionFilter;
                if (filter == null)
                {
                    logger.LogWarning("Subscription expression is not correct for of team {0}", team.Name);
                    return;
                }

                var definitionClause = filter.FilterModel.Clauses.FirstOrDefault(c => c.FieldName == "Definition name");

                if (definitionClause == null)
                {
                    logger.LogWarning("Subscription doesn't have correct expression filters for of team {0}", team.Name);
                    return;
                }

                if (definitionClause.Value != definitionName)
                {
                    definitionClause.Value = definitionName;

                    if (persistChanges)
                    {
                        var updateParameters = new NotificationSubscriptionUpdateParameters()
                        {
                            Channel     = subscription.Channel,
                            Description = subscription.Description,
                            Filter      = subscription.Filter,
                            Scope       = subscription.Scope,
                        };
                        logger.LogInformation("Updating Subscription expression for team {0} with correct definition name {1}", team.Name, definitionName);
                        subscription = await service.UpdatedSubscriptionAsync(updateParameters, subscription.Id.ToString());
                    }
                }
            }
        }
Example #22
0
        //Based on the XUnit V2 format: http://xunit.github.io/docs/format-xml-v2.html
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            List<TestCaseResultData> results = new List<TestCaseResultData>();

            XmlDocument doc = new XmlDocument();
            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Prohibit
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return null;
            }

            string runName = Name + " Test Run";
            string runUser = "";
            if (runContext != null)
            {
                if (!string.IsNullOrWhiteSpace(runContext.RunName))
                {
                    runName = runContext.RunName;
                }
                else
                {
                    runName = string.Format(CultureInfo.CurrentCulture, "{0} {1} {2}", runName, runContext.Configuration, runContext.Platform);
                }

                if (runContext.Owner != null)
                {
                    runUser = runContext.Owner;
                }
            }

            IdentityRef runUserIdRef = new IdentityRef();
            runUserIdRef.DisplayName = runUser;

            var runStartTime = DateTime.MinValue;
            double testRunDuration = 0;

            XmlNodeList assemblyNodes = doc.SelectNodes("/assemblies/assembly");
            foreach (XmlNode assemblyNode in assemblyNodes)
            {
                var assemblyRunStartTimeStamp = DateTime.MinValue;
                if (assemblyNode.Attributes["run-date"] != null && assemblyNode.Attributes["run-time"] != null)
                {
                    string runDate = assemblyNode.Attributes["run-date"].Value;
                    string runTime = assemblyNode.Attributes["run-time"].Value;

                    var startDate = DateTime.Now;
                    var startTime = TimeSpan.Zero;
                    if (DateTime.TryParse(runDate, out startDate))
                    {
                        TimeSpan.TryParse(runTime, out startTime);
                    }
                    assemblyRunStartTimeStamp = startDate + startTime;
                    if (testRunDuration == 0)
                    {
                        // first assembly start time is runstart time
                        runStartTime = assemblyRunStartTimeStamp;
                    }
                }

                XmlNodeList testCaseNodeList = assemblyNode.SelectNodes("./collection/test");
                foreach (XmlNode testCaseNode in testCaseNodeList)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData();

                    //Test storage.
                    if (assemblyNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestStorage = Path.GetFileName(assemblyNode.Attributes["name"].Value);
                    }

                    //Fully Qualified Name.
                    if (testCaseNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    //Test Method Name.
                    if (testCaseNode.Attributes["method"] != null)
                    {
                        resultCreateModel.TestCaseTitle = testCaseNode.Attributes["method"].Value;
                    }

                    //Test duration.
                    if (testCaseNode.Attributes["time"] != null && testCaseNode.Attributes["time"].Value != null)
                    {
                        double duration = 0;
                        double.TryParse(testCaseNode.Attributes["time"].Value, out duration);
                        var durationFromSeconds = TimeSpan.FromSeconds(duration);
                        resultCreateModel.DurationInMs = durationFromSeconds.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);

                        // no assemblystarttime available so dont set testcase start and completed
                        if (assemblyRunStartTimeStamp != DateTime.MinValue)
                        {
                            resultCreateModel.StartedDate = assemblyRunStartTimeStamp.ToString("o");
                            resultCreateModel.CompletedDate =
                                assemblyRunStartTimeStamp.AddTicks(durationFromSeconds.Ticks).ToString("o");
                            assemblyRunStartTimeStamp = assemblyRunStartTimeStamp.AddTicks(1) + durationFromSeconds;
                            //next start time
                        }

                        //Calculate overall run duration.
                        testRunDuration += duration;
                    }


                    //Test outcome.
                    if (testCaseNode.SelectSingleNode("./failure") != null)
                    {
                        resultCreateModel.Outcome = TestOutcome.Failed.ToString();

                        //Error message.
                        XmlNode failureMessageNode = testCaseNode.SelectSingleNode("./failure/message");
                        if (failureMessageNode != null && !string.IsNullOrWhiteSpace(failureMessageNode.InnerText))
                        {
                            resultCreateModel.ErrorMessage = failureMessageNode.InnerText;
                        }

                        //Stack trace.
                        XmlNode failureStackTraceNode = testCaseNode.SelectSingleNode("./failure/stack-trace");
                        if (failureStackTraceNode != null && !string.IsNullOrWhiteSpace(failureStackTraceNode.InnerText))
                        {
                            resultCreateModel.StackTrace = failureStackTraceNode.InnerText;
                        }
                    }
                    else if (testCaseNode.Attributes["result"] != null && string.Equals(testCaseNode.Attributes["result"].Value, "pass", StringComparison.OrdinalIgnoreCase))
                    {
                        resultCreateModel.Outcome = TestOutcome.Passed.ToString();
                    }
                    else
                    {
                        resultCreateModel.Outcome = TestOutcome.NotExecuted.ToString();
                    }

                    //Test priority.
                    XmlNode priorityTrait = testCaseNode.SelectSingleNode("./traits/trait[@name='priority']");
                    if (priorityTrait != null && priorityTrait.Attributes["value"] != null)
                    {
                        resultCreateModel.TestCasePriority = priorityTrait.Attributes["value"].Value;
                    }

                    //Test owner.
                    XmlNode ownerNode = testCaseNode.SelectSingleNode("./traits/trait[@name='owner']");
                    if (ownerNode != null && ownerNode.Attributes["value"] != null && ownerNode.Attributes["value"].Value != null)
                    {
                        IdentityRef ownerIdRef = new IdentityRef();
                        ownerIdRef.DisplayName = ownerNode.Attributes["value"].Value;
                        resultCreateModel.Owner = ownerIdRef;
                    }

                    resultCreateModel.RunBy = runUserIdRef;

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        results.Add(resultCreateModel);
                    }
                }
            }

            TestRunData testRunData = new TestRunData(
                name: runName,
                buildId: runContext != null ? runContext.BuildId : 0,
                startedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.ToString("o"),
                completedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.Add(TimeSpan.FromSeconds(testRunDuration)).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildFlavor: runContext != null ? runContext.Configuration : null,
                buildPlatform: runContext != null ? runContext.Platform : null,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results = results.ToArray();
            testRunData.Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0];

            return testRunData;
        }
Example #23
0
        /// <summary>
        /// Read testcases under testsuite node in xml
        /// </summary>
        /// <param name="rootNode"></param>
        private TestSuiteSummary ReadTestSuite(XmlNode rootNode, IdentityRef runUserIdRef)
        {
            TestSuiteSummary testSuiteSummary       = new TestSuiteSummary(Name);
            TimeSpan         totalTestSuiteDuration = TimeSpan.Zero;
            TimeSpan         totalTestCaseDuration  = TimeSpan.Zero;

            if (rootNode.Attributes["name"] != null && rootNode.Attributes["name"].Value != null)
            {
                testSuiteSummary.Name = rootNode.Attributes["name"].Value;
            }

            if (rootNode.Attributes["hostname"] != null && rootNode.Attributes["hostname"].Value != null)
            {
                testSuiteSummary.Host = rootNode.Attributes["hostname"].Value;
            }

            //assume runtimes from xml are current local time since timezone information is not in the xml, if xml datetime > current local time, fallback to local start time
            DateTime     timestampFromXml = DateTime.MinValue;
            XmlAttribute timestampNode    = rootNode.Attributes["timestamp"];

            if (timestampNode != null && timestampNode.Value != null)
            {
                if (DateTime.TryParse(timestampNode.Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out timestampFromXml))
                {
                    testSuiteSummary.TimeStamp = timestampFromXml;
                }
            }

            if (timestampFromXml == DateTime.MinValue)
            {
                testSuiteSummary.SuiteTimeStampAvailable = false;
            }

            bool SuiteTimeDataAvailable = false;

            totalTestSuiteDuration = GetTimeSpan(rootNode, out SuiteTimeDataAvailable);
            testSuiteSummary.SuiteTimeDataAvailable = SuiteTimeDataAvailable;

            var testSuiteStartTime = testSuiteSummary.TimeStamp;

            //find test case nodes in JUnit result xml
            XmlNodeList testCaseNodes = rootNode.SelectNodes("./testcase");

            if (testCaseNodes != null)
            {
                DateTime testCaseStartTime = testSuiteStartTime;

                //Add test case results to the test run
                foreach (XmlNode testCaseNode in testCaseNodes)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData();

                    //test case name and type
                    if (testCaseNode.Attributes["name"] != null && testCaseNode.Attributes["name"].Value != null)
                    {
                        resultCreateModel.TestCaseTitle     = testCaseNode.Attributes["name"].Value;
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    if (testCaseNode.Attributes["classname"] != null && testCaseNode.Attributes["classname"].Value != null)
                    {
                        resultCreateModel.AutomatedTestStorage = testCaseNode.Attributes["classname"].Value;
                    }

                    if (testCaseNode.Attributes["owner"]?.Value != null)
                    {
                        var ownerName = testCaseNode.Attributes["owner"].Value;
                        resultCreateModel.Owner = new IdentityRef {
                            DisplayName = ownerName, DirectoryAlias = ownerName
                        };
                    }

                    //test case duration
                    bool TestCaseTimeDataAvailable = false;
                    var  testCaseDuration          = GetTimeSpan(testCaseNode, out TestCaseTimeDataAvailable);
                    totalTestCaseDuration           = totalTestCaseDuration + testCaseDuration;
                    resultCreateModel.DurationInMs  = testCaseDuration.TotalMilliseconds;
                    resultCreateModel.StartedDate   = testCaseStartTime;
                    resultCreateModel.CompletedDate = testCaseStartTime.AddTicks(testCaseDuration.Ticks);
                    testCaseStartTime = testCaseStartTime.AddTicks(1) + testCaseDuration; //next start time

                    //test case outcome
                    XmlNode failure, error, skipped;

                    if ((failure = testCaseNode.SelectSingleNode("./failure")) != null)
                    {
                        ProcessFailureNode(failure, resultCreateModel);
                        AddSystemLogsToResult(testCaseNode, resultCreateModel);
                    }
                    else if ((error = testCaseNode.SelectSingleNode("./error")) != null)
                    {
                        ProcessFailureNode(error, resultCreateModel);
                        AddSystemLogsToResult(testCaseNode, resultCreateModel);
                    }
                    else if ((skipped = testCaseNode.SelectSingleNode("./skipped")) != null)
                    {
                        resultCreateModel.Outcome = TestOutcome.NotExecuted.ToString();
                        if (skipped.Attributes["message"] != null && !string.IsNullOrWhiteSpace(skipped.Attributes["message"].Value))
                        {
                            resultCreateModel.ErrorMessage = skipped.Attributes["message"].Value;
                        }
                    }
                    else
                    {
                        resultCreateModel.Outcome = TestOutcome.Passed.ToString();
                    }

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    //other properties - host name and user
                    resultCreateModel.ComputerName = testSuiteSummary.Host;

                    if (runUserIdRef != null)
                    {
                        resultCreateModel.RunBy = runUserIdRef;
                    }

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        testSuiteSummary.Results.Add(resultCreateModel);
                    }
                }
            }

            testSuiteSummary.TestSuiteDuration     = totalTestSuiteDuration;
            testSuiteSummary.TotalTestCaseDuration = totalTestCaseDuration;

            return(testSuiteSummary);
        }
Example #24
0
        /// <summary>
        /// Read testcases under testsuite node in xml
        /// </summary>
        /// <param name="rootNode"></param>
        private TestSuiteSummary ReadTestSuite(XmlNode rootNode, IdentityRef runUserIdRef)
        {
            TestSuiteSummary testSuiteSummary = new TestSuiteSummary(Name);
            TimeSpan totalTestSuiteDuration = TimeSpan.Zero;
            TimeSpan totalTestCaseDuration = TimeSpan.Zero;

            if (rootNode.Attributes["name"] != null && rootNode.Attributes["name"].Value != null)
            {
                testSuiteSummary.Name = rootNode.Attributes["name"].Value;
            }

            if (rootNode.Attributes["hostname"] != null && rootNode.Attributes["hostname"].Value != null)
            {
                testSuiteSummary.Host = rootNode.Attributes["hostname"].Value;
            }

            //assume runtimes from xml are current local time since timezone information is not in the xml, if xml datetime > current local time, fallback to local start time
            DateTime timestampFromXml = DateTime.MinValue;
            XmlAttribute timestampNode = rootNode.Attributes["timestamp"];
            if (timestampNode != null && timestampNode.Value != null)
            {
                if (DateTime.TryParse(timestampNode.Value, out timestampFromXml))
                {
                    testSuiteSummary.TimeStamp = timestampFromXml;
                }
            }

            totalTestSuiteDuration = GetTimeSpan(rootNode);

            DateTime testSuiteStartTime = testSuiteSummary.TimeStamp;

            //find test case nodes in JUnit result xml
            XmlNodeList testCaseNodes = rootNode.SelectNodes("./testcase");
            if (testCaseNodes != null)
            {
                DateTime testCaseStartTime = testSuiteStartTime;

                //Add test case results to the test run
                foreach (XmlNode testCaseNode in testCaseNodes)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData();

                    //test case name and type
                    if (testCaseNode.Attributes["name"] != null && testCaseNode.Attributes["name"].Value != null)
                    {
                        resultCreateModel.TestCaseTitle = testCaseNode.Attributes["name"].Value;
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    if (testCaseNode.Attributes["classname"] != null && testCaseNode.Attributes["classname"].Value != null)
                    {
                        resultCreateModel.AutomatedTestStorage = testCaseNode.Attributes["classname"].Value;
                    }

                    //test case duration
                    TimeSpan testCaseDuration = GetTimeSpan(testCaseNode);
                    resultCreateModel.DurationInMs = testCaseDuration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);

                    resultCreateModel.StartedDate = testCaseStartTime.ToString("o");
                    resultCreateModel.CompletedDate = testCaseStartTime.AddTicks(testCaseDuration.Ticks).ToString("o");
                    testCaseStartTime = testCaseStartTime.AddTicks(1) + testCaseDuration; //next start time

                    //test case outcome
                    XmlNode failure, error, skipped;

                    if ((failure = testCaseNode.SelectSingleNode("./failure")) != null)
                    {
                        ProcessFailureNode(failure, resultCreateModel);
                    }
                    else if ((error = testCaseNode.SelectSingleNode("./error")) != null)
                    {
                        ProcessFailureNode(error, resultCreateModel);
                    }
                    else if ((skipped = testCaseNode.SelectSingleNode("./skipped")) != null)
                    {
                        resultCreateModel.Outcome = TestOutcome.NotExecuted.ToString();
                        if (skipped.Attributes["message"] != null && !string.IsNullOrWhiteSpace(skipped.Attributes["message"].Value))
                        {
                            resultCreateModel.ErrorMessage = skipped.Attributes["message"].Value;
                        }
                    }
                    else
                    {
                        resultCreateModel.Outcome = TestOutcome.Passed.ToString();
                    }

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    //other properties - host name and user
                    resultCreateModel.ComputerName = testSuiteSummary.Host;

                    if (runUserIdRef != null)
                    {
                        resultCreateModel.RunBy = runUserIdRef;
                        resultCreateModel.Owner = runUserIdRef;
                    }

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        testSuiteSummary.Results.Add(resultCreateModel);
                    }

                }
            }

            if (TimeSpan.Compare(totalTestSuiteDuration, totalTestCaseDuration) < 0)
            {
                totalTestSuiteDuration = totalTestCaseDuration; //run duration may not be set in the xml, so use total test case duration 
            }
            testSuiteSummary.Duration = totalTestSuiteDuration;

            return testSuiteSummary;
        }
Example #25
0
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            List<TestCaseResultData> results = new List<TestCaseResultData>();

            XmlDocument doc = new XmlDocument();
            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Prohibit
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return null;
            }

            //read test run summary information - run name, start time
            string runName = Name + " Test Run";
            DateTime runStartTime = DateTime.MinValue; //Use local time instead of UTC as TestRunData uses local time for defaults. Also assuming timestamp is local is more accurate in cases where tests were run on build machine
            TimeSpan totalRunDuration = TimeSpan.Zero;
            TimeSpan totalTestCaseDuration = TimeSpan.Zero;

            XmlNode testResultsNode = doc.SelectSingleNode("test-results");
            if (testResultsNode != null)
            {
                //get test run summary information
                if (testResultsNode.Attributes["name"] != null)
                {
                    runName = testResultsNode.Attributes["name"].Value;
                }

                //run times
                DateTime dateFromXml = DateTime.MinValue.Date; //Use local time instead of UTC as TestRunData uses local time for defaults.
                if (testResultsNode.Attributes["date"] != null)
                {
                    DateTime.TryParse(testResultsNode.Attributes["date"].Value, out dateFromXml);
                }

                TimeSpan timeFromXml = TimeSpan.Zero;
                if (testResultsNode.Attributes["time"] != null)
                {
                    TimeSpan.TryParse(testResultsNode.Attributes["time"].Value, out timeFromXml);
                }

                //assume runtimes from xml are current local time since timezone information is not in the xml, if xml datetime > current local time, fallback to local start time
                DateTime runStartDateTimeFromXml = new DateTime(dateFromXml.Ticks).AddTicks(timeFromXml.Ticks);
                if (runStartTime == DateTime.MinValue)
                {
                    runStartTime = runStartDateTimeFromXml;
                }
            }

            //run environment - platform, config and hostname
            string platform = runContext != null ? runContext.Platform : string.Empty;
            string config = runContext != null ? runContext.Configuration : string.Empty;
            string runUser = runContext != null ? runContext.Owner : string.Empty;
            string hostName = string.Empty;

            XmlNode envNode = doc.SelectSingleNode("test-results/environment");
            if (envNode != null)
            {
                if (envNode.Attributes["machine-name"] != null && envNode.Attributes["machine-name"].Value != null)
                {
                    hostName = envNode.Attributes["machine-name"].Value;
                }

                if (envNode.Attributes["platform"] != null && envNode.Attributes["platform"].Value != null && runContext != null && runContext.BuildId > 0)
                {
                    //We cannot publish platform information without a valid build id.
                    platform = envNode.Attributes["platform"].Value;
                }
            }

            //run owner
            IdentityRef runUserIdRef = null;
            if (!string.IsNullOrEmpty(runUser))
            {
                runUserIdRef = new IdentityRef() { DisplayName = runUser };
            }

            //get all test assemblies
            if (testResultsNode != null)
            {
                XmlNodeList testAssemblyNodes = testResultsNode.SelectNodes("test-suite");
                if (testAssemblyNodes != null)
                {
                    foreach (XmlNode testAssemblyNode in testAssemblyNodes)
                    {
                        var assemblyStartTime = (runStartTime == DateTime.MinValue) ? DateTime.MinValue : runStartTime + totalTestCaseDuration;
                        List<TestCaseResultData> testCases = FindTestCaseNodes(testAssemblyNode, hostName, runUserIdRef, assemblyStartTime);
                        if (testCases != null)
                        {
                            results.AddRange(testCases);
                            testCases.ForEach(x => totalTestCaseDuration += TimeSpan.FromMilliseconds(double.Parse(x.DurationInMs)));
                        }
                    }
                }
            }

            if (TimeSpan.Compare(totalRunDuration, totalTestCaseDuration) < 0)
            {
                totalRunDuration = totalTestCaseDuration; //run duration may not be set in the xml, so use total test case duration 
            }

            if (runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName))
            {
                runName = runContext.RunName;
            }

            //create test run data
            TestRunData testRunData = new TestRunData(
                name: runName,
                startedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.ToString("o"),
                completedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.Add(totalRunDuration).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: config,
                buildPlatform: platform,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results = results.ToArray();
            testRunData.Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0];

            return testRunData;
        }
Example #26
0
        public TestRunData GetTestRunData(string filePath, XmlDocument doc, XmlNode testResultsNode, TestRunContext runContext, bool addResultsAsAttachments)
        {
            var results = new List <TestCaseResultData>();

            //read test run summary information - run name, start time
            string   runName               = "NUnit Test Run";
            DateTime runStartTime          = DateTime.MinValue; //Use local time instead of UTC as TestRunData uses local time for defaults. Also assuming timestamp is local is more accurate in cases where tests were run on build machine
            TimeSpan totalRunDuration      = TimeSpan.Zero;
            TimeSpan totalTestCaseDuration = TimeSpan.Zero;

            if (testResultsNode != null)
            {
                //get test run summary information
                if (testResultsNode.Attributes["name"] != null)
                {
                    runName = testResultsNode.Attributes["name"].Value;
                }

                //run times
                DateTime dateFromXml = DateTime.MinValue.Date; //Use local time instead of UTC as TestRunData uses local time for defaults.
                if (testResultsNode.Attributes["date"] != null)
                {
                    DateTime.TryParse(testResultsNode.Attributes["date"].Value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateFromXml);
                }

                TimeSpan timeFromXml = TimeSpan.Zero;
                if (testResultsNode.Attributes["time"] != null)
                {
                    TimeSpan.TryParse(testResultsNode.Attributes["time"].Value, CultureInfo.InvariantCulture, out timeFromXml);
                }

                //assume runtimes from xml are current local time since timezone information is not in the xml, if xml datetime > current local time, fallback to local start time
                DateTime runStartDateTimeFromXml = new DateTime(dateFromXml.Ticks).AddTicks(timeFromXml.Ticks);
                if (runStartTime == DateTime.MinValue)
                {
                    runStartTime = runStartDateTimeFromXml;
                }
            }

            //run environment - platform, config and hostname
            string platform = runContext != null ? runContext.Platform : string.Empty;
            string config   = runContext != null ? runContext.Configuration : string.Empty;
            string runUser  = runContext != null ? runContext.Owner : string.Empty;
            string hostName = string.Empty;

            XmlNode envNode = doc.SelectSingleNode(RootNodeName + "/environment");

            if (envNode != null)
            {
                if (envNode.Attributes["machine-name"] != null && envNode.Attributes["machine-name"].Value != null)
                {
                    hostName = envNode.Attributes["machine-name"].Value;
                }

                if (envNode.Attributes["platform"] != null && envNode.Attributes["platform"].Value != null && runContext != null && runContext.BuildId > 0)
                {
                    //We cannot publish platform information without a valid build id.
                    platform = envNode.Attributes["platform"].Value;
                }
            }

            //run owner
            IdentityRef runUserIdRef = null;

            if (!string.IsNullOrEmpty(runUser))
            {
                runUserIdRef = new IdentityRef()
                {
                    DisplayName = runUser
                };
            }

            //get all test assemblies
            if (testResultsNode != null)
            {
                XmlNodeList testAssemblyNodes = testResultsNode.SelectNodes("test-suite");
                if (testAssemblyNodes != null)
                {
                    foreach (XmlNode testAssemblyNode in testAssemblyNodes)
                    {
                        var assemblyStartTime = (runStartTime == DateTime.MinValue) ? DateTime.MinValue : runStartTime + totalTestCaseDuration;
                        List <TestCaseResultData> testCases = FindTestCaseNodes(testAssemblyNode, hostName, runUserIdRef, assemblyStartTime);
                        if (testCases != null)
                        {
                            results.AddRange(testCases);
                            testCases.ForEach(x => totalTestCaseDuration += TimeSpan.FromMilliseconds(x.DurationInMs));
                        }
                    }
                }
            }

            if (TimeSpan.Compare(totalRunDuration, totalTestCaseDuration) < 0)
            {
                totalRunDuration = totalTestCaseDuration; //run duration may not be set in the xml, so use total test case duration
            }

            if (runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName))
            {
                runName = runContext.RunName;
            }

            //create test run data
            TestRunData testRunData = new TestRunData(
                name: runName,
                startedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.ToString("o"),
                completedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.Add(totalRunDuration).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: config,
                buildPlatform: platform,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results     = results.ToArray();
            testRunData.Attachments = addResultsAsAttachments ? new string[] { filePath } : new string[0];

            return(testRunData);
        }
Example #27
0
        /// <summary>
        /// Reads a JUnit results file from disk, converts it into a TestRunData object.        
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="runContext"></param>
        /// <returns></returns>
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            // http://windyroad.com.au/dl/Open%20Source/JUnit.xsd

            XmlDocument doc = new XmlDocument();
            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Prohibit
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return null;
            }

            //init test run summary information - run name, host name, start time
            TestSuiteSummary runSummary = new TestSuiteSummary(Name);

            IdentityRef runUserIdRef = null;
            string runUser = runContext != null ? runContext.Owner : string.Empty;
            if (!string.IsNullOrEmpty(runUser))
            {
                runUserIdRef = new IdentityRef() { DisplayName = runUser };
            }

            //read data from testsuite nodes

            XmlNode testSuitesNode = doc.SelectSingleNode("testsuites");
            if (testSuitesNode != null)
            {
                //found testsuites node - some plugins generate it like karma junit plugin
                XmlNodeList testSuiteNodeList = doc.SelectNodes("/testsuites/testsuite");
                if (testSuiteNodeList != null)
                {
                    foreach (XmlNode testSuiteNode in testSuiteNodeList)
                    {
                        TestSuiteSummary testSuiteSummary = ReadTestSuite(testSuiteNode, runUserIdRef);
                        runSummary.Duration = runSummary.Duration.Add(testSuiteSummary.Duration);
                        runSummary.Results.AddRange(testSuiteSummary.Results);
                        runSummary.Host = testSuiteSummary.Host;
                        runSummary.Name = testSuiteSummary.Name;
                    }

                    if (testSuiteNodeList.Count > 1)
                    {
                        runSummary.Name = Name + "_" + Path.GetFileName(filePath);
                    }
                }
            }
            else
            {
                XmlNode testSuiteNode = doc.SelectSingleNode("testsuite");
                if (testSuiteNode != null)
                {
                    runSummary = ReadTestSuite(testSuiteNode, runUserIdRef);
                }
            }

            if (runContext != null && !string.IsNullOrWhiteSpace(runContext.RunName))
            {
                runSummary.Name = runContext.RunName;
            }

            if (runSummary.Results.Count > 0)
            {
                //first testsuite starteddate is the starteddate of the run
                runSummary.TimeStamp = DateTime.Parse(runSummary.Results[0].StartedDate);
            }

            //create test run data
            var testRunData = new TestRunData(
                name: runSummary.Name,
                startedDate: runSummary.TimeStamp.ToString("o"),
                completedDate: runSummary.TimeStamp.Add(runSummary.Duration).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildId: runContext != null ? runContext.BuildId : 0,
                buildFlavor: runContext != null ? runContext.Configuration : string.Empty,
                buildPlatform: runContext != null ? runContext.Platform : string.Empty,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                )
            {
                Results = runSummary.Results.ToArray(),
                Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0]

            };

            return testRunData;
        }
Example #28
0
        //Based on the XUnit V2 format: http://xunit.github.io/docs/format-xml-v2.html
        public TestRunData ReadResults(IExecutionContext executionContext, string filePath, TestRunContext runContext = null)
        {
            List <TestCaseResultData> results = new List <TestCaseResultData>();

            XmlDocument doc = new XmlDocument();

            try
            {
                var settings = new XmlReaderSettings
                {
                    DtdProcessing = DtdProcessing.Ignore
                };

                using (XmlReader reader = XmlReader.Create(filePath, settings))
                {
                    doc.Load(reader);
                }
            }
            catch (XmlException ex)
            {
                executionContext.Warning(StringUtil.Loc("FailedToReadFile", filePath, ex.Message));
                return(null);
            }

            string runName = Name + " Test Run";
            string runUser = "";

            if (runContext != null)
            {
                if (!string.IsNullOrWhiteSpace(runContext.RunName))
                {
                    runName = runContext.RunName;
                }
                else
                {
                    runName = string.Format(CultureInfo.CurrentCulture, "{0} {1} {2}", runName, runContext.Configuration, runContext.Platform);
                }

                if (runContext.Owner != null)
                {
                    runUser = runContext.Owner;
                }
            }

            IdentityRef runUserIdRef = new IdentityRef();

            runUserIdRef.DisplayName = runUser;

            var    runStartTime    = DateTime.MinValue;
            double testRunDuration = 0;

            XmlNodeList assemblyNodes = doc.SelectNodes("/assemblies/assembly");

            foreach (XmlNode assemblyNode in assemblyNodes)
            {
                var assemblyRunStartTimeStamp = DateTime.MinValue;
                if (assemblyNode.Attributes["run-date"] != null && assemblyNode.Attributes["run-time"] != null)
                {
                    string runDate = assemblyNode.Attributes["run-date"].Value;
                    string runTime = assemblyNode.Attributes["run-time"].Value;

                    var startDate = DateTime.Now;
                    var startTime = TimeSpan.Zero;
                    if (DateTime.TryParse(runDate, out startDate))
                    {
                        TimeSpan.TryParse(runTime, out startTime);
                    }
                    assemblyRunStartTimeStamp = startDate + startTime;
                    if (testRunDuration == 0)
                    {
                        // first assembly start time is runstart time
                        runStartTime = assemblyRunStartTimeStamp;
                    }
                }

                XmlNodeList testCaseNodeList = assemblyNode.SelectNodes("./collection/test");
                foreach (XmlNode testCaseNode in testCaseNodeList)
                {
                    TestCaseResultData resultCreateModel = new TestCaseResultData()
                    {
                        Priority = TestManagementConstants.UnspecifiedPriority,  //Priority is int type so if no priority set then its 255.
                    };

                    //Test storage.
                    if (assemblyNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestStorage = Path.GetFileName(assemblyNode.Attributes["name"].Value);
                    }

                    //Fully Qualified Name.
                    if (testCaseNode.Attributes["name"] != null)
                    {
                        resultCreateModel.AutomatedTestName = testCaseNode.Attributes["name"].Value;
                    }

                    //Test Method Name.
                    if (testCaseNode.Attributes["method"] != null)
                    {
                        resultCreateModel.TestCaseTitle = testCaseNode.Attributes["method"].Value;
                    }

                    //Test duration.
                    if (testCaseNode.Attributes["time"] != null && testCaseNode.Attributes["time"].Value != null)
                    {
                        double duration = 0;
                        double.TryParse(testCaseNode.Attributes["time"].Value, out duration);
                        var durationFromSeconds = TimeSpan.FromSeconds(duration);
                        resultCreateModel.DurationInMs = durationFromSeconds.TotalMilliseconds;

                        // no assemblystarttime available so dont set testcase start and completed
                        if (assemblyRunStartTimeStamp != DateTime.MinValue)
                        {
                            resultCreateModel.StartedDate   = assemblyRunStartTimeStamp;
                            resultCreateModel.CompletedDate = assemblyRunStartTimeStamp.AddTicks(durationFromSeconds.Ticks);
                            assemblyRunStartTimeStamp       = assemblyRunStartTimeStamp.AddTicks(1) + durationFromSeconds;
                            //next start time
                        }

                        //Calculate overall run duration.
                        testRunDuration += duration;
                    }


                    //Test outcome.
                    if (testCaseNode.SelectSingleNode("./failure") != null)
                    {
                        resultCreateModel.Outcome = TestOutcome.Failed.ToString();

                        //Error message.
                        XmlNode failureMessageNode = testCaseNode.SelectSingleNode("./failure/message");
                        if (failureMessageNode != null && !string.IsNullOrWhiteSpace(failureMessageNode.InnerText))
                        {
                            resultCreateModel.ErrorMessage = failureMessageNode.InnerText;
                        }

                        //Stack trace.
                        XmlNode failureStackTraceNode = testCaseNode.SelectSingleNode("./failure/stack-trace");
                        if (failureStackTraceNode != null && !string.IsNullOrWhiteSpace(failureStackTraceNode.InnerText))
                        {
                            resultCreateModel.StackTrace = failureStackTraceNode.InnerText;
                        }
                    }
                    else if (testCaseNode.Attributes["result"] != null && string.Equals(testCaseNode.Attributes["result"].Value, "pass", StringComparison.OrdinalIgnoreCase))
                    {
                        resultCreateModel.Outcome = TestOutcome.Passed.ToString();
                    }
                    else
                    {
                        resultCreateModel.Outcome = TestOutcome.NotExecuted.ToString();
                    }

                    //Test priority.
                    XmlNode priorityTrait = testCaseNode.SelectSingleNode("./traits/trait[@name='priority']");
                    if (priorityTrait != null && priorityTrait.Attributes["value"] != null)
                    {
                        var priorityValue = priorityTrait.Attributes["value"].Value;
                        resultCreateModel.Priority = !string.IsNullOrEmpty(priorityValue) ? Convert.ToInt32(priorityValue)
                                                        : TestManagementConstants.UnspecifiedPriority;
                    }

                    //Test owner.
                    XmlNode ownerNode = testCaseNode.SelectSingleNode("./traits/trait[@name='owner']");
                    if (ownerNode != null && ownerNode.Attributes["value"] != null && ownerNode.Attributes["value"].Value != null)
                    {
                        IdentityRef ownerIdRef = new IdentityRef();
                        ownerIdRef.DisplayName  = ownerNode.Attributes["value"].Value;
                        resultCreateModel.Owner = ownerIdRef;
                    }

                    resultCreateModel.RunBy = runUserIdRef;

                    resultCreateModel.State = "Completed";

                    resultCreateModel.AutomatedTestType = Name;

                    if (!string.IsNullOrEmpty(resultCreateModel.AutomatedTestName) && !string.IsNullOrEmpty(resultCreateModel.TestCaseTitle))
                    {
                        results.Add(resultCreateModel);
                    }
                }
            }

            TestRunData testRunData = new TestRunData(
                name: runName,
                buildId: runContext != null ? runContext.BuildId : 0,
                startedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.ToString("o"),
                completedDate: (runStartTime == DateTime.MinValue) ? string.Empty : runStartTime.Add(TimeSpan.FromSeconds(testRunDuration)).ToString("o"),
                state: TestRunState.InProgress.ToString(),
                isAutomated: true,
                buildFlavor: runContext != null ? runContext.Configuration : null,
                buildPlatform: runContext != null ? runContext.Platform : null,
                releaseUri: runContext != null ? runContext.ReleaseUri : null,
                releaseEnvironmentUri: runContext != null ? runContext.ReleaseEnvironmentUri : null
                );

            testRunData.Results     = results.ToArray();
            testRunData.Attachments = AddResultsFileToRunLevelAttachments ? new string[] { filePath } : new string[0];

            return(testRunData);
        }
Example #29
0
 static string Convert(IdentityRef identity) => identity.DisplayName + $" <{identity.UniqueName}>";