private void RollbackIISApplicationhostConfigFile()
 {
     if (IISConfigUtility.ApppHostTemporaryBackupFileExtention != null)
     {
         try
         {
             TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess);
         }
         catch
         {
             TestUtility.LogInformation("Failed to stop IIS worker processes");
         }
         try
         {
             IISConfigUtility.RestoreAppHostConfig(restoreFromMasterBackupFile: false);
         }
         catch
         {
             TestUtility.LogInformation("Failed to rollback applicationhost.config");
         }
         try
         {
             TestUtility.StartW3svc();
         }
         catch
         {
             TestUtility.LogInformation("Failed to start w3svc");
         }
         IISConfigUtility.ApppHostTemporaryBackupFileExtention = null;
     }
 }
        public void InitializeIISServer()
        {
            // Check if IIS server is installed or not
            bool isIISInstalled = true;

            if (!File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "iiscore.dll")))
            {
                isIISInstalled = false;
            }

            if (!File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "config", "applicationhost.config")))
            {
                isIISInstalled = false;
            }

            if (!isIISInstalled)
            {
                throw new ApplicationException("IIS server is not installed");
            }

            // Clean up IIS worker process
            TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess);

            // Reset applicationhost.config
            TestUtility.LogInformation("Restoring applicationhost.config");
            IISConfigUtility.RestoreAppHostConfig(restoreFromMasterBackupFile: true);
            TestUtility.StartW3svc();

            // check w3svc is running after resetting applicationhost.config
            if (IISConfigUtility.GetServiceStatus("w3svc") == "Running")
            {
                TestUtility.LogInformation("W3SVC service is restarted after restoring applicationhost.config");
            }
            else
            {
                throw new ApplicationException("WWW service can't start");
            }

            if (IISConfigUtility.ApppHostTemporaryBackupFileExtention == null)
            {
                throw new ApplicationException("Failed to backup applicationhost.config");
            }
        }
        public InitializeTestMachine()
        {
            _referenceCount++;

            if (_referenceCount == 1)
            {
                TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Start");

                _InitializeTestMachineCompleted = false;

                TestUtility.LogInformation("InitializeTestMachine::Start");
                if (Environment.ExpandEnvironmentVariables("%ANCMDebug%").ToLower() == "true")
                {
                    System.Diagnostics.Debugger.Launch();
                }

                TestUtility.ResetHelper(ResetHelperMode.KillIISExpress);
                TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess);
                // cleanup before starting
                string siteRootPath = Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "inetpub", "ANCMTest");
                try
                {
                    if (IISConfigUtility.IsIISInstalled == true)
                    {
                        IISConfigUtility.RestoreAppHostConfig();
                    }
                }
                catch
                {
                    TestUtility.LogInformation("Failed to restore applicationhost.config");
                }

                if (!Directory.Exists(siteRootPath))
                {
                    Directory.CreateDirectory(siteRootPath);
                }

                foreach (string directory in Directory.GetDirectories(siteRootPath))
                {
                    bool successDeleteChildDirectory = true;
                    try
                    {
                        TestUtility.DeleteDirectory(directory);
                    }
                    catch
                    {
                        successDeleteChildDirectory = false;
                        TestUtility.LogInformation("Failed to delete " + directory);
                    }
                    if (successDeleteChildDirectory)
                    {
                        try
                        {
                            TestUtility.DeleteDirectory(siteRootPath);
                        }
                        catch
                        {
                            TestUtility.LogInformation("Failed to delete " + siteRootPath);
                        }
                    }
                }

                if (InitializeTestMachine.UsePrivateAspNetCoreFile)
                {
                    PreparePrivateANCMFiles();

                    // update applicationhost.config for IIS server
                    if (IISConfigUtility.IsIISInstalled == true)
                    {
                        using (var iisConfig = new IISConfigUtility(ServerType.IIS))
                        {
                            iisConfig.AddModule("AspNetCoreModule", Aspnetcore_path, null);
                        }
                    }
                }

                _InitializeTestMachineCompleted = true;

                TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() End");
            }

            for (int i = 0; i < 120; i++)
            {
                if (_InitializeTestMachineCompleted)
                {
                    break;
                }
                else
                {
                    TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Waiting...");
                    Thread.Sleep(500);
                }
            }
            if (!_InitializeTestMachineCompleted)
            {
                throw new System.ApplicationException("InitializeTestMachine failed");
            }
        }
Example #4
0
        public InitializeTestMachine()
        {
            _referenceCount++;

            if (_referenceCount == 1)
            {
                CheckPerquisiteForANCMTest();

                TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Start");

                _InitializeTestMachineCompleted = false;

                TestUtility.LogInformation("InitializeTestMachine::Start");
                if (Environment.ExpandEnvironmentVariables("%ANCMTEST_DEBUG%").ToLower() == "true")
                {
                    System.Diagnostics.Debugger.Launch();
                }

                // check Makecert.exe exists
                try
                {
                    string makecertExeFilePath = TestUtility.GetMakeCertPath();
                    TestUtility.RunCommand(makecertExeFilePath, null, true, true);
                    TestUtility.LogInformation("Verified makecert.exe is available : " + makecertExeFilePath);
                }
                catch (Exception ex)
                {
                    throw new System.ApplicationException("makecert.exe is not available : " + ex.Message);
                }

                TestUtility.ResetHelper(ResetHelperMode.KillIISExpress);

                // check if we can use IIS server instead of IISExpress
                try
                {
                    IISConfigUtility.IsIISReady = false;
                    if (IISConfigUtility.IsIISInstalled == true)
                    {
                        var envValue = Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable);
                        if (envValue.ToLower().Contains(ANCMTestFlagsUseIISExpressContext.ToLower()))
                        {
                            TestUtility.LogInformation("UseIISExpress is set");
                            throw new System.ApplicationException("'ANCMTestServerType' environment variable is set to 'true'");
                        }
                        else
                        {
                            TestUtility.LogInformation("UseIISExpress is not set");
                        }

                        // check websocket is installed
                        if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "iiswsock.dll")))
                        {
                            TestUtility.LogInformation("Websocket is installed");
                        }
                        else
                        {
                            throw new System.ApplicationException("websocket module is not installed");
                        }

                        TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess);

                        // Reset applicationhost.config
                        TestUtility.LogInformation("Restoring applicationhost.config");
                        IISConfigUtility.RestoreAppHostConfig(restoreFromMasterBackupFile: true);
                        TestUtility.StartW3svc();

                        // check w3svc is running after resetting applicationhost.config
                        if (IISConfigUtility.GetServiceStatus("w3svc") == "Running")
                        {
                            TestUtility.LogInformation("W3SVC service is restarted after restoring applicationhost.config");
                        }
                        else
                        {
                            throw new System.ApplicationException("WWW service can't start");
                        }

                        // check URLRewrite module exists
                        if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "rewrite.dll")))
                        {
                            TestUtility.LogInformation("Verified URL Rewrite module installed for IIS server");
                        }
                        else
                        {
                            throw new System.ApplicationException("URL Rewrite module is not installed");
                        }

                        if (IISConfigUtility.ApppHostTemporaryBackupFileExtention == null)
                        {
                            throw new System.ApplicationException("Failed to backup applicationhost.config");
                        }
                        IISConfigUtility.IsIISReady = true;
                    }
                }
                catch (Exception ex)
                {
                    RollbackIISApplicationhostConfigFile();
                    TestUtility.LogInformation("We will use IISExpress instead of IIS: " + ex.Message);
                }

                string siteRootPath = Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "inetpub", "ANCMTest");
                if (!Directory.Exists(siteRootPath))
                {
                    Directory.CreateDirectory(siteRootPath);
                }

                foreach (string directory in Directory.GetDirectories(siteRootPath))
                {
                    bool successDeleteChildDirectory = true;
                    try
                    {
                        TestUtility.DeleteDirectory(directory);
                    }
                    catch
                    {
                        successDeleteChildDirectory = false;
                        TestUtility.LogInformation("Failed to delete " + directory);
                    }
                    if (successDeleteChildDirectory)
                    {
                        try
                        {
                            TestUtility.DeleteDirectory(siteRootPath);
                        }
                        catch
                        {
                            TestUtility.LogInformation("Failed to delete " + siteRootPath);
                        }
                    }
                }

                if (InitializeTestMachine.UsePrivateAspNetCoreFile == true)
                {
                    PreparePrivateANCMFiles();

                    // update applicationhost.config for IIS server
                    if (IISConfigUtility.IsIISReady)
                    {
                        using (var iisConfig = new IISConfigUtility(ServerType.IIS, null))
                        {
                            iisConfig.AddModule("AspNetCoreModule", FullIisAspnetcore_path, null);
                        }
                    }
                }

                _InitializeTestMachineCompleted = true;
                TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() End");
            }

            for (int i = 0; i < 120; i++)
            {
                if (_InitializeTestMachineCompleted)
                {
                    break;
                }
                else
                {
                    TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Waiting...");
                    Thread.Sleep(500);
                }
            }
            if (!_InitializeTestMachineCompleted)
            {
                throw new System.ApplicationException("InitializeTestMachine failed");
            }
        }