public void ChangeAllFeatureProperties() { using (HttpClient client = ApiHttpClient.Create()) { var webserverFeature = Utils.GetFeature(client, HANDLERS_URL, null, null); Assert.NotNull(webserverFeature); AllowOverride(client, webserverFeature); TestScopedFeature(client, webserverFeature); Sites.EnsureNoSite(client, TEST_SITE_NAME); var site = Sites.CreateSite(client, TEST_SITE_NAME, Utils.GetAvailablePort(), Sites.TEST_SITE_PATH); Assert.NotNull(site); try { var siteFeature = Utils.GetFeature(client, HANDLERS_URL, site.Value <string>("name"), "/"); Assert.NotNull(siteFeature); AllowOverride(client, siteFeature); TestScopedFeature(client, siteFeature); client.Delete(Utils.Self(siteFeature)); } finally { Sites.EnsureNoSite(client, site.Value <string>("name")); } } }
public void WebFileRange() { var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, "web_file_range_test"); if (Directory.Exists(physicalPath)) { Directory.Delete(physicalPath, true); Directory.CreateDirectory(physicalPath); } JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { try { var dirs = new List <string>() { "dir1", "dir2", "dir3", "dir4" }; var files = new List <string>() { "file1.txt", "file2.txt", "file3.txt", "file4.txt" }; Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); Assert.NotNull(site); foreach (var dir in dirs) { Directory.CreateDirectory(Path.Combine(physicalPath, dir)); } foreach (var file in files) { File.Create(Path.Combine(physicalPath, file)).Dispose(); } JObject folder = Utils.FollowLink(client, site, "files"); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, Utils.GetLink(folder, "files")); req.Headers.Add("Range", "files=2-5"); var res = client.SendAsync(req).Result; Assert.True(res.Content.Headers.Contains("Content-Range")); Assert.True(res.Content.Headers.GetValues("Content-Range").First().Equals("2-5/8")); var children = JObject.Parse(res.Content.ReadAsStringAsync().Result)["files"].ToObject <IEnumerable <JObject> >(); Assert.True(children.Count() == 4); } finally { Directory.Delete(physicalPath, true); if (site != null) { client.Delete(Utils.Self(site)); } } } }
public async Task WebServer() { using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, SiteName); int port = Utils.GetAvailablePort(); JObject site = Sites.CreateSite(client, SiteName, port, SitePath); try { using (var stresser = new SiteStresser($"http://localhost:{port}")) using (var serverMonitor = new ServerMonitor()) { int tries = 0; JObject snapshot = null; while (tries < 10) { snapshot = serverMonitor.Current; _output.WriteLine("Waiting for webserver to track requests per sec and processes"); _output.WriteLine(snapshot == null ? "Snapshot is null" : snapshot.ToString(Formatting.Indented)); if (snapshot != null && snapshot["requests"].Value <long>("per_sec") > 0 && snapshot["cpu"].Value <long>("threads") > 0) { break; } await Task.Delay(1000); tries++; } _output.WriteLine("Validating webserver monitoring data"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); Assert.True(snapshot["requests"].Value <long>("per_sec") > 0); Assert.True(snapshot["network"].Value <long>("total_bytes_sent") > 0); Assert.True(snapshot["network"].Value <long>("total_bytes_recv") > 0); Assert.True(snapshot["network"].Value <long>("total_connection_attempts") > 0); Assert.True(snapshot["requests"].Value <long>("total") > 0); Assert.True(snapshot["memory"].Value <long>("private_working_set") > 0); Assert.True(snapshot["memory"].Value <long>("system_in_use") > 0); Assert.True(snapshot["memory"].Value <long>("installed") > 0); Assert.True(snapshot["cpu"].Value <long>("threads") > 0); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); Assert.True(snapshot["cpu"].Value <long>("percent_usage") >= 0); Assert.True(snapshot["cpu"].Value <long>("system_percent_usage") >= 0); Assert.True(serverMonitor.ErrorCount == 0); } } finally { client.Delete(Utils.Self(site)); } } }
public async Task CanCreateCcsBinding() { RequireCcsTestInfrastructure(); CcsUser user = await CcsUser.Get(); Assert.True(Enable(FOLDER_PATH, user.Username, user.Password, PVK_PASS)); JObject site; const string siteName = "CcsBindingTestSite"; using (var client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, siteName); site = Sites.CreateSite(_output, client, siteName, Utils.GetAvailablePort(), Sites.TEST_SITE_PATH); Assert.NotNull(site); try { JObject cert = GetCertificates().FirstOrDefault(c => { return(c.Value <string>("alias").Equals(CERT_NAME + ".pfx") && c.Value <JObject>("store").Value <string>("name").Equals(NAME, StringComparison.OrdinalIgnoreCase)); }); Assert.NotNull(cert); site["bindings"] = JToken.FromObject(new object[] { new { port = 443, protocol = "https", ip_address = "*", hostname = CERT_NAME, certificate = cert, require_sni = true } }); site = client.Patch(Utils.Self(site), site); Assert.NotNull(site); string index = Path.Combine(site.Value <string>("physical_path"), "index.html"); if (!File.Exists(index)) { File.WriteAllText(index, $"<h1>{siteName}</h1>"); } site = client.Get(Utils.Self(site)); JObject binding = site["bindings"].ToObject <IEnumerable <JObject> >().First(); Assert.NotNull(binding["certificate"]); Assert.True(binding.Value <bool>("require_sni")); JObject certificate = client.Get(Utils.Self(binding.Value <JObject>("certificate"))); Assert.NotNull(certificate); Assert.True(certificate["store"].Value <string>("name").Equals(NAME)); } finally { Sites.EnsureNoSite(client, siteName); } } }
public async Task AppPool() { using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, SiteName); int port = Utils.GetAvailablePort(); JObject site = Sites.CreateSite(client, SiteName, port, SitePath); try { JObject appPool = client.Get(Utils.Self((JObject)site["application_pool"])); using (var stresser = new SiteStresser($"http://localhost:{port}")) using (var serverMonitor = new ServerMonitor(Utils.GetLink(appPool, "monitoring"))) { await Task.Delay(2000); JObject snapshot = serverMonitor.Current; _output.WriteLine("Validing monitoring data for application pool"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); Assert.True(snapshot["requests"].Value <long>("total") > 0); Assert.True(snapshot["memory"].Value <long>("private_working_set") > 0); Assert.True(snapshot["memory"].Value <long>("system_in_use") > 0); Assert.True(snapshot["memory"].Value <long>("installed") > 0); Assert.True(snapshot["cpu"].Value <long>("threads") > 0); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); int tries = 0; while (tries < 5) { snapshot = serverMonitor.Current; if (serverMonitor.Current["requests"].Value <long>("per_sec") > 0) { break; } await Task.Delay(1000); tries++; } _output.WriteLine("Validing monitoring data for application pool"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); Assert.True(snapshot["requests"].Value <long>("per_sec") > 0); Assert.True(serverMonitor.ErrorCount == 0); } } finally { client.Delete(Utils.Self(site)); } } }
public void CopyDirectory() { string startName = "copy_dir_test"; string destName = "copy_dir_dest"; var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, startName); var destPhysicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, destName); CreateTestDirectory(physicalPath); JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); try { var rootDir = Utils.FollowLink(client, site, "files"); var rootDirFileInfo = Utils.FollowLink(client, rootDir.Value <JObject>("file_info"), "self"); object copy = new { name = destName, parent = rootDirFileInfo.Value <JObject>("parent"), file = rootDirFileInfo }; var copyInfo = client.Post(Utils.GetLink(rootDirFileInfo, "copy"), copy); // // Wait for copy to finish HttpResponseMessage res = null; do { res = client.GetAsync(Utils.Self(copyInfo)).Result; } while (res.StatusCode == HttpStatusCode.OK); // Don't add code between copy end and verification so we can make sure files aren't being held Assert.True(VerifyTestDirectory(physicalPath)); Assert.True(VerifyTestDirectory(destPhysicalPath)); } finally { if (site != null) { Sites.DeleteSite(client, Utils.Self(site)); } if (Directory.Exists(physicalPath)) { Directory.Delete(physicalPath, true); } if (Directory.Exists(destPhysicalPath)) { Directory.Delete(destPhysicalPath, true); } } } }
public void MoveDirectory() { string startName = "move_dir_test"; string destName = "move_dir_dest"; var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, startName); var destPhysicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, destName); CreateTestDirectory(physicalPath); JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); try { var rootDir = Utils.FollowLink(client, site, "files"); var rootDirFileInfo = Utils.FollowLink(client, rootDir.Value <JObject>("file_info"), "self"); object move = new { name = destName, parent = rootDirFileInfo.Value <JObject>("parent"), file = rootDirFileInfo }; var moveInfo = client.Post(Utils.GetLink(rootDirFileInfo, "move"), move); // Wait for move to finish HttpResponseMessage res = null; while (res == null || res.StatusCode == HttpStatusCode.OK) { res = client.GetAsync(Utils.Self(moveInfo)).Result; Thread.Sleep(25); } Assert.True(!Directory.Exists(physicalPath)); Assert.True(VerifyTestDirectory(destPhysicalPath)); } finally { if (site != null) { Sites.DeleteSite(client, Utils.Self(site)); } if (Directory.Exists(destPhysicalPath)) { Directory.Delete(destPhysicalPath, true); } } } }
public async Task HandleRestartIis() { using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, SiteName); int port = Utils.GetAvailablePort(); JObject site = Sites.CreateSite(client, SiteName, port, SitePath); try { using (var stresser = new SiteStresser($"http://localhost:{port}")) using (var serverMonitor = new ServerMonitor()) using (var sc = new ServiceController("W3SVC")) { JObject snapshot = await serverMonitor.GetSnapshot(5000); _output.WriteLine("Validating server is running worker processes"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); _output.WriteLine("Restarting IIS"); sc.Stop(); DateTime stopTime = DateTime.Now; var timeout = TimeSpan.FromSeconds(5); while (DateTime.Now - stopTime < timeout && sc.Status != ServiceControllerStatus.Stopped) { await Task.Delay(1000); } sc.Start(); Assert.True(serverMonitor.ErrorCount == 0); int tries = 0; while (tries < 5) { snapshot = serverMonitor.Current; _output.WriteLine("checking for requests / sec counter increase after startup"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); if (snapshot["requests"].Value <long>("per_sec") > 0) { break; } await Task.Delay(1000); tries++; } Assert.True(snapshot["requests"].Value <long>("per_sec") > 0); Assert.True(serverMonitor.ErrorCount == 0); } } finally { client.Delete(Utils.Self(site)); } } }
public async Task WebSite() { const string name = SiteName + "z"; using (HttpClient client = ApiHttpClient.Create()) { JObject pool = ApplicationPools.GetAppPool(client, name); if (pool == null) { pool = ApplicationPools.CreateAppPool(client, name); } JObject site = Sites.GetSite(client, name); if (site == null) { site = Sites.CreateSite(client, name, Utils.GetAvailablePort(), SitePath, true, pool); } int port = site["bindings"].ToObject <IEnumerable <JObject> >().First().Value <int>("port"); try { using (var stresser = new SiteStresser($"http://localhost:{port}")) using (var serverMonitor = new ServerMonitor(Utils.GetLink(site, "monitoring"))) { int tries = 0; JObject snapshot = null; while (tries < 15) { snapshot = serverMonitor.Current; if (snapshot != null && serverMonitor.Current["requests"].Value <long>("per_sec") > 0 && snapshot["network"].Value <long>("total_bytes_sent") > 0) { break; } await Task.Delay(1000); tries++; } Assert.True(snapshot["requests"].Value <long>("per_sec") > 0); Assert.True(snapshot["network"].Value <long>("total_bytes_sent") > 0); Assert.True(snapshot["network"].Value <long>("total_bytes_recv") > 0); Assert.True(snapshot["network"].Value <long>("total_connection_attempts") > 0); Assert.True(snapshot["requests"].Value <long>("total") > 0); Assert.True(snapshot["memory"].Value <long>("private_working_set") > 0); Assert.True(snapshot["memory"].Value <long>("system_in_use") > 0); Assert.True(snapshot["memory"].Value <long>("installed") > 0); Assert.True(snapshot["cpu"].Value <long>("threads") > 0); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); Assert.True(serverMonitor.ErrorCount == 0); } } finally { client.Delete(Utils.Self(site)); client.Delete(Utils.Self(pool)); } } }
public async Task ManySites(int numberSites) { HttpClient client = null; SiteStresser[] stressers = new SiteStresser[numberSites]; ServerMonitor serverMonitor = null; var pools = new JObject[numberSites]; var sites = new JObject[numberSites]; try { client = ApiHttpClient.Create(); for (int i = 0; i < numberSites; i++) { string name = SiteName + i; var pool = ApplicationPools.GetAppPool(client, name); if (pool == null) { pool = ApplicationPools.CreateAppPool(client, name); } pools[i] = pool; } serverMonitor = new ServerMonitor(); for (int i = 0; i < numberSites; i++) { string name = SiteName + i; JObject site = Sites.GetSite(client, name); if (site == null) { site = Sites.CreateSite(client, name, Utils.GetAvailablePort(), SitePath, true, pools[i]); } sites[i] = site; int port = site["bindings"].ToObject <IEnumerable <JObject> >().First().Value <int>("port"); stressers[i] = new SiteStresser($"http://localhost:{port}"); } await Task.Delay(1000); var start = DateTime.Now; var timeout = TimeSpan.FromSeconds(20); _output.WriteLine($"Created {numberSites} sites"); _output.WriteLine($"Waiting for all site processes to start"); while (DateTime.Now - start <= timeout && (serverMonitor.Current["cpu"].Value <long>("processes") < numberSites || serverMonitor.Current["network"].Value <long>("total_bytes_sent") == 0)) { await Task.Delay(1000); } if (DateTime.Now - start > timeout) { throw new Exception("timeout"); } JObject snapshot = serverMonitor.Current; _output.WriteLine("Validating webserver monitoring data"); _output.WriteLine(snapshot.ToString(Formatting.Indented)); Assert.True(snapshot["network"].Value <long>("total_bytes_sent") > 0); Assert.True(snapshot["network"].Value <long>("total_bytes_recv") > 0); Assert.True(snapshot["network"].Value <long>("total_connection_attempts") > 0); Assert.True(snapshot["requests"].Value <long>("total") > 0); Assert.True(snapshot["memory"].Value <long>("private_working_set") > 0); Assert.True(snapshot["memory"].Value <long>("system_in_use") > 0); Assert.True(snapshot["memory"].Value <long>("installed") > 0); Assert.True(snapshot["cpu"].Value <long>("threads") > 0); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); Assert.True(snapshot["cpu"].Value <long>("percent_usage") >= 0); Assert.True(snapshot["cpu"].Value <long>("threads") > 0); Assert.True(snapshot["cpu"].Value <long>("processes") > 0); Assert.True(serverMonitor.ErrorCount == 0); } finally { for (int i = 0; i < stressers.Length; i++) { if (stressers[i] != null) { stressers[i].Dispose(); } } if (serverMonitor != null) { serverMonitor.Dispose(); } for (int i = 0; i < sites.Length; i++) { if (sites[i] != null) { client.Delete(Utils.Self(sites[i])); client.Delete(Utils.Self((JObject)sites[i]["application_pool"])); } } if (client != null) { client.Dispose(); } } }