|
|
@@ -19,34 +19,30 @@
|
|
|
package org.apache.ambari.server.state;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.LinkedList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import org.apache.ambari.server.AmbariException;
|
|
|
-import org.apache.ambari.server.ClusterNotFoundException;
|
|
|
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
|
|
|
import org.apache.ambari.server.checks.AbstractCheckDescriptor;
|
|
|
import org.apache.ambari.server.checks.CheckDescription;
|
|
|
-import org.apache.ambari.server.checks.ServicesUpCheck;
|
|
|
import org.apache.ambari.server.configuration.Configuration;
|
|
|
import org.apache.ambari.server.controller.PrereqCheckRequest;
|
|
|
-import org.apache.ambari.server.orm.dao.HostVersionDAO;
|
|
|
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
|
|
|
-import org.apache.ambari.server.orm.dao.UpgradeDAO;
|
|
|
-import org.apache.ambari.server.state.stack.OsFamily;
|
|
|
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
|
|
|
+import org.apache.ambari.server.state.repository.ClusterVersionSummary;
|
|
|
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
|
|
|
import org.apache.ambari.server.state.stack.PrereqCheckStatus;
|
|
|
import org.apache.ambari.server.state.stack.PrerequisiteCheck;
|
|
|
-import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
|
|
|
import org.easymock.EasyMock;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.mockito.Mock;
|
|
|
import org.mockito.Mockito;
|
|
|
-import org.mockito.invocation.InvocationOnMock;
|
|
|
-import org.mockito.stubbing.Answer;
|
|
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
|
|
-import com.google.inject.AbstractModule;
|
|
|
-import com.google.inject.Guice;
|
|
|
-import com.google.inject.Injector;
|
|
|
-import com.google.inject.util.Providers;
|
|
|
+import com.google.inject.Provider;
|
|
|
|
|
|
import junit.framework.Assert;
|
|
|
|
|
|
@@ -55,9 +51,41 @@ import junit.framework.Assert;
|
|
|
* Tests the {@link CheckHelper} class
|
|
|
* Makes sure that people don't forget to add new checks to registry.
|
|
|
*/
|
|
|
-
|
|
|
+@RunWith(MockitoJUnitRunner.class)
|
|
|
public class CheckHelperTest {
|
|
|
|
|
|
+ private final Clusters clusters = Mockito.mock(Clusters.class);
|
|
|
+
|
|
|
+ private MockCheck m_mockCheck;
|
|
|
+
|
|
|
+ private CheckDescription m_mockCheckDescription = Mockito.mock(CheckDescription.class);
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private ClusterVersionSummary m_clusterVersionSummary;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private VersionDefinitionXml m_vdfXml;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private RepositoryVersionEntity m_repositoryVersion;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private Object m_mockPerform;
|
|
|
+
|
|
|
+ final Map<String, Service> m_services = new HashMap<>();
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setup() throws Exception {
|
|
|
+ m_mockCheck = new MockCheck();
|
|
|
+
|
|
|
+ Mockito.when(m_mockPerform.toString()).thenReturn("Perform!");
|
|
|
+
|
|
|
+ m_services.clear();
|
|
|
+ Mockito.when(m_repositoryVersion.getRepositoryXml()).thenReturn(m_vdfXml);
|
|
|
+ Mockito.when(m_vdfXml.getClusterSummary(Mockito.any(Cluster.class))).thenReturn(m_clusterVersionSummary);
|
|
|
+ Mockito.when(m_clusterVersionSummary.getAvailableServiceNames()).thenReturn(m_services.keySet());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Sunny case when applicable.
|
|
|
*/
|
|
|
@@ -66,17 +94,15 @@ public class CheckHelperTest {
|
|
|
final CheckHelper helper = new CheckHelper();
|
|
|
Configuration configuration = EasyMock.createNiceMock(Configuration.class);
|
|
|
List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<>();
|
|
|
- AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class);
|
|
|
|
|
|
EasyMock.expect(configuration.isUpgradePrecheckBypass()).andReturn(false);
|
|
|
- descriptor.perform(EasyMock.<PrerequisiteCheck> anyObject(), EasyMock.<PrereqCheckRequest> anyObject());
|
|
|
- EasyMock.expectLastCall().times(1);
|
|
|
- EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest> anyObject())).andReturn(true);
|
|
|
- EasyMock.replay(descriptor, configuration);
|
|
|
- updateChecksRegistry.add(descriptor);
|
|
|
-
|
|
|
- helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry, configuration);
|
|
|
- EasyMock.verify(descriptor);
|
|
|
+ EasyMock.replay(configuration);
|
|
|
+ updateChecksRegistry.add(m_mockCheck);
|
|
|
+
|
|
|
+ PrereqCheckRequest request = new PrereqCheckRequest("cluster");
|
|
|
+ helper.performChecks(request, updateChecksRegistry, configuration);
|
|
|
+
|
|
|
+ Assert.assertEquals(PrereqCheckStatus.PASS, request.getResult(m_mockCheckDescription));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -84,16 +110,28 @@ public class CheckHelperTest {
|
|
|
*/
|
|
|
@Test
|
|
|
public void testPreUpgradeCheckNotApplicable() throws Exception {
|
|
|
+ final Cluster cluster = Mockito.mock(Cluster.class);
|
|
|
+ final Service service = Mockito.mock(Service.class);
|
|
|
+
|
|
|
+ m_services.put("KAFKA", service);
|
|
|
+
|
|
|
+ Mockito.when(cluster.getServices()).thenReturn(new HashMap<String, Service>());
|
|
|
+ Mockito.when(cluster.getClusterId()).thenReturn(1L);
|
|
|
+ Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster);
|
|
|
+
|
|
|
final CheckHelper helper = new CheckHelper();
|
|
|
Configuration configuration = EasyMock.createNiceMock(Configuration.class);
|
|
|
List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<>();
|
|
|
- AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class);
|
|
|
+
|
|
|
EasyMock.expect(configuration.isUpgradePrecheckBypass()).andReturn(false);
|
|
|
- EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest> anyObject())).andReturn(false);
|
|
|
- EasyMock.replay(descriptor, configuration);
|
|
|
- updateChecksRegistry.add(descriptor);
|
|
|
- helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry, configuration);
|
|
|
- EasyMock.verify(descriptor);
|
|
|
+ EasyMock.replay(configuration);
|
|
|
+ updateChecksRegistry.add(m_mockCheck);
|
|
|
+
|
|
|
+ PrereqCheckRequest request = new PrereqCheckRequest("cluster");
|
|
|
+ helper.performChecks(request, updateChecksRegistry, configuration);
|
|
|
+
|
|
|
+
|
|
|
+ Assert.assertEquals(null, request.getResult(m_mockCheckDescription));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -102,20 +140,20 @@ public class CheckHelperTest {
|
|
|
@Test
|
|
|
public void testPreUpgradeCheckThrowsException() throws Exception {
|
|
|
final CheckHelper helper = new CheckHelper();
|
|
|
- List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<>();
|
|
|
- AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class);
|
|
|
Configuration configuration = EasyMock.createNiceMock(Configuration.class);
|
|
|
+ List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<>();
|
|
|
|
|
|
EasyMock.expect(configuration.isUpgradePrecheckBypass()).andReturn(false);
|
|
|
- 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, configuration);
|
|
|
- updateChecksRegistry.add(descriptor);
|
|
|
- final List<PrerequisiteCheck> upgradeChecks = helper.performChecks(new PrereqCheckRequest("cluster"), updateChecksRegistry, configuration);
|
|
|
- EasyMock.verify(descriptor);
|
|
|
- Assert.assertEquals(PrereqCheckStatus.FAIL, upgradeChecks.get(0).getStatus());
|
|
|
+ EasyMock.replay(configuration);
|
|
|
+ updateChecksRegistry.add(m_mockCheck);
|
|
|
+
|
|
|
+ // this will cause an exception
|
|
|
+ Mockito.when(m_mockPerform.toString()).thenThrow(new RuntimeException());
|
|
|
+
|
|
|
+ PrereqCheckRequest request = new PrereqCheckRequest("cluster");
|
|
|
+ helper.performChecks(request, updateChecksRegistry, configuration);
|
|
|
+
|
|
|
+ Assert.assertEquals(PrereqCheckStatus.FAIL, request.getResult(m_mockCheckDescription));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -123,98 +161,80 @@ public class CheckHelperTest {
|
|
|
*/
|
|
|
@Test
|
|
|
public void testPreUpgradeCheckBypassesFailure() throws Exception {
|
|
|
- // This mock class extends CheckHelper and overrides the getPrerequisiteChecks method in order to return
|
|
|
- // a PrerequisiteCheck object whose status is FAIL.
|
|
|
- final CheckHelperMock helper = new CheckHelperMock();
|
|
|
+ final CheckHelper helper = new CheckHelper();
|
|
|
Configuration configuration = EasyMock.createNiceMock(Configuration.class);
|
|
|
List<AbstractCheckDescriptor> updateChecksRegistry = new ArrayList<>();
|
|
|
|
|
|
- PrereqCheckRequest checkRequest = EasyMock.createNiceMock(PrereqCheckRequest.class);
|
|
|
EasyMock.expect(configuration.isUpgradePrecheckBypass()).andReturn(true);
|
|
|
- EasyMock.expect(checkRequest.getClusterName()).andReturn("c1").anyTimes();
|
|
|
- EasyMock.replay(checkRequest, configuration);
|
|
|
+ EasyMock.replay(configuration);
|
|
|
+ updateChecksRegistry.add(m_mockCheck);
|
|
|
+
|
|
|
+ // this will cause an exception, triggering the bypass
|
|
|
+ Mockito.when(m_mockPerform.toString()).thenThrow(new RuntimeException());
|
|
|
|
|
|
- final List<PrerequisiteCheck> upgradeChecks = helper.performChecks(checkRequest, updateChecksRegistry, configuration);
|
|
|
- Assert.assertEquals(1, upgradeChecks.size());
|
|
|
- Assert.assertEquals(PrereqCheckStatus.BYPASS, upgradeChecks.get(0).getStatus());
|
|
|
+ PrereqCheckRequest request = new PrereqCheckRequest("cluster");
|
|
|
+ helper.performChecks(request, updateChecksRegistry, configuration);
|
|
|
+
|
|
|
+ Assert.assertEquals(PrereqCheckStatus.BYPASS, request.getResult(m_mockCheckDescription));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testPreUpgradeCheckClusterMissing() throws Exception {
|
|
|
- final Clusters clusters = Mockito.mock(Clusters.class);
|
|
|
+ final Cluster cluster = Mockito.mock(Cluster.class);
|
|
|
+ final Service service = Mockito.mock(Service.class);
|
|
|
+
|
|
|
+ m_services.put("KAFKA", service);
|
|
|
+
|
|
|
+ Mockito.when(cluster.getServices()).thenReturn(new HashMap<String, Service>());
|
|
|
+ Mockito.when(cluster.getClusterId()).thenReturn(1L);
|
|
|
+
|
|
|
+ Mockito.when(clusters.getCluster(Mockito.anyString())).thenReturn(cluster);
|
|
|
+
|
|
|
+ final CheckHelper helper = new CheckHelper();
|
|
|
Configuration configuration = EasyMock.createNiceMock(Configuration.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(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<>();
|
|
|
|
|
|
EasyMock.expect(configuration.isUpgradePrecheckBypass()).andReturn(false);
|
|
|
-
|
|
|
EasyMock.replay(configuration);
|
|
|
+ updateChecksRegistry.add(m_mockCheck);
|
|
|
|
|
|
- // mocked Cluster has no services, so the check should always be PASS
|
|
|
- updateChecksRegistry.add(injector.getInstance(ServicesUpCheck.class));
|
|
|
- List<PrerequisiteCheck> upgradeChecks = helper.performChecks(new PrereqCheckRequest("existing"), updateChecksRegistry, configuration);
|
|
|
- Assert.assertEquals(PrereqCheckStatus.PASS, upgradeChecks.get(0).getStatus());
|
|
|
- upgradeChecks = helper.performChecks(new PrereqCheckRequest("non-existing"), updateChecksRegistry, configuration);
|
|
|
- Assert.assertEquals(PrereqCheckStatus.FAIL, upgradeChecks.get(0).getStatus());
|
|
|
- //non existing cluster is an expected error
|
|
|
- Assert.assertTrue(!upgradeChecks.get(0).getFailReason().equals("Unexpected server error happened"));
|
|
|
- }
|
|
|
+ // this will cause an exception, triggering the fail
|
|
|
+ Mockito.when(m_mockPerform.toString()).thenThrow(new RuntimeException());
|
|
|
|
|
|
- class CheckHelperMock extends CheckHelper {
|
|
|
- @Override
|
|
|
- public List<DescriptorPreCheck> getApplicablePrerequisiteChecks(PrereqCheckRequest request,
|
|
|
- List<AbstractCheckDescriptor> checksRegistry) {
|
|
|
+ PrereqCheckRequest request = new PrereqCheckRequest("cluster");
|
|
|
+ request.setTargetRepositoryVersion(m_repositoryVersion);
|
|
|
|
|
|
- List<DescriptorPreCheck> applicablePreChecks = new LinkedList<>();
|
|
|
+ helper.performChecks(request, updateChecksRegistry, configuration);
|
|
|
|
|
|
- try {
|
|
|
- CheckDescription description = CheckDescription.SERVICES_UP;
|
|
|
- PrerequisiteCheck check = new PrerequisiteCheck(description, "c1");
|
|
|
- check.setStatus(PrereqCheckStatus.FAIL);
|
|
|
+ Assert.assertEquals(PrereqCheckStatus.FAIL, request.getResult(m_mockCheckDescription));
|
|
|
+ }
|
|
|
|
|
|
- AbstractCheckDescriptor descriptor = EasyMock.createNiceMock(AbstractCheckDescriptor.class);
|
|
|
- EasyMock.expect(descriptor.isApplicable(EasyMock.<PrereqCheckRequest>anyObject())).andReturn(true);
|
|
|
- EasyMock.expect(descriptor.getDescription()).andReturn(description).anyTimes();
|
|
|
+ class MockCheck extends AbstractCheckDescriptor {
|
|
|
|
|
|
- // Allow bypassing failures
|
|
|
- EasyMock.replay(descriptor);
|
|
|
+ protected MockCheck() {
|
|
|
+ super(m_mockCheckDescription);
|
|
|
|
|
|
- applicablePreChecks.add(new DescriptorPreCheck(descriptor, check));
|
|
|
- } catch (AmbariException e) {
|
|
|
- ;
|
|
|
- }
|
|
|
+ clustersProvider = new Provider<Clusters>() {
|
|
|
|
|
|
- return applicablePreChecks;
|
|
|
+ @Override
|
|
|
+ public Clusters get() {
|
|
|
+ return clusters;
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
+ /**
|
|
|
+ * {@inheritDoc}
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Set<String> getApplicableServices() {
|
|
|
+ return m_services.keySet();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request)
|
|
|
+ throws AmbariException {
|
|
|
+ m_mockPerform.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|