Exemple #1
0
        public void ReportFailedTest(Exception exception, string testName, string callerFilePath, int callerLineNumber)
        {
            if (exception == null)
            {
                return;
            }

            var sExecption = exception as SeleniumTestFailedException;

            var unwrappedTestExceptions = sExecption?.InnerExceptions.OfType <TestExceptionBase>().ToList();
            List <TestRunAttachmentInputData> attachments = GetAttachments(unwrappedTestExceptions);

            var data = new TestRunInputData()
            {
                TestResult    = 0,
                Attachments   = attachments,
                TestFullName  = testName,
                TestOutput    = exception.ToString(),
                BuildNumber   = reportingMetadataProvider.GetBuildNumber(),
                ProjectName   = reportingMetadataProvider.GetProjectName(),
                TestSuiteName = reportingMetadataProvider.GetTestSuiteName(),
            };

            EnqueueResult(data);
        }
Exemple #2
0
 private void EnqueueResult(TestRunInputData data)
 {
     if (configuration.Reporting.Parallel)
     {
         SendResultSync(data);
     }
     else
     {
         Queue.Enqueue(data);
     }
 }
        /// <summary>
        /// Rewrite or add metadata before the result is sent to the reporting app.
        /// </summary>
        /// <param name="data"></param>
        protected virtual void RewriteMetadata(TestRunInputData data)
        {
            // rewrite metadata
            string buildId = Environment.GetEnvironmentVariable("BUILD_BUILDID");

            if (!string.IsNullOrWhiteSpace(buildId))
            {
                data.BuildNumber = buildId;
                data.ProjectName =
                    Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT") + "|" +
                    Environment.GetEnvironmentVariable("BUILD_BUILDNUMBER") + "|" +
                    Environment.GetEnvironmentVariable("BUILD_DEFINITIONNAME");
            }
        }
Exemple #4
0
        public void ReportSuccessfulTest(string testName, string callerFilePath, int callerLineNumber)
        {
            var data = new TestRunInputData()
            {
                TestResult    = 1,
                TestFullName  = testName,
                BuildNumber   = reportingMetadataProvider.GetBuildNumber(),
                ProjectName   = reportingMetadataProvider.GetProjectName(),
                TestOutput    = "",
                TestSuiteName = reportingMetadataProvider.GetTestSuiteName(),
                Attachments   = new List <TestRunAttachmentInputData>()
            };

            EnqueueResult(data);
        }
Exemple #5
0
        public static async Task <IActionResult> PublishResult([HttpTrigger(AuthorizationLevel.Function, "post", Route = "publish")] TestRunInputData input, TraceWriter log)
        {
            try
            {
                // remove special characters
                input.ProjectName   = Helpers.RemoveUnsafeChars(input.ProjectName);
                input.TestSuiteName = Helpers.RemoveUnsafeChars(input.TestSuiteName);
                input.BuildNumber   = Helpers.RemoveUnsafeChars(input.BuildNumber);
                input.TestFullName  = Helpers.RemoveUnsafeChars(input.TestFullName, allowUrlChars: true);

                // save test run results
                var entity = new TestRun()
                {
                    PartitionKey = TestRun.CreatePartitionKey(input.TestSuiteName, input.BuildNumber),
                    RowKey       = TestRun.CreateRowKey(input.TestFullName),
                    Attachments  = new List <TestRunAttachmentLink>(),
                    Timestamp    = DateTimeOffset.UtcNow,
                    TestResult   = input.TestResult
                };

                // store test output - if it is short, put it directly in the table; if it is too long, put it in blob storage
                if (input.TestOutput.Length <= MaxFieldLength)
                {
                    entity.TestOutput = input.TestOutput;
                }
                else
                {
                    // prepare URL
                    var testOutputUrl = GetTestOutputBlobUrl(input.TestSuiteName, input.BuildNumber, input.TestFullName);

                    // upload test output
                    var container = await GetBlobContainerForProject(input.ProjectName);

                    var testOutputBlob = container.GetBlockBlobReference(testOutputUrl);
                    await testOutputBlob.UploadTextAsync(input.TestOutput);

                    entity.TestOutput    = input.TestOutput.Substring(0, MaxFieldLength);
                    entity.TestOutputUrl = testOutputUrl;
                }

                // save attachments to blob storage
                if (input.Attachments != null)
                {
                    foreach (var attachment in input.Attachments)
                    {
                        // prepare URL
                        var attachmentUrl = GetTestAttachmentBlobUrl(input.TestSuiteName, input.BuildNumber, input.TestFullName, attachment.FileName);

                        // upload blob
                        var container = await GetBlobContainerForProject(input.ProjectName);

                        var attachmentBlob = container.GetBlockBlobReference(attachmentUrl);
                        var attachmentData = Convert.FromBase64String(attachment.ContentBase64);
                        await attachmentBlob.UploadFromByteArrayAsync(attachmentData, 0, attachmentData.Length);

                        entity.Attachments.Add(new TestRunAttachmentLink()
                        {
                            FileName = attachment.FileName,
                            BlobUrl  = attachmentUrl
                        });
                    }
                }

                // store entity
                var table = await GetTableForProject(input.ProjectName, createIfNotExists : true);

                await table.ExecuteAsync(TableOperation.Insert(entity));

                // store index entity
                var indexTable = await GetIndexTable(createIfNotExists : true);

                await indexTable.ExecuteAsync(TableOperation.InsertOrReplace(new TestSuiteInfo()
                {
                    PartitionKey = TestSuiteInfo.CreatePartitionKey(input.ProjectName),
                    RowKey       = TestSuiteInfo.CreateRowKey(input.TestSuiteName, input.BuildNumber),
                    Timestamp    = DateTimeOffset.UtcNow
                }));

                return(new OkObjectResult(new TestRunInputResult()
                {
                    TestSuiteUrl = $"{AppBaseUrl}/results/{input.ProjectName}/{input.TestSuiteName}/{input.BuildNumber}",
                    TestResultUrl = $"{AppBaseUrl}/results/{input.ProjectName}/{input.TestSuiteName}/{input.BuildNumber}#{input.TestFullName}"
                }));
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                return(new BadRequestErrorMessageResult(ex.Message));
            }
        }
Exemple #6
0
        private IEnumerable <TestRunInputResult> SendResultSync(TestRunInputData data)
        {
            var results = ReportTestResult(data).RunSync();

            return(results);
        }
        protected virtual async Task <TestRunInputResult> SendReport(TestRunInputData data)
        {
            var response = await Client.PostAsync(new Uri(ReportTestResultUrl), new StringContent(JsonConvert.SerializeObject(data)));

            return(JsonConvert.DeserializeObject <TestRunInputResult>(await response.Content.ReadAsStringAsync()));
        }
 public Task <TestRunInputResult> ReportTestResult(TestRunInputData data)
 {
     RewriteMetadata(data);
     return(SendReport(data));
 }
Exemple #9
0
        public async System.Threading.Tasks.Task <TestRunInputResult> PostPublishAsync(TestRunInputData body, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/publish");

            var client_ = new System.Net.Http.HttpClient();

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    request_.Content             = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");
                    request_.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    PrepareRequest(client_, request_, urlBuilder_);
                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                    try
                    {
                        var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        foreach (var item_ in response_.Content.Headers)
                        {
                            headers_[item_.Key] = item_.Value;
                        }

                        ProcessResponse(client_, response_);

                        var status_ = ((int)response_.StatusCode).ToString();
                        if (status_ == "200")
                        {
                            var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            var result_ = default(TestRunInputResult);
                            try
                            {
                                result_ = Newtonsoft.Json.JsonConvert.DeserializeObject <TestRunInputResult>(responseData_, _settings.Value);
                                return(result_);
                            }
                            catch (System.Exception exception_)
                            {
                                throw new SwaggerException("Could not deserialize the response body.", status_, responseData_, headers_, exception_);
                            }
                        }
                        else
                        if (status_ == "400")
                        {
                            var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new SwaggerException("Invalid input", status_, responseData_, headers_, null);
                        }
                        else
                        if (status_ != "200" && status_ != "204")
                        {
                            var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new SwaggerException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", status_, responseData_, headers_, null);
                        }

                        return(default(TestRunInputResult));
                    }
                    finally
                    {
                        if (response_ != null)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (client_ != null)
                {
                    client_.Dispose();
                }
            }
        }
Exemple #10
0
 public TestRunInputResult PostPublish(TestRunInputData body)
 {
     return(System.Threading.Tasks.Task.Run(async() => await PostPublishAsync(body, System.Threading.CancellationToken.None)).GetAwaiter().GetResult());
 }