@Test public void testPreUpgradeCheckClusterMissing() throws Exception { final Clusters clusters = Mockito.mock(Clusters.class); Mockito.when(clusters.getCluster(Mockito.anyString())).thenAnswer(new Answer<Cluster>() { @Override public Cluster answer(InvocationOnMock invocation) throws Throwable { final String clusterName = invocation.getArguments()[0].toString(); if (clusterName.equals("existing")) { return Mockito.mock(Cluster.class); } else { throw new ClusterNotFoundException(clusterName); } } }); final OsFamily osFamily = Mockito.mock(OsFamily.class); final Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Clusters.class).toInstance(clusters); bind(ClusterVersionDAO.class).toProvider(Providers.<ClusterVersionDAO>of(null)); bind(HostVersionDAO.class).toProvider(Providers.<HostVersionDAO>of(null)); bind(UpgradeDAO.class).toProvider(Providers.<UpgradeDAO>of(null)); bind(RepositoryVersionDAO.class).toProvider(Providers.<RepositoryVersionDAO>of(null)); bind(RepositoryVersionHelper.class).toProvider(Providers.<RepositoryVersionHelper>of(null)); bind(AmbariMetaInfo.class).toProvider(Providers.<AmbariMetaInfo>of(null)); bind(ServicesUpCheck.class).toInstance(new ServicesUpCheck()); bind(OsFamily.class).toInstance(osFamily); } }); final CheckHelper helper = injector.getInstance(CheckHelper.class); List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<AbstractCheckDescriptor>(); updateChecksRegistry.add(injector.getInstance(ServicesUpCheck.class)); List<PrerequisiteCheck> upgradeChecks = helper.performChecks(new PrereqCheckRequest("existing"), updateChecksRegistry); Assert.assertEquals(PrereqCheckStatus.PASS, upgradeChecks.get(0).getStatus()); upgradeChecks = helper.performChecks(new PrereqCheckRequest("non-existing"), updateChecksRegistry); Assert.assertEquals(PrereqCheckStatus.FAIL, upgradeChecks.get(0).getStatus()); Assert.assertTrue(!upgradeChecks.get(0).getFailReason().equals("Unexpected server error happened")); }
public List<PrerequisiteCheck> performChecks(PrereqCheckRequest request, List<AbstractCheckDescriptor> checksRegistry) { final String clusterName = request.getClusterName(); final List<PrerequisiteCheck> prerequisiteCheckResults = new ArrayList<PrerequisiteCheck>(); List<DescriptorPreCheck> applicablePreChecks = getApplicablePrerequisiteChecks(request, checksRegistry); for (DescriptorPreCheck descriptorPreCheck : applicablePreChecks) { AbstractCheckDescriptor checkDescriptor = descriptorPreCheck.descriptor; PrerequisiteCheck prerequisiteCheck = descriptorPreCheck.check; try { checkDescriptor.perform(prerequisiteCheck, request); boolean canBypassPreChecks = checkDescriptor.isStackUpgradeAllowedToBypassPreChecks(); if (prerequisiteCheck.getStatus() == PrereqCheckStatus.FAIL && canBypassPreChecks) { LOG.error("Check {} failed but stack upgrade is allowed to bypass failures. Error to bypass: {}. Failed on: {}", checkDescriptor.getDescription().name(), prerequisiteCheck.getFailReason(), StringUtils.join(prerequisiteCheck.getFailedOn(), ", ")); prerequisiteCheck.setStatus(PrereqCheckStatus.BYPASS); } prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (ClusterNotFoundException ex) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Cluster with name " + clusterName + " doesn't exists"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (Exception ex) { LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Unexpected server error happened"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } } return prerequisiteCheckResults; }
CheckHelper { public List<PrerequisiteCheck> performChecks(PrereqCheckRequest request, List<AbstractCheckDescriptor> checksRegistry) { final String clusterName = request.getClusterName(); final List<PrerequisiteCheck> prerequisiteCheckResults = new ArrayList<PrerequisiteCheck>(); List<DescriptorPreCheck> applicablePreChecks = getApplicablePrerequisiteChecks(request, checksRegistry); for (DescriptorPreCheck descriptorPreCheck : applicablePreChecks) { AbstractCheckDescriptor checkDescriptor = descriptorPreCheck.descriptor; PrerequisiteCheck prerequisiteCheck = descriptorPreCheck.check; try { checkDescriptor.perform(prerequisiteCheck, request); boolean canBypassPreChecks = checkDescriptor.isStackUpgradeAllowedToBypassPreChecks(); if (prerequisiteCheck.getStatus() == PrereqCheckStatus.FAIL && canBypassPreChecks) { LOG.error("Check {} failed but stack upgrade is allowed to bypass failures. Error to bypass: {}. Failed on: {}", checkDescriptor.getDescription().name(), prerequisiteCheck.getFailReason(), StringUtils.join(prerequisiteCheck.getFailedOn(), ", ")); prerequisiteCheck.setStatus(PrereqCheckStatus.BYPASS); } prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (ClusterNotFoundException ex) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Cluster with name " + clusterName + " doesn't exists"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (Exception ex) { LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Unexpected server error happened"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } } return prerequisiteCheckResults; } }
CheckHelper { public List<PrerequisiteCheck> performChecks(PrereqCheckRequest request, List<AbstractCheckDescriptor> checksRegistry) { final String clusterName = request.getClusterName(); final List<PrerequisiteCheck> prerequisiteCheckResults = new ArrayList<PrerequisiteCheck>(); List<DescriptorPreCheck> applicablePreChecks = getApplicablePrerequisiteChecks(request, checksRegistry); for (DescriptorPreCheck descriptorPreCheck : applicablePreChecks) { AbstractCheckDescriptor checkDescriptor = descriptorPreCheck.descriptor; PrerequisiteCheck prerequisiteCheck = descriptorPreCheck.check; try { checkDescriptor.perform(prerequisiteCheck, request); boolean canBypassPreChecks = checkDescriptor.isStackUpgradeAllowedToBypassPreChecks(); if (prerequisiteCheck.getStatus() == PrereqCheckStatus.FAIL && canBypassPreChecks) { LOG.error("Check {} failed but stack upgrade is allowed to bypass failures. Error to bypass: {}. Failed on: {}", checkDescriptor.getDescription().name(), prerequisiteCheck.getFailReason(), StringUtils.join(prerequisiteCheck.getFailedOn(), ", ")); prerequisiteCheck.setStatus(PrereqCheckStatus.BYPASS); } prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (ClusterNotFoundException ex) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Cluster with name " + clusterName + " doesn't exists"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (Exception ex) { LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Unexpected server error happened"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } } return prerequisiteCheckResults; }  }
CheckHelper { public List<PrerequisiteCheck> performChecks(PrereqCheckRequest request, List<AbstractCheckDescriptor> checksRegistry) { final String clusterName = request.getClusterName(); final List<PrerequisiteCheck> prerequisiteCheckResults = new ArrayList<PrerequisiteCheck>(); List<DescriptorPreCheck> applicablePreChecks = getApplicablePrerequisiteChecks(request, checksRegistry); for (DescriptorPreCheck descriptorPreCheck : applicablePreChecks) { AbstractCheckDescriptor checkDescriptor = descriptorPreCheck.descriptor; PrerequisiteCheck prerequisiteCheck = descriptorPreCheck.check; try { checkDescriptor.perform(prerequisiteCheck, request); boolean canBypassPreChecks = checkDescriptor.isStackUpgradeAllowedToBypassPreChecks(); if (prerequisiteCheck.getStatus() == PrereqCheckStatus.FAIL && canBypassPreChecks) { LOG.error("Check {} failed but stack upgrade is allowed to bypass failures. Error to bypass: {}. Failed on: {}", checkDescriptor.getDescription().name(), prerequisiteCheck.getFailReason(), StringUtils.join(prerequisiteCheck.getFailedOn(), ", ")); prerequisiteCheck.setStatus(PrereqCheckStatus.BYPASS); } prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (ClusterNotFoundException ex) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Cluster with name " + clusterName + " doesn't exists"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (Exception ex) { LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Unexpected server error happened"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } } return prerequisiteCheckResults; }  List<DescriptorPreCheck> getApplicablePrerequisiteChecks(PrereqCheckRequest request,
                                                       List<AbstractCheckDescriptor> checksRegistry); List<PrerequisiteCheck> performChecks(PrereqCheckRequest request,
      List<AbstractCheckDescriptor> checksRegistry); }
CheckHelper { public List<PrerequisiteCheck> performChecks(PrereqCheckRequest request, List<AbstractCheckDescriptor> checksRegistry) { final String clusterName = request.getClusterName(); final List<PrerequisiteCheck> prerequisiteCheckResults = new ArrayList<PrerequisiteCheck>(); List<DescriptorPreCheck> applicablePreChecks = getApplicablePrerequisiteChecks(request, checksRegistry); for (DescriptorPreCheck descriptorPreCheck : applicablePreChecks) { AbstractCheckDescriptor checkDescriptor = descriptorPreCheck.descriptor; PrerequisiteCheck prerequisiteCheck = descriptorPreCheck.check; try { checkDescriptor.perform(prerequisiteCheck, request); boolean canBypassPreChecks = checkDescriptor.isStackUpgradeAllowedToBypassPreChecks(); if (prerequisiteCheck.getStatus() == PrereqCheckStatus.FAIL && canBypassPreChecks) { LOG.error("Check {} failed but stack upgrade is allowed to bypass failures. Error to bypass: {}. Failed on: {}", checkDescriptor.getDescription().name(), prerequisiteCheck.getFailReason(), StringUtils.join(prerequisiteCheck.getFailedOn(), ", ")); prerequisiteCheck.setStatus(PrereqCheckStatus.BYPASS); } prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (ClusterNotFoundException ex) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Cluster with name " + clusterName + " doesn't exists"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } catch (Exception ex) { LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason("Unexpected server error happened"); prerequisiteCheckResults.add(prerequisiteCheck); request.addResult(checkDescriptor.getDescription(), prerequisiteCheck.getStatus()); } } return prerequisiteCheckResults; }  List<DescriptorPreCheck> getApplicablePrerequisiteChecks(PrereqCheckRequest request,
                                                       List<AbstractCheckDescriptor> checksRegistry); List<PrerequisiteCheck> performChecks(PrereqCheckRequest request,
      List<AbstractCheckDescriptor> checksRegistry);  }