InstallPackagesCheck extends AbstractCheckDescriptor { @Override public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) throws AmbariException { final String clusterName = request.getClusterName(); final Cluster cluster = clustersProvider.get().getCluster(clusterName); final StackId targetStackId = request.getTargetStackId(); final String stackName = targetStackId.getStackName(); final String repoVersion = request.getRepositoryVersion(); final RepositoryVersionEntity rve = repositoryVersionDaoProvider.get().findByStackNameAndVersion(stackName, request.getRepositoryVersion()); if (StringUtils.isBlank(rve.getVersion()) || !rve.getVersion().matches("^\\d+(\\.\\d+)*\\-\\d+$")) { String message = MessageFormat.format("The Repository Version {0} for Stack {1} must contain a \"-\" followed by a build number. " + "Make sure that another registered repository does not have the same repo URL or " + "shares the same build number. Next, try reinstalling the Repository Version.", rve.getVersion(), rve.getStackVersion()); prerequisiteCheck.getFailedOn().add("Repository Version " + rve.getVersion()); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason(message); return; } final ClusterVersionEntity clusterVersion = clusterVersionDAOProvider.get().findByClusterAndStackAndVersion( clusterName, targetStackId, repoVersion); final Set<String> failedHosts = new HashSet<String>(); for (Host host : cluster.getHosts()) { if (host.getMaintenanceState(cluster.getClusterId()) != MaintenanceState.ON) { for (HostVersionEntity hve : hostVersionDaoProvider.get().findByHost(host.getHostName())) { if (hve.getRepositoryVersion().getVersion().equals(request.getRepositoryVersion()) && hve.getState() == RepositoryVersionState.INSTALL_FAILED) { failedHosts.add(host.getHostName()); } } } } if (!failedHosts.isEmpty()) { String message = MessageFormat.format("Hosts in cluster [{0},{1},{2},{3}] are in INSTALL_FAILED state because " + "Install Packages had failed. Please re-run Install Packages, if necessary place following hosts " + "in Maintenance mode: {4}", cluster.getClusterName(), targetStackId.getStackName(), targetStackId.getStackVersion(), repoVersion, StringUtils.join(failedHosts, ", ")); prerequisiteCheck.setFailedOn(new LinkedHashSet<String>(failedHosts)); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason(message); } else if (clusterVersion.getState() == RepositoryVersionState.INSTALL_FAILED) { String message = MessageFormat.format("Cluster [{0},{1},{2},{3}] is in INSTALL_FAILED state because " + "Install Packages failed. Please re-run Install Packages even if you placed the failed hosts " + "in Maintenance mode.", cluster.getClusterName(), targetStackId.getStackName(), targetStackId.getStackVersion(), repoVersion); LinkedHashSet<String> failedOn = new LinkedHashSet<String>(); failedOn.add(cluster.getClusterName()); prerequisiteCheck.setFailedOn(failedOn); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason(message); } } InstallPackagesCheck(); @Override void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request);  }
@Test public void testPerform() throws Exception { StackId stackId = new StackId("HDP", "2.2"); PowerMockito.mockStatic(HostComponentSummary.class); final InstallPackagesCheck installPackagesCheck = new InstallPackagesCheck(); installPackagesCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { return clusters; } }; installPackagesCheck.ambariMetaInfo = new Provider<AmbariMetaInfo>() { @Override public AmbariMetaInfo get() { return ambariMetaInfo; } }; installPackagesCheck.clusterVersionDAOProvider = new Provider<ClusterVersionDAO>() { @Override public ClusterVersionDAO get() { return clusterVersionDAO; } }; installPackagesCheck.hostVersionDaoProvider = new Provider<HostVersionDAO>() { @Override public HostVersionDAO get() { return hostVersionDAO; } }; installPackagesCheck.repositoryVersionDaoProvider = new Provider<RepositoryVersionDAO>() { @Override public RepositoryVersionDAO get() { return repositoryVersionDAO; } }; StackEntity stack = new StackEntity(); stack.setStackName(stackId.getStackName()); stack.setStackVersion(stackId.getStackVersion()); RepositoryVersionEntity rve = new RepositoryVersionEntity(stack, repositoryVersion, repositoryVersion, "rhel6"); Mockito.when(repositoryVersionDAO.findByStackNameAndVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(rve); final Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(cluster.getClusterName()).thenReturn(clusterName); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(cluster.getCurrentStackVersion()).thenReturn(stackId); Mockito.when(clusters.getCluster(clusterName)).thenReturn(cluster); ClusterVersionEntity clusterVersionEntity = Mockito.mock(ClusterVersionEntity.class); Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALLED); Mockito.when(clusterVersionDAO.findByClusterAndStackAndVersion( clusterName, targetStackId, repositoryVersion)).thenReturn(clusterVersionEntity); final List<String> hostNames = new ArrayList<String>(); hostNames.add("host1"); hostNames.add("host2"); hostNames.add("host3"); final List<Host> hosts = new ArrayList<Host>(); final List<HostVersionEntity> hostVersionEntities = new ArrayList<HostVersionEntity>(); for(String hostName : hostNames) { Host host = Mockito.mock(Host.class); Mockito.when(host.getHostName()).thenReturn(hostName); Mockito.when(host.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); hosts.add(host); HostVersionEntity hve = Mockito.mock(HostVersionEntity.class); Mockito.when(hve.getRepositoryVersion()).thenReturn(rve); Mockito.when(hve.getState()).thenReturn(RepositoryVersionState.INSTALLED); hostVersionEntities.add(hve); Mockito.when(hostVersionDAO.findByHost(hostName)).thenReturn(Collections.singletonList(hve)); } Mockito.when(cluster.getHosts()).thenReturn(hosts); PrereqCheckRequest checkRequest = new PrereqCheckRequest(clusterName); checkRequest.setRepositoryVersion(repositoryVersion); checkRequest.setSourceStackId(sourceStackId); checkRequest.setTargetStackId(targetStackId); PrerequisiteCheck check = new PrerequisiteCheck(null, null); installPackagesCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); Mockito.when(hostVersionEntities.get(0).getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED); Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); installPackagesCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); Assert.assertNotNull(check.getFailedOn()); Assert.assertTrue(check.getFailedOn().contains("host1")); Mockito.when(hostVersionEntities.get(0).getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED); Mockito.when(hosts.get(0).getMaintenanceState(1L)).thenReturn(MaintenanceState.ON); Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); installPackagesCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); Assert.assertNotNull(check.getFailedOn()); Assert.assertTrue(check.getFailedOn().contains(clusterName)); }