@Test
  public void testDeleteHost() throws Exception {
    String cluster1 = getUniqueName();

    createCluster(cluster1);

    Cluster cluster = clusters.getCluster(cluster1);
    cluster.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();  // Host will belong to the cluster and contain components
    String host2 = getUniqueName();  // Host will belong to the cluster and not contain any components

    addHostToCluster(host1, cluster1);
    addHostToCluster(host2, cluster1);
    String host3 = getUniqueName();  // Host is not registered

    // Add components to host1
    createServiceComponentHost(cluster1, serviceName, componentName1, host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName2, host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName3, host1, null);

    // Install
    installService(cluster1, serviceName, false, false);

    // Treat host components on host1 as up and healthy
    Map<String, ServiceComponentHost> hostComponents = cluster.getService(serviceName).getServiceComponent(componentName1).getServiceComponentHosts();
    for (Map.Entry<String, ServiceComponentHost> entry : hostComponents.entrySet()) {
      ServiceComponentHost cHost = entry.getValue();
      cHost.handleEvent(new ServiceComponentHostInstallEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis(), cluster.getDesiredStackVersion().getStackId()));
      cHost.handleEvent(new ServiceComponentHostOpSucceededEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis()));
    }
    hostComponents = cluster.getService(serviceName).getServiceComponent(componentName2).getServiceComponentHosts();
    for (Map.Entry<String, ServiceComponentHost> entry : hostComponents.entrySet()) {
      ServiceComponentHost cHost = entry.getValue();
      cHost.handleEvent(new ServiceComponentHostInstallEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis(), cluster.getDesiredStackVersion().getStackId()));
      cHost.handleEvent(new ServiceComponentHostOpSucceededEvent(cHost.getServiceComponentName(), cHost.getHostName(), System.currentTimeMillis()));
    }

    Set<HostRequest> requests = new HashSet<>();
    requests.clear();
    requests.add(new HostRequest(host1, cluster1));

    // Case 1: Delete host that is still part of cluster, but do not specify the cluster_name in the request
    Set<ServiceComponentHostRequest> schRequests = new HashSet<>();
    // Disable HC for non-clients
    schRequests.add(new ServiceComponentHostRequest(cluster1, serviceName, componentName1, host1, "DISABLED"));
    schRequests.add(new ServiceComponentHostRequest(cluster1, serviceName, componentName2, host1, "DISABLED"));
    updateHostComponents(schRequests, new HashMap<>(), false);

    // Delete HC
    schRequests.clear();
    schRequests.add(new ServiceComponentHostRequest(cluster1, serviceName, componentName1, host1, null));
    schRequests.add(new ServiceComponentHostRequest(cluster1, serviceName, componentName2, host1, null));
    schRequests.add(new ServiceComponentHostRequest(cluster1, serviceName, componentName3, host1, null));
    controller.deleteHostComponents(schRequests);

    Assert.assertEquals(0, cluster.getServiceComponentHosts(host1).size());

    Assert.assertNull(topologyHostInfoDAO.findByHostname(host1));

    Long firstHostId = clusters.getHost(host1).getHostId();

    // Deletion without specifying cluster should be successful
    requests.clear();
    requests.add(new HostRequest(host1, null));
    try {
      HostResourceProviderTest.deleteHosts(controller, requests);
    } catch (Exception e) {
      fail("Did not expect an error deleting the host from the cluster. Error: " + e.getMessage());
    }
    // Verify host is no longer part of the cluster
    Assert.assertFalse(clusters.getHostsForCluster(cluster1).containsKey(host1));
    Assert.assertFalse(clusters.getClustersForHost(host1).contains(cluster));
    Assert.assertNull(topologyHostInfoDAO.findByHostname(host1));

    // verify there are no host role commands for the host
    List<HostRoleCommandEntity> tasks = hostRoleCommandDAO.findByHostId(firstHostId);
    assertEquals(0, tasks.size());

    // Case 2: Delete host that is still part of the cluster, and specify the cluster_name in the request
    requests.clear();
    requests.add(new HostRequest(host2, cluster1));
    try {
      HostResourceProviderTest.deleteHosts(controller, requests);
    } catch (Exception e) {
      fail("Did not expect an error deleting the host from the cluster. Error: " + e.getMessage());
    }
    // Verify host is no longer part of the cluster
    Assert.assertFalse(clusters.getHostsForCluster(cluster1).containsKey(host2));
    Assert.assertFalse(clusters.getClustersForHost(host2).contains(cluster));
    Assert.assertNull(topologyHostInfoDAO.findByHostname(host2));

    // Case 3: Attempt to delete a host that has already been deleted
    requests.clear();
    requests.add(new HostRequest(host1, null));
    try {
      HostResourceProviderTest.deleteHosts(controller, requests);
      Assert.fail("Expected a HostNotFoundException trying to remove a host that was already deleted.");
    } catch (HostNotFoundException e) {
      // expected
    }

    // Verify host does not exist
    try {
      clusters.getHost(host1);
      Assert.fail("Expected a HostNotFoundException.");
    } catch (HostNotFoundException e) {
      // expected
    }

    // Case 4: Attempt to delete a host that was never added to the cluster
    requests.clear();
    requests.add(new HostRequest(host3, null));
    try {
      HostResourceProviderTest.deleteHosts(controller, requests);
      Assert.fail("Expected a HostNotFoundException trying to remove a host that was never added.");
    } catch (HostNotFoundException e) {
      // expected
    }
  }
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