/// <summary> /// Request the test agent environment for the job /// </summary> /// <param name="job">job</param> /// <param name="provider">environment provider</param> public void RequestTestAgentEnvironmentForJob(AutomationJob job, IEnvironmentProvider provider) { SupportedEnvironment supportEnvironment = job.GetSupportedEnv(); AutomationTask task = JobManagement.GetAutomationTaskOfJob(job); string templateName = new TestEnvironmentConfigHelper(supportEnvironment.Config).TestAgentConfiguration.Name; string environmentName = string.Format("{0}_{1}_{2}_{3}", task.Name, job.JobId, "TestAgent", Guid.NewGuid()); TestEnvironmentConfigHelper sutConfig = new TestEnvironmentConfigHelper(supportEnvironment.Config); // Get the avaliable permenent agent, typically we maintain a pool of machines act as the test agent, no need to deploy new vApps // If no available agents in the pool now, we'll create a new vApp from the template with name of templateName TestEnvironment availableReadyStaticAgent = TestEnvironment.GetAvalibleStaticTestAgent4SupportedEnvironment(supportEnvironment); EnvironmentDeploymentType deployType = new TestEnvironmentConfigHelper(supportEnvironment.Config).TestAgentConfiguration.DeploymentType; if (deployType == EnvironmentDeploymentType.Existing ) { if (availableReadyStaticAgent != null) { string info = string.Format("Find one avaliable permanent test agent [{0}:{1}] for job [{2}]", availableReadyStaticAgent.EnvironmentId, availableReadyStaticAgent.Name, job.Name); availableReadyStaticAgent.SetEnvironmentStatus(EnvironmentStatus.MachinesReady); ATFEnvironment.Log.logger.Info(info); job.AddJobProgressInformation(info); // set SUT information to test agent's config // set test agent type to TestAgentAlone /* TestEnvironmentConfigHelper testAgentConfig = new TestEnvironmentConfigHelper(availableReadyStaticAgent.Config); testAgentConfig.SUTConfiguration = sutConfig.SUTConfiguration; testAgentConfig.Type = EnvironmentType.TestAgentAlone; availableReadyStaticAgent.Config = testAgentConfig.ToXML(); info = string.Format("Change the permanent agent's status, AgentServiceInstalledAndReady -> Ocuppied"); ATFEnvironment.Log.logger.Info(info); job.AddJobProgressInformation(info); // Set this permanent agent to occuppied //availableReadyStaticAgent.Status = (int)EnvironmentStatus.Ocuppied; //availableReadyStaticAgent.Update(); TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(availableReadyStaticAgent.Config); //clear the finished job information config.TestAgentConfiguration.Categories.Clear(); //add the jobId into the configuration file to let the Test Agent know which job the test agent is for. config.TestAgentConfiguration.Categories.Add("JobId=" + job.JobId.ToString()); availableReadyStaticAgent.Config = config.ToXML(); availableReadyStaticAgent.Update(); //copy the config file to the test agent List<Machine> testAgents = GetMachinesNeedTestAgentInstalledOn(config); string ip = testAgents.Count() > 0 ? testAgents[0].ExternalIP : string.Empty; string domain = config.TestAgentConfiguration.TestAgentDomainConfig.Name; string administrator = config.TestAgentConfiguration.TestAgentDomainConfig.Adminstrator; string password = config.TestAgentConfiguration.TestAgentDomainConfig.Password; string targetPath = @"\\" + ip + @"\C$\SaberAgent"; string targetEnvironmentConfigFolder = targetPath + @"\Config"; string targetEnvironmentConfigFile = targetEnvironmentConfigFolder + @"\Environment.xml"; if (!NetUseHelper.NetUserMachine(ip, domain + @"\" + administrator, password)) { ATFEnvironment.Log.logger.Error(string.Format("Net use the machine [{0}] failed.", ip)); } Common.ScriptCommon.CMDScript.FlushDNSRemotely(ip, domain + @"\" + administrator, password); if (!FileHelper.IsExistsFolder(targetEnvironmentConfigFolder)) { FileHelper.CreateFolder(targetEnvironmentConfigFolder); } TXTHelper.ClearTXTContent(targetEnvironmentConfigFile); TXTHelper.WriteNewLine(targetEnvironmentConfigFile, config.ToXML(), System.Text.Encoding.Default); info = string.Format("Copy the file[{0}] to permanent agent", targetEnvironmentConfigFile); ATFEnvironment.Log.logger.Info(info); job.AddJobProgressInformation(info); */ job.SetTestAgentEnvironment(availableReadyStaticAgent.EnvironmentId); } else { string info = string.Format("There's no available test agents right now, please wait other tasks to free some environments."); ATFEnvironment.Log.logger.Info(info); job.AddJobProgressInformation(info); } } else { //create a new record in DB for Test Agent, and Galaxy will handle the environment later(install, config and so on) sutConfig.TestAgentConfiguration.DeploymentType = EnvironmentDeploymentType.ToBeCreated; sutConfig.Type = EnvironmentType.TestAgentAlone; string config = sutConfig.ToXML(); try { var testEnvironment = new TestEnvironment { ProviderId = provider.Provider.ProviderId, Name = environmentName, Type = provider.Provider.Name, Status = (int)EnvironmentStatus.New, CreateDate = DateTime.UtcNow, ModifyDate = DateTime.UtcNow, //Config = EnvironmentConfigHelper.SetResidenceType(supportEnvironment.Config, EnvironmentType.TestAgentAlone), Config = config, Description = templateName, }; if (job.JobStatus == JobStatus.Cancelled || job.JobStatus == JobStatus.End) { testEnvironment.SetEnvironmentStatus(EnvironmentStatus.Discard); } TestEnvironment.Add(testEnvironment); job.SetTestAgentEnvironment(testEnvironment.EnvironmentId); } catch (Exception ex) { job.SetJobsStatus(JobStatus.Failed); string info = string.Format("Failed to request Test Agent environment {0}, Exception: {1}", environmentName, ex.Message); job.AddJobProgressInformation(info); ATFEnvironment.Log.logger.Error(info, ex); } } }
/// <summary> /// Request the sut environment for job /// </summary> /// <param name="job">job</param> /// <param name="provider">environment provider</param> public void RequestSUTEnvironmentForJob(AutomationJob job, IEnvironmentProvider provider) { SupportedEnvironment supportEnvironment = job.GetSupportedEnv(); TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(supportEnvironment.Config); string templateName = config.SUTConfiguration.Name; string sutConfig = string.Empty; if (config.Type == EnvironmentType.Residence_Seperate) { sutConfig = EnvironmentConfigHelper.SetResidenceType(supportEnvironment.Config, EnvironmentType.SUTAlone); } else if (config.Type == EnvironmentType.Residence_Together) { sutConfig = EnvironmentConfigHelper.SetResidenceType(supportEnvironment.Config, EnvironmentType.Residence_Together); } AutomationTask task = JobManagement.GetAutomationTaskOfJob(job); if (config.SUTConfiguration.DeploymentType == EnvironmentDeploymentType.Existing) { //Note: the existing SUT environments are distinguished by it's name, two environments with same name are considered as the same one var sutEnvironment = TestEnvironment.GetAvalibleStaticSUT4SupportedEnvironment(supportEnvironment); if (sutEnvironment == null)//wait untill another environment is freed { string info = string.Format("There's no SUT environment in the pool available now, please wait for other tasks to free any environment."); ATFEnvironment.Log.logger.Info(info); job.AddJobProgressInformation(info); } else//reuse the record { string message = string.Format("Get an available SUT environment [{0}] for the job [{1}:{2}]", sutEnvironment.Name, job.JobId, job.Name); job.AddJobProgressInformation(message); message = string.Format("Change environment [{0}:{1}] status from {2} to {3}", sutEnvironment.EnvironmentId, sutEnvironment.Name, sutEnvironment.Status, "MachinesReady"); job.AddJobProgressInformation(message); sutEnvironment.SetEnvironmentStatus(EnvironmentStatus.MachinesReady); job.SetSUTEnvironment(sutEnvironment.EnvironmentId); } } else { string environmentName = string.Format("{0}_{1}_{2}_{3}", task.Name, job.JobId, "SUT", Guid.NewGuid()); try { var testEnvironment = new TestEnvironment { ProviderId = provider.Provider.ProviderId, Name = environmentName, Type = provider.Provider.Name, CreateDate = DateTime.UtcNow, ModifyDate = DateTime.UtcNow, Config = sutConfig, Description = templateName, }; if (job.JobStatus == JobStatus.Cancelled || job.JobStatus == JobStatus.End) testEnvironment.SetEnvironmentStatus(EnvironmentStatus.Discard); TestEnvironment.Add(testEnvironment); string message = string.Format("Environment [{0}:{1} is created for job [{2}:{3}]]", testEnvironment.EnvironmentId, testEnvironment.Name, job.JobId, job.Name); job.AddJobProgressInformation(message); job.SetSUTEnvironment(testEnvironment.EnvironmentId); } catch (Exception ex) { string info = string.Format("Failed to assign {0}, Exception: {1}", environmentName, ex.Message); job.SetJobsStatus(JobStatus.Failed); job.AddJobProgressInformation(info); ATFEnvironment.Log.logger.Error(info, ex); } } }