/// <exception cref="System.Exception"/>
 protected override void ServiceInit(Configuration conf)
 {
     if (NodeHealthScriptRunner.ShouldRun(conf))
     {
         nodeHealthScriptRunner = new NodeHealthScriptRunner();
         AddService(nodeHealthScriptRunner);
     }
     AddService(dirsHandler);
     base.ServiceInit(conf);
 }
            public NodeHealthMonitorExecutor(NodeHealthScriptRunner _enclosing, string[] args
                                             )
            {
                this._enclosing = _enclosing;
                AList <string> execScript = new AList <string>();

                execScript.AddItem(this._enclosing.nodeHealthScript);
                if (args != null)
                {
                    Sharpen.Collections.AddAll(execScript, Arrays.AsList(args));
                }
                this._enclosing.shexec = new Shell.ShellCommandExecutor(Sharpen.Collections.ToArray
                                                                            (execScript, new string[execScript.Count]), null, null, this._enclosing.scriptTimeout
                                                                        );
            }
        public virtual void TestNodeHealthScriptShouldRun()
        {
            // Node health script should not start if there is no property called
            // node health script path.
            NUnit.Framework.Assert.IsFalse("By default Health script should not have started"
                                           , NodeHealthScriptRunner.ShouldRun(new Configuration()));
            Configuration conf = GetConfForNodeHealthScript();

            // Node health script should not start if the node health script does not
            // exists
            NUnit.Framework.Assert.IsFalse("Node health script should start", NodeHealthScriptRunner
                                           .ShouldRun(conf));
            // Create script path.
            conf.WriteXml(new FileOutputStream(nodeHealthConfigFile));
            conf.AddResource(nodeHealthConfigFile.GetName());
            WriteNodeHealthScriptFile(string.Empty, false);
            // Node health script should not start if the node health script is not
            // executable.
            NUnit.Framework.Assert.IsFalse("Node health script should start", NodeHealthScriptRunner
                                           .ShouldRun(conf));
            WriteNodeHealthScriptFile(string.Empty, true);
            NUnit.Framework.Assert.IsTrue("Node health script should start", NodeHealthScriptRunner
                                          .ShouldRun(conf));
        }
        public virtual void TestNodeHealthScript()
        {
            RecordFactory    factory       = RecordFactoryProvider.GetRecordFactory(null);
            NodeHealthStatus healthStatus  = factory.NewRecordInstance <NodeHealthStatus>();
            string           errorScript   = "echo ERROR\n echo \"Tracker not healthy\"";
            string           normalScript  = "echo \"I am all fine\"";
            string           timeOutScript = Shell.Windows ? "@echo off\nping -n 4 127.0.0.1 >nul\necho \"I am fine\""
                                 : "sleep 4\necho \"I am fine\"";
            Configuration conf = GetConfForNodeHealthScript();

            conf.WriteXml(new FileOutputStream(nodeHealthConfigFile));
            conf.AddResource(nodeHealthConfigFile.GetName());
            WriteNodeHealthScriptFile(normalScript, true);
            NodeHealthCheckerService nodeHealthChecker = new NodeHealthCheckerService();

            nodeHealthChecker.Init(conf);
            NodeHealthScriptRunner nodeHealthScriptRunner = nodeHealthChecker.GetNodeHealthScriptRunner
                                                                ();
            TimerTask timerTask = nodeHealthScriptRunner.GetTimerTask();

            timerTask.Run();
            SetHealthStatus(healthStatus, nodeHealthChecker.IsHealthy(), nodeHealthChecker.GetHealthReport
                                (), nodeHealthChecker.GetLastHealthReportTime());
            Log.Info("Checking initial healthy condition");
            // Check proper report conditions.
            NUnit.Framework.Assert.IsTrue("Node health status reported unhealthy", healthStatus
                                          .GetIsNodeHealthy());
            NUnit.Framework.Assert.IsTrue("Node health status reported unhealthy", healthStatus
                                          .GetHealthReport().Equals(nodeHealthChecker.GetHealthReport()));
            // write out error file.
            // Healthy to unhealthy transition
            WriteNodeHealthScriptFile(errorScript, true);
            // Run timer
            timerTask.Run();
            // update health status
            SetHealthStatus(healthStatus, nodeHealthChecker.IsHealthy(), nodeHealthChecker.GetHealthReport
                                (), nodeHealthChecker.GetLastHealthReportTime());
            Log.Info("Checking Healthy--->Unhealthy");
            NUnit.Framework.Assert.IsFalse("Node health status reported healthy", healthStatus
                                           .GetIsNodeHealthy());
            NUnit.Framework.Assert.IsTrue("Node health status reported healthy", healthStatus
                                          .GetHealthReport().Equals(nodeHealthChecker.GetHealthReport()));
            // Check unhealthy to healthy transitions.
            WriteNodeHealthScriptFile(normalScript, true);
            timerTask.Run();
            SetHealthStatus(healthStatus, nodeHealthChecker.IsHealthy(), nodeHealthChecker.GetHealthReport
                                (), nodeHealthChecker.GetLastHealthReportTime());
            Log.Info("Checking UnHealthy--->healthy");
            // Check proper report conditions.
            NUnit.Framework.Assert.IsTrue("Node health status reported unhealthy", healthStatus
                                          .GetIsNodeHealthy());
            NUnit.Framework.Assert.IsTrue("Node health status reported unhealthy", healthStatus
                                          .GetHealthReport().Equals(nodeHealthChecker.GetHealthReport()));
            // Healthy to timeout transition.
            WriteNodeHealthScriptFile(timeOutScript, true);
            timerTask.Run();
            SetHealthStatus(healthStatus, nodeHealthChecker.IsHealthy(), nodeHealthChecker.GetHealthReport
                                (), nodeHealthChecker.GetLastHealthReportTime());
            Log.Info("Checking Healthy--->timeout");
            NUnit.Framework.Assert.IsFalse("Node health status reported healthy even after timeout"
                                           , healthStatus.GetIsNodeHealthy());
            NUnit.Framework.Assert.IsTrue("Node script time out message not propogated", healthStatus
                                          .GetHealthReport().Equals(NodeHealthScriptRunner.NodeHealthScriptTimedOutMsg + NodeHealthCheckerService
                                                                    .Separator + nodeHealthChecker.GetDiskHandler().GetDisksHealthReport(false)));
        }