@Test
  public void testCreateCustomActions() throws Exception {
    final String cluster1 = getUniqueName();
    // !!! weird, but the assertions are banking on alphabetical order
    final String host1 = "a" + getUniqueName();
    final String host2 = "b" + getUniqueName();
    final String host3 = "c" + getUniqueName();

    setupClusterWithHosts(cluster1, "HDP-2.0.6",
        new ArrayList<String>() {{
          add(host1);
          add(host2);
          add(host3);
        }},
        "centos6");

    Cluster cluster = clusters.getCluster(cluster1);
    cluster.setDesiredStackVersion(new StackId("HDP-2.0.6"));
    cluster.setCurrentStackVersion(new StackId("HDP-2.0.6"));

    ConfigFactory cf = injector.getInstance(ConfigFactory.class);
    Config config1 = cf.createNew(cluster, "global", "version1",
        new HashMap<String, String>() {{
          put("key1", "value1");
        }}, new HashMap<>());

    Config config2 = cf.createNew(cluster, "core-site", "version1",
        new HashMap<String, String>() {{
          put("key1", "value1");
        }}, new HashMap<>());

    Config config3 = cf.createNew(cluster, "yarn-site", "version1",
        new HashMap<String, String>() {{
          put("test.password", "supersecret");
        }}, new HashMap<>());

    RepositoryVersionEntity repositoryVersion = repositoryVersion206;

    Service hdfs = cluster.addService("HDFS", repositoryVersion);
    Service mapred = cluster.addService("YARN", repositoryVersion);

    hdfs.addServiceComponent(Role.HDFS_CLIENT.name());
    hdfs.addServiceComponent(Role.NAMENODE.name());
    hdfs.addServiceComponent(Role.DATANODE.name());

    mapred.addServiceComponent(Role.RESOURCEMANAGER.name());

    hdfs.getServiceComponent(Role.HDFS_CLIENT.name()).addServiceComponentHost(host1);
    hdfs.getServiceComponent(Role.NAMENODE.name()).addServiceComponentHost(host1);
    hdfs.getServiceComponent(Role.DATANODE.name()).addServiceComponentHost(host1);
    hdfs.getServiceComponent(Role.DATANODE.name()).addServiceComponentHost(host2);

    String actionDef1 = getUniqueName();
    String actionDef2 = getUniqueName();


    ActionDefinition a1 = new ActionDefinition(actionDef1, ActionType.SYSTEM,
        "test,[optional1]", "", "", "Does file exist", TargetHostType.SPECIFIC, Integer.valueOf("100"), null);
    controller.getAmbariMetaInfo().addActionDefinition(a1);
    controller.getAmbariMetaInfo().addActionDefinition(new ActionDefinition(
        actionDef2, ActionType.SYSTEM, "", "HDFS", "DATANODE", "Does file exist",
        TargetHostType.ALL, Integer.valueOf("1000"), null));

    Map<String, String> params = new HashMap<String, String>() {{
      put("test", "test");
      put("pwd", "SECRET:yarn-site:1:test.password");
    }};

    Map<String, String> requestProperties = new HashMap<>();
    requestProperties.put(REQUEST_CONTEXT_PROPERTY, "Called from a test");
    requestProperties.put("datanode", "abc");

    ArrayList<String> hosts = new ArrayList<String>() {{add(host1);}};
    RequestResourceFilter resourceFilter = new RequestResourceFilter("HDFS", "DATANODE", hosts);
    List<RequestResourceFilter> resourceFilters = new ArrayList<>();
    resourceFilters.add(resourceFilter);

    ExecuteActionRequest actionRequest = new ExecuteActionRequest(cluster1, null, actionDef1, resourceFilters, null, params, false);
    RequestStatusResponse response = controller.createAction(actionRequest, requestProperties);
    assertEquals(1, response.getTasks().size());
    ShortTaskStatus taskStatus = response.getTasks().get(0);
    Assert.assertEquals(host1, taskStatus.getHostName());

    List<HostRoleCommand> storedTasks = actionDB.getRequestTasks(response.getRequestId());
    Stage stage = actionDB.getAllStages(response.getRequestId()).get(0);
    Assert.assertNotNull(stage);

    Assert.assertEquals(1, storedTasks.size());
    HostRoleCommand task = storedTasks.get(0);
    Assert.assertEquals(RoleCommand.ACTIONEXECUTE, task.getRoleCommand());
    Assert.assertEquals(actionDef1, task.getRole().name());
    Assert.assertEquals(host1, task.getHostName());
    ExecutionCommand cmd = task.getExecutionCommandWrapper().getExecutionCommand();
    // h1 has only DATANODE, NAMENODE, CLIENT sch's
    Assert.assertEquals(host1, cmd.getHostname());
    Assert.assertFalse(cmd.getLocalComponents().isEmpty());
    Assert.assertTrue(cmd.getLocalComponents().contains(Role.DATANODE.name()));
    Assert.assertTrue(cmd.getLocalComponents().contains(Role.NAMENODE.name()));
    Assert.assertTrue(cmd.getLocalComponents().contains(Role.HDFS_CLIENT.name()));
    Assert.assertFalse(cmd.getLocalComponents().contains(Role.RESOURCEMANAGER.name()));
    Type type = new TypeToken<Map<String, String>>(){}.getType();
    Map<String, String> hostParametersStage = StageUtils.getGson().fromJson(stage.getHostParamsStage(), type);
    Map<String, String> commandParametersStage = StageUtils.getGson().fromJson(stage.getCommandParamsStage(), type);

    Assert.assertTrue(commandParametersStage.containsKey("test"));
    Assert.assertTrue(commandParametersStage.containsKey("pwd"));
    Assert.assertEquals(commandParametersStage.get("pwd"), "supersecret");
    Assert.assertEquals("HDFS", cmd.getServiceName());
    Assert.assertEquals("DATANODE", cmd.getComponentName());
    Assert.assertNotNull(hostParametersStage.get("jdk_location"));
    Assert.assertEquals("900", cmd.getCommandParams().get("command_timeout"));
    Assert.assertEquals(requestProperties.get(REQUEST_CONTEXT_PROPERTY), response.getRequestContext());

    // !!! test that the action execution helper is using the right timeout
    a1.setDefaultTimeout(1800);
    actionRequest = new ExecuteActionRequest(cluster1, null, actionDef1, resourceFilters, null, params, false);
    response = controller.createAction(actionRequest, requestProperties);

    List<HostRoleCommand> storedTasks1 = actionDB.getRequestTasks(response.getRequestId());
    cmd = storedTasks1.get(0).getExecutionCommandWrapper().getExecutionCommand();
    Assert.assertEquals("1800", cmd.getCommandParams().get("command_timeout"));

    resourceFilters.clear();
    resourceFilter = new RequestResourceFilter("HDFS", "", null);
    resourceFilters.add(resourceFilter);
    actionRequest = new ExecuteActionRequest(cluster1, null, actionDef2, resourceFilters, null, params, false);
    response = controller.createAction(actionRequest, requestProperties);
    assertEquals(2, response.getTasks().size());

    final List<HostRoleCommand> storedTasks2 = actionDB.getRequestTasks(response.getRequestId());
    task = storedTasks2.get(1);
    Assert.assertEquals(RoleCommand.ACTIONEXECUTE, task.getRoleCommand());
    Assert.assertEquals(actionDef2, task.getRole().name());
    HashSet<String> expectedHosts = new HashSet<String>() {{
      add(host2);
      add(host1);
    }};
    HashSet<String> actualHosts = new HashSet<String>() {{
      add(storedTasks2.get(1).getHostName());
      add(storedTasks2.get(0).getHostName());
    }};
    Assert.assertEquals(expectedHosts, actualHosts);

    cmd = task.getExecutionCommandWrapper().getExecutionCommand();
    commandParametersStage = StageUtils.getGson().fromJson(stage.getCommandParamsStage(), type);

    Assert.assertTrue(commandParametersStage.containsKey("test"));
    Assert.assertTrue(commandParametersStage.containsKey("pwd"));
    Assert.assertEquals(commandParametersStage.get("pwd"), "supersecret");
    Assert.assertEquals("HDFS", cmd.getServiceName());
    Assert.assertEquals("DATANODE", cmd.getComponentName());
    Assert.assertEquals(requestProperties.get(REQUEST_CONTEXT_PROPERTY), response.getRequestContext());
    // h2 has only DATANODE sch
    Assert.assertEquals(host2, cmd.getHostname());
    Assert.assertFalse(cmd.getLocalComponents().isEmpty());
    Assert.assertTrue(cmd.getLocalComponents().contains(Role.DATANODE.name()));
    Assert.assertFalse(cmd.getLocalComponents().contains(Role.NAMENODE.name()));
    Assert.assertFalse(cmd.getLocalComponents().contains(Role.HDFS_CLIENT.name()));
    Assert.assertFalse(cmd.getLocalComponents().contains(Role.RESOURCEMANAGER.name()));

    hosts = new ArrayList<String>() {{add(host3);}};
    resourceFilters.clear();
    resourceFilter = new RequestResourceFilter("HDFS", "", hosts);
    resourceFilters.add(resourceFilter);

    actionRequest = new ExecuteActionRequest(cluster1, null, actionDef1, resourceFilters, null, params, false);
    response = controller.createAction(actionRequest, requestProperties);
    assertEquals(1, response.getTasks().size());
    taskStatus = response.getTasks().get(0);
    Assert.assertEquals(host3, taskStatus.getHostName());
    Assert.assertEquals(requestProperties.get(REQUEST_CONTEXT_PROPERTY), response.getRequestContext());
  }
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