ComponentVersionCheckAction extends FinalizeUpgradeAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { Map<String, String> commandParams = getExecutionCommand().getCommandParams(); String version = commandParams.get(VERSION_KEY); StackId targetStackId = new StackId(commandParams.get(TARGET_STACK_KEY)); String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); List<InfoTuple> errors = checkHostComponentVersions(cluster, version, targetStackId); StringBuilder outSB = new StringBuilder(); StringBuilder errSB = new StringBuilder(); if (errors.isEmpty()) { outSB.append("No version mismatches found for components"); errSB.append("No errors found for components"); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", outSB.toString(), errSB.toString()); } else { String structuredOut = getErrors(outSB, errSB, errors); return createCommandReport(-1, HostRoleStatus.FAILED, structuredOut, outSB.toString(), errSB.toString()); } }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testMatchingVersions() throws Exception { StackId sourceStack = HDP_21_STACK; StackId targetStack = HDP_21_STACK; String sourceRepo = HDP_2_1_1_0; String targetRepo = HDP_2_1_1_1; makeUpgradeCluster(sourceStack, sourceRepo, targetStack, targetRepo); AmbariMetaInfo metaInfo = m_injector.getInstance(AmbariMetaInfo.class); RepositoryInfo repo = metaInfo.getRepository(sourceStack.getStackName(), sourceStack.getStackVersion(), "redhat6", sourceStack.getStackId()); assertEquals(HDP_211_CENTOS6_REPO_URL, repo.getBaseUrl()); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade"); commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(null, null, null, null); hostRoleCommand.setExecutionCommandWrapper(new ExecutionCommandWrapper(executionCommand)); ComponentVersionCheckAction action = m_injector.getInstance(ComponentVersionCheckAction.class); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hostRoleCommand); CommandReport report = action.execute(null); assertNotNull(report); assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus()); assertEquals(0, report.getExitCode()); }
@Test public void testMixedComponentVersions() throws Exception { StackId sourceStack = HDP_21_STACK; StackId targetStack = HDP_22_STACK; String sourceRepo = HDP_2_1_1_0; String targetRepo = HDP_2_2_1_0; String clusterName = "c1"; String hostName = "h1"; makeCrossStackUpgradeCluster(sourceStack, sourceRepo, targetStack, targetRepo, clusterName, hostName); Clusters clusters = m_injector.getInstance(Clusters.class); Cluster cluster = clusters.getCluster("c1"); Service service = installService(cluster, "HDFS"); addServiceComponent(cluster, service, "NAMENODE"); addServiceComponent(cluster, service, "DATANODE"); createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", "h1"); createNewServiceComponentHost(cluster, "HDFS", "DATANODE", "h1"); createNewRepoVersion(targetStack, targetRepo, clusterName, hostName); createConfigs(cluster); cluster.setCurrentStackVersion(sourceStack); cluster.setDesiredStackVersion(targetStack); cluster.getServiceComponentHosts("HDFS", "NAMENODE").get(0).setVersion(targetRepo); ClusterVersionEntity upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion( "c1", HDP_22_STACK, targetRepo); upgradingClusterVersion.setState(RepositoryVersionState.INSTALLING); upgradingClusterVersion = clusterVersionDAO.merge(upgradingClusterVersion); upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion("c1", HDP_22_STACK, targetRepo); List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion("c1", HDP_22_STACK, targetRepo); assertEquals(RepositoryVersionState.INSTALLING, upgradingClusterVersion.getState()); assertTrue(hostVersions.size() > 0); for (HostVersionEntity hostVersion : hostVersions) { assertEquals(RepositoryVersionState.INSTALLED, hostVersion.getState()); } Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade"); commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo); commandParams.put(FinalizeUpgradeAction.ORIGINAL_STACK_KEY, sourceStack.getStackId()); commandParams.put(FinalizeUpgradeAction.TARGET_STACK_KEY, targetStack.getStackId()); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(null, null, null, null); hostRoleCommand.setExecutionCommandWrapper(new ExecutionCommandWrapper(executionCommand)); ComponentVersionCheckAction action = m_injector.getInstance(ComponentVersionCheckAction.class); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hostRoleCommand); CommandReport report = action.execute(null); assertNotNull(report); assertEquals(HostRoleStatus.FAILED.name(), report.getStatus()); assertEquals(-1, report.getExitCode()); }