public override void RunTestCase(TestCaseExecution execution) { //get the test case source ids to be used by the Minitest string webId = execution.TestCase.SourceId; //update the execution status to be running SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of job {1} to running", execution.ExecutionId, Job2Run.JobId)); execution.SetStatus(ExecutionStatus.Running); string scriptRootFolderForProject = scriptRootFolder + @"\" + vcsRootPath + @"\RubyMinitest"; string scriptFilePath = GetTestScriptFilesnFolderContainTestCaseWithWebId(scriptRootFolderForProject, webId); if (string.IsNullOrEmpty(scriptFilePath)) { SaberAgent.Log.logger.Info(string.Format("Could not find the test script for the test execution {0} of job {1}", execution.ExecutionId, Job2Run.JobId)); execution.SetStatus(ExecutionStatus.Fail); } else { try { string logfilePath = string.Format(@"{0}\webid_{1}.log", this.RootResultPath, execution.TestCase.SourceId); string testcasePattern = string.Format(@"/_webid_{0}/", webId); string parameters = string.Format(@"{0} ""{1}"" ""{2}"" ""{3}""", testcasePattern, scriptFilePath, scriptRootFolderForProject, logfilePath); string runBATFile = @"C:\SaberAgent\RunRubyMiniTest.bat"; string command = string.Format(@"{0} /C {1}", runBATFile, parameters); SaberAgent.Log.logger.Info(command); string result = CMDScript.RumCmdWithWindowsVisible(runBATFile, parameters); SaberAgent.Log.logger.Info(result); SaberAgent.Log.logger.Info(string.Format("Execution of test case has been finished for job {0}", Job2Run.JobId)); string defaultMiniTestResultFile = string.Format(@"{0}\test\reports\_webid_{1}.xml", scriptRootFolderForProject, webId); //copy related files to Result folder FileHelper.CopyFile(@"C:\SaberAgent\Config\Environment.xml", this.RootResultPath, true); if (FileHelper.IsExistsFile(defaultMiniTestResultFile)) { FileHelper.CopyFile(defaultMiniTestResultFile, this.RootResultPath, true); } else { string message = string.Format("Could not find the result file for case [{0}] at location [{1}]", execution.TestCase.Name, defaultMiniTestResultFile); SaberAgent.Log.logger.Error(message); Job2Run.AddJobProgressInformation(message); execution.SetStatus(ExecutionStatus.Fail); return; } //Parse the test result SaberAgent.Log.logger.Info(string.Format("Start to parse the result for execution {0} of job {1}", execution.ExecutionId, Job2Run.JobId)); resultParser.ParseResult(string.Format(@"{0}\_webid_{1}.xml", this.RootResultPath, webId), Job2Run.JobId); SaberAgent.Log.logger.Info(string.Format("Result parsing has been completed for execution {0} of job {1}", execution.ExecutionId, Job2Run.JobId)); //set execution status to complete SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of job {1} to Compleate", execution.ExecutionId, Job2Run.JobId)); execution.SetStatus(ExecutionStatus.Complete); } catch (Exception ex) { execution.SetStatus(ExecutionStatus.Fail); string error = string.Format("Failed to run the execution {0}", execution.ExecutionId); SaberAgent.Log.logger.Error(error, ex); } } }
public static TestCaseExecution CreateExecution(TestCaseExecution instance) { using (ES1AutomationEntities context = new Core.Model.ES1AutomationEntities()) { TestCaseExecution execution = context.TestCaseExecutions.Add(instance); context.SaveChanges(); return execution; } }
public override void RunTestCase(TestCaseExecution execution) { FileHelper.EmptyFolder(this.RootResultPath); string resultFileFullPath = this.RootResultPath + @"\" + execution.TestCase.SourceId.ToString() + @".xml"; SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of job {1} to running", execution.ExecutionId, Job2Run.JobId)); execution.SetStatus(ExecutionStatus.Running); //get the test case source ids to used by the NUnit console string testCaseIdList = "WebId=" + execution.TestCase.SourceId; string resultLogFilePath = string.Format(@"{0}\{1}_RunCSharpNUnitTest.bat.log", this.RootResultPath, execution.TestCase.SourceId); string parameters = string.Format(@"""{0}"" {1}.xml ""{2}"" {3}> ""{4}""", testCaseIdList, execution.TestCase.SourceId, this.RootResultPath, this.vcsRootFolder + @"/CSharpNUnit/Saber", resultLogFilePath); string runBATFile = @"C:\SaberAgent\RunCSharpNUnitTest.bat"; string command = string.Format(@"{0} /C {1}", runBATFile, parameters); SaberAgent.Log.logger.Info(command); string result = CMDScript.RumCmd(runBATFile, parameters); SaberAgent.Log.logger.Info(result); SaberAgent.Log.logger.Info(string.Format("Execution of test case [{0}] has been finished for job {1}", execution.TestCase.Name, Job2Run.JobId)); //Parse the test result SaberAgent.Log.logger.Info(string.Format("Start to parse the result for test Case [{0}]", execution.TestCase.Name)); FileHelper.CopyFile(@"C:\SaberAgent\Config\Environment.xml", this.RootResultPath, true); try { resultParser.ParseResult(resultFileFullPath, Job2Run.JobId); SaberAgent.Log.logger.Info(string.Format("Result parsing has been completed for test Case [{0}]", execution.TestCase.Name)); } catch (Exception ex) { execution.SetStatus(ExecutionStatus.Fail); SaberAgent.Log.logger.Error(string.Format("Met error when parse the result of test case [{0}]", execution.TestCase.Name), ex); return; } //set execution status to complete SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of test case {1} to Complete", execution.TestCase.Name, Job2Run.JobId)); execution.SetStatus(ExecutionStatus.Complete); }
public override void RunTestCase(TestCaseExecution execution) { try { AutomationTask at = AutomationJob.GetTaskOfJobByJobId(Job2Run.JobId); Project p = Project.GetProjectById((int)at.ProjectId); string codePath = scriptRootFolder + p.VCSRootPath + @"\"; string remoteCodePath = remoteLinuxAgentFolder + @"automation/"; SaberAgent.Log.logger.Debug(string.Format("SSHWrapper.CreateRemoteFolder({0}, {1}, {2}, {3})", remote.Server, remote.Admin, remote.Password, remoteCodePath)); SSHWrapper.CreateRemoteFolder(remote.Server, remote.Admin, remote.Password, remoteCodePath); if (p.VCSType != (int)Core.VCSType.NotSync) { SSHWrapper.CopyDirectoryToRemote(remote.Server, remote.Admin, remote.Password, codePath, remoteCodePath); } string command = "cd '" + remoteLinuxAgentFolder + "';" + this.executionCommand; SaberAgent.Log.logger.Debug(string.Format("SSHWrapper.RunCommand({0}, {1}, {2}, {3})", remote.Server, remote.Admin, remote.Password, command)); string res = SSHWrapper.RunCommand(remote.Server, remote.Admin, remote.Password, command); SaberAgent.Log.logger.Info(string.Format("Test Cases have been executed, result is {0}", res)); SaberAgent.Log.logger.Debug(string.Format("SSHWrapper.CopyDirectoryFromRemoteToLocal({0}, {1}, {2}, {3}, {4});", remote.Server, remote.Admin, remote.Password, RootResultPath, remoteLogFolder)); SSHWrapper.CopyDirectoryFromRemoteToLocal(remote.Server, remote.Admin, remote.Password, RootResultPath, remoteLogFolder); SaberAgent.Log.logger.Debug(string.Format("SSHWrapper.CopyDirectoryFromRemoteToLocal({0}, {1}, {2}, {3}, {4});", remote.Server, remote.Admin, remote.Password, RootResultPath, remoteReportFolder)); SSHWrapper.CopyDirectoryFromRemoteToLocal(remote.Server, remote.Admin, remote.Password, RootResultPath, remoteReportFolder); //Parse the test result resultParser.ParseResult(RootResultPath, Job2Run.JobId); SaberAgent.Log.logger.Info(string.Format("Result parsing has been completed for execution {0} of job {1}", execution.ExecutionId, Job2Run.JobId)); //Clean the remote results and logs SSHWrapper.DeleteRemoteFolder(remote.Server, remote.Admin, remote.Password, remoteLogFolder); SSHWrapper.DeleteRemoteFolder(remote.Server, remote.Admin, remote.Password, remoteReportFolder); } catch (Exception e) { string message = string.Format("Failed to result parsing {0}, result Path {1}", this.remote.Server, RootResultPath); SaberAgent.Log.logger.Error(message, e); } }
private void RunAllTestCases() { string message = string.Empty; AutomationTask at = AutomationJob.GetTaskOfJobByJobId(job2Run.JobId); string rootResultPath = GetLocalResultFolderForJob(); //Currently, the NotExisting suite type is used by CIS and DPSearch, thus the scripts will run on remote linux machine. if (TestSuite.GetTestSuite(int.Parse(at.TestContent)).Type == (int)SuiteType.NotExisting) { //Neil, TO DO NEXT WEEK!!!!! //TODO, refine the logic to determine whether the handler should be remote minitest execution handler TestCaseExecution useless = new TestCaseExecution(); SaberTestExecutionHandler handler = new Runtime_RubyMinitest.RemoteRubyMiniTestExecutionHandler(job2Run, rootResultPath); handler.RunTestCase(useless); } else { List<TestCaseExecution> executions = TestCaseExecution.GetTestCaseExectionByJob(job2Run.JobId).ToList(); //This is the folder where the test result will copy to. And at last, the result files will be copied to central file server. foreach (TestCaseExecution ex in executions) { // check the precondition for each execution SaberAgent.Log.logger.Info(string.Format("Check precondition for execution {0} of job {1}", ex.ExecutionId, job2Run.JobId)); productDeploymentHelper.CheckPreconditionForEachTestExecution(TestEnvironment.GetEnvironmentById(job2Run.SUTEnvironmentId.Value)); //update the execution status to be running SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of job {1} to running", ex.ExecutionId, job2Run.JobId)); ex.SetStatus(ExecutionStatus.Running); TestCase testCase = TestCase.GetTestCase(ex.TestCaseId); message = string.Format("Start Execution of test case [{0}:{1}]", testCase.SourceId, testCase.Name); job2Run.AddJobProgressInformation(message); Runtime runtime = testCase.GetRuntime(); if (runtime == Runtime.CSharpNUnit) { SaberTestExecutionHandler handler = new CSharpNUnitTestExecutionHandler(job2Run, rootResultPath, this.project); handler.RunTestCase(ex); } else if (runtime == Runtime.RubyMiniTest) { SaberTestExecutionHandler handler = new RubyMiniTestExecutionHandler(job2Run, rootResultPath, this.project); handler.RunTestCase(ex); } else if (runtime == Runtime.CSharpMSUnit) { SaberTestExecutionHandler handler = new CSharpMSUnitTestExecutionHandler(job2Run, rootResultPath, this.project); handler.RunTestCase(ex); } else { message = string.Format("The runtime of test case [{0}] is not specified.", ex.TestCase.Name); SaberAgent.Log.logger.Error(message); job2Run.AddJobProgressInformation(message); ex.SetStatus(ExecutionStatus.Fail); } message = string.Format("End execution of test case [{0}:{1}]", testCase.SourceId, testCase.Name); job2Run.AddJobProgressInformation(message); } }//end of else productDeploymentHelper.CollectProductionLogsAfterExecutionToLocal(TestEnvironment.GetEnvironmentById(job2Run.SUTEnvironmentId.Value), rootResultPath); }
public abstract void RunTestCase(TestCaseExecution execution);
private void RunAutomationCase(TestCaseExecution testCaseExecution, CancellationToken token) { ATFEnvironment.Log.logger.Info("Run automation case #" + testCaseExecution.TestCaseId); testCaseExecution.SetStatus(ExecutionStatus.Running); var result = new TestResult { ExecutionId = testCaseExecution.ExecutionId, Result = (int)ResultType.NotRun, IsTriaged = false, TriagedBy = null, Files = null, }; TestResult.CreateRunResult(result); try { // todo: call agent to run test cases // change test result Thread.Sleep(60 * 1000); testCaseExecution.SetStatus(ExecutionStatus.Complete); result.SetResult(ResultType.Pass); if (token.IsCancellationRequested) { ATFEnvironment.Log.logger.Info("Timeout to run automation case #" + testCaseExecution.TestCaseId); testCaseExecution.SetStatus(ExecutionStatus.Complete); result.SetResult(ResultType.TimeOut); } } catch (EnvironmentException ex) { string errorMsg = string.Format("Failed to run automation case #{0}, error: {1}", testCaseExecution.TestCaseId, ex.Message); ATFEnvironment.Log.logger.Error(errorMsg); testCaseExecution.SetStatus(ExecutionStatus.Fail); result.SetResult(ResultType.Exception); } catch (Exception ex) { string errorMsg = string.Format("Failed to run automation case #{0}, error: {1}", testCaseExecution.TestCaseId, ex.Message); ATFEnvironment.Log.logger.Error(errorMsg); testCaseExecution.SetStatus(ExecutionStatus.Fail); result.SetResult(ResultType.Failed); } }
public override void ParseResult(string resultFilePath, int jobId) { string[] files = System.IO.Directory.GetFiles(resultFilePath); try { foreach (string resultPath in files) { string fi = Path.GetFileNameWithoutExtension(resultPath); XmlDocument document = new XmlDocument(); document.Load(resultPath); XmlNodeList testCaseResultList = document.SelectNodes("//testsuite/testcase"); foreach (XmlNode testCaseResult in testCaseResultList) { try { string caseName = GetTestCase(testCaseResult); //get name from xml string failureInfo = GetFailureInfo(testCaseResult); Product product = AutomationJob.GetProductOfJobByJobId(jobId); TestCase testcase = TestCase.GetTestCaseByName(caseName); if (testcase == null) { TestCase tc = new TestCase() { ProviderId = 2,//ATFTestCaseProvider Name = caseName, ProductId = product.ProductId, SourceId = "-1", // set a default value to sign as a not existing case Feature = "Galaxy automation", Weight = 100, IsAutomated = true, IsActive = false, }; testcase = TestCase.CreateCase(tc); } TestCaseExecution ex = new TestCaseExecution() { TestCaseId = testcase.TestCaseId, JobId = jobId, Status = (int)ExecutionStatus.NotRunning, StartTime = null, EndTime = null, Info = fi, RetryTimes = 0, Timeout = ATFEnvironment.DefaultTestCaseTimeout, }; TestCaseExecution exInDB = TestCaseExecution.CreateExecution(ex); SaberAgent.Log.logger.Info(string.Format("Start to parse the result for execution {0} of job {1}", exInDB.ExecutionId, jobId)); ResultType result = GetResult(testCaseResult); TestResult re = new TestResult { ExecutionId = exInDB.ExecutionId, Result = (int)result, IsTriaged = false, TriagedBy = null, Description = string.IsNullOrEmpty(failureInfo) ? "" : failureInfo, Files = resultPath, }; TestResult resultInDB = TestResult.CreateRunResult(re); SaberAgent.Log.logger.Info(string.Format("Set status of execution {0} of job {1} to Compleate", exInDB.ExecutionId, jobId)); exInDB.SetStatus(ExecutionStatus.Complete); } catch (Exception e) { SaberAgent.Log.logger.Error(e); } } } } catch (Exception ex) { SaberAgent.Log.logger.Error(ex); } try { CopyResultFilesToServer(resultFilePath, jobId); } catch (Exception ex) { SaberAgent.Log.logger.Error(ex); } }
/// <summary> /// Invoked when <see cref="ToEntity"/> operation is about to return. /// </summary> /// <param name="entity"><see cref="TestCaseExecution"/> converted from <see cref="TestCaseExecutionDTO"/>.</param> partial static void OnEntity(this TestCaseExecutionDTO dto, TestCaseExecution entity);
/// <summary> /// Converts this instance of <see cref="TestCaseExecutionDTO"/> to an instance of <see cref="TestCaseExecution"/>. /// </summary> /// <param name="dto"><see cref="TestCaseExecutionDTO"/> to convert.</param> public static TestCaseExecution ToEntity(this TestCaseExecutionDTO dto) { if (dto == null) return null; var entity = new TestCaseExecution(); entity.ExecutionId = dto.ExecutionId; entity.TestCaseId = dto.TestCaseId; entity.JobId = dto.JobId; entity.Status = dto.Status; entity.StartTime = DateTime.Parse(dto.StartTime); entity.EndTime = DateTime.Parse(dto.EndTime); entity.RetryTimes = dto.RetryTimes; entity.Info = dto.Info; dto.OnEntity(entity); return entity; }
/// <summary> /// Dispatch task to jobs based on the test suites defined for dependance /// </summary> /// <param name="task"></param> /// <param name="allCasesForTask"></param> private static void DispatchTaskToJobs(AutomationTask task, List<int> allCasesForTask) { int i = 0; string message = string.Empty; //Store whether the test cases have been dispatched to job or not based on dependency test suite Dictionary<string, bool> testCaseDispatchIndicator = new Dictionary<string, bool>(); int testcaseProviderId = Product.GetProductByID(task.ProductId.GetValueOrDefault()).TestCaseProviderId.GetValueOrDefault(); List<TestSuite> dependenceSuites = TestSuite.GetTestSuitesByProviderIdAndType(testcaseProviderId, SuiteType.Dependence).ToList(); foreach (TestSuite dependenceSuite in dependenceSuites) { string testCasesForJob = string.Empty; foreach (string tc in dependenceSuite.TestCases.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { if (allCasesForTask.Contains(int.Parse(tc)) && !testCaseDispatchIndicator.ContainsKey(tc)) { testCasesForJob = testCasesForJob == string.Empty ? tc : testCasesForJob + ',' + tc; testCaseDispatchIndicator.Add(tc, true); } } if (testCasesForJob != string.Empty) { // Create AutomationJob ++i; AutomationJob job = new AutomationJob() { Name = "JobForTask " + task.TaskId.ToString() + "_" + i.ToString(), Status = (int)JobStatus.Assigned, Type = (int)JobType.Sequence, Priority = task.Priority, RetryTimes = 1, Timeout = ATFConfiguration.GetIntValue("DefaultAutomationJobTimeout"),//1 hours CreateDate = System.DateTime.UtcNow, ModifyDate = System.DateTime.UtcNow, CreateBy = 0,//automation, pre-defined user ModifyBy = 0,//automation, pre-defined user }; AutomationJob bCreateJob = AutomationJob.CreateJob(job); message = string.Format("Job [{0}] is created.", bCreateJob.JobId); task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); TaskJobMap map = new TaskJobMap() { TaskId = task.TaskId, JobId = job.JobId, }; TaskJobMap.CreateMap(map); foreach (string id in testCasesForJob.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { TestCaseExecution ex = new TestCaseExecution() { TestCaseId = int.Parse(id), JobId = job.JobId, Status = (int)ExecutionStatus.NotRunning, StartTime = null, EndTime = null, RetryTimes = 0, Timeout = ATFEnvironment.DefaultTestCaseTimeout, }; TestCaseExecution exInDB = TestCaseExecution.CreateExecution(ex); message = string.Format("Execution [{0}] is created.", exInDB.ExecutionId); //task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); TestResult result = new TestResult { ExecutionId = exInDB.ExecutionId, Result = (int)ResultType.NotRun, IsTriaged = false, TriagedBy = null, Files = null, }; TestResult.CreateRunResult(result); message = string.Format("Test result [{0}] is created.", result.ResultId); //task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); } } } foreach (int testcaseId in allCasesForTask) { if (!testCaseDispatchIndicator.ContainsKey(testcaseId.ToString()))//not dispatched to jobs yet { // Create AutomationJob ++i; AutomationJob job = new AutomationJob() { Name = "JobForTask " + task.TaskId.ToString() + "_" + i.ToString(), Status = (int)JobStatus.Assigned, Type = (int)JobType.Sequence, Priority = task.Priority, RetryTimes = 1, Timeout = ATFConfiguration.GetIntValue("DefaultAutomationJobTimeout"),//1 hours CreateDate = System.DateTime.UtcNow, ModifyDate = System.DateTime.UtcNow, CreateBy = 0,//automation, pre-defined user ModifyBy = 0,//automation, pre-defined user }; AutomationJob bCreateJob = AutomationJob.CreateJob(job); message = string.Format("Job [{0}] is created.", bCreateJob.JobId); task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); TaskJobMap map = new TaskJobMap() { TaskId = task.TaskId, JobId = job.JobId, }; TaskJobMap.CreateMap(map); TestCaseExecution ex = new TestCaseExecution() { TestCaseId = testcaseId, JobId = job.JobId, Status = (int)ExecutionStatus.NotRunning, StartTime = null, EndTime = null, RetryTimes = 0, Timeout = ATFEnvironment.DefaultTestCaseTimeout, }; TestCaseExecution exInDB = TestCaseExecution.CreateExecution(ex); message = string.Format("Execution [{0}] is created.", exInDB.ExecutionId); //task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); TestResult result = new TestResult { ExecutionId = exInDB.ExecutionId, Result = (int)ResultType.NotRun, IsTriaged = false, TriagedBy = null, Files = null, }; TestResult.CreateRunResult(result); message = string.Format("Test result [{0}] is created.", result.ResultId); //task.AddProgressInformation(message); ATFEnvironment.Log.logger.Info(message); } } }