private static void RewriteWebConfig(AutoRemovableDirectory tempFiles, ConfigReplacements replacements) { var webConfig = tempFiles.ReadFile("Web.config"); var configRewriter = new ConfigRewriter(new ConfigSources(new Dictionary <string, string>())); var rewrittenWebConfig = configRewriter.Rewrite(webConfig, replacements); tempFiles.WriteFile("Web.config", rewrittenWebConfig); }
public void HeartbeatService_Basic() { // Verify that heartbeat service works by modifying its configuration file // to monitor two simulated services and then verify that the monitoring // works. HttpServer httpServer = null; Process svcProcess = null; ConfigRewriter rewriter; Assembly assembly; string iniPath; assembly = typeof(LillTek.Datacenter.HeartbeatService.Program).Assembly; iniPath = Config.GetConfigPath(assembly); rewriter = new ConfigRewriter(iniPath); try { // Initialize the local HTTP server that will simulate the monitored services. httpServer = new HttpServer(new IPEndPoint[] { new IPEndPoint(IPAddress.Any, 666) }, new IHttpModule[] { this }, 10, 10, 8196); httpServer.Start(); host2Health = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase); host2Health["testservice01.lilltek.com"] = true; host2Health["testservice02.lilltek.com"] = true; host2Health["testservice03.lilltek.com"] = true; // Rewrite the config file to have it start in hub mode // and then start the router service as a form application. rewriter.Rewrite(new ConfigRewriteTag[0]); // $hack(jeff.lill): // // Append test related settings to the configuration file. Since these settings // appear after the defaults, the written settings will override. using (var file = File.AppendText(iniPath)) { const string prefix = "LillTek.Datacenter.HeartbeatService."; file.WriteLine(prefix + "MonitorUri[0] = http://testservice01.lilltek.com:666/Heartbeat.aspx?global=0"); file.WriteLine(prefix + "MonitorUri[1] = http://testservice02.lilltek.com:666/Heartbeat.aspx?global=0"); file.WriteLine(prefix + "MonitorUri[2] = http://testservice03.lilltek.com:666/Heartbeat.aspx?global=0"); file.WriteLine(prefix + "BkInterval = 1s"); file.WriteLine(prefix + "PollInterval = 1s"); } svcProcess = Helper.StartProcess(assembly, "-mode:form -start"); Thread.Sleep(10000); // Give the process a chance to spin up // Give the service a chance to ping the test a few times and then // make a request to the heartbeat handler to verify that it indicates // that the server is healthy. Thread.Sleep(3000); Assert.IsTrue(GetHealth()); // Now indicate that the first target service is unhealthy, give the heartbeat // handler a chance to ping the service, and verify that it determined // the server is unhealthy. host2Health["testservice01.lilltek.com"] = false; Thread.Sleep(3000); Assert.IsFalse(GetHealth()); // Now indicate that the first target service is healthy, give the heartbeat // handler a chance to ping the service, and verify that it determined // the server is healthy. host2Health["testservice01.lilltek.com"] = true; Thread.Sleep(3000); Assert.IsTrue(GetHealth()); // Now indicate that the second and third target services are unhealthy, give the heartbeat // handler a chance to ping the service, and verify that it determined // the server is unhealthy. host2Health["testservice02.lilltek.com"] = false; host2Health["testservice03.lilltek.com"] = false; Thread.Sleep(3000); Assert.IsFalse(GetHealth()); // Now mark the second service as healthy and verify that the // server is still unhealthy (due to the third service). host2Health["testservice02.lilltek.com"] = true; Thread.Sleep(3000); Assert.IsFalse(GetHealth()); // Now mark all services as healthy and verify that // the server is considered healthy. host2Health["testservice03.lilltek.com"] = true; Thread.Sleep(3000); Assert.IsTrue(GetHealth()); // Stop the simulated target HTTP server and verify that the handler // detects the failure. httpServer.Stop(); httpServer = null; Thread.Sleep(3000); Assert.IsFalse(GetHealth()); } finally { rewriter.Restore(); if (httpServer != null) { httpServer.Stop(); httpServer = null; } if (svcProcess != null) { svcProcess.Kill(); svcProcess.Close(); } } }
public void ConfigRewriter_Test() { string fname = Path.GetTempPath() + "ConfigRewriteTest.ini"; ConfigRewriter rewriter = new ConfigRewriter(fname); StreamReader reader = null; StreamWriter writer = null; string orgText = @" // This is a test foo=bar // $replace(tag1) -- $REPLACE(TAG2) bar=foo // $( "; string newText = @" // This is a test foo=bar key1=10 key2=20 bar=foo // $( "; try { writer = new StreamWriter(fname); writer.Write(orgText); writer.Close(); writer = null; rewriter.Restore(); // This shouldn't case a problem rewriter.Rewrite(new ConfigRewriteTag[] { new ConfigRewriteTag("tag1", "key1=10"), new ConfigRewriteTag("tag2", "key2=20\r\n") }); reader = new StreamReader(fname); Assert.AreEqual(newText, reader.ReadToEnd()); reader.Close(); reader = null; rewriter.Restore(); reader = new StreamReader(fname); Assert.AreEqual(orgText, reader.ReadToEnd()); reader.Close(); reader = null; rewriter.Restore(); // This shouldn't case a problem } finally { if (reader != null) { reader.Close(); } if (writer != null) { writer.Close(); } if (File.Exists(fname)) { File.Delete(fname); } } }
public void RouterService_RootMode() { // Verify that the router service actually deploys a root router // by starting an instance of the application and a couple of // hub routers and making sure that the service is routing messages // between the two hubs. Process svcProcess = null; HubRouter hub1 = null; HubRouter hub2 = null; ConfigRewriter rewriter; Assembly assembly; string iniPath; assembly = typeof(LillTek.Datacenter.RouterService.Program).Assembly; iniPath = Config.GetConfigPath(assembly); rewriter = new ConfigRewriter(iniPath); try { // Rewrite the config file to have it start in root mode // and then start the router service as a form application. rewriter.Rewrite(new ConfigRewriteTag[] { new ConfigRewriteTag("Mode", "#undef HUBMODE\r\n#define ROOTMODE\r\n#define HUBNAME hub\r\n") }); svcProcess = Helper.StartProcess(assembly, "-mode:form -start"); Thread.Sleep(10000); // Give the process a chance to spin up // Crank up a couple of hub routers and send a message from // one to the other. hub1 = CreateHub(Helper.MachineName + ":" + Const.DCRootPort.ToString(), "hub1", group1); hub2 = CreateHub(Helper.MachineName + ":" + Const.DCRootPort.ToString(), "hub2", group2); Thread.Sleep(2000); recvMsg = null; hub1.SendTo("logical://hub2", new PropertyMsg()); Thread.Sleep(1000); Assert.IsNotNull(recvMsg); } finally { rewriter.Restore(); if (svcProcess != null) { svcProcess.Kill(); svcProcess.Close(); } if (hub1 != null) { hub1.Stop(); } if (hub2 != null) { hub2.Stop(); } } }
public void RouterService_HubMode() { // Verify that the router service actually deploys a hub router // by starting an instance of the application and a couple of // non-P2P leaf routers and making sure that the service is // routing messages between the two leaves. Process svcProcess = null; LeafRouter leaf1 = null; LeafRouter leaf2 = null; ConfigRewriter rewriter; Assembly assembly; string iniPath; assembly = typeof(LillTek.Datacenter.RouterService.Program).Assembly; iniPath = Config.GetConfigPath(assembly); rewriter = new ConfigRewriter(iniPath); try { // Rewrite the config file to have it start in hub mode // and then start the router service as a form application. rewriter.Rewrite(new ConfigRewriteTag[] { new ConfigRewriteTag("Mode", "#define HUBMODE\r\n#undef ROOTMODE\r\n#define HUBNAME hub\r\n") }); svcProcess = Helper.StartProcess(assembly, "-mode:form -start"); Thread.Sleep(10000); // Give the process a chance to spin up // Crank up a couple of leaf routers and send a message from // one to the other. leaf1 = CreateLeaf("detached", Const.DCDefHubName, "leaf1", Const.DCCloudEP.ToString(), false); leaf2 = CreateLeaf("detached", Const.DCDefHubName, "leaf2", Const.DCCloudEP.ToString(), false); Thread.Sleep(2000); recvMsg = null; leaf1.SendTo("logical://leaf2", new PropertyMsg()); Thread.Sleep(1000); Assert.IsNotNull(recvMsg); } finally { rewriter.Restore(); if (svcProcess != null) { svcProcess.Kill(); svcProcess.Close(); } if (leaf1 != null) { leaf1.Stop(); } if (leaf2 != null) { leaf2.Stop(); } } }