Пример #1
0
 //TODO
 public static string SetResidenceType(string xmlConfig, EnvironmentType type)
 {
     TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(xmlConfig);
     config.Type = type;
     return config.ToXML();
 }
Пример #2
0
        public static string SetMachineIP(string xmlConfig, EnvironmentType type, string machineName, string ip)
        {
            TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(xmlConfig);
            //No matter SUT and TestAgent, update the SUT
            if (config.SUTConfiguration != null)
            {
                foreach (Machine m in config.SUTConfiguration.Machines)
                {
                    if (m.Name.ToLower() == machineName.ToLower())
                    {
                        m.IP = ip;
                    }
                }
            }
            if (config.TestAgentConfiguration != null)
            {
                foreach (Machine m in config.TestAgentConfiguration.Machines)
                {
                    if (m.Name.ToLower() == machineName.ToLower())
                    {
                        m.IP = ip;
                    }
                }
            }

            return config.ToXML();
        }
Пример #3
0
        /// <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);
                }
            }
        }
Пример #4
0
        private static void DiscardTestEnvironmentsOfJob(AutomationJob job)
        {
            List<TestEnvironment> environments = GetAllTestEnvironmentsOfJob(job);
            foreach (TestEnvironment e in environments)
            {
                if (e.Type != EnvironmentCreateType.Static)
                {
                    e.SetEnvironmentStatus(EnvironmentStatus.Discard);
                }
                else
                {
                    TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(e.Config);
                    //remove the job information from the config of the environment because it'll be reused by following new jobs.
                    config.TestAgentConfiguration.Categories.RemoveAll(category => category == "JobId=" + job.JobId.ToString());
                    e.SetEnvironmentConfig(config.ToXML());

                    if (config.Type == EnvironmentType.SUTAlone)
                    {
                        e.SetEnvironmentStatus(EnvironmentStatus.Disposed);
                        job.AddJobProgressInformation(string.Format("Free the SUT environment [{0}]", e.Name));
                    }
                    else if (config.Type == EnvironmentType.TestAgentAlone)
                    {
                        //shutdown the current running saber agent on the test agent
                        foreach (Machine m in config.TestAgentConfiguration.Machines)
                        {
                            if (EnvironmentManager.IsSaberAgentRequiredInThisMachine(m))
                            {
                                string ip = string.IsNullOrEmpty(m.ExternalIP) ? m.IP : m.ExternalIP;
                                string domainName = config.TestAgentConfiguration.TestAgentDomainConfig.Name;
                                string domainAdmin = config.TestAgentConfiguration.TestAgentDomainConfig.Adminstrator;
                                string domainAdminPassword = config.TestAgentConfiguration.TestAgentDomainConfig.Password;
                                Common.ScriptCommon.CMDScript.CloseRunningApplicationRemotely(ip, domainName + @"\" + domainAdmin, domainAdminPassword, Core.AgentName.WindowsFormApp);
                            }
                        }
                        e.SetEnvironmentStatus(EnvironmentStatus.Disposed);
                        job.AddJobProgressInformation(string.Format("Free the Test Agent environment [{0}]", e.Name));
                    }
                    else
                    {
                        //code should not arrive here.
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Handle the test environment with machines ready, work include:
        /// 1. Install the Saber Agent services on the machines and start the services on remote machine
        /// 2. Update the environment information for the TestAgent and SUT, then the TestAgent and SUT know the details of each other(mainly IPs here)
        /// 3. Specify what kinds of works the Saber Agent services will take after it restarted. such as 
        ///   1). to tell services hosted on test agent to install the S1 build
        ///   2). to tell the services hosted on test agent to run the test case for which job
        /// 4. After that, the test environment status is AgentServiceInstalling or AgentServiceInstalledAndReady
        /// Below actions are taken by other components
        /// 5. The saber agent service on the test agent will install the S1 build, before that it'll wait the SUT to be AgentServiceInstalledAndReady
        /// 6. The saber agent service will set the environment status to be BuildInstalled
        /// 7. Then the environment manager will restart all the machines in SUT
        /// 8. After restarted, the saber agent in test agent will check all the machines in SUT are started and start to run the test cases.
        /// </summary>
        /// <param name="environment"></param>
        public void HandleTestEnvironmentWithStatusMachinesReady(TestEnvironment environment)
        {
            EnvironmentType type = EnvironmentConfigHelper.GetResidenceType(environment.Config);
            if (type == EnvironmentType.Residence_Together)
            {
                //add the jobId into the configuration file to let the Test Agent know which job the test agent is for.
                TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(environment.Config);
                config.TestAgentConfiguration.Categories.Add(string.Format("JobId={0}", EnvironmentManager.GetAutomationJobOfTestEnvironment(environment).JobId.ToString()));
                //config.TestAgentConfiguration.Categories.Add(string.Format("mode={0}", SaberAgentMode.InstallS1Build));
                environment.SetEnvironmentConfig(config.ToXML());
                environment.SetEnvironmentStatus(EnvironmentStatus.AgentServiceInstalling);
                try
                {
                    InstallTestAgentWindowsServiceOnEnvironmentAsynchronous(environment);
                }
                catch (Exception ex)
                {
                    ATFEnvironment.Log.logger.Error(string.Format("Execption captured when install test agent windows service on environment asynchronously for environment {0}",environment.EnvironmentId), ex);
                }

                ATFEnvironment.Log.logger.Info(string.Format("Test Agents started to be installed on environment {0}", environment.Name));
                ATFEnvironment.Log.logger.Info(string.Format("Environment status changes from MachinesReady -> AgentServiceInstalling"));
            }
            else if (type == EnvironmentType.TestAgentAlone)
            {
                //to make sure in the test agent, we have the information about the SUT,
                //we'll first check whether the SUT is MachinesReady,
                //if yes, we'll copy the SUT config to the TestAgent config, then setup the Saber Agent
                //else, do nothing and wait another loop
                TestEnvironment sutEnvironment = EnvironmentManager.GetSUTEnvironmentOfTestAgentEnvironment(environment);
                if (null != sutEnvironment)
                {
                    //if the sut is machine ready or other status after machine ready
                    if (sutEnvironment.EnvironmentStatus == EnvironmentStatus.MachinesReady ||
                        sutEnvironment.EnvironmentStatus == EnvironmentStatus.AgentServiceInstalling ||
                        sutEnvironment.EnvironmentStatus == EnvironmentStatus.AgentServiceInstalledAndReady ||
                        sutEnvironment.EnvironmentStatus == EnvironmentStatus.BuildInstalled ||
                        sutEnvironment.EnvironmentStatus == EnvironmentStatus.Ready
                    )
                    {
                        //update the SUT part of the configuration of the test agent configuration.
                        //then the test agent know the detail of the SUT.
                        TestEnvironmentConfigHelper sutConfig = new TestEnvironmentConfigHelper(sutEnvironment.Config);
                        TestEnvironmentConfigHelper testAgentConfig = new TestEnvironmentConfigHelper(environment.Config);
                        testAgentConfig.SUTConfiguration = sutConfig.SUTConfiguration;
                        environment.SetEnvironmentConfig(testAgentConfig.ToXML());

                        //add the jobId into the configuration file to let the Test Agent know which job the test agent is for.
                        TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(environment.Config);
                        config.TestAgentConfiguration.Categories.Add("JobId=" + EnvironmentManager.GetAutomationJobOfTestEnvironment(environment).JobId.ToString());
                        environment.SetEnvironmentConfig(config.ToXML());

                        //Install the Saber Agent service into the environment
                        environment.SetEnvironmentStatus(EnvironmentStatus.AgentServiceInstalling);
                        InstallTestAgentWindowsServiceOnEnvironmentAsynchronous(environment);

                        ATFEnvironment.Log.logger.Info(string.Format("Start to install Saber Agent on environment {0}", environment.Name));
                        ATFEnvironment.Log.logger.Info(string.Format("Environment status changes from MachinesReady -> AgentServiceInstalling"));
                    }
                }
            }
            else if (type == EnvironmentType.SUTAlone)//TODO, do we need to install the build on the SUT? currently we do nothing and assume that the environment is ready
            {
                TestEnvironmentConfigHelper config = new TestEnvironmentConfigHelper(environment.Config);
                config.TestAgentConfiguration.Categories.Add(string.Format("JobId={0}", EnvironmentManager.GetAutomationJobOfTestEnvironment(environment).JobId.ToString()));
                environment.SetEnvironmentConfig(config.ToXML());

                ATFEnvironment.Log.logger.Info(string.Format("Test Agents started to be installed on environment {0}", environment.Name));
                //ATFEnvironment.Log.logger.Info(string.Format("Environment status changes from {0} -> AgentServiceInstalling", environment.EnvironmentStatus));
                environment.SetEnvironmentStatus(EnvironmentStatus.AgentServiceInstalling);
                InstallTestAgentWindowsServiceOnEnvironmentAsynchronous(environment);
                ATFEnvironment.Log.logger.Info(string.Format("Test Agents have been installed on environment {0}", environment.Name));

            }
        }