@Test public void testAction() throws Exception { Pattern regex = Pattern.compile("^\\s*export\\s(?<property>HIVE_HOME|HIVE_CONF_DIR)\\s*=\\s*(?<value>.*)$", Pattern.MULTILINE); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); HiveEnvClasspathAction action = new HiveEnvClasspathAction(); m_clusterField.set(action, m_clusters); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); CommandReport report = action.execute(null); assertNotNull(report); Cluster c = m_clusters.getCluster("c1"); Config config = c.getDesiredConfigByType("hive-env"); Map<String, String> map = config.getProperties(); Assert.assertTrue(map.containsKey("content")); String content = map.get("content"); Matcher m = regex.matcher(content); int matches_found = 0; while (m.find()){ if (m.group("property").equals("HIVE_HOME")){ matches_found++; assertEquals("${HIVE_HOME:-{{hive_home_dir}}}", m.group("value")); } else if (m.group("property").equals("HIVE_CONF_DIR")) { matches_found++; assertEquals("test", m.group("value")); } } Assert.assertEquals(2, matches_found); }
@Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); Config config = cluster.getDesiredConfigByType(TARGET_CONFIG_TYPE); if (config == null) { return createCommandReport(0, HostRoleStatus.FAILED,"{}", String.format("Source type %s not found", TARGET_CONFIG_TYPE), ""); } Pattern regex = Pattern.compile(String.format(VERIFY_REGEXP, HIVE_HOME, HIVE_CONF_DIR), Pattern.MULTILINE); Map<String, String> properties = config.getProperties(); String oldContent = properties.get(CONTENT_PROPERTY_NAME); Matcher regexMatcher = regex.matcher(oldContent); boolean isHiveHomeFound = false; boolean isHiveConfFound = false; while (regexMatcher.find()){ String propertyName = regexMatcher.group("property"); if (propertyName.equalsIgnoreCase(HIVE_CONF_DIR)){ isHiveConfFound = true; } else if (propertyName.equalsIgnoreCase(HIVE_HOME)){ isHiveHomeFound = true; } } StringBuilder stringBuilder = new StringBuilder(oldContent); if (!isHiveConfFound) { stringBuilder.append("\n").append(HIVE_CONF_DIR_APPEND); } if (!isHiveHomeFound) { stringBuilder.append("\n").append(HIVE_HOME_APPEND); } String newContent = stringBuilder.toString(); if (newContent.equals(oldContent)) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("%s/%s option has been already added to hive-env", HIVE_HOME, HIVE_CONF_DIR), ""); } else { properties.put(CONTENT_PROPERTY_NAME, newContent); } config.setProperties(properties); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("Added %s, %s to content at %s", HIVE_CONF_DIR, HIVE_HOME, TARGET_CONFIG_TYPE), ""); }
HiveEnvClasspathAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); Config config = cluster.getDesiredConfigByType(TARGET_CONFIG_TYPE); if (config == null) { return createCommandReport(0, HostRoleStatus.FAILED,"{}", String.format("Source type %s not found", TARGET_CONFIG_TYPE), ""); } Pattern regex = Pattern.compile(String.format(VERIFY_REGEXP, HIVE_HOME, HIVE_CONF_DIR), Pattern.MULTILINE); Map<String, String> properties = config.getProperties(); String oldContent = properties.get(CONTENT_PROPERTY_NAME); Matcher regexMatcher = regex.matcher(oldContent); boolean isHiveHomeFound = false; boolean isHiveConfFound = false; while (regexMatcher.find()){ String propertyName = regexMatcher.group("property"); if (propertyName.equalsIgnoreCase(HIVE_CONF_DIR)){ isHiveConfFound = true; } else if (propertyName.equalsIgnoreCase(HIVE_HOME)){ isHiveHomeFound = true; } } StringBuilder stringBuilder = new StringBuilder(oldContent); if (!isHiveConfFound) { stringBuilder.append("\n").append(HIVE_CONF_DIR_APPEND); } if (!isHiveHomeFound) { stringBuilder.append("\n").append(HIVE_HOME_APPEND); } String newContent = stringBuilder.toString(); if (newContent.equals(oldContent)) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("%s/%s option has been already added to hive-env", HIVE_HOME, HIVE_CONF_DIR), ""); } else { properties.put(CONTENT_PROPERTY_NAME, newContent); } config.setProperties(properties); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("Added %s, %s to content at %s", HIVE_CONF_DIR, HIVE_HOME, TARGET_CONFIG_TYPE), ""); } }
HiveEnvClasspathAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); Config config = cluster.getDesiredConfigByType(TARGET_CONFIG_TYPE); if (config == null) { return createCommandReport(0, HostRoleStatus.FAILED,"{}", String.format("Source type %s not found", TARGET_CONFIG_TYPE), ""); } Pattern regex = Pattern.compile(String.format(VERIFY_REGEXP, HIVE_HOME, HIVE_CONF_DIR), Pattern.MULTILINE); Map<String, String> properties = config.getProperties(); String oldContent = properties.get(CONTENT_PROPERTY_NAME); Matcher regexMatcher = regex.matcher(oldContent); boolean isHiveHomeFound = false; boolean isHiveConfFound = false; while (regexMatcher.find()){ String propertyName = regexMatcher.group("property"); if (propertyName.equalsIgnoreCase(HIVE_CONF_DIR)){ isHiveConfFound = true; } else if (propertyName.equalsIgnoreCase(HIVE_HOME)){ isHiveHomeFound = true; } } StringBuilder stringBuilder = new StringBuilder(oldContent); if (!isHiveConfFound) { stringBuilder.append("\n").append(HIVE_CONF_DIR_APPEND); } if (!isHiveHomeFound) { stringBuilder.append("\n").append(HIVE_HOME_APPEND); } String newContent = stringBuilder.toString(); if (newContent.equals(oldContent)) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("%s/%s option has been already added to hive-env", HIVE_HOME, HIVE_CONF_DIR), ""); } else { properties.put(CONTENT_PROPERTY_NAME, newContent); } config.setProperties(properties); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("Added %s, %s to content at %s", HIVE_CONF_DIR, HIVE_HOME, TARGET_CONFIG_TYPE), ""); }  }
HiveEnvClasspathAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); Config config = cluster.getDesiredConfigByType(TARGET_CONFIG_TYPE); if (config == null) { return createCommandReport(0, HostRoleStatus.FAILED,"{}", String.format("Source type %s not found", TARGET_CONFIG_TYPE), ""); } Pattern regex = Pattern.compile(String.format(VERIFY_REGEXP, HIVE_HOME, HIVE_CONF_DIR), Pattern.MULTILINE); Map<String, String> properties = config.getProperties(); String oldContent = properties.get(CONTENT_PROPERTY_NAME); Matcher regexMatcher = regex.matcher(oldContent); boolean isHiveHomeFound = false; boolean isHiveConfFound = false; while (regexMatcher.find()){ String propertyName = regexMatcher.group("property"); if (propertyName.equalsIgnoreCase(HIVE_CONF_DIR)){ isHiveConfFound = true; } else if (propertyName.equalsIgnoreCase(HIVE_HOME)){ isHiveHomeFound = true; } } StringBuilder stringBuilder = new StringBuilder(oldContent); if (!isHiveConfFound) { stringBuilder.append("\n").append(HIVE_CONF_DIR_APPEND); } if (!isHiveHomeFound) { stringBuilder.append("\n").append(HIVE_HOME_APPEND); } String newContent = stringBuilder.toString(); if (newContent.equals(oldContent)) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("%s/%s option has been already added to hive-env", HIVE_HOME, HIVE_CONF_DIR), ""); } else { properties.put(CONTENT_PROPERTY_NAME, newContent); } config.setProperties(properties); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("Added %s, %s to content at %s", HIVE_CONF_DIR, HIVE_HOME, TARGET_CONFIG_TYPE), ""); }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext); }
HiveEnvClasspathAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); Config config = cluster.getDesiredConfigByType(TARGET_CONFIG_TYPE); if (config == null) { return createCommandReport(0, HostRoleStatus.FAILED,"{}", String.format("Source type %s not found", TARGET_CONFIG_TYPE), ""); } Pattern regex = Pattern.compile(String.format(VERIFY_REGEXP, HIVE_HOME, HIVE_CONF_DIR), Pattern.MULTILINE); Map<String, String> properties = config.getProperties(); String oldContent = properties.get(CONTENT_PROPERTY_NAME); Matcher regexMatcher = regex.matcher(oldContent); boolean isHiveHomeFound = false; boolean isHiveConfFound = false; while (regexMatcher.find()){ String propertyName = regexMatcher.group("property"); if (propertyName.equalsIgnoreCase(HIVE_CONF_DIR)){ isHiveConfFound = true; } else if (propertyName.equalsIgnoreCase(HIVE_HOME)){ isHiveHomeFound = true; } } StringBuilder stringBuilder = new StringBuilder(oldContent); if (!isHiveConfFound) { stringBuilder.append("\n").append(HIVE_CONF_DIR_APPEND); } if (!isHiveHomeFound) { stringBuilder.append("\n").append(HIVE_HOME_APPEND); } String newContent = stringBuilder.toString(); if (newContent.equals(oldContent)) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("%s/%s option has been already added to hive-env", HIVE_HOME, HIVE_CONF_DIR), ""); } else { properties.put(CONTENT_PROPERTY_NAME, newContent); } config.setProperties(properties); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format("Added %s, %s to content at %s", HIVE_CONF_DIR, HIVE_HOME, TARGET_CONFIG_TYPE), ""); }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }