public ActionResult GetTestRunParametersForRobot(string RobotName)
        {
            if(RobotName == null)
            {
                return HttpNotFound();
            }

            var TestRunner = _context.TestRunner.Single(t => t.Name == RobotName);
            if (TestRunner == null)
            {
                return HttpNotFound();
            }

            var TestRunnerID = TestRunner.TestRunnerID;
            var trgID = TestRunner.TestRunnerGroupID;
            var Response = "No Test Run ready for robot";

            var i = 0;
            var TestFound = false;
            do
            {
                var ProjectsWithTestRunnerGroup = _context.Project.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var FoldersWithParentProjectWithTestRunnerGroup = _context.Folder.Where
                    (t => t.TestRunnerGroupID == null && ProjectsWithTestRunnerGroup.Any(l => t.ProjectID == l.ID)).ToList();

                var FoldersWithTestRunnerGroup = _context.Folder.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedFolders = FoldersWithParentProjectWithTestRunnerGroup.Union(FoldersWithTestRunnerGroup);

                var GroupsWithTestRunnerGroup = _context.Group.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedGroups = GroupsWithTestRunnerGroup.Union
                    (_context.Group.Where(t => t.TestRunnerGroupID == null && combinedFolders.Any(l => t.FolderID == l.FolderID)));

                var RunsWithTestRunnerGroup = _context.Run.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedRuns = RunsWithTestRunnerGroup.Union
                    (_context.Run.Where(t => t.TestRunnerGroupID == null && combinedGroups.Any(l => t.GroupID == l.GroupID)));

                var TestRunsWithTestRunnerGroups = _context.TestRun.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedTestRuns = TestRunsWithTestRunnerGroups.Union
                    (_context.TestRun.Where(t => t.TestRunnerGroupID == null && combinedRuns.Any(l => t.RunID == l.RunID)));

                var ActiveTestRuns = combinedTestRuns.Where(t => t.Status == "Ready" && t.StartTime < DateTime.Now
                && (t.Retries == null || t.RetriesLeft > 0) && t.TestRunner == null);

                if (ActiveTestRuns.Any())
                {
                    var PossibleCandidateForRun = ActiveTestRuns.First();
                    var RefreshedCandidate = _context.TestRun.SingleOrDefault(t => t.TestRunID == PossibleCandidateForRun.TestRunID);

                    if (RefreshedCandidate.TestRunner == null)
                    {
                        RefreshedCandidate.TestRunner = TestRunnerID;
                        _context.Update(RefreshedCandidate);
                        _context.SaveChanges();
                    }
                    else
                    {
                        //pick another candidate
                    }

                    //Check it's not been picked up elsewhere at near exact same time
                    var SecondaryilyRefreshedCandidate = _context.TestRun.SingleOrDefault(t => t.TestRunID == PossibleCandidateForRun.TestRunID);

                    if (SecondaryilyRefreshedCandidate.TestRunner == TestRunnerID)
                    {
                        TestFound = true;

                        if (SecondaryilyRefreshedCandidate.Retries != null)
                        {
                            SecondaryilyRefreshedCandidate.RetriesLeft = SecondaryilyRefreshedCandidate.RetriesLeft - 1;
                            _context.Update(SecondaryilyRefreshedCandidate);

                        }
                        Test test = _context.Test.Single(t => t.TestID ==
                            SecondaryilyRefreshedCandidate.TestID);

                        SecondaryilyRefreshedCandidate.Status = "Running";

                        CommandAndControlParameters commandAndControlParameters = new CommandAndControlParameters();
                        commandAndControlParameters.TestRunID = SecondaryilyRefreshedCandidate.TestRunID;
                        commandAndControlParameters.TestID = SecondaryilyRefreshedCandidate.TestID;
                        commandAndControlParameters.TestName = test.Name;
                        commandAndControlParameters.TestDataFilename = test.ExcelFilePath;
                        commandAndControlParameters.TestEnvironmentID = SecondaryilyRefreshedCandidate.TestEnvironmentID.Value;
                        commandAndControlParameters.Browser = SecondaryilyRefreshedCandidate.Browser;
                        commandAndControlParameters.TestEnvironmentFilename = _context.TestEnvironment.Single(t => t.TestEnvironmentID ==
                            SecondaryilyRefreshedCandidate.TestEnvironmentID).XMLFilePath;
                        commandAndControlParameters.ClearRemoteLogOnNextAccess = TestRunner.ClearRemoteLogOnNextAccess;

                        Response = JsonConvert.SerializeObject(commandAndControlParameters);

                        _context.SaveChanges();

                        break;
                    }
                }

                i++;
            } while (TestFound && i < 10);

            return new ObjectResult(Response);
        }
Exemplo n.º 2
0
        public ActionResult GetTestRunParametersForRobot(string RobotName)
        {
            if (RobotName == null)
            {
                return(HttpNotFound());
            }

            var TestRunner = _context.TestRunner.Single(t => t.Name == RobotName);

            if (TestRunner == null)
            {
                return(HttpNotFound());
            }

            var TestRunnerID = TestRunner.TestRunnerID;
            var trgID        = TestRunner.TestRunnerGroupID;
            var Response     = "No Test Run ready for robot";

            var i         = 0;
            var TestFound = false;

            do
            {
                var ProjectsWithTestRunnerGroup = _context.Project.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var FoldersWithParentProjectWithTestRunnerGroup = _context.Folder.Where
                                                                      (t => t.TestRunnerGroupID == null && ProjectsWithTestRunnerGroup.Any(l => t.ProjectID == l.ID)).ToList();

                var FoldersWithTestRunnerGroup = _context.Folder.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedFolders            = FoldersWithParentProjectWithTestRunnerGroup.Union(FoldersWithTestRunnerGroup);

                var GroupsWithTestRunnerGroup = _context.Group.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedGroups            = GroupsWithTestRunnerGroup.Union
                                                    (_context.Group.Where(t => t.TestRunnerGroupID == null && combinedFolders.Any(l => t.FolderID == l.FolderID)));

                var RunsWithTestRunnerGroup = _context.Run.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedRuns            = RunsWithTestRunnerGroup.Union
                                                  (_context.Run.Where(t => t.TestRunnerGroupID == null && combinedGroups.Any(l => t.GroupID == l.GroupID)));

                var TestRunsWithTestRunnerGroups = _context.TestRun.Where(t => t.TestRunnerGroupID == trgID).ToList();
                var combinedTestRuns             = TestRunsWithTestRunnerGroups.Union
                                                       (_context.TestRun.Where(t => t.TestRunnerGroupID == null && combinedRuns.Any(l => t.RunID == l.RunID)));

                var ActiveTestRuns = combinedTestRuns.Where(t => t.Status == "Ready" && t.StartTime < DateTime.Now &&
                                                            (t.Retries == null || t.RetriesLeft > 0) && t.TestRunner == null);

                if (ActiveTestRuns.Any())
                {
                    var PossibleCandidateForRun = ActiveTestRuns.First();
                    var RefreshedCandidate      = _context.TestRun.SingleOrDefault(t => t.TestRunID == PossibleCandidateForRun.TestRunID);

                    if (RefreshedCandidate.TestRunner == null)
                    {
                        RefreshedCandidate.TestRunner = TestRunnerID;
                        _context.Update(RefreshedCandidate);
                        _context.SaveChanges();
                    }
                    else
                    {
                        //pick another candidate
                    }

                    //Check it's not been picked up elsewhere at near exact same time
                    var SecondaryilyRefreshedCandidate = _context.TestRun.SingleOrDefault(t => t.TestRunID == PossibleCandidateForRun.TestRunID);

                    if (SecondaryilyRefreshedCandidate.TestRunner == TestRunnerID)
                    {
                        TestFound = true;

                        if (SecondaryilyRefreshedCandidate.Retries != null)
                        {
                            SecondaryilyRefreshedCandidate.RetriesLeft = SecondaryilyRefreshedCandidate.RetriesLeft - 1;
                            _context.Update(SecondaryilyRefreshedCandidate);
                        }
                        Test test = _context.Test.Single(t => t.TestID ==
                                                         SecondaryilyRefreshedCandidate.TestID);

                        SecondaryilyRefreshedCandidate.Status = "Running";

                        CommandAndControlParameters commandAndControlParameters = new CommandAndControlParameters();
                        commandAndControlParameters.TestRunID               = SecondaryilyRefreshedCandidate.TestRunID;
                        commandAndControlParameters.TestID                  = SecondaryilyRefreshedCandidate.TestID;
                        commandAndControlParameters.TestName                = test.Name;
                        commandAndControlParameters.TestDataFilename        = test.ExcelFilePath;
                        commandAndControlParameters.TestEnvironmentID       = SecondaryilyRefreshedCandidate.TestEnvironmentID.Value;
                        commandAndControlParameters.Browser                 = SecondaryilyRefreshedCandidate.Browser;
                        commandAndControlParameters.TestEnvironmentFilename = _context.TestEnvironment.Single(t => t.TestEnvironmentID ==
                                                                                                              SecondaryilyRefreshedCandidate.TestEnvironmentID).XMLFilePath;
                        commandAndControlParameters.ClearRemoteLogOnNextAccess = TestRunner.ClearRemoteLogOnNextAccess;

                        Response = JsonConvert.SerializeObject(commandAndControlParameters);

                        _context.SaveChanges();

                        break;
                    }
                }

                i++;
            } while (TestFound && i < 10);

            return(new ObjectResult(Response));
        }