Repository: ambari Updated Branches: refs/heads/branch-2.5 71cf8c252 -> 86c2cda7e
Revert "AMBARI-18601 : Analyze and Optimize Ambari Server Unit Tests - Group 4 (Commit 1) (avijayan)" This reverts commit 71cf8c252db91400f54cda45de8edf5bfaa00b55. Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/86c2cda7 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/86c2cda7 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/86c2cda7 Branch: refs/heads/branch-2.5 Commit: 86c2cda7e13e7db18beb464264a02f164cb14f21 Parents: 71cf8c2 Author: Aravindan Vijayan <avija...@hortonworks.com> Authored: Fri Oct 14 11:29:54 2016 -0700 Committer: Aravindan Vijayan <avija...@hortonworks.com> Committed: Fri Oct 14 11:29:54 2016 -0700 ---------------------------------------------------------------------- .../timeline/AMSPropertyProviderTest.java | 95 +++++++------ .../ambari/server/state/ConfigHelperTest.java | 139 +++++-------------- .../server/state/ServiceComponentTest.java | 116 +++++++--------- .../server/upgrade/UpgradeCatalog220Test.java | 20 ++- 4 files changed, 147 insertions(+), 223 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/86c2cda7/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java index 251327f..05fa880 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java @@ -40,7 +40,6 @@ import org.apache.ambari.server.controller.spi.TemporalInfo; import org.apache.ambari.server.controller.utilities.PropertyHelper; import org.apache.ambari.server.security.TestAuthenticationFactory; import org.apache.ambari.server.security.authorization.AuthorizationException; -import org.apache.ambari.server.security.authorization.internal.InternalAuthenticationToken; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.ComponentInfo; @@ -48,11 +47,8 @@ import org.apache.ambari.server.state.StackId; import org.apache.http.client.utils.URIBuilder; import org.easymock.EasyMock; import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; @@ -116,41 +112,75 @@ public class AMSPropertyProviderTest { @Before public void setupCache() { cacheEntryFactory = new TimelineMetricCacheEntryFactory(new Configuration()); - InternalAuthenticationToken authenticationToken = new InternalAuthenticationToken("admin"); - authenticationToken.setAuthenticated(true); - SecurityContextHolder.getContext().setAuthentication(authenticationToken); } - // SecurityContextHolder.getContext().setAuthentication(null); + @After + public void clearAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); + } @Test - public void testRbacForAMSPropertyProvider() throws Exception { - - SecurityContextHolder.getContext().setAuthentication(null); - //Cluster Administrator + public void testAMSPropertyProviderAsClusterAdministrator() throws Exception { + //Setup user with Role 'ClusterAdministrator'. SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L)); + SecurityContextHolder.getContext(); testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + testPopulateResourcesForHostComponentMetricsForMultipleHosts(); + testPopulateResourcesHostBatches(); + testPopulateResourcesForMultipleComponentsMetric(); + } + + @Test + public void testAMSPropertyProviderAsAdministrator() throws Exception { + //Setup user with Role 'Administrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin")); + + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + testPopulateResourcesForHostComponentMetricsForMultipleHosts(); + testPopulateResourcesHostBatches(); + testPopulateResourcesForMultipleComponentsMetric(); + } - SecurityContextHolder.getContext().setAuthentication(null); + @Test + public void testAMSPropertyProviderAsServiceAdministrator() throws Exception { //Setup user with 'ServiceAdministrator' SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L)); - SecurityContextHolder.getContext(); - testPopulateResourcesForSingleHostMetricPointInTime(); - SecurityContextHolder.getContext().setAuthentication(null); - // Setup user with 'ViewUser' - // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus - // can't retrieve the Metrics. SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L)); - SecurityContextHolder.getContext(); - try { - testPopulateResourcesForMultipleHostMetricscPointInTime(); - } catch (Exception e) { - Assert.assertTrue(e instanceof AuthorizationException); - } + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + testPopulateResourcesForHostComponentMetricsForMultipleHosts(); + testPopulateResourcesHostBatches(); + testPopulateResourcesForMultipleComponentsMetric(); } - @Ignore + @Test(expected = AuthorizationException.class) public void testAMSPropertyProviderAsViewUser() throws Exception { // Setup user with 'ViewUser' // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus @@ -172,7 +202,6 @@ public class AMSPropertyProviderTest { testPopulateResourcesForMultipleComponentsMetric(); } - @Test public void testPopulateResourcesForSingleHostMetric() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH); @@ -218,7 +247,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(111, val.length); } - @Test public void testPopulateResourcesForSingleHostMetricPointInTime() throws Exception { setUpCommonMocks(); @@ -265,7 +293,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(41.088, val, 0.001); } - @Test public void testPopulateResourcesForMultipleHostMetricscPointInTime() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_HOST_METRICS_FILE_PATH); @@ -321,7 +348,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(2.47025664E8, val2, 0.1); } - @Test public void testPopulateResourcesForMultipleHostMetrics() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_HOST_METRICS_FILE_PATH); @@ -385,7 +411,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(86, val.length); } - @Test public void testPopulateResourcesForRegexpMetrics() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_COMPONENT_REGEXP_METRICS_FILE_PATH); @@ -441,7 +466,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(238, val.length); } - @Test public void testPopulateResourcesForSingleComponentMetric() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_COMPONENT_METRICS_FILE_PATH); @@ -491,7 +515,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(238, val.length); } - @Test public void testPopulateResourcesForMultipleComponentsMetric() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_COMPONENT_METRICS_FILE_PATH); @@ -544,7 +567,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(2, allSpecs.size()); } - @Test public void testPopulateMetricsForEmbeddedHBase() throws Exception { AmbariManagementController ams = createNiceMock(AmbariManagementController.class); PowerMock.mockStatic(AmbariServer.class); @@ -618,7 +640,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(189, val.length); } - @Test public void testAggregateFunctionForComponentMetrics() throws Exception { AmbariManagementController ams = createNiceMock(AmbariManagementController.class); PowerMock.mockStatic(AmbariServer.class); @@ -692,7 +713,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(32, val.length); } - @Test public void testFilterOutOfBandMetricData() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH); @@ -763,7 +783,6 @@ public class AMSPropertyProviderTest { } } - @Test public void testPopulateResourcesForHostComponentHostMetrics() throws Exception { setUpCommonMocks(); TestStreamProviderForHostComponentHostMetricsTest streamProvider = @@ -869,7 +888,6 @@ public class AMSPropertyProviderTest { } } - @Test public void testPopulateResourcesHostBatches() throws Exception { setUpCommonMocks(); TestStreamProviderForHostComponentMultipleHostsMetricsTest streamProvider = @@ -921,7 +939,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(2, allSpecs.size()); } - @Test public void testPopulateResourcesForHostComponentMetricsForMultipleHosts() throws Exception { setUpCommonMocks(); TestStreamProviderForHostComponentMultipleHostsMetricsTest streamProvider = http://git-wip-us.apache.org/repos/asf/ambari/blob/86c2cda7/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java index fd6ce1e..37a48f0 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ConfigHelperTest.java @@ -63,9 +63,7 @@ import org.apache.ambari.server.state.host.HostFactory; import org.apache.ambari.server.state.stack.OsFamily; import org.apache.ambari.server.utils.SynchronousThreadPoolExecutor; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; @@ -86,17 +84,17 @@ import junit.framework.Assert; public class ConfigHelperTest { public static class RunWithInMemoryDefaultTestModule { private final static Logger LOG = LoggerFactory.getLogger(ConfigHelperTest.class); - private static Clusters clusters; - private static Injector injector; - private static String clusterName; - private static Cluster cluster; - private static ConfigGroupFactory configGroupFactory; - private static ConfigHelper configHelper; - private static AmbariManagementController managementController; - private static AmbariMetaInfo metaInfo; - - @BeforeClass - public static void setup() throws Exception { + private Clusters clusters; + private Injector injector; + private String clusterName; + private Cluster cluster; + private ConfigGroupFactory configGroupFactory; + private ConfigHelper configHelper; + private AmbariManagementController managementController; + private AmbariMetaInfo metaInfo; + + @Before + public void setup() throws Exception { // Set the authenticated user // TODO: remove this or replace the authenticated user to test authorization rules SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("admin")); @@ -185,57 +183,6 @@ public class ConfigHelperTest { add(clusterRequest3); }}, null); - - //Another version of core-site & global. - // core-site2 - ConfigurationRequest crr = new ConfigurationRequest(); - crr.setClusterName(clusterName); - crr.setType("core-site2"); - crr.setVersionTag("version1"); - crr.setProperties(new HashMap<String, String>() {{ - put("ipc.client.connect.max.retries", "30"); - put("fs.trash.interval", "30"); - }}); - crr.setPropertiesAttributes(new HashMap<String, Map<String, String>>() {{ - Map<String, String> attrs = new HashMap<String, String>(); - attrs.put("ipc.client.connect.max.retries", "1"); - attrs.put("fs.trash.interval", "2"); - put("attribute1", attrs); - }}); - - final ClusterRequest clusterRequestDup = - new ClusterRequest(cluster.getClusterId(), clusterName, - cluster.getDesiredStackVersion().getStackVersion(), null); - - clusterRequestDup.setDesiredConfig(Collections.singletonList(crr)); - managementController.updateClusters(new HashSet<ClusterRequest>() {{ - add(clusterRequestDup); - }}, null); - - // global - crr.setType("global2"); - crr.setVersionTag("version1"); - crr.setProperties(new HashMap<String, String>() {{ - put("dfs_namenode_name_dir", "/hadoop/hdfs/namenode"); - put("namenode_heapsize", "1024"); - }}); - crr.setPropertiesAttributes(new HashMap<String, Map<String, String>>() {{ - Map<String, String> attrs = new HashMap<String, String>(); - attrs.put("dfs_namenode_name_dir", "3"); - attrs.put("namenode_heapsize", "4"); - put("attribute2", attrs); - }}); - - final ClusterRequest clusterRequestGlobalDup = - new ClusterRequest(cluster.getClusterId(), clusterName, - cluster.getDesiredStackVersion().getStackVersion(), null); - - clusterRequestGlobalDup.setDesiredConfig(Collections.singletonList(crr)); - managementController.updateClusters(new HashSet<ClusterRequest>() {{ - add(clusterRequestGlobalDup); - }}, null); - - // oozie-site ConfigurationRequest cr4 = new ConfigurationRequest(); cr4.setClusterName(clusterName); @@ -255,30 +202,10 @@ public class ConfigHelperTest { managementController.updateClusters(new HashSet<ClusterRequest>() {{ add(clusterRequest4); }}, null); - - // ams-site - ConfigurationRequest cr5 = new ConfigurationRequest(); - cr5.setClusterName(clusterName); - cr5.setType("ams-site"); - cr5.setVersionTag("version1"); - cr5.setProperties(new HashMap<String, String>() {{ - put("timeline.service.operating.mode", "embedded"); - put("timeline.service.fifo.enabled", "false"); - }}); - cr4.setPropertiesAttributes(null); - - final ClusterRequest clusterRequest5 = - new ClusterRequest(cluster.getClusterId(), clusterName, - cluster.getDesiredStackVersion().getStackVersion(), null); - - clusterRequest5.setDesiredConfig(Collections.singletonList(cr5)); - managementController.updateClusters(new HashSet<ClusterRequest>() {{ - add(clusterRequest5); - }}, null); } - @AfterClass - public static void tearDown() { + @After + public void tearDown() { injector.getInstance(PersistService.class).stop(); // Clear the authenticated user @@ -419,7 +346,7 @@ public class ConfigHelperTest { properties2.put("namenode_heapsize", "1111"); config2.setProperties(properties2); - Long groupId = addConfigGroup("g2", "t1", new ArrayList<String>() {{ + Long groupId = addConfigGroup("g1", "t1", new ArrayList<String>() {{ add("h1"); }}, new ArrayList<Config>() {{ add(config1); @@ -446,7 +373,7 @@ public class ConfigHelperTest { @Test public void testEffectivePropertiesAttributesWithOverrides() throws Exception { - final Config config1 = new ConfigImpl("core-site2"); + final Config config1 = new ConfigImpl("core-site"); config1.setTag("version122"); Map<String, String> attributes = new HashMap<String, String>(); @@ -456,7 +383,7 @@ public class ConfigHelperTest { config1Attributes.put("attribute1", attributes); config1.setPropertiesAttributes(config1Attributes); - final Config config2 = new ConfigImpl("global2"); + final Config config2 = new ConfigImpl("global"); config2.setTag("version122"); attributes = new HashMap<String, String>(); attributes.put("namenode_heapsize", "z"); @@ -465,8 +392,8 @@ public class ConfigHelperTest { config2Attributes.put("attribute2", attributes); config2.setPropertiesAttributes(config2Attributes); - Long groupId = addConfigGroup("g3", "t1", new ArrayList<String>() {{ - add("h2"); + Long groupId = addConfigGroup("g1", "t1", new ArrayList<String>() {{ + add("h1"); }}, new ArrayList<Config>() {{ add(config1); add(config2); @@ -476,13 +403,13 @@ public class ConfigHelperTest { Map<String, Map<String, Map<String, String>>> effectiveAttributes = configHelper .getEffectiveConfigAttributes(cluster, - configHelper.getEffectiveDesiredTags(cluster, "h2")); + configHelper.getEffectiveDesiredTags(cluster, "h1")); Assert.assertNotNull(effectiveAttributes); - Assert.assertEquals(7, effectiveAttributes.size()); + Assert.assertEquals(4, effectiveAttributes.size()); - Assert.assertTrue(effectiveAttributes.containsKey("global2")); - Map<String, Map<String, String>> globalAttrs = effectiveAttributes.get("global2"); + Assert.assertTrue(effectiveAttributes.containsKey("global")); + Map<String, Map<String, String>> globalAttrs = effectiveAttributes.get("global"); Assert.assertEquals(1, globalAttrs.size()); Assert.assertTrue(globalAttrs.containsKey("attribute2")); Map<String, String> attribute2Occurances = globalAttrs.get("attribute2"); @@ -494,8 +421,8 @@ public class ConfigHelperTest { Assert.assertTrue(attribute2Occurances.containsKey("c")); Assert.assertEquals("q", attribute2Occurances.get("c")); - Assert.assertTrue(effectiveAttributes.containsKey("core-site2")); - Map<String, Map<String, String>> coreAttrs = effectiveAttributes.get("core-site2"); + Assert.assertTrue(effectiveAttributes.containsKey("core-site")); + Map<String, Map<String, String>> coreAttrs = effectiveAttributes.get("core-site"); Assert.assertEquals(1, coreAttrs.size()); Assert.assertTrue(coreAttrs.containsKey("attribute1")); Map<String, String> attribute1Occurances = coreAttrs.get("attribute1"); @@ -800,28 +727,28 @@ public class ConfigHelperTest { @Test public void testUpdateConfigTypeRemovals() throws Exception { - Config currentConfig = cluster.getDesiredConfigByType("ams-site"); + Config currentConfig = cluster.getDesiredConfigByType("oozie-site"); Map<String, String> properties = currentConfig.getProperties(); // Config tag before update Assert.assertEquals("version1", currentConfig.getTag()); // Properties before update - Assert.assertEquals("embedded", properties.get("timeline.service.operating.mode")); - Assert.assertEquals("false", properties.get("timeline.service.fifo.enabled")); + Assert.assertEquals("simple", properties.get("oozie.authentication.type")); + Assert.assertEquals("false", properties.get("oozie.service.HadoopAccessorService.kerberos.enabled")); List<String> removals = new ArrayList<String>(); - removals.add("timeline.service.operating.mode"); + removals.add("oozie.authentication.type"); - configHelper.updateConfigType(cluster, managementController, "ams-site", null, removals, "admin", "Test note"); + configHelper.updateConfigType(cluster, managementController, "oozie-site", null, removals, "admin", "Test note"); - Config updatedConfig = cluster.getDesiredConfigByType("ams-site"); + Config updatedConfig = cluster.getDesiredConfigByType("oozie-site"); // Config tag updated Assert.assertFalse("version1".equals(updatedConfig.getTag())); // Property removed properties = updatedConfig.getProperties(); - Assert.assertFalse(properties.containsKey("timeline.service.operating.mode")); + Assert.assertFalse(properties.containsKey("oozie.authentication.type")); // Property unchanged - Assert.assertTrue(properties.containsKey("timeline.service.fifo.enabled")); - Assert.assertEquals("false", properties.get("timeline.service.fifo.enabled")); + Assert.assertTrue(properties.containsKey("oozie.service.HadoopAccessorService.kerberos.enabled")); + Assert.assertEquals("false", properties.get("oozie.service.HadoopAccessorService.kerberos.enabled")); } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/86c2cda7/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java index 7cef8e3..0bf21f9 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java @@ -50,9 +50,7 @@ import org.apache.ambari.server.orm.entities.UpgradeEntity; import org.apache.ambari.server.state.stack.upgrade.Direction; import org.apache.ambari.server.state.stack.upgrade.UpgradeType; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import com.google.inject.Guice; @@ -63,24 +61,21 @@ import junit.framework.Assert; public class ServiceComponentTest { - private static Clusters clusters; - private static Cluster cluster; - private static Cluster cluster2; - private static Service service; - private static Service service2; - private static String clusterName; - private static String cluster2Name; - private static String serviceName; - private static Injector injector; - private static ServiceFactory serviceFactory; - private static ServiceComponentFactory serviceComponentFactory; - private static ServiceComponentHostFactory serviceComponentHostFactory; - private static AmbariMetaInfo metaInfo; - private static OrmTestHelper helper; - private static HostDAO hostDAO; - - @BeforeClass - public static void setup() throws Exception { + private Clusters clusters; + private Cluster cluster; + private Service service; + private String clusterName; + private String serviceName; + private Injector injector; + private ServiceFactory serviceFactory; + private ServiceComponentFactory serviceComponentFactory; + private ServiceComponentHostFactory serviceComponentHostFactory; + private AmbariMetaInfo metaInfo; + private OrmTestHelper helper; + private HostDAO hostDAO; + + @Before + public void setup() throws Exception { injector = Guice.createInjector(new InMemoryDefaultTestModule()); injector.getInstance(GuiceJpaInitializer.class); clusters = injector.getInstance(Clusters.class); @@ -94,40 +89,27 @@ public class ServiceComponentTest { metaInfo = injector.getInstance(AmbariMetaInfo.class); clusterName = "foo"; - cluster2Name = "bar"; serviceName = "HDFS"; StackId stackId = new StackId("HDP-0.1"); clusters.addCluster(clusterName, stackId); - clusters.addCluster(cluster2Name, stackId); - cluster = clusters.getCluster(clusterName); - cluster2 = clusters.getCluster(cluster2Name); + cluster.setDesiredStackVersion(stackId); - cluster2.setCurrentStackVersion(stackId); Assert.assertNotNull(cluster); - Assert.assertNotNull(cluster2); helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion()); cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin", RepositoryVersionState.INSTALLING); - cluster2.createClusterVersion(stackId, stackId.getStackVersion(), "admin", - RepositoryVersionState.INSTALLING); Service s = serviceFactory.createNew(cluster, serviceName); cluster.addService(s); s.persist(); service = cluster.getService(serviceName); Assert.assertNotNull(service); - - s = serviceFactory.createNew(cluster2, serviceName); - cluster2.addService(s); - s.persist(); - service2 = cluster2.getService(serviceName); - Assert.assertNotNull(service2); } - @AfterClass - public static void teardown() throws AmbariException { + @After + public void teardown() throws AmbariException { injector.getInstance(PersistService.class).stop(); } @@ -215,7 +197,7 @@ public class ServiceComponentTest { @Test public void testAddAndGetServiceComponentHosts() throws AmbariException { - String componentName = "DATANODE"; + String componentName = "NAMENODE"; ServiceComponent component = serviceComponentFactory.createNew(service, componentName); service.addServiceComponent(component); @@ -316,20 +298,20 @@ public class ServiceComponentTest { @Test public void testConvertToResponse() throws AmbariException { - String componentName = "SECONDARY_NAMENODE"; + String componentName = "NAMENODE"; ServiceComponent component = serviceComponentFactory.createNew(service, componentName); service.addServiceComponent(component); component.persist(); - addHostToCluster("h12", service.getCluster().getClusterName()); + addHostToCluster("h1", service.getCluster().getClusterName()); ServiceComponentHost sch = - serviceComponentHostFactory.createNew(component, "h12"); + serviceComponentHostFactory.createNew(component, "h1"); sch.setState(State.INSTALLED); Map<String, ServiceComponentHost> compHosts = new HashMap<String, ServiceComponentHost>(); - compHosts.put("h12", sch); + compHosts.put("h1", sch); component.addServiceComponentHosts(compHosts); Assert.assertEquals(1, component.getServiceComponentHosts().size()); sch.persist(); @@ -369,7 +351,7 @@ public class ServiceComponentTest { String componentName = "NAMENODE"; ServiceComponent component = serviceComponentFactory.createNew(service, componentName); - addHostToCluster("h11", service.getCluster().getClusterName()); + addHostToCluster("h1", service.getCluster().getClusterName()); ServiceComponentHost sch = serviceComponentHostFactory.createNew(component, "h1"); component.addServiceComponentHost(sch); @@ -394,7 +376,7 @@ public class ServiceComponentTest { ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance( ServiceComponentDesiredStateDAO.class); - String componentName = "HDFS_CLIENT"; + String componentName = "NAMENODE"; ServiceComponent component = serviceComponentFactory.createNew(service, componentName); service.addServiceComponent(component); component.persist(); @@ -416,7 +398,7 @@ public class ServiceComponentTest { Assert.assertNotNull(serviceComponentDesiredStateEntity); - UpgradeEntity upgradeEntity = createUpgradeEntity(cluster, "2.2.0.0", "2.2.0.1"); + UpgradeEntity upgradeEntity = createUpgradeEntity("2.2.0.0", "2.2.0.1"); ServiceComponentHistoryEntity history = new ServiceComponentHistoryEntity(); history.setFromStack(serviceComponentDesiredStateEntity.getDesiredStack()); history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack()); @@ -446,38 +428,38 @@ public class ServiceComponentTest { ServiceComponentDesiredStateDAO.class); String componentName = "NAMENODE"; - ServiceComponent component = serviceComponentFactory.createNew(service2, componentName); - service2.addServiceComponent(component); + ServiceComponent component = serviceComponentFactory.createNew(service, componentName); + service.addServiceComponent(component); component.persist(); - ServiceComponent sc = service2.getServiceComponent(componentName); + ServiceComponent sc = service.getServiceComponent(componentName); Assert.assertNotNull(sc); sc.setDesiredState(State.STARTED); Assert.assertEquals(State.STARTED, sc.getDesiredState()); ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName( - cluster2.getClusterId(), serviceName, componentName); + cluster.getClusterId(), serviceName, componentName); Assert.assertNotNull(serviceComponentDesiredStateEntity); Assert.assertTrue(sc.getServiceComponentHosts().isEmpty()); - addHostToCluster("h21", service2.getCluster().getClusterName()); - addHostToCluster("h22", service2.getCluster().getClusterName()); + addHostToCluster("h1", service.getCluster().getClusterName()); + addHostToCluster("h2", service.getCluster().getClusterName()); - HostEntity hostEntity1 = hostDAO.findByName("h21"); + HostEntity hostEntity1 = hostDAO.findByName("h1"); assertNotNull(hostEntity1); ServiceComponentHost sch1 = - serviceComponentHostFactory.createNew(sc, "h21"); + serviceComponentHostFactory.createNew(sc, "h1"); ServiceComponentHost sch2 = - serviceComponentHostFactory.createNew(sc, "h22"); + serviceComponentHostFactory.createNew(sc, "h2"); Map<String, ServiceComponentHost> compHosts = new HashMap<String, ServiceComponentHost>(); - compHosts.put("h21", sch1); - compHosts.put("h22", sch2); + compHosts.put("h1", sch1); + compHosts.put("h2", sch2); sc.addServiceComponentHosts(compHosts); sch1.setState(State.STARTED); @@ -497,7 +479,7 @@ public class ServiceComponentTest { // verify history is gone, too serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName( - cluster2.getClusterId(), serviceName, componentName); + cluster.getClusterId(), serviceName, componentName); Assert.assertNull(serviceComponentDesiredStateEntity); } @@ -513,12 +495,12 @@ public class ServiceComponentTest { ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance( ServiceComponentDesiredStateDAO.class); - String componentName = "DATANODE"; - ServiceComponent component = serviceComponentFactory.createNew(service2, componentName); - service2.addServiceComponent(component); + String componentName = "NAMENODE"; + ServiceComponent component = serviceComponentFactory.createNew(service, componentName); + service.addServiceComponent(component); component.persist(); - ServiceComponent sc = service2.getServiceComponent(componentName); + ServiceComponent sc = service.getServiceComponent(componentName); Assert.assertNotNull(sc); sc.setDesiredState(State.INSTALLED); @@ -531,12 +513,12 @@ public class ServiceComponentTest { Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId()); ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName( - cluster2.getClusterId(), serviceName, componentName); + cluster.getClusterId(), serviceName, componentName); Assert.assertNotNull(serviceComponentDesiredStateEntity); - UpgradeEntity upgradeEntity = createUpgradeEntity(cluster2, "2.2.0.0", "2.2.0.1"); + UpgradeEntity upgradeEntity = createUpgradeEntity("2.2.0.0", "2.2.0.1"); ServiceComponentHistoryEntity history = new ServiceComponentHistoryEntity(); history.setFromStack(serviceComponentDesiredStateEntity.getDesiredStack()); history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack()); @@ -549,7 +531,7 @@ public class ServiceComponentTest { serviceComponentDesiredStateEntity); serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName( - cluster2.getClusterId(), serviceName, componentName); + cluster.getClusterId(), serviceName, componentName); assertEquals(1, serviceComponentDesiredStateEntity.getHistory().size()); @@ -564,7 +546,7 @@ public class ServiceComponentTest { // verify history is gone, too serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName( - cluster2.getClusterId(), serviceName, componentName); + cluster.getClusterId(), serviceName, componentName); Assert.assertNull(serviceComponentDesiredStateEntity); @@ -695,10 +677,10 @@ public class ServiceComponentTest { * @param toVersion * @return */ - private UpgradeEntity createUpgradeEntity(Cluster cl, String fromVersion, String toVersion) { + private UpgradeEntity createUpgradeEntity(String fromVersion, String toVersion) { UpgradeDAO upgradeDao = injector.getInstance(UpgradeDAO.class); UpgradeEntity upgradeEntity = new UpgradeEntity(); - upgradeEntity.setClusterId(cl.getClusterId()); + upgradeEntity.setClusterId(cluster.getClusterId()); upgradeEntity.setDirection(Direction.UPGRADE); upgradeEntity.setFromVersion(fromVersion); upgradeEntity.setToVersion(toVersion); @@ -707,7 +689,7 @@ public class ServiceComponentTest { upgradeEntity.setRequestId(1L); upgradeDao.create(upgradeEntity); - List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cl.getClusterId()); + List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId()); assertEquals(1, upgrades.size()); return upgradeEntity; } http://git-wip-us.apache.org/repos/asf/ambari/blob/86c2cda7/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java index 2b3764b..7c1605d 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java @@ -77,10 +77,8 @@ import org.easymock.EasyMock; import org.easymock.EasyMockSupport; import org.easymock.IMocksControl; import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import javax.persistence.EntityManager; @@ -116,11 +114,11 @@ import static org.junit.Assert.assertTrue; * {@link org.apache.ambari.server.upgrade.UpgradeCatalog220} unit tests. */ public class UpgradeCatalog220Test { - private static Injector injector; - private static Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class); - private static EntityManager entityManager = createNiceMock(EntityManager.class); - private static UpgradeCatalogHelper upgradeCatalogHelper; - private static StackEntity desiredStackEntity; + private Injector injector; + private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class); + private EntityManager entityManager = createNiceMock(EntityManager.class); + private UpgradeCatalogHelper upgradeCatalogHelper; + private StackEntity desiredStackEntity; private AmbariManagementController amc = createNiceMock(AmbariManagementController.class); private AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); private StackDAO stackDAO = createNiceMock(StackDAO.class); @@ -131,8 +129,8 @@ public class UpgradeCatalog220Test { private IMocksControl mocksControl = EasyMock.createControl(); - @BeforeClass - public static void init() { + @Before + public void init() { reset(entityManagerProvider); expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes(); replay(entityManagerProvider); @@ -147,8 +145,8 @@ public class UpgradeCatalog220Test { desiredStackEntity = stackDAO.find("HDP", "2.2.0"); } - @AfterClass - public static void tearDown() { + @After + public void tearDown() { injector.getInstance(PersistService.class).stop(); }