@Test
  public void testMixedComponentVersions() throws Exception {
    StackId sourceStack = HDP_21_STACK;
    StackId targetStack = HDP_22_STACK;
    String sourceVersion = HDP_2_1_1_0;
    String targetVersion = HDP_2_2_1_0;
    String clusterName = "c1";
    String hostName = "h1";

    makeCrossStackUpgradeCluster(sourceStack, sourceVersion, targetStack, targetVersion,
        clusterName, hostName);

    Clusters clusters = m_injector.getInstance(Clusters.class);
    Cluster cluster = clusters.getCluster("c1");

    RepositoryVersionEntity sourceRepoVersion = m_helper.getOrCreateRepositoryVersion(HDP_21_STACK, HDP_2_1_1_0);
    RepositoryVersionEntity targetRepoVersion = m_helper.getOrCreateRepositoryVersion(HDP_22_STACK, HDP_2_2_1_0);

    Service service = installService(cluster, "HDFS", sourceRepoVersion);
    addServiceComponent(cluster, service, "NAMENODE");
    addServiceComponent(cluster, service, "DATANODE");
    createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", hostName);
    createNewServiceComponentHost(cluster, "HDFS", "DATANODE", hostName);

    // create some configs
    createConfigs(cluster);

    // install the target repo
    installRepositoryOnHost(hostName, targetRepoVersion);

    // setup the cluster for the upgrade across stacks
    cluster.setCurrentStackVersion(sourceStack);
    cluster.setDesiredStackVersion(targetStack);

    // tell the upgrade that HDFS is upgrading - without this, no services will
    // be participating in the upgrade
    UpgradeEntity upgrade = cluster.getUpgradeInProgress();
    UpgradeHistoryEntity history = new UpgradeHistoryEntity();
    history.setUpgrade(upgrade);
    history.setServiceName("HDFS");
    history.setComponentName("NAMENODE");
    history.setFromRepositoryVersion(sourceRepoVersion);
    history.setTargetRepositoryVersion(targetRepoVersion);
    upgrade.addHistory(history);

    history = new UpgradeHistoryEntity();
    history.setUpgrade(upgrade);
    history.setServiceName("HDFS");
    history.setComponentName("DATANODE");
    history.setFromRepositoryVersion(sourceRepoVersion);
    history.setTargetRepositoryVersion(targetRepoVersion);
    upgrade.addHistory(history);

    UpgradeDAO upgradeDAO = m_injector.getInstance(UpgradeDAO.class);
    upgrade = upgradeDAO.merge(upgrade);

    // set the SCH versions to the new stack so that the finalize action is
    // happy - don't update DATANODE - we want to make the action complain
    cluster.getServiceComponentHosts("HDFS", "NAMENODE").get(0).setVersion(targetVersion);

    // verify the conditions for the test are met properly
    List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion("c1",
        HDP_22_STACK, targetVersion);

    assertTrue(hostVersions.size() > 0);
    for (HostVersionEntity hostVersion : hostVersions) {
      assertEquals(RepositoryVersionState.INSTALLED, hostVersion.getState());
    }

    // now finalize and ensure we can transition from UPGRADING to UPGRADED
    // automatically before CURRENT
    Map<String, String> commandParams = new HashMap<>();
    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());

    // OK, now set the datanode so it completes
    cluster.getServiceComponentHosts("HDFS", "DATANODE").get(0).setVersion(targetVersion);

    report = action.execute(null);
    assertNotNull(report);
    assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus());
    assertEquals(0, report.getExitCode());
  }
Creates a new {@link HostVersionEntity} instance in the
{@link RepositoryVersionState#INSTALLED} for the specified host.

@param hostName
@param repositoryVersion
@throws AmbariException
ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java