AgentProviderService extends AbstractProviderService implements
    ProviderCore,
    AgentKeys,
    SliderKeys, AgentRestOperations { protected void addStopCommand(String roleName, String roleGroup, String containerId, HeartBeatResponse response, String scriptPath, long timeout, boolean isInUpgradeMode) throws SliderException { assert getAmState().isApplicationLive(); ConfTreeOperations appConf = getAmState().getAppConfSnapshot(); ConfTreeOperations internalsConf = getAmState().getInternalsSnapshot(); ExecutionCommand cmdStop = new ExecutionCommand( AgentCommandType.EXECUTION_COMMAND); cmdStop.setTaskId(taskId.get()); cmdStop.setCommandId(cmdStop.getTaskId() + "-1"); String clusterName = internalsConf.get(OptionKeys.APPLICATION_NAME); String hostName = getClusterInfoPropertyValue(StatusKeys.INFO_AM_HOSTNAME); cmdStop.setHostname(hostName); cmdStop.setClusterName(clusterName); cmdStop.setRoleCommand(Command.transform(Command.STOP, isInUpgradeMode)); cmdStop.setServiceName(clusterName); cmdStop.setComponentName(roleName); cmdStop.setRole(roleName); Map<String, String> hostLevelParamsStop = new TreeMap<String, String>(); hostLevelParamsStop.put(JAVA_HOME, appConf.getGlobalOptions() .getMandatoryOption(JAVA_HOME)); hostLevelParamsStop.put(CONTAINER_ID, containerId); cmdStop.setHostLevelParams(hostLevelParamsStop); cmdStop.setCommandParams(commandParametersSet(scriptPath, timeout, true)); Map<String, Map<String, String>> configurationsStop = buildCommandConfigurations( appConf, containerId, roleName, roleGroup); cmdStop.setConfigurations(configurationsStop); response.addExecutionCommand(cmdStop); } AgentProviderService(); @Override String getHumanName(); @Override List<ProviderRole> getRoles(); @Override Configuration loadProviderConfigurationInformation(File confDir); @Override void validateInstanceDefinition(AggregateConf instanceDefinition); @Override void initializeApplicationConfiguration(
      AggregateConf instanceDefinition, SliderFileSystem fileSystem,
      String roleGroup); @Override void buildContainerLaunchContext(ContainerLauncher launcher,
                                          AggregateConf instanceDefinition,
                                          Container container,
                                          ProviderRole providerRole,
                                          SliderFileSystem fileSystem,
                                          Path generatedConfPath,
                                          MapOperations resourceComponent,
                                          MapOperations appComponent,
                                          Path containerTmpDirPath); @Override void rebuildContainerDetails(List<Container> liveContainers,
                                      String applicationId, Map<Integer, ProviderRole> providerRoleMap); @Override boolean isSupportedRole(String role); @Override RegistrationResponse handleRegistration(Register registration); @Override HeartBeatResponse handleHeartBeat(HeartBeat heartBeat); @Override Map<String, MonitorDetail> buildMonitorDetails(ClusterDescription clusterDesc); @Override void applyInitialRegistryDefinitions(URL amWebURI,
      URL agentOpsURI,
      URL agentStatusURI,
      ServiceRecord serviceRecord); @Override void notifyContainerCompleted(ContainerId containerId); void setInUpgradeMode(boolean inUpgradeMode); void addUpgradeContainers(Set<String> upgradeContainers); void setAppStopInitiated(boolean appStopInitiated); Map<String, String> buildProviderStatus();  }
@Test public void testAddStopCommand() throws Exception { AgentProviderService aps = createAgentProviderService(new Configuration()); HeartBeatResponse hbr = new HeartBeatResponse(); StateAccessForProviders access = createNiceMock(StateAccessForProviders.class); AgentProviderService mockAps = Mockito.spy(aps); doReturn(access).when(mockAps).getAmState(); AggregateConf aggConf = new AggregateConf(); ConfTreeOperations treeOps = aggConf.getAppConfOperations(); treeOps.getGlobalOptions().put(AgentKeys.JAVA_HOME, "java_home"); treeOps.set(OptionKeys.APPLICATION_NAME, "HBASE"); treeOps.set("site.fs.defaultFS", "hdfs: treeOps.set("internal.data.dir.path", "hdfs: treeOps.set(OptionKeys.ZOOKEEPER_HOSTS, "HOST1"); treeOps.getGlobalOptions().put("site.hbase-site.a.port", "${HBASE_MASTER.ALLOCATED_PORT}"); treeOps.getGlobalOptions().put("site.hbase-site.b.port", "${HBASE_MASTER.ALLOCATED_PORT}"); treeOps.getGlobalOptions().put("site.hbase-site.random.port", "${HBASE_MASTER.ALLOCATED_PORT}{PER_CONTAINER}"); treeOps.getGlobalOptions().put("site.hbase-site.random2.port", "${HBASE_MASTER.ALLOCATED_PORT}"); Map<String, DefaultConfig> defaultConfigMap = new HashMap<String, DefaultConfig>(); DefaultConfig defaultConfig = new DefaultConfig(); PropertyInfo propertyInfo1 = new PropertyInfo(); propertyInfo1.setName("defaultA"); propertyInfo1.setValue("Avalue"); defaultConfig.addPropertyInfo(propertyInfo1); propertyInfo1 = new PropertyInfo(); propertyInfo1.setName("defaultB"); propertyInfo1.setValue(""); defaultConfig.addPropertyInfo(propertyInfo1); defaultConfigMap.put("hbase-site", defaultConfig); expect(access.getAppConfSnapshot()).andReturn(treeOps).anyTimes(); expect(access.getInternalsSnapshot()).andReturn(treeOps).anyTimes(); expect(access.isApplicationLive()).andReturn(true).anyTimes(); doReturn("HOST1").when(mockAps).getClusterInfoPropertyValue(anyString()); doReturn(defaultConfigMap).when(mockAps).getDefaultConfigs(anyString()); List<String> configurations = new ArrayList<String>(); configurations.add("hbase-site"); configurations.add("global"); List<String> sysConfigurations = new ArrayList<String>(); configurations.add("core-site"); doReturn(configurations).when(mockAps).getApplicationConfigurationTypes(anyString()); doReturn(sysConfigurations).when(mockAps).getSystemConfigurationsRequested(any(ConfTreeOperations.class)); Map<String, Map<String, ClusterNode>> roleClusterNodeMap = new HashMap<String, Map<String, ClusterNode>>(); Map<String, ClusterNode> container = new HashMap<String, ClusterNode>(); ClusterNode cn1 = new ClusterNode(new MockContainerId(1)); cn1.host = "HOST1"; container.put("cid1", cn1); roleClusterNodeMap.put("HBASE_MASTER", container); doReturn(roleClusterNodeMap).when(mockAps).getRoleClusterNodeMapping(); Map<String, String> allocatedPorts = new HashMap<String, String>(); allocatedPorts.put("hbase-site.a.port", "10023"); allocatedPorts.put("hbase-site.b.port", "10024"); doReturn(allocatedPorts).when(mockAps).getAllocatedPorts(); Map<String, String> allocatedPorts2 = new HashMap<String, String>(); allocatedPorts2.put("hbase-site.random.port", "10025"); doReturn(allocatedPorts2).when(mockAps).getAllocatedPorts(anyString()); replay(access); mockAps.addStopCommand("HBASE_MASTER", "HBASE_MASTER", "cid1", hbr, "/tmp/stop_cmd.sh", 10, false); Assert.assertTrue(hbr.getExecutionCommands().get(0).getConfigurations().containsKey("hbase-site")); Assert.assertTrue(hbr.getExecutionCommands().get(0).getConfigurations().containsKey("core-site")); Map<String, String> hbaseSiteConf = hbr.getExecutionCommands().get(0).getConfigurations().get("hbase-site"); Assert.assertTrue(hbaseSiteConf.containsKey("a.port")); Assert.assertEquals("10023", hbaseSiteConf.get("a.port")); Assert.assertEquals("10024", hbaseSiteConf.get("b.port")); Assert.assertEquals("10025", hbaseSiteConf.get("random.port")); assertEquals("${HBASE_MASTER.ALLOCATED_PORT}", hbaseSiteConf.get("random2.port")); ExecutionCommand cmd = hbr.getExecutionCommands().get(0); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_log_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_pid_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_install_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_input_conf_dir")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_container_id")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("pid_file")); Assert.assertTrue(cmd.getConfigurations().get("global").containsKey("app_root")); Assert.assertTrue(cmd.getConfigurations().get("hbase-site").containsKey("defaultA")); Assert.assertFalse(cmd.getConfigurations().get("hbase-site").containsKey("defaultB")); }