private List <IRepoFileContainerDescriptor> EnumerateMonths( IRepoFileContainerDescriptor yearDir) { Check.DoAssertLambda(yearDir.Level == FolderLevelYear, () => new ArgumentException("Year level expected in yearDir")); List <IRepoFileContainerDescriptor> retval = new List <IRepoFileContainerDescriptor>(); string root = Path.Combine(this.RootPath, yearDir.RelativePath); if (_directoryProvider.Exists(root)) { IEnumerable <string> monthDirs = _directoryProvider.EnumerateDirectories( root , _dirLevelTraits[FolderLevelMonth].DirNameMask); foreach (string monthDir in monthDirs) { int month = 0; if (_dirLevelTraits[FolderLevelMonth].IsValidDir(_directoryProvider.GetLastPathComponent(monthDir), ref month)) { IRepoFileContainerDescriptor descrMonth = _dirLevelTraits[FolderLevelMonth].GetFolderDescriptorByRange( yearDir.Start.AddMonths(month - 1)); retval.Add(descrMonth); } } // foreach (string monthDir in monthDirs) } // if (Directory.Exists(this.RootPath)) return(retval); }
private void ProcessArchive(Archive archive, DateTime yesterday) { //add in older than yesterdays .log files var archiveFilesToCompress = _fileGatherer.FilesOlderThan(archive.FilePath, yesterday); if (!archiveFilesToCompress.Any()) { Log.Info($"No files found to be archived. FilePath: {archive.FilePath}"); return; } var fileListToCompress = _fileBatchProvider.Batch(archiveFilesToCompress); foreach (var filesToCompress in fileListToCompress) { var filesTo = _archiveNameProvider.DecideArchiveName(archive, yesterday); var filesFrom = filesToCompress.ToArray(); if (!_directoryProvider.Exists(archive.ArchivePath)) { Log.Warn($"Archive folder does not exist. Creating... {archive.ArchivePath}"); _directoryProvider.CreateDirectory(archive.ArchivePath); } var failed = _compressor.Compress(filesTo, filesFrom); //Deletes old logs if there were no error from the compression process if (!failed) { _fileDeleter.TryDeleteFiles(filesFrom, archive.DeleteArchivedFiles); } } }
public static bool Exists(string path) { if (_provider == null) { return(Directory.Exists(path)); } return(_provider.Exists(path)); }
private void DeleteAllFilesInResultsDirectory(string testResultsFolder) { if (_directoryProvider.Exists(testResultsFolder)) { var files = _directoryProvider.GetFiles(testResultsFolder); foreach (var file in files) { if (_fileProvider.IsWithExtension(file, "trx")) { _fileProvider.Delete(file); } } } }
private IEnumerable <TPluginService> LoadPlugins <TPluginService>() { string path = _pathProvider.Combine(_reflectionProvider.GetRunningAssemblyPath(), "Plugins"); if (!_directoryProvider.Exists(path)) { _directoryProvider.CreateDirectory(path); } var allAssemblies = new List <Assembly>(); foreach (string dll in _directoryProvider.GetFiles(path, "*.dll")) { allAssemblies.Add(_assemblyProvider.LoadFile(dll)); } var configuration = new ContainerConfiguration().WithAssemblies(allAssemblies); var container = configuration.CreateContainer(); var services = container.GetExports <TPluginService>(); return(services); }
private void InitializeSampleTemplate() { var templateContent = new EmbeddedFileProvider().GetContentsForFile("Commands.InitContent.Sample.template"); var folder = "Templates"; var fileName = "Sample.template"; var folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, folder); var templatePath = Path.Combine(folderPath, fileName); if (!_directoryProvider.Exists(folderPath)) { _directoryProvider.CreateDirectory(folderPath); } if (_fileProvider.Exists(templatePath)) { _logger.Info($"{folder}/{fileName} file already exists."); } else { File.WriteAllText(templatePath, templateContent); _logger.Info($"{fileName} created under the {folder} folder."); } }
private async Task <string> ExecuteTestsWithNativeRunnerAsync( string workingDir, string testsLibraryPath, string assemblyName, bool runInParallel, int maxParallelProcessesCount, string nativeArguments, int testAgentRunTimeout, bool isTimeBasedBalance, bool sameMachineByClass, List <TestCase> distributedTestCases, CancellationTokenSource outerCancellationTokenSource) { var outputFilesDir = _pathProvider.GetDirectoryName(testsLibraryPath); var availableCores = runInParallel ? maxParallelProcessesCount : 1; // Merge logic of CreateRunFilterArgument here. remove inner foreach var listOfDistributedTestCases = _nativeTestsRunner.SplitTestCases(availableCores, sameMachineByClass, distributedTestCases); var testRunsToBeMerged = new List <object>(); var testRunProcesses = new List <Process>(); var resultsFiles = new List <string>(); var processCreationTime = _dateTimeProvider.GetCurrentTime(); foreach (var distributedTestCasesList in listOfDistributedTestCases) { var currentTestResultsFilePath = _pathProvider.GetTempFileName(); resultsFiles.Add(currentTestResultsFilePath); var arguments = _nativeTestsRunner.BuildNativeRunnerArguments( assemblyName, testsLibraryPath, distributedTestCasesList, currentTestResultsFilePath, outputFilesDir, nativeArguments); var currentProcess = _processStarter.InitializeProcess( _nativeTestsRunner.RunnerFile, workingDir, arguments, LogStandardOutput, LogErrorOutput); testRunProcesses.Add(currentProcess); } var innerCancellationTokenSource = new CancellationTokenSource(); var waitForNativeRunnerProcessesToFinishTask = _taskProvider.StartNewLongRunning( (c) => { var ranProcesses = new List <int>(); do { var coresCount = availableCores; if (runInParallel) { foreach (var process in testRunProcesses) { if (coresCount == 0) { break; } if (!ranProcesses.Contains(process.GetHashCode())) { _processStarter.StartProcess(process, LogStandardOutput, LogErrorOutput); Thread.Sleep(500); ranProcesses.Add(process.GetHashCode()); coresCount--; } } } foreach (var process in testRunProcesses) { if (outerCancellationTokenSource.Token.IsCancellationRequested) { return; } if (!runInParallel) { // Start processes one by one, otherwise they are started all upfront. _processStarter.StartProcess(process, LogStandardOutput, LogErrorOutput); ranProcesses.Add(process.GetHashCode()); } if (ranProcesses.Contains(process.GetHashCode())) { _processStarter.WaitForProcessToFinish(testAgentRunTimeout, process); } } }while (ranProcesses.Count != testRunProcesses.Count); }, innerCancellationTokenSource); var checkCancellationRequestsTask = _taskProvider.StartNewLongRunningRepeating( innerCancellationTokenSource, () => { if (waitForNativeRunnerProcessesToFinishTask.IsCompleted || waitForNativeRunnerProcessesToFinishTask.IsFaulted) { if (waitForNativeRunnerProcessesToFinishTask.IsFaulted) { _testRunLogService.CreateTestRunLogAsync($"waitForNativeRunnerProcessesToFinishTask FAULTED- {waitForNativeRunnerProcessesToFinishTask.Exception}", _currentTestRunId).Wait(); } innerCancellationTokenSource.Cancel(); } else if (outerCancellationTokenSource.Token.IsCancellationRequested) { foreach (var processName in _nativeTestsRunner.RunnerProcessesNamesToKill) { var processes = Process.GetProcessesByName(processName); foreach (var process in processes) { try { if (process.StartTime > processCreationTime) { process.Kill(); process.WaitForExit(); } } catch (Exception e) { _consoleProvider.WriteLine(e.ToString()); } } } } }, 500); checkCancellationRequestsTask.Wait(); string result = null; if (!outerCancellationTokenSource.Token.IsCancellationRequested) { foreach (var testResultFile in resultsFiles) { if (_fileProvider.Exists(testResultFile)) { var testTestResults = _fileProvider.ReadAllText(testResultFile); var currentTestRun = _nativeTestsRunner.DeserializeTestResults(testTestResults); testRunsToBeMerged.Add(currentTestRun); } else if (_directoryProvider.Exists(testResultFile)) { // TODO: Added this because of the Protractor Plugin, since it produces folder instead of file. // Fix it later with something more generic solution. Maybe, we have to save the test results to plugin folder and // add a plugin method for deleting them at the end. var files = _directoryProvider.GetFiles(testResultFile); if (!files.Any()) { throw new Exception("No test results' file was produced for test run"); } var firstFile = _directoryProvider.GetFiles(testResultFile).First(); var testTestResults = _fileProvider.ReadAllText(firstFile); var currentTestRun = _nativeTestsRunner.DeserializeTestResults(testTestResults); testRunsToBeMerged.Add(currentTestRun); } else { throw new Exception("No test results' file was produced for test run"); } } var mergedTestRun = _nativeTestsRunner.MergeTestResults(testRunsToBeMerged); if (isTimeBasedBalance) { var testCaseRuns = _nativeTestsRunner.UpdateTestCasesHistory(mergedTestRun, assemblyName); await _testCasesHistoryService.UpdateTestCaseExecutionHistoryAsync(testCaseRuns).ConfigureAwait(false); } try { _nativeTestsRunner.ExecutePostRunActions(); } catch (Exception ex) { _testRunLogService.CreateTestRunLogAsync($"There was a problem executing ExecutePostRunActions on {Environment.MachineName}. Exception: {ex}", _currentTestRunId).Wait(); } result = mergedTestRun; } return(result); }
private async Task <string> ExecuteTestsWithNativeRunnerAsync( string workingDir, string testsLibraryPath, string assemblyName, bool runInParallel, int maxParallelProcessesCount, string nativeArguments, int testAgentRunTimeout, bool isTimeBasedBalance, bool sameMachineByClass, List <TestCase> distributedTestCases, CancellationTokenSource outerCancellationTokenSource) { string outputFilesDir = _pathProvider.GetDirectoryName(testsLibraryPath); ////var updatedMaxParallelProcessesCount = maxParallelProcessesCount > 1 ? maxParallelProcessesCount - 1 : maxParallelProcessesCount; int availableCores = runInParallel ? maxParallelProcessesCount : 1; // Merge logic of CreateRunFilterArgument here. remove inner foreach var listOfDistributedTestCases = _nativeTestsRunner.SplitTestCases(availableCores, sameMachineByClass, distributedTestCases); var testRunsToBeMerged = new List <object>(); var testRunProcesses = new List <Process>(); var resultsFiles = new List <string>(); DateTime processCreationTime = _dateTimeProvider.GetCurrentTime(); // DEBUG: ////await _testRunLogService.CreateTestRunLogAsync($"Number of tests to be executed- {distributedTestCases.Count}", _currentTestRunId); // DEBUG: ////await _testRunLogService.CreateTestRunLogAsync($"Number of listOfDistributedTestCases- {listOfDistributedTestCases.Count()}", _currentTestRunId); foreach (var distributedTestCasesList in listOfDistributedTestCases) { // DEBUG: ////await _testRunLogService.CreateTestRunLogAsync($"{index++} distributedTestCasesList items- {distributedTestCasesList.Count()}", _currentTestRunId); var currentTestResultsFilePath = _pathProvider.GetTempFileName(); resultsFiles.Add(currentTestResultsFilePath); var arguments = _nativeTestsRunner.BuildNativeRunnerArguments( assemblyName, testsLibraryPath, distributedTestCasesList, currentTestResultsFilePath, outputFilesDir, nativeArguments); var currentProcess = _processStarter.InitializeProcess( _nativeTestsRunner.RunnerFile, workingDir, arguments, LogStandardOutput, LogErrorOutput); testRunProcesses.Add(currentProcess); } var innerCancellationTokenSource = new CancellationTokenSource(); // DEBUG: ////await _testRunLogService.CreateTestRunLogAsync($"The native runs will be executed in parallel- {runInParallel}. Available core on machine {Environment.MachineName}- {availableCores}", _currentTestRunId); ////await _testRunLogService.CreateTestRunLogAsync($"Number of native test runner process to be run- {testRunProcesses.Count}", _currentTestRunId); var waitForNativeRunnerProcessesToFinishTask = _taskProvider.StartNewLongRunning( (c) => { var ranProcesses = new List <int>(); do { var coresCount = availableCores; if (runInParallel) { foreach (var process in testRunProcesses) { if (coresCount == 0) { break; } if (!ranProcesses.Contains(process.GetHashCode())) { // Set when the last run started. _environmentService.SetEnvironmentVariable("Meissa_LRDR", _dateTimeProvider.GetCurrentTimeUtc().ToString()); ////_consoleProvider.WriteLine("SET Meissa_LRDR"); _processStarter.StartProcess(process, LogStandardOutput, LogErrorOutput); Thread.Sleep(1000); ranProcesses.Add(process.GetHashCode()); coresCount--; } } } // Do not start all processes upfront // if parallel here start all of them? // run in task and pass cancelation token. If is canceled kill all processes. DO IT for NUNIT TOO foreach (var process in testRunProcesses) { if (outerCancellationTokenSource.Token.IsCancellationRequested) { return; } if (!runInParallel) { // Start processes one by one, otherwise they are started all upfront. _environmentService.SetEnvironmentVariable("Meissa_LRDR", _dateTimeProvider.GetCurrentTimeUtc().ToString()); ////_consoleProvider.WriteLine("SET Meissa_LRDR"); _processStarter.StartProcess(process, LogStandardOutput, LogErrorOutput); ranProcesses.Add(process.GetHashCode()); } if (ranProcesses.Contains(process.GetHashCode())) { _processStarter.WaitForProcessToFinish(testAgentRunTimeout, process); } } // DEBUG: ////_testRunLogService.CreateTestRunLogAsync($"ranProcesses.Count {ranProcesses.Count} testRunProcesses.Count {testRunProcesses.Count} {_dateTimeProvider.GetCurrentTime()}", _currentTestRunId).Wait(); }while (ranProcesses.Count != testRunProcesses.Count); }, innerCancellationTokenSource); var checkCancellationRequestsTask = _taskProvider.StartNewLongRunningRepeating( innerCancellationTokenSource, () => { if (waitForNativeRunnerProcessesToFinishTask.IsCompleted || waitForNativeRunnerProcessesToFinishTask.IsFaulted) { if (waitForNativeRunnerProcessesToFinishTask.IsFaulted) { _testRunLogService.CreateTestRunLogAsync($"waitForNativeRunnerProcessesToFinishTask FAULTED- {waitForNativeRunnerProcessesToFinishTask.Exception}", _currentTestRunId).Wait(); } innerCancellationTokenSource.Cancel(); } else if (outerCancellationTokenSource.Token.IsCancellationRequested) { foreach (var processName in _nativeTestsRunner.RunnerProcessesNamesToKill) { var processes = Process.GetProcessesByName(processName); foreach (var process in processes) { try { if (process.StartTime > processCreationTime) { process.Kill(); process.WaitForExit(); } } catch (Exception e) { _consoleProvider.WriteLine(e.ToString()); } } } } }, 5000); checkCancellationRequestsTask.Wait(); string result = null; if (!outerCancellationTokenSource.Token.IsCancellationRequested) { // DEBUG: ////_testRunLogService.CreateTestRunLogAsync($"START MERGING RESULTS- on machine {Environment.MachineName}", _currentTestRunId).Wait(); foreach (var testResultFile in resultsFiles) { if (_fileProvider.Exists(testResultFile)) { var testTestResults = _fileProvider.ReadAllText(testResultFile); var currentTestRun = _nativeTestsRunner.DeserializeTestResults(testTestResults); testRunsToBeMerged.Add(currentTestRun); } else if (_directoryProvider.Exists(testResultFile)) { // TODO: Added this because of the Protractor Plugin, since it produces folder instead of file. // Fix it later with something more generic solution. Maybe, we have to save the test results to plugin folder and // add a plugin method for deleting them at the end. var files = _directoryProvider.GetFiles(testResultFile); if (!files.Any()) { throw new Exception("No test results' file was produced for test run"); } var firstFile = _directoryProvider.GetFiles(testResultFile).First(); var testTestResults = _fileProvider.ReadAllText(firstFile); var currentTestRun = _nativeTestsRunner.DeserializeTestResults(testTestResults); testRunsToBeMerged.Add(currentTestRun); } else { throw new Exception("No test results' file was produced for test run"); } } var mergedTestRun = _nativeTestsRunner.MergeTestResults(testRunsToBeMerged); if (isTimeBasedBalance) { // DEBUG: ////var startTime = _dateTimeProvider.GetCurrentTime(); ////_testRunLogService.CreateTestRunLogAsync($"START updating test case history- on machine {Environment.MachineName}", _currentTestRunId).Wait(); var testCaseRuns = _nativeTestsRunner.UpdateTestCasesHistory(mergedTestRun, assemblyName); await _testCasesHistoryService.UpdateTestCaseExecutionHistoryAsync(testCaseRuns); // DEBUG: ////var endTime = _dateTimeProvider.GetCurrentTime(); ////_testRunLogService.CreateTestRunLogAsync($"END updating test case history- on machine {Environment.MachineName} for {(endTime - startTime).Seconds} seconds", _currentTestRunId).Wait(); } try { _nativeTestsRunner.ExecutePostRunActions(); } catch (Exception ex) { _testRunLogService.CreateTestRunLogAsync($"There was a problem executing ExecutePostRunActions on {Environment.MachineName}. Exception: {ex}", _currentTestRunId).Wait(); } result = mergedTestRun; } return(result); }