示例#1
0
文件: TaskJobMap.cs 项目: wangn6/rep2
        public static bool CreateMap(TaskJobMap instance)
        {
            try
            {
                using (ES1AutomationEntities context = new Core.Model.ES1AutomationEntities())
                {
                    context.TaskJobMaps.Add(instance);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                ATFEnvironment.Log.logger.Error(e);
                return false;
            }

            return true;
        }
示例#2
0
文件: TaskJobMap.cs 项目: wangn6/rep2
        public static bool Delete(TaskJobMap instance)
        {
            try
            {
                using (ES1AutomationEntities context = new Core.Model.ES1AutomationEntities())
                {
                    //if (!context.TaskJobMaps.Contains(instance))
                    //    return false;
                    context.TaskJobMaps.Attach(instance);

                    context.TaskJobMaps.Remove(instance);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                ATFEnvironment.Log.logger.Error(e);
                return false;
            }

            return true;
        }
示例#3
0
文件: Core.DTO.cs 项目: wangn6/rep2
        /// <summary>
        /// Invoked when <see cref="ToEntity"/> operation is about to return.
        /// </summary>
        /// <param name="entity"><see cref="TaskJobMap"/> converted from <see cref="TaskJobMapDTO"/>.</param>
partial         static void OnEntity(this TaskJobMapDTO dto, TaskJobMap entity);
示例#4
0
文件: Core.DTO.cs 项目: wangn6/rep2
        /// <summary>
        /// Converts this instance of <see cref="TaskJobMapDTO"/> to an instance of <see cref="TaskJobMap"/>.
        /// </summary>
        /// <param name="dto"><see cref="TaskJobMapDTO"/> to convert.</param>
        public static TaskJobMap ToEntity(this TaskJobMapDTO dto)
        {
            if (dto == null) return null;

            var entity = new TaskJobMap();

            entity.MapId = dto.MapId;
            entity.TaskId = dto.TaskId;
            entity.JobId = dto.JobId;

            dto.OnEntity(entity);

            return entity;
        }
示例#5
0
        /// <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);
                }
            }
        }
示例#6
0
        /// <summary>
        /// Monitor scheduled tasks
        /// </summary>
        public static void MonitorScheduledTasks()
        {
            string message = string.Empty;
            ATFEnvironment.Log.logger.Debug("Monitor scheduled tasks");
            List<AutomationTask> tasksScheduled = AutomationTask.GetActiveAutomationTask((int)TaskStatus.Scheduled);
            if (tasksScheduled == null)
            {
                ATFEnvironment.Log.logger.Debug("Scheduled tasks count: 0");
                return;
            }
            ATFEnvironment.Log.logger.Debug("Scheduled tasks count:" + tasksScheduled.Count().ToString());
            foreach (AutomationTask task in tasksScheduled)
            {
                if (task.RecurrencePattern == (int)RecurrencePattern.AtOnce)
                {
                    // Check for Build
                    message = "Checking specific build for Task: " + task.Name.ToString();
                    ATFEnvironment.Log.logger.Info(message);
                    task.AddProgressInformation(message);
                    if (!GetBuildAndCheckStatusOfItForTask(task))
                    {
                        continue;
                    }

                    // Check for Enviroment
                    ATFEnvironment.Log.logger.Info("Checking specific Enviromnet for Task: " + task.Name.ToString());
                    if (!GetSupporttedEnvironmentAndCheckStatusOfItForTask(task))
                    {
                        continue;
                    }

                    // Get all the test cases for this task, update the test suite(id=task.TestContent) to contains only test cases
                    // The test suite's content may be updated in future, so here we convert the sub test suites into test cases
                    // This is to handle the scenario that after the task is finished, when user look the historical test result, the test case of the task should not be modified.
                    TestSuite testSuite = TestSuite.GetTestSuite(int.Parse(task.TestContent));
                    // Only run the active case
                    List<int> allCasesForTask = null;
                    while (true)
                    {
                        if (!ATFConfiguration.IsTestCasesSuitesSyncing())
                        {
                            List<TestCase> testCases = TestSuite.GetAllCases(testSuite.SuiteId, true);
                            allCasesForTask = testCases.Select(tc => tc.TestCaseId).ToList();
                            break;
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(30 * 1000);
                            task.AddProgressInformation(string.Format("Test cases or suites are being syncing now. Wait 30 seconds."));
                        }
                    }

                    if (allCasesForTask == null || allCasesForTask.Count() <= 0 && testSuite.Type != (int)SuiteType.NotExisting)
                    {
                        ATFEnvironment.Log.logger.Error("No cases to run in Task [" + task.Name + @"], suiteId= " + task.TestContent.ToString());
                        task.AddProgressInformation("No cases to run.");
                        task.SetTaskStatus(TaskStatus.Complete);
                        continue;
                    }
                    else if (testSuite.Type == (int)SuiteType.NotExisting)
                    {
                        try                         // create one job
                        {
                            AutomationJob job = new AutomationJob()
                            {
                                Name = "JobForTask " + task.TaskId.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,
                                ModifyBy = 0,
                            };
                            AutomationJob bCreateJob = AutomationJob.CreateJob(job);

                            message = string.Format("Job [{0}:{1}] is created.", bCreateJob.JobId, bCreateJob.Name);
                            task.AddProgressInformation(message);
                            ATFEnvironment.Log.logger.Info(message);

                            TaskJobMap map = new TaskJobMap()
                            {
                                TaskId = task.TaskId,
                                JobId = job.JobId,
                            };
                            TaskJobMap.CreateMap(map);

                            task.SetTaskStatus(TaskStatus.Dispatched);
                            message = string.Format("Change Task [{0}] status Scheduled to Dispatched", task.Name);
                            ATFEnvironment.Log.logger.Info(message);
                            task.AddProgressInformation(message);
                        }
                        catch (Exception ex)
                        {
                            message = string.Format("Failed to dispatch task [{0}] to jobs.", task.Name);
                            ATFEnvironment.Log.logger.Error(message, ex);
                            task.AddProgressInformation(message);
                            foreach (AutomationJob job in task.GetJobs())
                            {
                                job.SetJobsStatus(JobStatus.Cancelled);
                            }
                            task.SetTaskStatus(TaskStatus.Failed);
                        }
                    }
                    else
                    {
                        message = string.Format("Totally {0} cases selected.", allCasesForTask.Count);
                        task.AddProgressInformation(message);
                        ATFEnvironment.Log.logger.Info(message);
                        allCasesForTask = allCasesForTask.Distinct().ToList();
                        message = string.Format("Totally {0} distinct cases selected after dedupe.", allCasesForTask.Count);
                        task.AddProgressInformation(message);
                        ATFEnvironment.Log.logger.Info(message);
                        //take the snapshot of the test cases and save it to the task
                        string testCaseIdList = string.Empty;
                        foreach (int caseId in allCasesForTask)
                        {
                            if (string.IsNullOrEmpty(testCaseIdList))
                            {
                                testCaseIdList = caseId.ToString();
                            }
                            else
                            {
                                testCaseIdList = string.Format("{0},{1}", testCaseIdList, caseId);
                            }
                        }

                        // For temporary suite, we'll modify the suite in place, else we'll create another temporary suite to contain the cases at present in the suite.
                        if (testSuite.Type == (int)SuiteType.Temporary)
                        {
                            testSuite.SubSuites = "";
                            testSuite.TestCases = testCaseIdList;
                            testSuite.Update();
                        }
                        else
                        {
                            TestSuite tempSuite = new TestSuite
                            {
                                ProviderId = testSuite.ProviderId,
                                SourceId = testSuite.SourceId,
                                Name = testSuite.Name,
                                Type = (int)SuiteType.Temporary,
                                SubSuites = "",
                                TestCases = testCaseIdList,
                                IsActive = true,
                                CreateBy = 0,
                                ModityBy = 0,
                                Description = testSuite.Description,
                                CreateTime = System.DateTime.UtcNow,
                                ModifyTime = System.DateTime.UtcNow,
                            };
                            TestSuite newSuite = TestSuite.CreateSuite(tempSuite);
                            task.SetTestContent(newSuite.SuiteId.ToString());
                        }

                        // Remove the test case which is not suitable for the support environment
                        allCasesForTask = allCasesForTask.FindAll(delegate(int caseId)
                        {
                            if (CouldThisTestCaseRunOnSupporttedEnvironment(caseId, task.EnvironmentId))
                            {
                                return true;
                            }
                            else
                            {
                                TestCase testCase = TestCase.GetTestCase(caseId);
                                SupportedEnvironment testEnvironment = SupportedEnvironment.GetSupportedEnvironmentById(task.EnvironmentId);
                                message = string.Format(@"Test case [{0}](Platform={1}) could not run on the environment [{2}](Platform={3})", testCase.Name, testCase.GetPlatform().ToString(), testEnvironment.Name, testEnvironment.GetPlatformOfEnvironment().ToString());
                                ATFEnvironment.Log.logger.Warn(message);
                                task.AddProgressInformation(message);
                                return false;
                            }
                        });

                        // Dispatch to JOBs
                        message = string.Format("Dispatching to Jobs for Task [{0}]", task.Name);
                        ATFEnvironment.Log.logger.Info(message);
                        task.AddProgressInformation(message);

                        try
                        {
                            DispatchTaskToJobs(task, allCasesForTask);
                            task.SetTaskStatus(TaskStatus.Dispatched);
                            message = string.Format("Change Task [{0}] status Scheduled to Dispatched", task.Name);
                            ATFEnvironment.Log.logger.Info(message);
                            task.AddProgressInformation(message);
                        }
                        catch (Exception ex)
                        {
                            message = string.Format("Failed to dispatch task [{0}] to jobs.", task.Name);
                            ATFEnvironment.Log.logger.Error(message, ex);
                            task.AddProgressInformation(message);
                            foreach (AutomationJob job in task.GetJobs())
                            {
                                job.SetJobsStatus(JobStatus.Cancelled);
                            }
                            task.SetTaskStatus(TaskStatus.Failed);
                        }
                    }

                }
                else if (task.RecurrencePattern == (int)RecurrencePattern.OneTime)
                {
                    CreateOneTimeWindowsScheduledTask(task);
                    ATFEnvironment.Log.logger.Info("Change Task [" + task.Name.ToString() + "] status Scheduled to Scheduling");
                    task.AddProgressInformation("Change status Scheduled to Scheduling");
                    task.SetTaskStatus(TaskStatus.Scheduling);

                }
                else if (task.RecurrencePattern == (int)RecurrencePattern.Weekly)
                {
                    CreateWeeklyWindowsScheduledTask(task);
                    ATFEnvironment.Log.logger.Info("Change Task [" + task.Name.ToString() + "] status Scheduled to Scheduling");
                    task.AddProgressInformation("Change status Scheduled to Completed");
                    task.SetTaskStatus(TaskStatus.Scheduling);
                }
            }
        }