Ejemplo n.º 1
0
        public bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult)
        { 
            testResult = default(CachedTestResult);

            var storageFolder = GetStorageFolder(checksum);
            if (!Directory.Exists(storageFolder))
            {
                return false;
            }

            try
            {
                var exitCode = Read(checksum, StorageKind.ExitCode);
                var standardOutput = Read(checksum, StorageKind.StandardOutput);
                var errorOutput = Read(checksum, StorageKind.ErrorOutput);
                var resultsFileName = Read(checksum, StorageKind.ResultsFileName);
                var resultsFileContent = Read(checksum, StorageKind.ResultsFileContent);
                var elapsed = Read(checksum, StorageKind.ElapsedSeconds);

                testResult = new CachedTestResult(
                    exitCode: int.Parse(exitCode),
                    standardOutput: standardOutput,
                    errorOutput: errorOutput,
                    resultsFileContent: resultsFileContent,
                    elapsed: TimeSpan.FromSeconds(int.Parse(elapsed)));
                return true;
            }
            catch (Exception e)
            {
                // Okay for exception to occur here on I/O
                Logger.Log($"Failed to read cache {checksum} {e.Message}");
            }

            return false;
        }
Ejemplo n.º 2
0
        public async Task<CachedTestResult?> TryGetCachedTestResult(string checksum)
        {
            try
            {
                var request = new RestRequest($"api/testcache/{checksum}");
                var response = await _restClient.ExecuteGetTaskAsync(request);
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return null;
                }

                var obj = JObject.Parse(response.Content);
                var result = new CachedTestResult(
                    exitCode: obj.Value<int>(NameExitCode),
                    standardOutput: obj.Value<string>(NameOutputStandard),
                    errorOutput: obj.Value<string>(NameOutputError),
                    resultsFileContent: obj.Value<string>(NameResultsFileContent),
                    ellapsed: TimeSpan.FromSeconds(obj.Value<int>(NameEllapsedSeconds)));
                return result;
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception retrieving cached test result {checksum}: {ex}");
                return null;
            }
        }
Ejemplo n.º 3
0
 private static TestCacheData CreateTestCacheData(AssemblyInfo assemblyInfo, string resultsFileName, CachedTestResult testResult)
 {
     return new TestCacheData()
     {
         TestResultData = CreateTestResultData(resultsFileName, testResult),
         TestSourceData = CreateTestSourceData(assemblyInfo)
     };
 }
Ejemplo n.º 4
0
 private static JObject CreateTestResultData(string resultsFileName, CachedTestResult testResult)
 {
     var obj = new JObject();
     obj[NameExitCode] = testResult.ExitCode;
     obj[NameOutputStandard] = testResult.StandardOutput;
     obj[NameOutputStandard] = testResult.ErrorOutput;
     obj[NameResultsFileName] = resultsFileName;
     obj[NameResultsFileContent] = testResult.ResultsFileContent;
     obj[NameEllapsedSeconds] = (int)testResult.Ellapsed.TotalSeconds;
     return obj;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Recreate the on disk artifacts for the cached data and return the correct <see cref="TestResult"/>
        /// value.
        /// </summary>
        private TestResult Migrate(string assemblyPath, CachedTestResult cachedTestResult)
        {
            var resultsDir = Path.Combine(Path.GetDirectoryName(assemblyPath), Constants.ResultsDirectoryName);
            FileUtil.EnsureDirectory(resultsDir);
            var resultsFilePath = Path.Combine(resultsDir, cachedTestResult.ResultsFileName);
            File.WriteAllText(resultsFilePath, cachedTestResult.ResultsFileContent);
            var commandLine = _testExecutor.GetCommandLine(assemblyPath);

            return new TestResult(
                exitCode: cachedTestResult.ExitCode,
                assemblyPath: assemblyPath,
                resultDir: resultsDir,
                resultsFilePath: resultsFilePath,
                commandLine: commandLine,
                elapsed: TimeSpan.FromMilliseconds(0),
                standardOutput: cachedTestResult.StandardOutput,
                errorOutput: cachedTestResult.ErrorOutput);
        }
Ejemplo n.º 6
0
        public async Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            try
            {
                var testCacheData = CreateTestCacheData(assemblyInfo, assemblyInfo.ResultsFileName, testResult);
                var request = new RestRequest($"api/testData/cache/{contentFile.Checksum}");
                request.Method = Method.PUT;
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("text/json", JsonConvert.SerializeObject(testCacheData), ParameterType.RequestBody);

                var response = await _restClient.ExecuteTaskAsync(request);
                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    Logger.Log($"Error adding web cached result: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Exception uploading cached test result");
            }
        }
Ejemplo n.º 7
0
        public async Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            try
            {
                var obj = new JObject();
                obj["TestResultData"] = CreateTestResultData(assemblyInfo.ResultsFileName, testResult);
                obj["TestSourceData"] = CreateTestSourceData(assemblyInfo);

                var request = new RestRequest($"api/testcache/{contentFile.Checksum}");
                request.Method = Method.PUT;
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("text/json", obj.ToString(), ParameterType.RequestBody);
                var response = await _restClient.ExecuteTaskAsync(request);
                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    Logger.Log($"Error adding web cached result: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception adding web cached result:  {ex}");
            }
        }
Ejemplo n.º 8
0
        public async Task<CachedTestResult?> TryGetCachedTestResult(string checksum)
        {
            try
            {
                var request = new RestRequest($"api/testData/cache/{checksum}");

                // Add query parameters the web service uses for additional tracking
                request.AddParameter("machineName", Environment.MachineName, ParameterType.QueryString);
                request.AddParameter("enlistmentRoot", Constants.EnlistmentRoot, ParameterType.QueryString);

                if (Constants.IsJenkinsRun)
                {
                    request.AddParameter("source", "jenkins", ParameterType.QueryString);
                }

                var response = await _restClient.ExecuteGetTaskAsync(request);
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return null;
                }

                var testCacheData = JsonConvert.DeserializeObject<TestResultData>(response.Content);
                var result = new CachedTestResult(
                    exitCode: testCacheData.ExitCode,
                    standardOutput: testCacheData.OutputStandard,
                    errorOutput: testCacheData.OutputError,
                    resultsFileContent: testCacheData.ResultsFileContent,
                    elapsed: TimeSpan.FromSeconds(testCacheData.ElapsedSeconds));
                return result;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Exception downloading cached test result for {checksum}");
                return null;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Recreate the on disk artifacts for the cached data and return the correct <see cref="TestResult"/>
        /// value.
        /// </summary>
        private TestResult Migrate(AssemblyInfo assemblyInfo, CachedTestResult cachedTestResult)
        {
            var resultsDir = Path.Combine(Path.GetDirectoryName(assemblyInfo.AssemblyPath), Constants.ResultsDirectoryName);

            FileUtil.EnsureDirectory(resultsDir);
            var resultsFilePath = Path.Combine(resultsDir, assemblyInfo.ResultsFileName);

            File.WriteAllText(resultsFilePath, cachedTestResult.ResultsFileContent);
            var testResultInfo = new TestResultInfo(
                exitCode: cachedTestResult.ExitCode,
                resultsDirectory: resultsDir,
                resultsFilePath: resultsFilePath,
                elapsed: TimeSpan.FromMilliseconds(0),
                standardOutput: cachedTestResult.StandardOutput,
                errorOutput: cachedTestResult.ErrorOutput);

            var commandLine = _testExecutor.GetCommandLine(assemblyInfo);

            return(new TestResult(
                       assemblyInfo,
                       testResultInfo,
                       commandLine,
                       isFromCache: true));
        }
Ejemplo n.º 10
0
        public bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult)
        {
            testResult = default(CachedTestResult);

            var storageFolder = GetStorageFolder(checksum);

            if (!Directory.Exists(storageFolder))
            {
                return(false);
            }

            try
            {
                var exitCode           = Read(checksum, StorageKind.ExitCode);
                var standardOutput     = Read(checksum, StorageKind.StandardOutput);
                var errorOutput        = Read(checksum, StorageKind.ErrorOutput);
                var resultsFileName    = Read(checksum, StorageKind.ResultsFileName);
                var resultsFileContent = Read(checksum, StorageKind.ResultsFileContent);
                var elapsed            = Read(checksum, StorageKind.ElapsedSeconds);

                testResult = new CachedTestResult(
                    exitCode: int.Parse(exitCode),
                    standardOutput: standardOutput,
                    errorOutput: errorOutput,
                    resultsFileContent: resultsFileContent,
                    elapsed: TimeSpan.FromSeconds(int.Parse(elapsed)));
                return(true);
            }
            catch (Exception e)
            {
                // Okay for exception to occur here on I/O
                Logger.Log($"Failed to read cache {checksum}", e);
            }

            return(false);
        }
Ejemplo n.º 11
0
        private static Tuple<int, int, int> GetTestNumbers(string resultsFileName, CachedTestResult testResult)
        {
            if (!resultsFileName.EndsWith("xml", StringComparison.OrdinalIgnoreCase))
            {
                return null;
            }

            try
            {
                using (var reader = new StringReader(testResult.ResultsFileContent))
                { 
                    var document = XDocument.Load(reader);
                    var assembly = document.Element("assemblies").Element("assembly");
                    var passed = int.Parse(assembly.Attribute("passed").Value);
                    var failed = int.Parse(assembly.Attribute("failed").Value);
                    var skipped = int.Parse(assembly.Attribute("skipped").Value);
                    return Tuple.Create(passed, failed, skipped);
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception reading test numbers: {ex}");
                return null;
            }
        }
Ejemplo n.º 12
0
 private static TestCacheData CreateTestCacheData(AssemblyInfo assemblyInfo, string resultsFileName, CachedTestResult testResult)
 {
     return(new TestCacheData()
     {
         TestResultData = CreateTestResultData(resultsFileName, testResult),
         TestSourceData = CreateTestSourceData(assemblyInfo)
     });
 }
Ejemplo n.º 13
0
        public async Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            try
            {
                var testCacheData = CreateTestCacheData(assemblyInfo, assemblyInfo.ResultsFileName, testResult);
                Logger.Log($"Source data for ${assemblyInfo.DisplayName}: {JsonConvert.SerializeObject(testCacheData.TestSourceData, Formatting.Indented)}");

                var request = new RestRequest($"api/testData/cache/{contentFile.Checksum}");
                request.Method        = Method.PUT;
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("text/json", JsonConvert.SerializeObject(testCacheData), ParameterType.RequestBody);

                var response = await _restClient.ExecuteTaskAsync(request);

                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    Logger.Log($"Error adding web cached result: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Exception uploading cached test result");
            }
        }
Ejemplo n.º 14
0
        public Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            var checksum = contentFile.Checksum;
            var storagePath = Path.Combine(_storagePath, checksum);
            try
            {
                if (!FileUtil.EnsureDirectory(storagePath))
                {
                    return Task.FromResult(true);
                }

                Write(checksum, StorageKind.ExitCode, testResult.ExitCode.ToString());
                Write(checksum, StorageKind.StandardOutput, testResult.StandardOutput);
                Write(checksum, StorageKind.ErrorOutput, testResult.ErrorOutput);
                Write(checksum, StorageKind.ResultsFileContent, testResult.ResultsFileContent);
                Write(checksum, StorageKind.ElapsedSeconds, testResult.Elapsed.TotalSeconds.ToString());
                Write(checksum, StorageKind.Content, contentFile.Content);
            }
            catch (Exception e)
            {
                // I/O errors are expected and okay here.
                Logger.Log($"Failed to log {checksum}", e);
                FileUtil.DeleteDirectory(storagePath);
            }

            return Task.FromResult(true);
        }
Ejemplo n.º 15
0
        public async Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            try
            {
                var obj = new JObject();
                obj["TestResultData"] = CreateTestResultData(assemblyInfo.ResultsFileName, testResult);
                obj["TestSourceData"] = CreateTestSourceData(assemblyInfo);

                var request = new RestRequest($"api/testcache/{contentFile.Checksum}");
                request.Method        = Method.PUT;
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("text/json", obj.ToString(), ParameterType.RequestBody);

                var response = await _restClient.ExecuteTaskAsync(request);

                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    Logger.Log($"Error adding web cached result: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception adding web cached result:  {ex}");
            }
        }
Ejemplo n.º 16
0
 public Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile conentFile, CachedTestResult testResult)
 {
     var source = new TaskCompletionSource<bool>();
     source.SetResult(true);
     return source.Task;
 }
Ejemplo n.º 17
0
 private static TestResultData CreateTestResultData(string resultsFileName, CachedTestResult testResult)
 {
     var numbers = GetTestNumbers(resultsFileName, testResult) ?? Tuple.Create(-1, -1, -1);
     return new TestResultData()
     {
         ExitCode = testResult.ExitCode,
         OutputStandard = testResult.StandardOutput,
         OutputError = testResult.ErrorOutput,
         ResultsFileName = resultsFileName,
         ResultsFileContent = testResult.ResultsFileContent,
         ElapsedSeconds = (int)testResult.Elapsed.TotalSeconds,
         TestPassed = numbers.Item1,
         TestFailed = numbers.Item2,
         TestSkipped = numbers.Item3
     };
 }
Ejemplo n.º 18
0
 private void CacheTestResult(ContentFile contentFile, TestResult testResult)
 {
     try
     {
         var resultFileContent = File.ReadAllText(testResult.ResultsFilePath);
         var cachedTestResult = new CachedTestResult(
             exitCode: testResult.ExitCode,
             standardOutput: testResult.StandardOutput,
             errorOutput: testResult.ErrorOutput,
             resultsFileName: Path.GetFileName(testResult.ResultsFilePath),
             resultsFileContent: resultFileContent);
         _dataStorage.AddCachedTestResult(contentFile, cachedTestResult);
     }
     catch (Exception ex)
     {
         Logger.Log($"Failed to create cached {ex}");
     }
 }
Ejemplo n.º 19
0
        public async Task<CachedTestResult?> TryGetCachedTestResult(string checksum)
        {
            try
            {
                var request = new RestRequest($"api/testcache/{checksum}");

                // Add query parameters the web service uses for additional tracking
                request.AddParameter("machineName", Environment.MachineName, ParameterType.QueryString);
                request.AddParameter("enlistmentRoot", Constants.EnlistmentRoot, ParameterType.QueryString);

                if (Constants.IsJenkinsRun)
                {
                    request.AddParameter("source", "jenkins", ParameterType.QueryString);
                }

                var response = await _restClient.ExecuteGetTaskAsync(request);
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return null;
                }

                var obj = JObject.Parse(response.Content);

                // During the transition from ellapsed to elapsed the client needs to accept either
                // value from the json object.
                var elapsedProperty = obj.Property(NameElapsedSeconds) ?? obj.Property(NameElapsedSecondsMisspelled);

                var result = new CachedTestResult(
                    exitCode: obj.Value<int>(NameExitCode),
                    standardOutput: obj.Value<string>(NameOutputStandard),
                    errorOutput: obj.Value<string>(NameOutputError),
                    resultsFileContent: obj.Value<string>(NameResultsFileContent),
                    elapsed: TimeSpan.FromSeconds(elapsedProperty.Value.Value<int>()));
                return result;
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception retrieving cached test result {checksum}: {ex}");
                return null;
            }
        }
Ejemplo n.º 20
0
        public Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile contentFile, CachedTestResult testResult)
        {
            var checksum = contentFile.Checksum;
            var storagePath = Path.Combine(_storagePath, checksum);
            try
            {
                if (!FileUtil.EnsureDirectory(storagePath))
                {
                    return Task.FromResult(true);
                }

                Write(checksum, StorageKind.ExitCode, testResult.ExitCode.ToString());
                Write(checksum, StorageKind.StandardOutput, testResult.StandardOutput);
                Write(checksum, StorageKind.ErrorOutput, testResult.ErrorOutput);
                Write(checksum, StorageKind.ResultsFileContent, testResult.ResultsFileContent);
                Write(checksum, StorageKind.ElapsedSeconds, testResult.Elapsed.TotalSeconds.ToString());
                Write(checksum, StorageKind.Content, contentFile.Content);
            }
            catch (Exception e)
            {
                // I/O errors are expected and okay here.
                Logger.Log($"Failed to log {checksum} {e.Message}");
                FileUtil.DeleteDirectory(storagePath);
            }

            return Task.FromResult(true);
        }
Ejemplo n.º 21
0
        public Task AddCachedTestResult(AssemblyInfo assemblyInfo, ContentFile conentFile, CachedTestResult testResult)
        {
            var source = new TaskCompletionSource <bool>();

            source.SetResult(true);
            return(source.Task);
        }
Ejemplo n.º 22
0
 private async Task CacheTestResult(ContentFile contentFile, TestResult testResult)
 {
     try
     {
         var resultFileContent = File.ReadAllText(testResult.ResultsFilePath);
         var cachedTestResult = new CachedTestResult(
             exitCode: testResult.ExitCode,
             standardOutput: testResult.StandardOutput,
             errorOutput: testResult.ErrorOutput,
             resultsFileContent: resultFileContent,
             elapsed: testResult.Elapsed);
         await _dataStorage.AddCachedTestResult(testResult.AssemblyInfo, contentFile, cachedTestResult).ConfigureAwait(true);
     }
     catch (Exception ex)
     {
         Logger.Log($"Failed to create cached {ex}");
     }
 }
Ejemplo n.º 23
0
        private static JObject CreateTestResultData(string resultsFileName, CachedTestResult testResult)
        {
            var numbers = GetTestNumbers(resultsFileName, testResult) ?? Tuple.Create(-1, -1, -1);
            var obj = new JObject();
            obj[NameExitCode] = testResult.ExitCode;
            obj[NameOutputStandard] = testResult.StandardOutput;
            obj[NameOutputStandard] = testResult.ErrorOutput;
            obj[NameResultsFileName] = resultsFileName;
            obj[NameResultsFileContent] = testResult.ResultsFileContent;
            obj[NameElapsedSeconds] = (int)testResult.Elapsed.TotalSeconds;
            obj[NameElapsedSecondsMisspelled] = (int)testResult.Elapsed.TotalSeconds;
            obj[NameTestPassed] = numbers.Item1;
            obj[NameTestFailed] = numbers.Item2;
            obj[NameTestSkipped] = numbers.Item3;

            return obj;
        }
Ejemplo n.º 24
0
        public async Task AddCachedTestResult(string assemblyName, ContentFile contentFile, CachedTestResult testResult)
        {
            var obj = new JObject();

            obj["TestResultData"] = CreateTestResultData(testResult);
            obj["TestSourceData"] = CreateTestSourceData(assemblyName);

            var request = new RestRequest($"api/testcache/{contentFile.Checksum}");

            request.Method        = Method.PUT;
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("text/json", obj.ToString(), ParameterType.RequestBody);
            var response = await _restClient.ExecuteTaskAsync(request);
        }