public void SetAppPoolSetting(string appPoolName, string attribute, object value) { TestUtility.LogInformation("Setting Apppool : " + appPoolName + "::" + attribute.ToString() + " <== " + value.ToString()); using (ServerManager serverManager = GetServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools"); ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection(); ConfigurationElement addElement = FindElement(applicationPoolsCollection, "add", "name", appPoolName); if (addElement == null) { throw new InvalidOperationException("Element not found!"); } switch (attribute) { case "privateMemory": case "memory": ConfigurationElement recyclingElement = addElement.GetChildElement("recycling"); ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart"); periodicRestartElement[attribute] = value; break; case "rapidFailProtectionMaxCrashes": ConfigurationElement failureElement = addElement.GetChildElement("failure"); failureElement["rapidFailProtectionMaxCrashes"] = value; break; default: addElement[attribute] = value; break; } serverManager.CommitChanges(); } }
public void RemoveBindingFromSite(string siteName, BindingInfo bindingInfo) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Removing Binding {0} from Site {1} ####################", bindingInfo.ToBindingString(), siteName)); for (int i = 0; i < serverManager.Sites[siteName].Bindings.Count; i++) { if (serverManager.Sites[siteName].Bindings[i].BindingInformation.ToString() == bindingInfo.ToBindingString()) { serverManager.Sites[siteName].Bindings.RemoveAt(i); serverManager.CommitChanges(); return; } } TestUtility.LogInformation(String.Format("#################### Remove binding failed because binding was not found ####################")); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Remove binding failed. Reason: {0} ####################", ex.Message)); } }
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; } }
private void StartOrStopAppPool(string appPoolName, bool start) { string action = start ? "Starting" : "Stopping"; TestUtility.LogTrace(String.Format("#################### {0} app pool {1} ####################", action, appPoolName)); try { using (ServerManager serverManager = GetServerManager()) { ApplicationPoolCollection appPools = serverManager.ApplicationPools; if (start) { appPools[appPoolName].Start(); } else { appPools[appPoolName].Stop(); } serverManager.CommitChanges(); } } catch (Exception ex) { string message = ex.Message; TestUtility.LogInformation(String.Format("#################### {0} app pool {1} failed. Reason: {2} ####################", action, appPoolName, ex.Message)); } }
public void AddApplicationToSite(string siteName, string appPath, string physicalPath, string poolName) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Adding Application {0} with App Pool {1} to Site {2} ####################", appPath, poolName, siteName)); SiteCollection sites = serverManager.Sites; Application app = sites[siteName].Applications.CreateElement(); app.SetAttributeValue("path", appPath); app.SetAttributeValue("applicationPool", poolName); sites[siteName].Applications.Add(app); VirtualDirectory vdir = app.VirtualDirectories.CreateElement(); vdir.SetAttributeValue("path", "/"); vdir.SetAttributeValue("physicalPath", physicalPath); app.VirtualDirectories.Add(vdir); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Add Application {0} with App Pool {1} to Site {2} failed. Reason: {3} ####################", appPath, poolName, siteName, ex.Message)); } }
public void Dispose() { _referenceCount--; if (_referenceCount == 0) { TestUtility.LogInformation("InitializeTestMachine::Dispose() Start"); TestUtility.ResetHelper(ResetHelperMode.KillIISExpress); if (InitializeTestMachine.UsePrivateAspNetCoreFile) { if (IISConfigUtility.IsIISInstalled == true) { using (var iisConfig = new IISConfigUtility(ServerType.IIS)) { try { iisConfig.AddModule("AspNetCoreModule", Aspnetcore_path_original, null); } catch { TestUtility.LogInformation("Failed to restore aspnetcore.dll path!!!"); } } } } TestUtility.LogInformation("InitializeTestMachine::Dispose() End"); } }
public void Dispose() { TestUtility.LogInformation("TestWebSite::Dispose() Start"); if (IisExpressPidBackup != -1) { try { var iisExpressProcess = Process.GetProcessById(Convert.ToInt32(IisExpressPidBackup)); iisExpressProcess.Kill(); iisExpressProcess.WaitForExit(); iisExpressProcess.Close(); } catch { if (this.AspNetCoreApp.HostingModel == TestWebApplication.HostingModelValue.Inprocess) { // IISExpress seems to be already recycled under Inprocess mode. } else { TestUtility.RunPowershellScript("stop-process -id " + IisExpressPidBackup); } } } TestUtility.LogInformation("TestWebSite::Dispose() End"); }
private void StartOrStopSite(string siteName, bool start) { string action = start ? "Starting" : "Stopping"; TestUtility.LogTrace(String.Format("#################### {0} site {1} ####################", action, siteName)); try { using (ServerManager serverManager = GetServerManager()) { SiteCollection sites = serverManager.Sites; if (start) { sites[siteName].Start(); sites[siteName].SetAttributeValue("serverAutoStart", true); } else { sites[siteName].Stop(); sites[siteName].SetAttributeValue("serverAutoStart", false); } serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### {0} site {1} failed. Reason: {2} ####################", action, siteName, ex.Message)); } }
public void ModifyBindingForSite(string siteName, BindingInfo bindingInfoOld, BindingInfo bindingInfoNew) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Changing Binding {0} for Site {1} to {2} ####################", bindingInfoOld.ToBindingString(), siteName, bindingInfoNew.ToBindingString())); for (int i = 0; i < serverManager.Sites[siteName].Bindings.Count; i++) { if (serverManager.Sites[siteName].Bindings[i].BindingInformation.ToString() == bindingInfoOld.ToBindingString()) { serverManager.Sites[siteName].Bindings[i].SetAttributeValue("bindingInformation", bindingInfoNew.ToBindingString()); serverManager.CommitChanges(); return; } } TestUtility.LogInformation(String.Format("#################### Modify binding failed because binding was not found ####################")); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Changing binding failed. Reason: {0} ####################", ex.Message)); } }
public void StartIISExpress(string verificationCommand = null) { if (IisServerType == ServerType.IIS) { return; } string cmdline; string argument = "/siteid:" + _siteId + " /config:" + IisExpressConfigPath; if (Directory.Exists(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%")) && _appPoolBitness == IISConfigUtility.AppPoolBitness.enable32Bit) { cmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), "IIS Express", "iisexpress.exe"); } else { cmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), "IIS Express", "iisexpress.exe"); } TestUtility.LogInformation("TestWebSite::TestWebSite() Start IISExpress: " + cmdline + " " + argument); _iisExpressPidBackup = TestUtility.RunCommand(cmdline, argument, false, false); bool isIISExpressReady = false; int timeout = 3; for (int i = 0; i < timeout * 5; i++) { string statusCode = string.Empty; try { if (verificationCommand == null) { verificationCommand = "( invoke-webrequest http://localhost:" + TcpPort + " ).StatusCode"; } statusCode = TestUtility.RunPowershellScript(verificationCommand); } catch { statusCode = "ExceptionError"; } if ("200" == statusCode) { isIISExpressReady = true; break; } else { System.Threading.Thread.Sleep(200); } } if (isIISExpressReady) { TestUtility.LogInformation("IISExpress is ready to use"); } else { throw new ApplicationException("IISExpress is not responding within " + timeout + " seconds"); } }
public void VerifyWorkerProcessRecycledUnderInprocessMode(string backendProcessId, int timeout = 5000) { if (AspNetCoreApp.HostingModel != TestWebApplication.HostingModelValue.Inprocess) { return; // do nothing for outofprocess } bool succeeded = false; for (int i = 0; i < (timeout / 1000); i++) { Process backendProcess = null; try { backendProcess = Process.GetProcessById(Convert.ToInt32(backendProcessId)); } catch { succeeded = true; TestUtility.LogInformation("Process not found."); break; } if (backendProcess == null) { succeeded = true; break; } backendProcess.WaitForExit(1000); if (this.IisServerType == ServerType.IISExpress && i == 3) { // exit after 3 seconds for IISExpress case break; } } if (succeeded == false) { if (this.IisServerType == ServerType.IIS) { throw new Exception("Failed to recycle IIS worker process"); } else { // IISExpress should be killed if it can't be recycled TestUtility.LogInformation("BugBug: Restart IISExpress..."); TestUtility.ResetHelper(ResetHelperMode.KillIISExpress); } } if (IisServerType == ServerType.IISExpress) { // restart IISExpress StartIISExpress(); } }
public void Dispose() { _referenceCount--; if (_referenceCount == 0) { TestUtility.LogInformation("InitializeTestMachine::Dispose() Start"); TestUtility.ResetHelper(ResetHelperMode.KillIISExpress); RollbackIISApplicationhostConfigFile(); TestUtility.LogInformation("InitializeTestMachine::Dispose() End"); } }
public void CreateSiteEx(int siteId, string siteName, string poolName, string dirRoot, string Ip, int Port, string host) { try { using (ServerManager serverManager = GetServerManager()) { string bindingInfo = ""; if (Ip == null) { Ip = "*"; } bindingInfo += Ip; bindingInfo += ":"; bindingInfo += Port; bindingInfo += ":"; if (host != null) { bindingInfo += host; } TestUtility.LogTrace(String.Format("#################### Adding Site {0} with App Pool {1} with BindingInfo {2} ####################", siteName, poolName, bindingInfo)); SiteCollection sites = serverManager.Sites; Site site = sites.CreateElement(); site.Id = siteId; site.SetAttributeValue("name", siteName); sites.Add(site); Application app = site.Applications.CreateElement(); app.SetAttributeValue("path", "/"); app.SetAttributeValue("applicationPool", poolName); site.Applications.Add(app); VirtualDirectory vdir = app.VirtualDirectories.CreateElement(); vdir.SetAttributeValue("path", "/"); vdir.SetAttributeValue("physicalPath", dirRoot); app.VirtualDirectories.Add(vdir); Binding b = site.Bindings.CreateElement(); b.SetAttributeValue("protocol", "http"); b.SetAttributeValue("bindingInformation", bindingInfo); site.Bindings.Add(b); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Create site {0} failed. Reason: {1} ####################", siteName, ex.Message)); } }
public void Dispose() { TestUtility.LogInformation("TestWebSite::Dispose() Start"); if (_iisExpressPidBackup != -1) { var iisExpressProcess = Process.GetProcessById(Convert.ToInt32(_iisExpressPidBackup)); iisExpressProcess.Kill(); iisExpressProcess.WaitForExit(); } TestUtility.LogInformation("TestWebSite::Dispose() End"); }
public void SetCompression(string siteName, bool enabled) { TestUtility.LogInformation("Enable Compression : " + siteName); using (ServerManager serverManager = GetServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); ConfigurationSection urlCompressionSection = config.GetSection("system.webServer/urlCompression", siteName); urlCompressionSection["doStaticCompression"] = enabled; urlCompressionSection["doDynamicCompression"] = enabled; serverManager.CommitChanges(); } }
private bool CheckPerquisiteForANCMTest() { bool result = true; TestUtility.LogInformation("CheckPerquisiteForANCMTest(): Environment.Is64BitOperatingSystem: {0}, Environment.Is64BitProcess {1}", Environment.Is64BitOperatingSystem, Environment.Is64BitProcess); TestUtility.LogInformation("%ANCMTestFlags%: {0}", Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable)); if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) { TestUtility.LogInformation("CheckPerquisiteForANCMTest() Failed: ANCM test should be started with x64 process mode on 64 bit machine; if you run this test on Visual Studio, you should set X64 first after selecting 'Test -> Test Settings -> Default Process Architecture' menu"); result = false; } return(result); }
public void DisableWindowsAuthentication(string siteName) { TestUtility.LogInformation("Enable Windows authentication : " + siteName); using (ServerManager serverManager = GetServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", siteName); anonymousAuthenticationSection["enabled"] = true; ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", siteName); windowsAuthenticationSection["enabled"] = false; serverManager.CommitChanges(); } }
public void DetachAppverifier() { try { string cmdline; string processName = "iisexpress.exe"; string debuggerCmdline; if (IisServerType == ServerType.IIS) { processName = "w3wp.exe"; } string argument = "-disable * -for " + processName; if (Directory.Exists(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%")) && AppPoolBitness == IISConfigUtility.AppPoolBitness.enable32Bit) { cmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "syswow64", "appverif.exe"); if (!File.Exists(cmdline)) { throw new ApplicationException("Not found :" + cmdline); } debuggerCmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), "Debugging Tools for Windows (x64)", "wow64", "windbg.exe"); if (!File.Exists(debuggerCmdline)) { throw new ApplicationException("Not found :" + debuggerCmdline); } } else { cmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "appverif.exe"); if (!File.Exists(cmdline)) { throw new ApplicationException("Not found :" + cmdline); } debuggerCmdline = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), "Debugging Tools for Windows (x64)", "windbg.exe"); if (!File.Exists(debuggerCmdline)) { throw new ApplicationException("Not found :" + debuggerCmdline); } } TestUtility.RunCommand(cmdline, argument, true, false); } catch { TestUtility.LogInformation("Failed to detach Appverifier"); } }
public static void ClearApplicationEventLog() { using (EventLog eventLog = new EventLog("Application")) { eventLog.Clear(); } for (int i = 0; i < 5; i++) { TestUtility.LogInformation("Waiting 1 seconds for eventlog to clear..."); Thread.Sleep(1000); EventLog systemLog = new EventLog("Application"); if (systemLog.Entries.Count == 0) { break; } } }
public static List <String> GetApplicationEvent(int id, DateTime startFrom) { var result = new List <String>(); TestUtility.LogInformation("Waiting 1 seconds for eventlog to update..."); Thread.Sleep(1000); EventLog systemLog = new EventLog("Application"); foreach (EventLogEntry entry in systemLog.Entries) { if (entry.InstanceId == id && entry.TimeWritten >= startFrom) { result.Add(entry.ReplacementStrings[0]); } } return(result); }
public void SetIdleTimeoutForAppPool(string appPoolName, int idleTimeoutMinutes) { TestUtility.LogTrace(String.Format("#################### Setting idleTimeout to {0} minutes for AppPool {1} ####################", idleTimeoutMinutes, appPoolName)); try { using (ServerManager serverManager = GetServerManager()) { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName].ProcessModel.IdleTimeout = TimeSpan.FromMinutes(idleTimeoutMinutes); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Setting idleTimeout to {0} minutes for AppPool {1} failed. Reason: {2} ####################", idleTimeoutMinutes, appPoolName, ex.Message)); } }
public void ChangeApplicationPath(string siteName, int appIndex, string path) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Changing Path for App {0} of Site {1} to {2} ####################", appIndex, siteName, path)); serverManager.Sites[siteName].Applications[appIndex].SetAttributeValue("path", path); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Changing Path for App {0} of Site {1} to {2} failed. Reason: {3} ####################", appIndex, siteName, path, ex.Message)); } }
public void SetMaxProcessesForAppPool(string appPoolName, int maxProcesses) { TestUtility.LogTrace(String.Format("#################### Setting maxProcesses to {0} for AppPool {1} ####################", maxProcesses, appPoolName)); try { using (ServerManager serverManager = GetServerManager()) { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName].ProcessModel.MaxProcesses = maxProcesses; serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Setting maxProcesses to {0} for AppPool {1} failed. Reason: {2} ####################", maxProcesses, appPoolName, ex.Message)); } }
public void RemoveApplication(string siteName, int appIndex) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Deleting App {0} from Site {1} ####################", appIndex, siteName)); serverManager.Sites[siteName].Applications.RemoveAt(appIndex); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Deleting App {0} from Site {1} failed. Reason: {2} ####################", appIndex, siteName, ex.Message)); } }
public static void KillProcess(string processFileName) { string query = "Select * from Win32_Process Where Name = \"" + processFileName + "\""; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processList = searcher.Get(); foreach (ManagementObject obj in processList) { obj.InvokeMethod("Terminate", null); } Thread.Sleep(1000); processList = searcher.Get(); if (processList.Count > 0) { TestUtility.LogInformation("Failed to kill process " + processFileName); } }
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 void DeleteAppPool(string poolName) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Deleting App Pool {0} ####################", poolName)); ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools.Remove(appPools[poolName]); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Delete app pool {0} failed. Reason: {1} ####################", poolName, ex.Message)); } }
public void DeleteSite(string siteName) { try { using (ServerManager serverManager = GetServerManager()) { TestUtility.LogTrace(String.Format("#################### Deleting Site {0} ####################", siteName)); SiteCollection sites = serverManager.Sites; sites.Remove(sites[siteName]); serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Delete site {0} failed. Reason: {1} ####################", siteName, ex.Message)); } }
public void SetIdentityForAppPool(string appPoolName, string userName, string password) { TestUtility.LogTrace(String.Format("#################### Setting userName {0} and password {1} for AppPool {2} ####################", userName, password, appPoolName)); try { using (ServerManager serverManager = GetServerManager()) { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName].ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser; appPools[appPoolName].ProcessModel.UserName = userName; appPools[appPoolName].ProcessModel.Password = password; serverManager.CommitChanges(); } } catch (Exception ex) { TestUtility.LogInformation(String.Format("#################### Setting userName {0} and password {1} for AppPool {2} failed. Reason: {2} ####################", userName, password, appPoolName, ex.Message)); } }
public void Dispose() { TestUtility.LogInformation("TestWebSite::Dispose() Start"); if (_iisExpressPidBackup != -1) { var iisExpressProcess = Process.GetProcessById(Convert.ToInt32(_iisExpressPidBackup)); try { iisExpressProcess.Kill(); iisExpressProcess.WaitForExit(); iisExpressProcess.Close(); } catch { TestUtility.RunPowershellScript("stop-process -id " + _iisExpressPidBackup); } } TestUtility.LogInformation("TestWebSite::Dispose() End"); }