HostsMasterMaintenanceCheck 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 stackId = cluster.getDesiredStackVersion(); final Set<String> hostsWithMasterComponent = new HashSet<String>(); final String upgradePackName = repositoryVersionHelper.get().getUpgradePackageName(stackId.getStackName(), stackId.getStackVersion(), request.getRepositoryVersion(), null); if (upgradePackName == null) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); String fail = getFailReason(KEY_NO_UPGRADE_NAME, prerequisiteCheck, request); prerequisiteCheck.setFailReason(String.format(fail, stackId.getStackName(), stackId.getStackVersion())); return; } final UpgradePack upgradePack = ambariMetaInfo.get().getUpgradePacks(stackId.getStackName(), stackId.getStackVersion()).get(upgradePackName); if (upgradePack == null) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); String fail = getFailReason(KEY_NO_UPGRADE_PACK, prerequisiteCheck, request); prerequisiteCheck.setFailReason(String.format(fail, upgradePackName)); return; } final Set<String> componentsFromUpgradePack = new HashSet<String>(); for (Map<String, ProcessingComponent> task: upgradePack.getTasks().values()) { componentsFromUpgradePack.addAll(task.keySet()); } for (Service service: cluster.getServices().values()) { for (ServiceComponent serviceComponent: service.getServiceComponents().values()) { if (serviceComponent.isMasterComponent() && componentsFromUpgradePack.contains(serviceComponent.getName())) { hostsWithMasterComponent.addAll(serviceComponent.getServiceComponentHosts().keySet()); } } } final Map<String, Host> clusterHosts = clustersProvider.get().getHostsForCluster(clusterName); for (Map.Entry<String, Host> hostEntry : clusterHosts.entrySet()) { final Host host = hostEntry.getValue(); if (host.getMaintenanceState(cluster.getClusterId()) == MaintenanceState.ON && hostsWithMasterComponent.contains(host.getHostName())) { prerequisiteCheck.getFailedOn().add(host.getHostName()); } } if (!prerequisiteCheck.getFailedOn().isEmpty()) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason(getFailReason(prerequisiteCheck, request)); } } HostsMasterMaintenanceCheck(); @Override boolean isApplicable(PrereqCheckRequest request); @Override void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request);  }
@Test public void testPerform() throws Exception { final String upgradePackName = "upgrade_pack"; final HostsMasterMaintenanceCheck hostsMasterMaintenanceCheck = new HostsMasterMaintenanceCheck(); hostsMasterMaintenanceCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { return clusters; } }; hostsMasterMaintenanceCheck.repositoryVersionDaoProvider = new Provider<RepositoryVersionDAO>() { @Override public RepositoryVersionDAO get() { return repositoryVersionDAO; } }; hostsMasterMaintenanceCheck.repositoryVersionHelper = new Provider<RepositoryVersionHelper>() { @Override public RepositoryVersionHelper get() { return repositoryVersionHelper; } }; hostsMasterMaintenanceCheck.ambariMetaInfo = new Provider<AmbariMetaInfo>() { @Override public AmbariMetaInfo get() { return ambariMetaInfo; } }; final Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); Mockito.when(cluster.getDesiredStackVersion()).thenReturn(new StackId("HDP", "1.0")); Mockito.when(repositoryVersionHelper.getUpgradePackageName(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), (UpgradeType) Mockito.anyObject())).thenReturn(null); PrerequisiteCheck check = new PrerequisiteCheck(null, null); hostsMasterMaintenanceCheck.perform(check, new PrereqCheckRequest("cluster")); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); Mockito.when(repositoryVersionHelper.getUpgradePackageName(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), (UpgradeType) Mockito.anyObject())).thenReturn(upgradePackName); Mockito.when(ambariMetaInfo.getUpgradePacks(Mockito.anyString(), Mockito.anyString())).thenReturn(new HashMap<String, UpgradePack>()); check = new PrerequisiteCheck(null, null); hostsMasterMaintenanceCheck.perform(check, new PrereqCheckRequest("cluster")); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); final Map<String, UpgradePack> upgradePacks = new HashMap<String, UpgradePack>(); final UpgradePack upgradePack = Mockito.mock(UpgradePack.class); Mockito.when(upgradePack.getName()).thenReturn(upgradePackName); upgradePacks.put(upgradePack.getName(), upgradePack); Mockito.when(ambariMetaInfo.getUpgradePacks(Mockito.anyString(), Mockito.anyString())).thenReturn(upgradePacks); Mockito.when(upgradePack.getTasks()).thenReturn(new HashMap<String, Map<String,ProcessingComponent>>()); Mockito.when(cluster.getServices()).thenReturn(new HashMap<String, Service>()); Mockito.when(clusters.getHostsForCluster(Mockito.anyString())).thenReturn(new HashMap<String, Host>()); check = new PrerequisiteCheck(null, null); hostsMasterMaintenanceCheck.perform(check, new PrereqCheckRequest("cluster")); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); }