@Test
  public void testServiceCheckWhenHostIsUnhealthy() throws Exception, AuthorizationException {
    String cluster1 = getUniqueName();
    createCluster(cluster1);
    clusters.getCluster(cluster1).setDesiredStackVersion(new StackId("HDP-0.1"));
    String serviceName = "HDFS";
    createService(cluster1, serviceName, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";

    createServiceComponent(cluster1, serviceName, componentName1,
        State.INIT);
    createServiceComponent(cluster1, serviceName, componentName2,
        State.INIT);
    createServiceComponent(cluster1, serviceName, componentName3,
        State.INIT);

    String host1 = getUniqueName();
    String host2 = getUniqueName();
    String host3 = getUniqueName();

    addHostToCluster(host1, cluster1);
    addHostToCluster(host2, cluster1);
    addHostToCluster(host3, cluster1);

    createServiceComponentHost(cluster1, serviceName, componentName1,
        host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName2,
        host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName3,
        host2, null);
    createServiceComponentHost(cluster1, serviceName, componentName3,
        host3, null);

    // Install
    installService(cluster1, serviceName, false, false);
    clusters.getHost(host3).setState(HostState.UNHEALTHY);
    clusters.getHost(host2).setState(HostState.HEALTHY);

    // Start
    long requestId = startService(cluster1, serviceName, true, false);
    List<HostRoleCommand> commands = actionDB.getRequestTasks(requestId);
    int commandCount = 0;
    for(HostRoleCommand command : commands) {
      if(command.getRoleCommand() == RoleCommand.SERVICE_CHECK &&
          command.getRole() == Role.HDFS_SERVICE_CHECK) {
        Assert.assertTrue(command.getHostName().equals(host2));
        commandCount++;
      }
    }
    Assert.assertEquals("Expect only one service check.", 1, commandCount);

    stopService(cluster1, serviceName, false, false);

    clusters.getHost(host3).setState(HostState.HEALTHY);
    clusters.getHost(host2).setState(HostState.HEARTBEAT_LOST);

    requestId = startService(cluster1, serviceName, true, false);
    commands = actionDB.getRequestTasks(requestId);
    commandCount = 0;
    for(HostRoleCommand command : commands) {
      if(command.getRoleCommand() == RoleCommand.SERVICE_CHECK &&
          command.getRole() == Role.HDFS_SERVICE_CHECK) {
        Assert.assertTrue(command.getHostName().equals(host3));
        commandCount++;
      }
    }
    Assert.assertEquals("Expect only one service check.", 1, commandCount);

    RequestResourceFilter resourceFilter = new RequestResourceFilter("HDFS", null, null);
    ExecuteActionRequest actionRequest = new ExecuteActionRequest(cluster1, Role.HDFS_SERVICE_CHECK.name(), null, false);
    actionRequest.getResourceFilters().add(resourceFilter);
    Map<String, String> requestProperties = new HashMap<>();

    RequestStatusResponse response = controller.createAction(actionRequest, requestProperties);
    commands = actionDB.getRequestTasks(response.getRequestId());
    commandCount = 0;
    for(HostRoleCommand command : commands) {
      if(command.getRoleCommand() == RoleCommand.SERVICE_CHECK &&
          command.getRole() == Role.HDFS_SERVICE_CHECK) {
        Assert.assertTrue(command.getHostName().equals(host3));
        commandCount++;
      }
    }
    Assert.assertEquals("Expect only one service check.", 1, commandCount);

    // When both are unhealthy then it should raise an exception.
    clusters.getHost(host3).setState(HostState.WAITING_FOR_HOST_STATUS_UPDATES);
    clusters.getHost(host2).setState(HostState.INIT);
    try {
      controller.createAction(actionRequest, requestProperties);
      assertTrue("Exception should have been raised.", false);
    } catch (Exception e) {
      assertTrue(e.getMessage().contains("there were no healthy eligible hosts"));
    }
  }
Create a cluster with a service, and verify that the request tasks have the correct output log and error log paths.
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java