http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java index ba4b61e..f41b34c 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java @@ -17,8 +17,9 @@ */ package org.apache.ambari.server.checks; -import java.util.Arrays; +import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.controller.PrereqCheckRequest; @@ -30,6 +31,8 @@ import org.apache.ambari.server.state.stack.UpgradePack.PrerequisiteCheckConfig; import org.apache.ambari.server.utils.VersionUtils; import org.apache.commons.lang.BooleanUtils; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import com.google.inject.Singleton; /** @@ -55,43 +58,16 @@ public class YarnTimelineServerStatePreservingCheck extends AbstractCheckDescrip * {@inheritDoc} */ @Override - public boolean isApplicable(PrereqCheckRequest request) throws AmbariException { - if (!super.isApplicable(request, Arrays.asList("YARN"), true)) { - return false; - } - - final Cluster cluster = clustersProvider.get().getCluster(request.getClusterName()); - - String minApplicableStackVersion = null; - PrerequisiteCheckConfig prerequisiteCheckConfig = request.getPrerequisiteCheckConfig(); - Map<String, String> checkProperties = null; - if(prerequisiteCheckConfig != null) { - checkProperties = prerequisiteCheckConfig.getCheckProperties(this.getClass().getName()); - } - if(checkProperties != null && checkProperties.containsKey(MIN_APPLICABLE_STACK_VERSION_PROPERTY_NAME)) { - minApplicableStackVersion = checkProperties.get(MIN_APPLICABLE_STACK_VERSION_PROPERTY_NAME); - } - - // Due to the introduction of YARN Timeline state recovery only from certain - // stack-versions onwards, this check is not applicable to earlier versions - // of the stack. - // Applicable only if min-applicable-stack-version config property is not defined, or - // version equals or exceeds the configured version. - if(minApplicableStackVersion != null && !minApplicableStackVersion.isEmpty()) { - String[] minStack = minApplicableStackVersion.split("-"); - if(minStack.length == 2) { - String minStackName = minStack[0]; - String minStackVersion = minStack[1]; - Service yarnService = cluster.getService("YARN"); - String stackName = yarnService.getDesiredStackId().getStackName(); - if (minStackName.equals(stackName)) { - String currentRepositoryVersion = yarnService.getDesiredRepositoryVersion().getVersion(); - return VersionUtils.compareVersions(currentRepositoryVersion, minStackVersion) >= 0; - } - } - } + public Set<String> getApplicableServices() { + return Sets.newHashSet("YARN"); + } - return true; + /** + * {@inheritDoc} + */ + @Override + public List<CheckQualification> getQualifications() { + return Lists.<CheckQualification> newArrayList(new YarnTimelineServerMinVersionQualification()); } /** @@ -108,4 +84,53 @@ public class YarnTimelineServerStatePreservingCheck extends AbstractCheckDescrip prerequisiteCheck.setFailReason(getFailReason(prerequisiteCheck, request)); } } + + /** + * The {@link YarnTimelineServerMinVersionQualification} is used to determine + * if the ATS component needs to have the + * {@value #MIN_APPLICABLE_STACK_VERSION_PROPERTY_NAME} set. + */ + private class YarnTimelineServerMinVersionQualification implements CheckQualification { + + /** + * {@inheritDoc} + */ + @Override + public boolean isApplicable(PrereqCheckRequest request) throws AmbariException { + final Cluster cluster = clustersProvider.get().getCluster(request.getClusterName()); + + String minApplicableStackVersion = null; + PrerequisiteCheckConfig prerequisiteCheckConfig = request.getPrerequisiteCheckConfig(); + Map<String, String> checkProperties = null; + if(prerequisiteCheckConfig != null) { + checkProperties = prerequisiteCheckConfig.getCheckProperties(this.getClass().getName()); + } + + if(checkProperties != null && checkProperties.containsKey(MIN_APPLICABLE_STACK_VERSION_PROPERTY_NAME)) { + minApplicableStackVersion = checkProperties.get(MIN_APPLICABLE_STACK_VERSION_PROPERTY_NAME); + } + + // Due to the introduction of YARN Timeline state recovery only from certain + // stack-versions onwards, this check is not applicable to earlier versions + // of the stack. + // Applicable only if min-applicable-stack-version config property is not defined, or + // version equals or exceeds the configured version. + if(minApplicableStackVersion != null && !minApplicableStackVersion.isEmpty()) { + String[] minStack = minApplicableStackVersion.split("-"); + if(minStack.length == 2) { + String minStackName = minStack[0]; + String minStackVersion = minStack[1]; + Service yarnService = cluster.getService("YARN"); + String stackName = yarnService.getDesiredStackId().getStackName(); + if (minStackName.equals(stackName)) { + String currentRepositoryVersion = yarnService.getDesiredRepositoryVersion().getVersion(); + return VersionUtils.compareVersions(currentRepositoryVersion, minStackVersion) >= 0; + } + } + } + + return true; + + } + } }
http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java index 8f15479..6d9f655 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java @@ -64,10 +64,6 @@ public class PrereqCheckRequest { return m_upgradeType; } - public String getTargetVersion() { - return m_targetRepositoryVersion.getVersion(); - } - /** * Sets the result of a check. * @param description the description @@ -106,15 +102,6 @@ public class PrereqCheckRequest { } /** - * Gets the target stack of the upgrade. - * - * @return the targetStackId - */ - public StackId getTargetStackId() { - return m_targetRepositoryVersion.getStackId(); - } - - /** * Gets the target repository of the upgrade. * * @return the target repository. http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/main/java/org/apache/ambari/server/state/CheckHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/CheckHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/CheckHelper.java index f3fbc63..b73c866 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/CheckHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/CheckHelper.java @@ -73,7 +73,9 @@ public class CheckHelper { applicablePreChecks.add(new DescriptorPreCheck(checkDescriptor, prerequisiteCheck)); } } catch (Exception ex) { - LOG.error("Check " + checkDescriptor.getDescription().name() + " failed", ex); + LOG.error( + "Unable to determine whether the pre-upgrade check {} is applicable to this upgrade", + checkDescriptor.getDescription().name(), ex); } } return applicablePreChecks; http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/AbstractCheckDescriptorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/AbstractCheckDescriptorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/AbstractCheckDescriptorTest.java index a96ca6c..c294ee7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/AbstractCheckDescriptorTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/AbstractCheckDescriptorTest.java @@ -19,23 +19,28 @@ package org.apache.ambari.server.checks; import static org.easymock.EasyMock.anyString; import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.controller.PrereqCheckRequest; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Service; +import org.apache.ambari.server.state.repository.ClusterVersionSummary; +import org.apache.ambari.server.state.repository.VersionDefinitionXml; import org.apache.ambari.server.state.stack.PrereqCheckType; import org.apache.ambari.server.state.stack.PrerequisiteCheck; import org.apache.ambari.server.state.stack.upgrade.UpgradeType; +import org.apache.hadoop.metrics2.sink.relocated.google.common.collect.Sets; import org.easymock.EasyMock; +import org.easymock.EasyMockSupport; +import org.easymock.Mock; +import org.junit.Before; import org.junit.Test; import com.google.inject.Provider; @@ -45,81 +50,25 @@ import junit.framework.Assert; /** * Unit tests for AbstractCheckDescriptor */ -public class AbstractCheckDescriptorTest { - final private Clusters clusters = EasyMock.createNiceMock(Clusters.class); +public class AbstractCheckDescriptorTest extends EasyMockSupport { + @Mock + private Clusters clusters; - @UpgradeCheck( - group = UpgradeCheckGroup.DEFAULT, - order = 1.0f, - required = { UpgradeType.ROLLING, UpgradeType.NON_ROLLING, UpgradeType.HOST_ORDERED }) - private class TestCheckImpl extends AbstractCheckDescriptor { - private PrereqCheckType m_type; - - TestCheckImpl(PrereqCheckType type) { - super(null); - m_type = type; - - clustersProvider = new Provider<Clusters>() { - @Override - public Clusters get() { - return clusters; - } - }; - } - - @Override - public PrereqCheckType getType() { - return m_type; - } - - @Override - public void perform(PrerequisiteCheck prerequisiteCheck, - PrereqCheckRequest request) throws AmbariException { - } - } - - @UpgradeCheck(group = UpgradeCheckGroup.DEFAULT, order = 1.0f, required = { UpgradeType.ROLLING }) - private class RollingTestCheckImpl extends AbstractCheckDescriptor { - private PrereqCheckType m_type; - - RollingTestCheckImpl(PrereqCheckType type) { - super(null); - m_type = type; - - clustersProvider = new Provider<Clusters>() { - @Override - public Clusters get() { - return clusters; - } - }; - } - - @Override - public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) - throws AmbariException { - } - } - - @UpgradeCheck(group = UpgradeCheckGroup.DEFAULT, order = 1.0f) - private class NotRequiredCheckTest extends AbstractCheckDescriptor { - private PrereqCheckType m_type; - - NotRequiredCheckTest(PrereqCheckType type) { - super(null); - m_type = type; + /** + * Used to mock out what services will be provided to us by the VDF/cluster. + */ + @Mock + private ClusterVersionSummary m_clusterVersionSummary; - clustersProvider = new Provider<Clusters>() { - @Override - public Clusters get() { - return clusters; - } - }; - } + /** + * + */ + @Mock + private VersionDefinitionXml m_vdfXml; - @Override - public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) - throws AmbariException { - } + @Before + public void setup() throws Exception { + injectMocks(this); } @Test @@ -153,7 +102,7 @@ public class AbstractCheckDescriptorTest { @Test public void testIsApplicable() throws Exception{ final String clusterName = "c1"; - final Cluster cluster = EasyMock.createMock(Cluster.class); + final Cluster cluster = createMock(Cluster.class); Map<String, Service> services = new HashMap<String, Service>(){{ @@ -162,44 +111,88 @@ public class AbstractCheckDescriptorTest { put("SERVICE3", null); }}; + Set<String> oneServiceList = Sets.newHashSet("SERVICE1"); + Set<String> atLeastOneServiceList = Sets.newHashSet("SERVICE1", "MISSING_SERVICE"); + Set<String> allServicesList = Sets.newHashSet("SERVICE1", "SERVICE2"); + Set<String> missingServiceList = Sets.newHashSet("MISSING_SERVICE"); + expect(clusters.getCluster(anyString())).andReturn(cluster).atLeastOnce(); expect(cluster.getServices()).andReturn(services).atLeastOnce(); - replay(clusters, cluster); + RepositoryVersionEntity repositoryVersion = createNiceMock(RepositoryVersionEntity.class); + expect(repositoryVersion.getRepositoryXml()).andReturn(m_vdfXml).atLeastOnce(); + expect(m_vdfXml.getClusterSummary(EasyMock.anyObject(Cluster.class))).andReturn( + m_clusterVersionSummary).atLeastOnce(); - AbstractCheckDescriptor check = new TestCheckImpl(PrereqCheckType.SERVICE); - PrereqCheckRequest request = new PrereqCheckRequest(clusterName, UpgradeType.ROLLING); + expect(m_clusterVersionSummary.getAvailableServiceNames()).andReturn( + allServicesList).atLeastOnce(); - List<String> oneServiceList = new ArrayList<String>() {{ - add("SERVICE1"); - }}; - List<String> atLeastOneServiceList = new ArrayList<String>() {{ - add("SERVICE1"); - add("NON_EXISTED_SERVICE"); - }}; - List<String> allServicesList = new ArrayList<String>(){{ - add("SERVICE1"); - add("SERVICE2"); - }}; - List<String> nonExistedList = new ArrayList<String>(){{ - add("NON_EXISTED_SERVICE"); - }}; + + replayAll(); + + TestCheckImpl check = new TestCheckImpl(PrereqCheckType.SERVICE); + PrereqCheckRequest request = new PrereqCheckRequest(clusterName, UpgradeType.ROLLING); + request.setTargetRepositoryVersion(repositoryVersion); // case, where we need at least one service to be present - Assert.assertEquals(true, check.isApplicable(request, oneServiceList, false)); - Assert.assertEquals(true, check.isApplicable(request, atLeastOneServiceList, false)); + check.setApplicableServices(oneServiceList); + Assert.assertTrue(check.isApplicable(request)); - // case, where all services need to be present - Assert.assertEquals(false, check.isApplicable(request,atLeastOneServiceList, true)); - Assert.assertEquals(true, check.isApplicable(request, allServicesList, true)); + check.setApplicableServices(atLeastOneServiceList); + Assert.assertTrue(check.isApplicable(request)); - // Case with empty list of the required services - Assert.assertEquals(false, check.isApplicable(request, new ArrayList<String>(), true)); - Assert.assertEquals(false, check.isApplicable(request, new ArrayList<String>(), false)); + check.setApplicableServices(missingServiceList); + Assert.assertFalse(check.isApplicable(request)); + } + + /** + * Tests that even though the services are installed, the check doesn't match + * since it's for a service not in the PATCH. + * + * @throws Exception + */ + @Test + public void testIsApplicableForPatch() throws Exception { + final String clusterName = "c1"; + final Cluster cluster = createMock(Cluster.class); - // Case with non existed services - Assert.assertEquals(false, check.isApplicable(request, nonExistedList, false)); - Assert.assertEquals(false, check.isApplicable(request, nonExistedList, true)); + Map<String, Service> services = new HashMap<String, Service>() { + { + put("SERVICE1", null); + put("SERVICE2", null); + put("SERVICE3", null); + } + }; + + Set<String> oneServiceList = Sets.newHashSet("SERVICE1"); + + expect(clusters.getCluster(anyString())).andReturn(cluster).atLeastOnce(); + expect(cluster.getServices()).andReturn(services).atLeastOnce(); + + RepositoryVersionEntity repositoryVersion = createNiceMock(RepositoryVersionEntity.class); + expect(repositoryVersion.getRepositoryXml()).andReturn(m_vdfXml).atLeastOnce(); + expect(m_vdfXml.getClusterSummary(EasyMock.anyObject(Cluster.class))).andReturn( + m_clusterVersionSummary).atLeastOnce(); + + // the cluster summary will only return 1 service for the upgrade, even + // though this cluster has 2 services installed + expect(m_clusterVersionSummary.getAvailableServiceNames()).andReturn( + oneServiceList).atLeastOnce(); + + replayAll(); + + TestCheckImpl check = new TestCheckImpl(PrereqCheckType.SERVICE); + PrereqCheckRequest request = new PrereqCheckRequest(clusterName, UpgradeType.ROLLING); + request.setTargetRepositoryVersion(repositoryVersion); + + // since the check is for SERVICE2, it should not match even though its + // installed since the repository is only for SERVICE1 + check.setApplicableServices(Sets.newHashSet("SERVICE2")); + Assert.assertFalse(check.isApplicable(request)); + + // ok, so now change the check to match against SERVICE1 + check.setApplicableServices(Sets.newHashSet("SERVICE1")); + Assert.assertTrue(check.isApplicable(request)); } /** @@ -224,4 +217,90 @@ public class AbstractCheckDescriptorTest { Assert.assertTrue(requiredCheck.isRequired(UpgradeType.HOST_ORDERED)); } + @UpgradeCheck( + group = UpgradeCheckGroup.DEFAULT, + order = 1.0f, + required = { UpgradeType.ROLLING, UpgradeType.NON_ROLLING, UpgradeType.HOST_ORDERED }) + private class TestCheckImpl extends AbstractCheckDescriptor { + private PrereqCheckType m_type; + private Set<String> m_applicableServices = Sets.newHashSet(); + + TestCheckImpl(PrereqCheckType type) { + super(null); + m_type = type; + + clustersProvider = new Provider<Clusters>() { + @Override + public Clusters get() { + return clusters; + } + }; + } + + @Override + public PrereqCheckType getType() { + return m_type; + } + + @Override + public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) + throws AmbariException { + } + + /** + * {@inheritDoc} + */ + @Override + public Set<String> getApplicableServices() { + return m_applicableServices; + } + + void setApplicableServices(Set<String> applicableServices) { + m_applicableServices = applicableServices; + } + } + + @UpgradeCheck(group = UpgradeCheckGroup.DEFAULT, order = 1.0f, required = { UpgradeType.ROLLING }) + private class RollingTestCheckImpl extends AbstractCheckDescriptor { + private PrereqCheckType m_type; + + RollingTestCheckImpl(PrereqCheckType type) { + super(null); + m_type = type; + + clustersProvider = new Provider<Clusters>() { + @Override + public Clusters get() { + return clusters; + } + }; + } + + @Override + public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) + throws AmbariException { + } + } + + @UpgradeCheck(group = UpgradeCheckGroup.DEFAULT, order = 1.0f) + private class NotRequiredCheckTest extends AbstractCheckDescriptor { + private PrereqCheckType m_type; + + NotRequiredCheckTest(PrereqCheckType type) { + super(null); + m_type = type; + + clustersProvider = new Provider<Clusters>() { + @Override + public Clusters get() { + return clusters; + } + }; + } + + @Override + public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) + throws AmbariException { + } + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/ClientRetryPropertyCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/ClientRetryPropertyCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/ClientRetryPropertyCheckTest.java index 85774bf..206b451 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/ClientRetryPropertyCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/ClientRetryPropertyCheckTest.java @@ -29,30 +29,45 @@ import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /** * Tests for {@link ClientRetryPropertyCheckTest} */ +@RunWith(MockitoJUnitRunner.class) public class ClientRetryPropertyCheckTest { private final Clusters m_clusters = Mockito.mock(Clusters.class); private final ClientRetryPropertyCheck m_check = new ClientRetryPropertyCheck(); - final RepositoryVersionEntity m_repositoryVersion = Mockito.mock(RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); /** * */ @Before - public void setup() { + public void setup() throws Exception { m_check.clustersProvider = new Provider<Clusters>() { @Override @@ -65,6 +80,11 @@ public class ClientRetryPropertyCheckTest { Mockito.when(m_repositoryVersion.getVersion()).thenReturn("2.3.0.0-1234"); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "2.3")); + + 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()); } /** @@ -76,8 +96,7 @@ public class ClientRetryPropertyCheckTest { Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(m_clusters.getCluster("cluster")).thenReturn(cluster); - Map<String, Service> services = new HashMap<>(); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); request.setTargetRepositoryVersion(m_repositoryVersion); @@ -86,12 +105,12 @@ public class ClientRetryPropertyCheckTest { Assert.assertFalse(m_check.isApplicable(request)); // HDFS installed - services.put("HDFS", Mockito.mock(Service.class)); + m_services.put("HDFS", Mockito.mock(Service.class)); Assert.assertTrue(m_check.isApplicable(request)); // OOZIE installed - services.clear(); - services.put("OOZIE", Mockito.mock(Service.class)); + m_services.clear(); + m_services.put("OOZIE", Mockito.mock(Service.class)); Assert.assertTrue(m_check.isApplicable(request)); } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentExistsInRepoCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentExistsInRepoCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentExistsInRepoCheckTest.java index af28619..ede1daa 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentExistsInRepoCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentExistsInRepoCheckTest.java @@ -34,6 +34,8 @@ import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceInfo; import org.apache.ambari.server.state.StackId; +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.commons.lang.StringUtils; @@ -94,6 +96,12 @@ public class ComponentExistsInRepoCheckTest extends EasyMockSupport { private ServiceComponent m_zookeeperServer; @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock private RepositoryVersionEntity m_repositoryVersion; @Before @@ -116,6 +124,9 @@ public class ComponentExistsInRepoCheckTest extends EasyMockSupport { }; expect(m_cluster.getServices()).andReturn(CLUSTER_SERVICES).atLeastOnce(); + expect(m_cluster.getService("ZOOKEEPER")).andReturn(m_zookeeperService).anyTimes(); + expect(m_cluster.getService("FOO_SERVICE")).andReturn(m_fooService).anyTimes(); + expect(m_clusters.getCluster((String) anyObject())).andReturn(m_cluster).anyTimes(); ZK_SERVICE_COMPONENTS.put("ZOOKEEPER_SERVER", m_zookeeperServer); @@ -138,6 +149,11 @@ public class ComponentExistsInRepoCheckTest extends EasyMockSupport { expect(m_repositoryVersion.getStackId()).andReturn(TARGET_STACK).anyTimes(); expect(m_repositoryVersion.getVersion()).andReturn("2.2.0").anyTimes(); + + expect(m_repositoryVersion.getRepositoryXml()).andReturn(m_vdfXml).anyTimes(); + expect(m_vdfXml.getClusterSummary(anyObject(Cluster.class))).andReturn(m_clusterVersionSummary).anyTimes(); + expect(m_clusterVersionSummary.getAvailableServiceNames()).andReturn(CLUSTER_SERVICES.keySet()).anyTimes(); + } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentsInstallationCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentsInstallationCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentsInstallationCheckTest.java index ef833b0..4ae33f8 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentsInstallationCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/ComponentsInstallationCheckTest.java @@ -37,13 +37,17 @@ import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.StackId; import org.apache.ambari.server.state.State; +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.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; @@ -63,15 +67,30 @@ public class ComponentsInstallationCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private AmbariMetaInfo ambariMetaInfo = Mockito.mock(AmbariMetaInfo.class); - final RepositoryVersionEntity m_repositoryVersion = Mockito.mock(RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); /** * */ @Before - public void setup() { + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + + m_services.clear(); Mockito.when(m_repositoryVersion.getVersion()).thenReturn("2.2.0.0-1234"); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "2.2")); + 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()); } @Test @@ -118,10 +137,9 @@ public class ComponentsInstallationCheckTest { Mockito.when(tezService.getMaintenanceState()).thenReturn(MaintenanceState.OFF); Mockito.when(amsService.getMaintenanceState()).thenReturn(MaintenanceState.OFF); - HashMap<String, Service> clusterServices = new HashMap<>(); - clusterServices.put("HDFS", hdfsService); - clusterServices.put("TEZ", tezService); - clusterServices.put("AMBARI_METRICS", amsService); + m_services.put("HDFS", hdfsService); + m_services.put("TEZ", tezService); + m_services.put("AMBARI_METRICS", amsService); Mockito.when(hdfsService.getName()).thenReturn("HDFS"); Mockito.when(tezService.getName()).thenReturn("TEZ"); @@ -131,7 +149,11 @@ public class ComponentsInstallationCheckTest { Mockito.when(tezService.isClientOnlyService()).thenReturn(true); Mockito.when(amsService.isClientOnlyService()).thenReturn(false); - Mockito.when(cluster.getServices()).thenReturn(clusterServices); + Mockito.when(cluster.getServices()).thenReturn(m_services); + + Mockito.when(cluster.getService("HDFS")).thenReturn(hdfsService); + Mockito.when(cluster.getService("TEZ")).thenReturn(tezService); + Mockito.when(cluster.getService("AMBARI_METRICS")).thenReturn(amsService); Mockito.when(ambariMetaInfo.getComponent(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<ComponentInfo>() { @@ -260,22 +282,26 @@ public class ComponentsInstallationCheckTest { Mockito.when(hcs.getDesiredState()).thenReturn(State.INSTALLED); Mockito.when(hcs.getCurrentState()).thenReturn(State.STARTED); } + + PrereqCheckRequest request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); + Mockito.when(hcsTezClient.getCurrentState()).thenReturn(State.INSTALLED); PrerequisiteCheck check = new PrerequisiteCheck(null, null); - componentsInstallationCheck.perform(check, new PrereqCheckRequest("cluster")); + componentsInstallationCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); // Case 2. Ensure that AMS is ignored even if their current state is not INSTALLED Mockito.when(hcsMetricsCollector.getCurrentState()).thenReturn(State.INSTALL_FAILED); Mockito.when(hcsMetricsMonitor.getCurrentState()).thenReturn(State.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); - componentsInstallationCheck.perform(check, new PrereqCheckRequest("cluster")); + componentsInstallationCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); // Case 3: Change TEZ client state to INSTALL_FAILED, should fail Mockito.when(hcsTezClient.getCurrentState()).thenReturn(State.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); - componentsInstallationCheck.perform(check, new PrereqCheckRequest("cluster")); + componentsInstallationCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); Assert.assertTrue(check.getFailReason().indexOf("Service components in INSTALL_FAILED state") > -1); @@ -283,7 +309,7 @@ public class ComponentsInstallationCheckTest { Mockito.when(tezService.getMaintenanceState()).thenReturn(MaintenanceState.ON); Mockito.when(hcsTezClient.getCurrentState()).thenReturn(State.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); - componentsInstallationCheck.perform(check, new PrereqCheckRequest("cluster")); + componentsInstallationCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); // Case 5: Change TEZ client state to INSTALL_FAILED and place host2 in Maintenance mode, should succeed @@ -291,7 +317,7 @@ public class ComponentsInstallationCheckTest { Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.ON); Mockito.when(hcsTezClient.getCurrentState()).thenReturn(State.INSTALL_FAILED); check = new PrerequisiteCheck(null, null); - componentsInstallationCheck.perform(check, new PrereqCheckRequest("cluster")); + componentsInstallationCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveDynamicServiceDiscoveryCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveDynamicServiceDiscoveryCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveDynamicServiceDiscoveryCheckTest.java index 14fab7c..606d4b7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveDynamicServiceDiscoveryCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveDynamicServiceDiscoveryCheckTest.java @@ -52,7 +52,7 @@ public class HiveDynamicServiceDiscoveryCheckTest { * */ @Before - public void setup() { + public void setup() throws Exception { m_check.clustersProvider = new Provider<Clusters>() { @Override http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveMultipleMetastoreCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveMultipleMetastoreCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveMultipleMetastoreCheckTest.java index 5e7b1e5..7a1fca3 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveMultipleMetastoreCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveMultipleMetastoreCheckTest.java @@ -30,29 +30,44 @@ import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; import org.apache.ambari.server.state.StackId; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /** * Tests {@link HiveMultipleMetastoreCheck} */ +@RunWith(MockitoJUnitRunner.class) public class HiveMultipleMetastoreCheckTest { private final Clusters m_clusters = Mockito.mock(Clusters.class); private final HiveMultipleMetastoreCheck m_check = new HiveMultipleMetastoreCheck(); - final RepositoryVersionEntity m_repositoryVersion = Mockito.mock(RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); /** * */ @Before - public void setup() { + public void setup() throws Exception { m_check.clustersProvider = new Provider<Clusters>() { @Override @@ -65,6 +80,11 @@ public class HiveMultipleMetastoreCheckTest { Mockito.when(m_repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); + + 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()); } /** @@ -78,10 +98,9 @@ public class HiveMultipleMetastoreCheckTest { Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(m_clusters.getCluster("cluster")).thenReturn(cluster); - Map<String, Service> services = new HashMap<>(); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); - services.put("HDFS", Mockito.mock(Service.class)); + m_services.put("HDFS", Mockito.mock(Service.class)); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); request.setTargetRepositoryVersion(m_repositoryVersion); @@ -90,7 +109,7 @@ public class HiveMultipleMetastoreCheckTest { Assert.assertFalse(m_check.isApplicable(request)); // install HIVE - services.put("HIVE", Mockito.mock(Service.class)); + m_services.put("HIVE", Mockito.mock(Service.class)); // HIVE installed Assert.assertTrue(m_check.isApplicable(request)); http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveNotRollingWarningTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveNotRollingWarningTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveNotRollingWarningTest.java index 665d6ec..3b0c900 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveNotRollingWarningTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HiveNotRollingWarningTest.java @@ -21,28 +21,57 @@ import java.util.HashMap; import java.util.Map; import org.apache.ambari.server.controller.PrereqCheckRequest; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.UpgradeType; import org.easymock.EasyMock; import org.easymock.EasyMockSupport; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /** * Tests {@link HiveNotRollingWarning}. */ +@RunWith(MockitoJUnitRunner.class) public class HiveNotRollingWarningTest extends EasyMockSupport { private final String m_clusterName = "c1"; private final Clusters m_clusters = niceMock(Clusters.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + + @Before + public void setup() throws Exception { + 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()); + } + + /** * @throws Exception */ @@ -60,18 +89,18 @@ public class HiveNotRollingWarningTest extends EasyMockSupport { final Cluster cluster = niceMock(Cluster.class); final Service hive = niceMock(Service.class); - final Map<String, Service> services = new HashMap<>(); - services.put("HIVE", hive); + m_services.put("HIVE", hive); EasyMock.expect(cluster.getClusterId()).andReturn(1L).anyTimes(); EasyMock.expect(cluster.getCurrentStackVersion()).andReturn(new StackId("HDP", "2.3")).anyTimes(); - EasyMock.expect(cluster.getServices()).andReturn(services).atLeastOnce(); + EasyMock.expect(cluster.getServices()).andReturn(m_services).anyTimes(); EasyMock.expect(m_clusters.getCluster(m_clusterName)).andReturn(cluster).atLeastOnce(); PrereqCheckRequest request = niceMock(PrereqCheckRequest.class); - EasyMock.expect(request.getClusterName()).andReturn(m_clusterName); - EasyMock.expect(request.getUpgradeType()).andReturn(UpgradeType.ROLLING); + EasyMock.expect(request.getClusterName()).andReturn(m_clusterName).anyTimes(); + EasyMock.expect(request.getUpgradeType()).andReturn(UpgradeType.ROLLING).anyTimes(); + EasyMock.expect(request.getTargetRepositoryVersion()).andReturn(m_repositoryVersion).atLeastOnce(); replayAll(); http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsHeartbeatCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsHeartbeatCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsHeartbeatCheckTest.java index a15f9c1..daf2e4c 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsHeartbeatCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsHeartbeatCheckTest.java @@ -52,7 +52,7 @@ public class HostsHeartbeatCheckTest { * */ @Before - public void setup() { + public void setup() throws Exception { Mockito.when(m_repositoryVersion.getVersion()).thenReturn("2.2.0.0-1234"); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "2.2")); } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsMasterMaintenanceCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsMasterMaintenanceCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsMasterMaintenanceCheckTest.java index e4901c7..8afa1ea 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsMasterMaintenanceCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsMasterMaintenanceCheckTest.java @@ -30,6 +30,8 @@ import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.UpgradePack; @@ -37,8 +39,12 @@ import org.apache.ambari.server.state.stack.UpgradePack.ProcessingComponent; import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper; import org.apache.ambari.server.state.stack.upgrade.UpgradeType; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; @@ -46,22 +52,39 @@ import com.google.inject.Provider; * Unit tests for HostsMasterMaintenanceCheck * */ +@RunWith(MockitoJUnitRunner.class) public class HostsMasterMaintenanceCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final RepositoryVersionDAO repositoryVersionDAO = Mockito.mock(RepositoryVersionDAO.class); private final RepositoryVersionHelper repositoryVersionHelper = Mockito.mock(RepositoryVersionHelper.class); private final AmbariMetaInfo ambariMetaInfo = Mockito.mock(AmbariMetaInfo.class); - final RepositoryVersionEntity repositoryVersion = Mockito.mock( - RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + + @Before + public void setup() throws Exception { + 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()); + } + @Test public void testIsApplicable() throws Exception { - Mockito.when(repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); - Mockito.when(repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); + Mockito.when(m_repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); + Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); final PrereqCheckRequest request = new PrereqCheckRequest("cluster"); - request.setTargetRepositoryVersion(repositoryVersion); + request.setTargetRepositoryVersion(m_repositoryVersion); HostsMasterMaintenanceCheck hmmc = new HostsMasterMaintenanceCheck(); Configuration config = Mockito.mock(Configuration.class); hmmc.config = config; @@ -71,19 +94,13 @@ public class HostsMasterMaintenanceCheckTest { HostsMasterMaintenanceCheck hmmc2 = new HostsMasterMaintenanceCheck(); hmmc2.config = config; Assert.assertTrue(hmmc2.isApplicable(request)); - request.setTargetRepositoryVersion(repositoryVersion); - - // reset the mock - Mockito.reset(repositoryVersion); - - hmmc2.config = config; - Assert.assertFalse(hmmc2.isApplicable(request)); + request.setTargetRepositoryVersion(m_repositoryVersion); } @Test public void testPerform() throws Exception { - Mockito.when(repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); - Mockito.when(repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); + Mockito.when(m_repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); + Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); final String upgradePackName = "upgrade_pack"; final HostsMasterMaintenanceCheck hostsMasterMaintenanceCheck = new HostsMasterMaintenanceCheck(); @@ -121,7 +138,7 @@ public class HostsMasterMaintenanceCheckTest { PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest checkRequest = new PrereqCheckRequest("cluster"); - checkRequest.setTargetRepositoryVersion(repositoryVersion); + checkRequest.setTargetRepositoryVersion(m_repositoryVersion); hostsMasterMaintenanceCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); @@ -131,7 +148,7 @@ public class HostsMasterMaintenanceCheckTest { check = new PrerequisiteCheck(null, null); checkRequest = new PrereqCheckRequest("cluster"); - checkRequest.setTargetRepositoryVersion(repositoryVersion); + checkRequest.setTargetRepositoryVersion(m_repositoryVersion); hostsMasterMaintenanceCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); @@ -147,7 +164,7 @@ public class HostsMasterMaintenanceCheckTest { check = new PrerequisiteCheck(null, null); checkRequest = new PrereqCheckRequest("cluster"); - checkRequest.setTargetRepositoryVersion(repositoryVersion); + checkRequest.setTargetRepositoryVersion(m_repositoryVersion); hostsMasterMaintenanceCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsRepositoryVersionCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsRepositoryVersionCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsRepositoryVersionCheckTest.java index 67e8dfa..5539618 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsRepositoryVersionCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HostsRepositoryVersionCheckTest.java @@ -17,7 +17,6 @@ */ package org.apache.ambari.server.checks; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -33,13 +32,19 @@ import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.MaintenanceState; import org.apache.ambari.server.state.RepositoryVersionState; +import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; @@ -47,23 +52,38 @@ import com.google.inject.Provider; * Unit tests for HostsRepositoryVersionCheck * */ +@RunWith(MockitoJUnitRunner.class) public class HostsRepositoryVersionCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final HostVersionDAO hostVersionDAO = Mockito.mock(HostVersionDAO.class); private final RepositoryVersionDAO repositoryVersionDAO = Mockito.mock(RepositoryVersionDAO.class); - final RepositoryVersionEntity repositoryVersion = Mockito.mock(RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); @Before - public void setup() { - Mockito.when(repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); - Mockito.when(repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); + public void setup() throws Exception { + m_services.clear(); + + Mockito.when(m_repositoryVersion.getVersion()).thenReturn("1.0.0.0-1234"); + Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "1.0")); + 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()); } @Test public void testIsApplicable() throws Exception { final PrereqCheckRequest request = new PrereqCheckRequest("cluster"); - request.setTargetRepositoryVersion(repositoryVersion); + request.setTargetRepositoryVersion(m_repositoryVersion); HostsRepositoryVersionCheck hrvc = new HostsRepositoryVersionCheck(); Configuration config = Mockito.mock(Configuration.class); hrvc.config = config; @@ -72,13 +92,6 @@ public class HostsRepositoryVersionCheckTest { HostsRepositoryVersionCheck hrvc2 = new HostsRepositoryVersionCheck(); hrvc2.config = config; Assert.assertTrue(hrvc2.isApplicable(request)); - - Mockito.reset(repositoryVersion); - request.setTargetRepositoryVersion(repositoryVersion); - - HostsMasterMaintenanceCheck hmmc2 = new HostsMasterMaintenanceCheck(); - hmmc2.config = config; - Assert.assertFalse(hmmc2.isApplicable(request)); } @Test @@ -131,7 +144,7 @@ public class HostsRepositoryVersionCheckTest { PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest checkRequest = new PrereqCheckRequest("cluster"); - checkRequest.setTargetRepositoryVersion(repositoryVersion); + checkRequest.setTargetRepositoryVersion(m_repositoryVersion); hostsRepositoryVersionCheck.perform(check, checkRequest); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); @@ -201,22 +214,29 @@ public class HostsRepositoryVersionCheckTest { Mockito.when(host1.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host3.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); + + Mockito.when(host1.getHostName()).thenReturn("host1"); + Mockito.when(host2.getHostName()).thenReturn("host2"); + Mockito.when(host3.getHostName()).thenReturn("host3"); + hosts.put("host1", host1); hosts.put("host2", host2); hosts.put("host3", host3); Mockito.when(clusters.getHostsForCluster("cluster")).thenReturn(hosts); HostVersionEntity hve = new HostVersionEntity(); - hve.setRepositoryVersion(repositoryVersion); + hve.setRepositoryVersion(m_repositoryVersion); hve.setState(RepositoryVersionState.INSTALLED); - Mockito.when( - hostVersionDAO.findByHost(Mockito.anyString())).thenReturn( - Collections.singletonList(hve)); + for (String hostName : hosts.keySet()) { + Mockito.when(hostVersionDAO.findByClusterStackVersionAndHost("cluster", + m_repositoryVersion.getStackId(), m_repositoryVersion.getVersion(), hostName)).thenReturn( + hve); + } PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); - request.setTargetRepositoryVersion(repositoryVersion); + request.setTargetRepositoryVersion(m_repositoryVersion); hostsRepositoryVersionCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); @@ -256,22 +276,30 @@ public class HostsRepositoryVersionCheckTest { Mockito.when(host1.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host3.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); + + Mockito.when(host1.getHostName()).thenReturn("host1"); + Mockito.when(host2.getHostName()).thenReturn("host2"); + Mockito.when(host3.getHostName()).thenReturn("host3"); + hosts.put("host1", host1); hosts.put("host2", host2); hosts.put("host3", host3); Mockito.when(clusters.getHostsForCluster("cluster")).thenReturn(hosts); HostVersionEntity hve = new HostVersionEntity(); - hve.setRepositoryVersion(repositoryVersion); + hve.setRepositoryVersion(m_repositoryVersion); hve.setState(RepositoryVersionState.NOT_REQUIRED); - Mockito.when( - hostVersionDAO.findByHost(Mockito.anyString())).thenReturn( - Collections.singletonList(hve)); + for (String hostName : hosts.keySet()) { + Mockito.when(hostVersionDAO.findByClusterStackVersionAndHost("cluster", + m_repositoryVersion.getStackId(), m_repositoryVersion.getVersion(), hostName)).thenReturn( + hve); + } PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); - request.setTargetRepositoryVersion(repositoryVersion); + request.setTargetRepositoryVersion(m_repositoryVersion); + hostsRepositoryVersionCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java index 9d623ec..cdfbb0a 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java @@ -72,7 +72,7 @@ public class InstallPackagesCheckTest { * */ @Before - public void setup() { + public void setup() throws Exception { Mockito.when(m_repositoryVersion.getVersion()).thenReturn(repositoryVersion); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(targetStackId); } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/KafkaKerberosCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/KafkaKerberosCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/KafkaKerberosCheckTest.java index 41962f9..9a95c42 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/KafkaKerberosCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/KafkaKerberosCheckTest.java @@ -21,26 +21,44 @@ import java.util.HashMap; import java.util.Map; import org.apache.ambari.server.controller.PrereqCheckRequest; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.SecurityType; import org.apache.ambari.server.state.Service; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; +@RunWith(MockitoJUnitRunner.class) public class KafkaKerberosCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final KafkaKerberosCheck kafkaKerberosCheck = new KafkaKerberosCheck(); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + @Before - public void setup() { + public void setup() throws Exception { kafkaKerberosCheck.clustersProvider = new Provider<Clusters>() { @Override @@ -48,24 +66,31 @@ public class KafkaKerberosCheckTest { return clusters; } }; + + 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()); } @Test public void testIsApplicable() throws Exception { final Cluster cluster = Mockito.mock(Cluster.class); - final Map<String, Service> services = new HashMap<>(); final Service service = Mockito.mock(Service.class); - services.put("KAFKA", service); + m_services.put("KAFKA", service); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); - Assert.assertTrue(kafkaKerberosCheck.isApplicable(new PrereqCheckRequest("cluster"))); + PrereqCheckRequest request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); + + Assert.assertTrue(kafkaKerberosCheck.isApplicable(request)); - services.remove("KAFKA"); - Assert.assertFalse(kafkaKerberosCheck.isApplicable(new PrereqCheckRequest("cluster"))); + m_services.remove("KAFKA"); + Assert.assertFalse(kafkaKerberosCheck.isApplicable(request)); } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/MapReduce2JobHistoryStatePreservingCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/MapReduce2JobHistoryStatePreservingCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/MapReduce2JobHistoryStatePreservingCheckTest.java index a232f77..fc322b3 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/MapReduce2JobHistoryStatePreservingCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/MapReduce2JobHistoryStatePreservingCheckTest.java @@ -31,31 +31,46 @@ import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.RepositoryType; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /** * Tests for {@link org.apache.ambari.server.checks.MapReduce2JobHistoryStatePreservingCheckTest} */ +@RunWith(MockitoJUnitRunner.class) public class MapReduce2JobHistoryStatePreservingCheckTest { private final Clusters m_clusters = Mockito.mock(Clusters.class); private final RepositoryVersionDAO m_repositoryVersionDao = Mockito.mock(RepositoryVersionDAO.class); private final MapReduce2JobHistoryStatePreservingCheck m_check = new MapReduce2JobHistoryStatePreservingCheck(); - final RepositoryVersionEntity m_repositoryVersion = Mockito.mock(RepositoryVersionEntity.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); /** * */ @Before - public void setup() { + public void setup() throws Exception { m_check.clustersProvider = new Provider<Clusters>() { @Override @@ -80,6 +95,11 @@ public class MapReduce2JobHistoryStatePreservingCheckTest { Mockito.when(m_repositoryVersion.getVersion()).thenReturn("2.3.1.1-1234"); Mockito.when(m_repositoryVersion.getStackId()).thenReturn(new StackId("HDP", "2.3")); + + 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()); } /** @@ -92,8 +112,7 @@ public class MapReduce2JobHistoryStatePreservingCheckTest { Mockito.when(m_clusters.getCluster("cluster")).thenReturn(cluster); Mockito.when(cluster.getCurrentStackVersion()).thenReturn(new StackId("HDP-2.3")); - Map<String, Service> services = new HashMap<>(); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); request.setSourceStackId(new StackId("HDP", "2.3.0.0")); @@ -103,7 +122,7 @@ public class MapReduce2JobHistoryStatePreservingCheckTest { Assert.assertFalse(m_check.isApplicable(request)); // MAPREDUCE2 installed - services.put("MAPREDUCE2", Mockito.mock(Service.class)); + m_services.put("MAPREDUCE2", Mockito.mock(Service.class)); Assert.assertTrue(m_check.isApplicable(request)); } @@ -157,11 +176,15 @@ public class MapReduce2JobHistoryStatePreservingCheckTest { } }); Mockito.when(cluster.getCurrentStackVersion()).thenReturn(new StackId("MYSTACK-12.2")); - RepositoryVersionEntity repositoryVersionEntity = Mockito.mock(RepositoryVersionEntity.class); Mockito.when(m_clusters.getCluster("c1")).thenReturn(cluster); PrereqCheckRequest request = new PrereqCheckRequest("c1"); + request.setTargetRepositoryVersion(m_repositoryVersion); + + Mockito.when(m_repositoryVersion.getVersion()).thenReturn("2.0.0.1"); + + // MAPREDUCE2 installed + m_services.put("MAPREDUCE2", Mockito.mock(Service.class)); - Mockito.when(repositoryVersionEntity.getVersion()).thenReturn("2.0.0.1"); boolean isApplicable = m_check.isApplicable(request); Assert.assertTrue(isApplicable); } http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerAuditDbCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerAuditDbCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerAuditDbCheckTest.java index e3e14d5..ec91e48 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerAuditDbCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerAuditDbCheckTest.java @@ -22,28 +22,45 @@ import java.util.Map; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.PrereqCheckRequest; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.Service; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /* Tests for RangerAuditDbCheck */ - +@RunWith(MockitoJUnitRunner.class) public class RangerAuditDbCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final RangerAuditDbCheck rangerAuditDbCheck = new RangerAuditDbCheck(); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + @Before - public void setup() { + public void setup() throws Exception { rangerAuditDbCheck.clustersProvider = new Provider<Clusters>() { @Override @@ -53,24 +70,31 @@ public class RangerAuditDbCheckTest { }; Configuration config = Mockito.mock(Configuration.class); rangerAuditDbCheck.config = config; + + 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()); } @Test public void testIsApplicable() throws Exception { final Cluster cluster = Mockito.mock(Cluster.class); - final Map<String, Service> services = new HashMap<>(); final Service service = Mockito.mock(Service.class); - services.put("RANGER", service); + m_services.put("RANGER", service); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); - Assert.assertTrue(rangerAuditDbCheck.isApplicable(new PrereqCheckRequest("cluster"))); + PrereqCheckRequest request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); - services.remove("RANGER"); - Assert.assertFalse(rangerAuditDbCheck.isApplicable(new PrereqCheckRequest("cluster"))); + Assert.assertTrue(rangerAuditDbCheck.isApplicable(request)); + + m_services.remove("RANGER"); + Assert.assertFalse(rangerAuditDbCheck.isApplicable(request)); } @Test @@ -107,5 +131,5 @@ public class RangerAuditDbCheckTest { Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); } - + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerPasswordCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerPasswordCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerPasswordCheckTest.java index e5b231f..f9569c3 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerPasswordCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerPasswordCheckTest.java @@ -22,7 +22,6 @@ import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; @@ -35,18 +34,24 @@ import java.util.Map; import org.apache.ambari.server.controller.PrereqCheckRequest; import org.apache.ambari.server.controller.internal.URLStreamProvider; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.StackId; +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.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.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -82,12 +87,25 @@ public class RangerPasswordCheckTest { "]}"; private Clusters m_clusters = EasyMock.createMock(Clusters.class); - private Map<String, String> m_configMap = new HashMap<String, String>(); + private Map<String, String> m_configMap = new HashMap<>(); private RangerPasswordCheck m_rpc = null; private URLStreamProvider m_streamProvider = EasyMock.createMock(URLStreamProvider.class); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + @Before public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + m_configMap.put("policymgr_external_url", RANGER_URL); m_configMap.put("admin_username", "admin"); m_configMap.put("admin_password", "pass"); @@ -129,38 +147,29 @@ public class RangerPasswordCheckTest { EasyMock.reset(m_streamProvider); PowerMockito.whenNew(URLStreamProvider.class).withAnyArguments().thenReturn(m_streamProvider); + + 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()); } @Test public void testApplicable() throws Exception { final Service service = EasyMock.createMock(Service.class); - Map<String, Service> services = new HashMap<>(); - services.put("RANGER", service); + m_services.put("RANGER", service); Cluster cluster = m_clusters.getCluster("cluster"); EasyMock.reset(cluster); - expect(cluster.getServices()).andReturn(services).anyTimes(); + expect(cluster.getServices()).andReturn(m_services).anyTimes(); expect(cluster.getCurrentStackVersion()).andReturn(new StackId("HDP-2.3")).anyTimes(); replay(cluster); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); - request.setSourceStackId(new StackId("HDP-2.3")); - assertTrue(m_rpc.isApplicable(request)); - - request = new PrereqCheckRequest("cluster"); - request.setSourceStackId(new StackId("HDP-2.2")); - assertFalse(m_rpc.isApplicable(request)); + request.setTargetRepositoryVersion(m_repositoryVersion); - EasyMock.reset(cluster); - expect(cluster.getServices()).andReturn(services).anyTimes(); - expect(cluster.getCurrentStackVersion()).andReturn(new StackId("WILDSTACK-2.0")).anyTimes(); - replay(cluster); - - request = new PrereqCheckRequest("cluster"); - request.setSourceStackId(new StackId("HDP-2.2")); assertTrue(m_rpc.isApplicable(request)); - } @SuppressWarnings("unchecked") http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerSSLConfigCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerSSLConfigCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerSSLConfigCheckTest.java index 2af5502..af016b0 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerSSLConfigCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/RangerSSLConfigCheckTest.java @@ -22,29 +22,47 @@ import java.util.Map; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.PrereqCheckRequest; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.Service; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; /* Test for RangerSSLConfigCheck */ +@RunWith(MockitoJUnitRunner.class) public class RangerSSLConfigCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final RangerSSLConfigCheck rangerSSLConfigCheck = new RangerSSLConfigCheck(); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + @Before - public void setup() { + public void setup() throws Exception { rangerSSLConfigCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { @@ -53,25 +71,31 @@ public class RangerSSLConfigCheckTest { }; Configuration config = Mockito.mock(Configuration.class); rangerSSLConfigCheck.config = config; + + 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()); } @Test public void testIsApplicable() throws Exception { final Cluster cluster = Mockito.mock(Cluster.class); - final Map<String, Service> services = new HashMap<>(); final Service service = Mockito.mock(Service.class); - services.put("RANGER", service); + m_services.put("RANGER", service); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); - Assert.assertTrue(rangerSSLConfigCheck.isApplicable(new PrereqCheckRequest("cluster"))); + PrereqCheckRequest request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); - services.remove("RANGER"); - Assert.assertFalse(rangerSSLConfigCheck.isApplicable(new PrereqCheckRequest("cluster"))); + Assert.assertTrue(rangerSSLConfigCheck.isApplicable(request)); + m_services.remove("RANGER"); + Assert.assertFalse(rangerSSLConfigCheck.isApplicable(request)); } @Test @@ -88,13 +112,13 @@ public class RangerSSLConfigCheckTest { final DesiredConfig desiredConfig = Mockito.mock(DesiredConfig.class); Mockito.when(desiredConfig.getTag()).thenReturn("tag"); - Map<String, DesiredConfig> configMap = new HashMap<String, DesiredConfig>(); + Map<String, DesiredConfig> configMap = new HashMap<>(); configMap.put("ranger-admin-site", desiredConfig); Mockito.when(cluster.getDesiredConfigs()).thenReturn(configMap); final Config config = Mockito.mock(Config.class); Mockito.when(cluster.getConfig(Mockito.anyString(), Mockito.anyString())).thenReturn(config); - final Map<String, String> properties = new HashMap<String, String>(); + final Map<String, String> properties = new HashMap<>(); Mockito.when(config.getProperties()).thenReturn(properties); properties.put("ranger.service.http.enabled","true"); http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/SecondaryNamenodeDeletedCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/SecondaryNamenodeDeletedCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/SecondaryNamenodeDeletedCheckTest.java index 3ce9757..5d0ca5f 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/SecondaryNamenodeDeletedCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/SecondaryNamenodeDeletedCheckTest.java @@ -21,22 +21,27 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.apache.ambari.server.ServiceNotFoundException; import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.PrereqCheckRequest; import org.apache.ambari.server.orm.dao.HostComponentStateDAO; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; +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.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import com.google.inject.Provider; @@ -44,14 +49,26 @@ import com.google.inject.Provider; * Unit tests for SecondaryNamenodeDeletedCheck * */ +@RunWith(MockitoJUnitRunner.class) public class SecondaryNamenodeDeletedCheckTest { private final Clusters clusters = Mockito.mock(Clusters.class); private final HostComponentStateDAO hostComponentStateDAO = Mockito.mock(HostComponentStateDAO.class); private final SecondaryNamenodeDeletedCheck secondaryNamenodeDeletedCheck = new SecondaryNamenodeDeletedCheck(); + @Mock + private ClusterVersionSummary m_clusterVersionSummary; + + @Mock + private VersionDefinitionXml m_vdfXml; + + @Mock + private RepositoryVersionEntity m_repositoryVersion; + + final Map<String, Service> m_services = new HashMap<>(); + @Before - public void setup() { + public void setup() throws Exception { secondaryNamenodeDeletedCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { @@ -69,31 +86,40 @@ public class SecondaryNamenodeDeletedCheckTest { secondaryNamenodeDeletedCheck.hostComponentStateDao = hostComponentStateDAO; Configuration config = Mockito.mock(Configuration.class); secondaryNamenodeDeletedCheck.config = config; + + 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()); } @Test public void testIsApplicable() throws Exception { final Cluster cluster = Mockito.mock(Cluster.class); - final Map<String, Service> services = new HashMap<>(); final Service service = Mockito.mock(Service.class); - services.put("HDFS", service); + m_services.put("HDFS", service); Mockito.when(cluster.getClusterId()).thenReturn(1L); - Mockito.when(cluster.getServices()).thenReturn(services); + Mockito.when(cluster.getServices()).thenReturn(m_services); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); - Assert.assertTrue(secondaryNamenodeDeletedCheck.isApplicable(new PrereqCheckRequest("cluster"))); + PrereqCheckRequest request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); + + Assert.assertTrue(secondaryNamenodeDeletedCheck.isApplicable(request)); + + request = new PrereqCheckRequest("cluster"); + request.setTargetRepositoryVersion(m_repositoryVersion); - PrereqCheckRequest req = new PrereqCheckRequest("cluster"); - req.addResult(CheckDescription.SERVICES_NAMENODE_HA, PrereqCheckStatus.FAIL); - Assert.assertFalse(secondaryNamenodeDeletedCheck.isApplicable(req)); + request.addResult(CheckDescription.SERVICES_NAMENODE_HA, PrereqCheckStatus.FAIL); + Assert.assertFalse(secondaryNamenodeDeletedCheck.isApplicable(request)); - req.addResult(CheckDescription.SERVICES_NAMENODE_HA, PrereqCheckStatus.PASS); - Assert.assertTrue(secondaryNamenodeDeletedCheck.isApplicable(req)); + request.addResult(CheckDescription.SERVICES_NAMENODE_HA, PrereqCheckStatus.PASS); + Assert.assertTrue(secondaryNamenodeDeletedCheck.isApplicable(request)); - services.remove("HDFS"); - Assert.assertFalse(secondaryNamenodeDeletedCheck.isApplicable(new PrereqCheckRequest("cluster"))); + m_services.remove("HDFS"); + Assert.assertFalse(secondaryNamenodeDeletedCheck.isApplicable(request)); } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/1f95e149/ambari-server/src/test/java/org/apache/ambari/server/checks/ServicePresenceCheckTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/ServicePresenceCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/ServicePresenceCheckTest.java index ae8b5c24..e27ff65 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/checks/ServicePresenceCheckTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/ServicePresenceCheckTest.java @@ -50,7 +50,7 @@ public class ServicePresenceCheckTest { * */ @Before - public void setup() { + public void setup() throws Exception { m_check.clustersProvider = new Provider<Clusters>() { @Override