}
@Test
public void testNodeHealthScript() throws Exception {
RecordFactory factory = RecordFactoryProvider.getRecordFactory(null);
NodeHealthStatus healthStatus =
factory.newRecordInstance(NodeHealthStatus.class);
String errorScript = "echo ERROR\n echo \"Tracker not healthy\"";
String normalScript = "echo \"I am all fine\"";
String timeOutScript = "sleep 4\n echo\"I am fine\"";
Configuration conf = getConfForNodeHealthScript();
conf.writeXml(new FileOutputStream(nodeHealthConfigFile));
conf.addResource(nodeHealthConfigFile.getName());
NodeHealthCheckerService nodeHealthChecker = new NodeHealthCheckerService(
conf);
TimerTask timer = nodeHealthChecker.getTimer();
writeNodeHealthScriptFile(normalScript, true);
timer.run();
nodeHealthChecker.setHealthStatus(healthStatus);
LOG.info("Checking initial healthy condition");
// Check proper report conditions.
Assert.assertTrue("Node health status reported unhealthy", healthStatus
.getIsNodeHealthy());
Assert.assertTrue("Node health status reported unhealthy", healthStatus
.getHealthReport().isEmpty());
// write out error file.
// Healthy to unhealthy transition
writeNodeHealthScriptFile(errorScript, true);
// Run timer
timer.run();
// update health status
nodeHealthChecker.setHealthStatus(healthStatus);
LOG.info("Checking Healthy--->Unhealthy");
Assert.assertFalse("Node health status reported healthy", healthStatus
.getIsNodeHealthy());
Assert.assertFalse("Node health status reported healthy", healthStatus
.getHealthReport().isEmpty());
// Check unhealthy to healthy transitions.
writeNodeHealthScriptFile(normalScript, true);
timer.run();
nodeHealthChecker.setHealthStatus(healthStatus);
LOG.info("Checking UnHealthy--->healthy");
// Check proper report conditions.
Assert.assertTrue("Node health status reported unhealthy", healthStatus
.getIsNodeHealthy());
Assert.assertTrue("Node health status reported unhealthy", healthStatus
.getHealthReport().isEmpty());
// Healthy to timeout transition.
writeNodeHealthScriptFile(timeOutScript, true);
timer.run();
nodeHealthChecker.setHealthStatus(healthStatus);
LOG.info("Checking Healthy--->timeout");
Assert.assertFalse("Node health status reported healthy even after timeout",
healthStatus.getIsNodeHealthy());
Assert.assertEquals("Node time out message not propogated", healthStatus
.getHealthReport(),
NodeHealthCheckerService.NODE_HEALTH_SCRIPT_TIMED_OUT_MSG);
}