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);  }
@Test public void testPreUpgradeCheck() throws Exception { final CheckHelper helper = new CheckHelper(); List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<AbstractCheckDescriptor>(); AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class); descriptor.perform(EasyMock.<PrerequisiteCheck> anyObject(), EasyMock.<PrereqCheckRequest> anyObject()); EasyMock.expectLastCall().times(1); EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest> anyObject())).andReturn(true); EasyMock.replay(descriptor); updateChecksRegistry.add(descriptor); helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry); EasyMock.verify(descriptor); }
@Test public void testPreUpgradeCheckNotApplicable() throws Exception { final CheckHelper helper = new CheckHelper(); List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<AbstractCheckDescriptor>(); AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class); EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest> anyObject())).andReturn(false); EasyMock.replay(descriptor); updateChecksRegistry.add(descriptor); helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry); EasyMock.verify(descriptor); }
@Test public void testPreUpgradeCheckThrowsException() throws Exception { final CheckHelper helper = new CheckHelper(); List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<AbstractCheckDescriptor>(); AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class); descriptor.perform(EasyMock.<PrerequisiteCheck> anyObject(), EasyMock.<PrereqCheckRequest> anyObject()); EasyMock.expectLastCall().andThrow(new AmbariException("error")); EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest> anyObject())).andReturn(true); EasyMock.expect(descriptor.getDescription()).andReturn(CheckDescription.HOSTS_HEARTBEAT).anyTimes(); EasyMock.replay(descriptor); updateChecksRegistry.add(descriptor); final List<PrerequisiteCheck> upgradeChecks = helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry); EasyMock.verify(descriptor); Assert.assertEquals(PrereqCheckStatus.FAIL, upgradeChecks.get(0).getStatus()); }
@Test public void testPreUpgradeCheckBypassesFailure() throws Exception { final CheckHelperMock helper = new CheckHelperMock(); List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<AbstractCheckDescriptor>(); PrereqCheckRequest checkRequest = EasyMock.createNiceMock(PrereqCheckRequest.class); EasyMock.expect(checkRequest.getClusterName()).andReturn("c1").anyTimes(); EasyMock.replay(checkRequest); final List<PrerequisiteCheck> upgradeChecks = helper.performChecks(checkRequest, updateChecksRegistry); Assert.assertEquals(1, upgradeChecks.size()); Assert.assertEquals(PrereqCheckStatus.BYPASS, upgradeChecks.get(0).getStatus()); }
@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")); }