You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by av...@apache.org on 2016/10/14 17:46:00 UTC

ambari git commit: AMBARI-18601 : Analyze and Optimize Ambari Server Unit Tests - Group 4 (Commit 1) (avijayan)

Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 cb9db0aef -> 71cf8c252


AMBARI-18601 : Analyze and Optimize Ambari Server Unit Tests - Group 4 (Commit 1) (avijayan)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/71cf8c25
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/71cf8c25
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/71cf8c25

Branch: refs/heads/branch-2.5
Commit: 71cf8c252db91400f54cda45de8edf5bfaa00b55
Parents: cb9db0a
Author: Aravindan Vijayan <av...@hortonworks.com>
Authored: Fri Oct 14 10:45:50 2016 -0700
Committer: Aravindan Vijayan <av...@hortonworks.com>
Committed: Fri Oct 14 10:45:50 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, 223 insertions(+), 147 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/71cf8c25/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 05fa880..251327f 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,6 +40,7 @@ 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;
@@ -47,8 +48,11 @@ 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;
@@ -112,75 +116,41 @@ public class AMSPropertyProviderTest {
   @Before
   public void setupCache() {
     cacheEntryFactory = new TimelineMetricCacheEntryFactory(new Configuration());
+    InternalAuthenticationToken authenticationToken = new InternalAuthenticationToken("admin");
+    authenticationToken.setAuthenticated(true);
+    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
   }
 
-  @After
-  public void clearAuthentication() {
-    SecurityContextHolder.getContext().setAuthentication(null);
-  }
+  //    SecurityContextHolder.getContext().setAuthentication(null);
 
   @Test
-  public void testAMSPropertyProviderAsClusterAdministrator() throws Exception {
-    //Setup user with Role 'ClusterAdministrator'.
-    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L));
+  public void testRbacForAMSPropertyProvider() throws Exception {
 
+    SecurityContextHolder.getContext().setAuthentication(null);
+    //Cluster Administrator
+    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();
-  }
 
-  @Test
-  public void testAMSPropertyProviderAsServiceAdministrator() throws Exception {
+    SecurityContextHolder.getContext().setAuthentication(null);
     //Setup user with 'ServiceAdministrator'
     SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L));
-
-    testPopulateResourcesForSingleHostMetric();
+    SecurityContextHolder.getContext();
     testPopulateResourcesForSingleHostMetricPointInTime();
-    testPopulateResourcesForMultipleHostMetricscPointInTime();
-    testPopulateResourcesForMultipleHostMetrics();
-    testPopulateResourcesForRegexpMetrics();
-    testPopulateResourcesForSingleComponentMetric();
-    testPopulateMetricsForEmbeddedHBase();
-    testAggregateFunctionForComponentMetrics();
-    testFilterOutOfBandMetricData();
-    testPopulateResourcesForHostComponentHostMetrics();
-    testPopulateResourcesForHostComponentMetricsForMultipleHosts();
-    testPopulateResourcesHostBatches();
-    testPopulateResourcesForMultipleComponentsMetric();
+
+    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);
+    }
   }
 
-  @Test(expected = AuthorizationException.class)
+  @Ignore
   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
@@ -202,6 +172,7 @@ public class AMSPropertyProviderTest {
     testPopulateResourcesForMultipleComponentsMetric();
   }
 
+  @Test
   public void testPopulateResourcesForSingleHostMetric() throws Exception {
     setUpCommonMocks();
     TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH);
@@ -247,6 +218,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(111, val.length);
   }
 
+  @Test
   public void testPopulateResourcesForSingleHostMetricPointInTime() throws Exception {
     setUpCommonMocks();
 
@@ -293,6 +265,7 @@ 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);
@@ -348,6 +321,7 @@ 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);
@@ -411,6 +385,7 @@ 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);
@@ -466,6 +441,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(238, val.length);
   }
 
+  @Test
   public void testPopulateResourcesForSingleComponentMetric() throws Exception {
     setUpCommonMocks();
     TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_COMPONENT_METRICS_FILE_PATH);
@@ -515,6 +491,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(238, val.length);
   }
 
+  @Test
   public void testPopulateResourcesForMultipleComponentsMetric() throws Exception {
     setUpCommonMocks();
     TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_COMPONENT_METRICS_FILE_PATH);
@@ -567,6 +544,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(2, allSpecs.size());
   }
 
+  @Test
   public void testPopulateMetricsForEmbeddedHBase() throws Exception {
     AmbariManagementController ams = createNiceMock(AmbariManagementController.class);
     PowerMock.mockStatic(AmbariServer.class);
@@ -640,6 +618,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(189, val.length);
   }
 
+  @Test
   public void testAggregateFunctionForComponentMetrics() throws Exception {
     AmbariManagementController ams = createNiceMock(AmbariManagementController.class);
     PowerMock.mockStatic(AmbariServer.class);
@@ -713,6 +692,7 @@ public class AMSPropertyProviderTest {
     Assert.assertEquals(32, val.length);
   }
 
+  @Test
   public void testFilterOutOfBandMetricData() throws Exception {
     setUpCommonMocks();
     TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH);
@@ -783,6 +763,7 @@ public class AMSPropertyProviderTest {
     }
   }
 
+  @Test
   public void testPopulateResourcesForHostComponentHostMetrics() throws Exception {
     setUpCommonMocks();
     TestStreamProviderForHostComponentHostMetricsTest streamProvider =
@@ -888,6 +869,7 @@ public class AMSPropertyProviderTest {
     }
   }
 
+  @Test
   public void testPopulateResourcesHostBatches() throws Exception {
     setUpCommonMocks();
     TestStreamProviderForHostComponentMultipleHostsMetricsTest streamProvider =
@@ -939,6 +921,7 @@ 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/71cf8c25/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 37a48f0..fd6ce1e 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,7 +63,9 @@ 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;
@@ -84,17 +86,17 @@ import junit.framework.Assert;
 public class ConfigHelperTest {
   public static class RunWithInMemoryDefaultTestModule {
     private final static Logger LOG = LoggerFactory.getLogger(ConfigHelperTest.class);
-    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 {
+    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 {
       // Set the authenticated user
       // TODO: remove this or replace the authenticated user to test authorization rules
       SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("admin"));
@@ -183,6 +185,57 @@ 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);
@@ -202,10 +255,30 @@ 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);
     }
 
-    @After
-    public void tearDown() {
+    @AfterClass
+    public static void tearDown() {
       injector.getInstance(PersistService.class).stop();
 
       // Clear the authenticated user
@@ -346,7 +419,7 @@ public class ConfigHelperTest {
       properties2.put("namenode_heapsize", "1111");
       config2.setProperties(properties2);
 
-      Long groupId = addConfigGroup("g1", "t1", new ArrayList<String>() {{
+      Long groupId = addConfigGroup("g2", "t1", new ArrayList<String>() {{
         add("h1");
       }}, new ArrayList<Config>() {{
         add(config1);
@@ -373,7 +446,7 @@ public class ConfigHelperTest {
 
     @Test
     public void testEffectivePropertiesAttributesWithOverrides() throws Exception {
-      final Config config1 = new ConfigImpl("core-site");
+      final Config config1 = new ConfigImpl("core-site2");
       config1.setTag("version122");
 
       Map<String, String> attributes = new HashMap<String, String>();
@@ -383,7 +456,7 @@ public class ConfigHelperTest {
       config1Attributes.put("attribute1", attributes);
       config1.setPropertiesAttributes(config1Attributes);
 
-      final Config config2 = new ConfigImpl("global");
+      final Config config2 = new ConfigImpl("global2");
       config2.setTag("version122");
       attributes = new HashMap<String, String>();
       attributes.put("namenode_heapsize", "z");
@@ -392,8 +465,8 @@ public class ConfigHelperTest {
       config2Attributes.put("attribute2", attributes);
       config2.setPropertiesAttributes(config2Attributes);
 
-      Long groupId = addConfigGroup("g1", "t1", new ArrayList<String>() {{
-        add("h1");
+      Long groupId = addConfigGroup("g3", "t1", new ArrayList<String>() {{
+        add("h2");
       }}, new ArrayList<Config>() {{
         add(config1);
         add(config2);
@@ -403,13 +476,13 @@ public class ConfigHelperTest {
 
       Map<String, Map<String, Map<String, String>>> effectiveAttributes = configHelper
           .getEffectiveConfigAttributes(cluster,
-              configHelper.getEffectiveDesiredTags(cluster, "h1"));
+              configHelper.getEffectiveDesiredTags(cluster, "h2"));
 
       Assert.assertNotNull(effectiveAttributes);
-      Assert.assertEquals(4, effectiveAttributes.size());
+      Assert.assertEquals(7, effectiveAttributes.size());
 
-      Assert.assertTrue(effectiveAttributes.containsKey("global"));
-      Map<String, Map<String, String>> globalAttrs = effectiveAttributes.get("global");
+      Assert.assertTrue(effectiveAttributes.containsKey("global2"));
+      Map<String, Map<String, String>> globalAttrs = effectiveAttributes.get("global2");
       Assert.assertEquals(1, globalAttrs.size());
       Assert.assertTrue(globalAttrs.containsKey("attribute2"));
       Map<String, String> attribute2Occurances = globalAttrs.get("attribute2");
@@ -421,8 +494,8 @@ public class ConfigHelperTest {
       Assert.assertTrue(attribute2Occurances.containsKey("c"));
       Assert.assertEquals("q", attribute2Occurances.get("c"));
 
-      Assert.assertTrue(effectiveAttributes.containsKey("core-site"));
-      Map<String, Map<String, String>> coreAttrs = effectiveAttributes.get("core-site");
+      Assert.assertTrue(effectiveAttributes.containsKey("core-site2"));
+      Map<String, Map<String, String>> coreAttrs = effectiveAttributes.get("core-site2");
       Assert.assertEquals(1, coreAttrs.size());
       Assert.assertTrue(coreAttrs.containsKey("attribute1"));
       Map<String, String> attribute1Occurances = coreAttrs.get("attribute1");
@@ -727,28 +800,28 @@ public class ConfigHelperTest {
 
     @Test
     public void testUpdateConfigTypeRemovals() throws Exception {
-      Config currentConfig = cluster.getDesiredConfigByType("oozie-site");
+      Config currentConfig = cluster.getDesiredConfigByType("ams-site");
       Map<String, String> properties = currentConfig.getProperties();
       // Config tag before update
       Assert.assertEquals("version1", currentConfig.getTag());
       // Properties before update
-      Assert.assertEquals("simple", properties.get("oozie.authentication.type"));
-      Assert.assertEquals("false", properties.get("oozie.service.HadoopAccessorService.kerberos.enabled"));
+      Assert.assertEquals("embedded", properties.get("timeline.service.operating.mode"));
+      Assert.assertEquals("false", properties.get("timeline.service.fifo.enabled"));
 
       List<String> removals = new ArrayList<String>();
-      removals.add("oozie.authentication.type");
+      removals.add("timeline.service.operating.mode");
 
-      configHelper.updateConfigType(cluster, managementController, "oozie-site", null, removals, "admin", "Test note");
+      configHelper.updateConfigType(cluster, managementController, "ams-site", null, removals, "admin", "Test note");
 
-      Config updatedConfig = cluster.getDesiredConfigByType("oozie-site");
+      Config updatedConfig = cluster.getDesiredConfigByType("ams-site");
       // Config tag updated
       Assert.assertFalse("version1".equals(updatedConfig.getTag()));
       // Property removed
       properties = updatedConfig.getProperties();
-      Assert.assertFalse(properties.containsKey("oozie.authentication.type"));
+      Assert.assertFalse(properties.containsKey("timeline.service.operating.mode"));
       // Property unchanged
-      Assert.assertTrue(properties.containsKey("oozie.service.HadoopAccessorService.kerberos.enabled"));
-      Assert.assertEquals("false", properties.get("oozie.service.HadoopAccessorService.kerberos.enabled"));
+      Assert.assertTrue(properties.containsKey("timeline.service.fifo.enabled"));
+      Assert.assertEquals("false", properties.get("timeline.service.fifo.enabled"));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/71cf8c25/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 0bf21f9..7cef8e3 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,7 +50,9 @@ 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;
@@ -61,21 +63,24 @@ import junit.framework.Assert;
 
 public class ServiceComponentTest {
 
-  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 {
+  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 {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     clusters = injector.getInstance(Clusters.class);
@@ -89,27 +94,40 @@ 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);
-    cluster = clusters.getCluster(clusterName);
+    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);
   }
 
-  @After
-  public void teardown() throws AmbariException {
+  @AfterClass
+  public static void teardown() throws AmbariException {
     injector.getInstance(PersistService.class).stop();
   }
 
@@ -197,7 +215,7 @@ public class ServiceComponentTest {
 
   @Test
   public void testAddAndGetServiceComponentHosts() throws AmbariException {
-    String componentName = "NAMENODE";
+    String componentName = "DATANODE";
     ServiceComponent component = serviceComponentFactory.createNew(service,
         componentName);
     service.addServiceComponent(component);
@@ -298,20 +316,20 @@ public class ServiceComponentTest {
 
   @Test
   public void testConvertToResponse() throws AmbariException {
-    String componentName = "NAMENODE";
+    String componentName = "SECONDARY_NAMENODE";
     ServiceComponent component = serviceComponentFactory.createNew(service,
         componentName);
     service.addServiceComponent(component);
     component.persist();
 
-    addHostToCluster("h1", service.getCluster().getClusterName());
+    addHostToCluster("h12", service.getCluster().getClusterName());
     ServiceComponentHost sch =
-      serviceComponentHostFactory.createNew(component, "h1");
+      serviceComponentHostFactory.createNew(component, "h12");
     sch.setState(State.INSTALLED);
 
     Map<String, ServiceComponentHost> compHosts =
       new HashMap<String, ServiceComponentHost>();
-    compHosts.put("h1", sch);
+    compHosts.put("h12", sch);
     component.addServiceComponentHosts(compHosts);
     Assert.assertEquals(1, component.getServiceComponentHosts().size());
     sch.persist();
@@ -351,7 +369,7 @@ public class ServiceComponentTest {
     String componentName = "NAMENODE";
     ServiceComponent component = serviceComponentFactory.createNew(service,
                                                                    componentName);
-    addHostToCluster("h1", service.getCluster().getClusterName());
+    addHostToCluster("h11", service.getCluster().getClusterName());
     ServiceComponentHost sch = serviceComponentHostFactory.createNew(component, "h1");
     component.addServiceComponentHost(sch);
 
@@ -376,7 +394,7 @@ public class ServiceComponentTest {
     ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance(
         ServiceComponentDesiredStateDAO.class);
 
-    String componentName = "NAMENODE";
+    String componentName = "HDFS_CLIENT";
     ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
     service.addServiceComponent(component);
     component.persist();
@@ -398,7 +416,7 @@ public class ServiceComponentTest {
 
     Assert.assertNotNull(serviceComponentDesiredStateEntity);
 
-    UpgradeEntity upgradeEntity = createUpgradeEntity("2.2.0.0", "2.2.0.1");
+    UpgradeEntity upgradeEntity = createUpgradeEntity(cluster, "2.2.0.0", "2.2.0.1");
     ServiceComponentHistoryEntity history = new ServiceComponentHistoryEntity();
     history.setFromStack(serviceComponentDesiredStateEntity.getDesiredStack());
     history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack());
@@ -428,38 +446,38 @@ public class ServiceComponentTest {
         ServiceComponentDesiredStateDAO.class);
 
     String componentName = "NAMENODE";
-    ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
-    service.addServiceComponent(component);
+    ServiceComponent component = serviceComponentFactory.createNew(service2, componentName);
+    service2.addServiceComponent(component);
     component.persist();
 
-    ServiceComponent sc = service.getServiceComponent(componentName);
+    ServiceComponent sc = service2.getServiceComponent(componentName);
     Assert.assertNotNull(sc);
 
     sc.setDesiredState(State.STARTED);
     Assert.assertEquals(State.STARTED, sc.getDesiredState());
 
     ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        cluster.getClusterId(), serviceName, componentName);
+        cluster2.getClusterId(), serviceName, componentName);
 
     Assert.assertNotNull(serviceComponentDesiredStateEntity);
 
     Assert.assertTrue(sc.getServiceComponentHosts().isEmpty());
 
-    addHostToCluster("h1", service.getCluster().getClusterName());
-    addHostToCluster("h2", service.getCluster().getClusterName());
+    addHostToCluster("h21", service2.getCluster().getClusterName());
+    addHostToCluster("h22", service2.getCluster().getClusterName());
 
-    HostEntity hostEntity1 = hostDAO.findByName("h1");
+    HostEntity hostEntity1 = hostDAO.findByName("h21");
     assertNotNull(hostEntity1);
 
     ServiceComponentHost sch1 =
-        serviceComponentHostFactory.createNew(sc, "h1");
+        serviceComponentHostFactory.createNew(sc, "h21");
     ServiceComponentHost sch2 =
-        serviceComponentHostFactory.createNew(sc, "h2");
+        serviceComponentHostFactory.createNew(sc, "h22");
 
     Map<String, ServiceComponentHost> compHosts =
         new HashMap<String, ServiceComponentHost>();
-    compHosts.put("h1", sch1);
-    compHosts.put("h2", sch2);
+    compHosts.put("h21", sch1);
+    compHosts.put("h22", sch2);
     sc.addServiceComponentHosts(compHosts);
 
     sch1.setState(State.STARTED);
@@ -479,7 +497,7 @@ public class ServiceComponentTest {
 
     // verify history is gone, too
     serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        cluster.getClusterId(), serviceName, componentName);
+        cluster2.getClusterId(), serviceName, componentName);
 
     Assert.assertNull(serviceComponentDesiredStateEntity);
  }
@@ -495,12 +513,12 @@ public class ServiceComponentTest {
     ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance(
         ServiceComponentDesiredStateDAO.class);
 
-    String componentName = "NAMENODE";
-    ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
-    service.addServiceComponent(component);
+    String componentName = "DATANODE";
+    ServiceComponent component = serviceComponentFactory.createNew(service2, componentName);
+    service2.addServiceComponent(component);
     component.persist();
 
-    ServiceComponent sc = service.getServiceComponent(componentName);
+    ServiceComponent sc = service2.getServiceComponent(componentName);
     Assert.assertNotNull(sc);
 
     sc.setDesiredState(State.INSTALLED);
@@ -513,12 +531,12 @@ public class ServiceComponentTest {
     Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId());
 
     ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        cluster.getClusterId(), serviceName, componentName);
+        cluster2.getClusterId(), serviceName, componentName);
 
 
     Assert.assertNotNull(serviceComponentDesiredStateEntity);
 
-    UpgradeEntity upgradeEntity = createUpgradeEntity("2.2.0.0", "2.2.0.1");
+    UpgradeEntity upgradeEntity = createUpgradeEntity(cluster2, "2.2.0.0", "2.2.0.1");
     ServiceComponentHistoryEntity history = new ServiceComponentHistoryEntity();
     history.setFromStack(serviceComponentDesiredStateEntity.getDesiredStack());
     history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack());
@@ -531,7 +549,7 @@ public class ServiceComponentTest {
         serviceComponentDesiredStateEntity);
 
     serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        cluster.getClusterId(), serviceName, componentName);
+        cluster2.getClusterId(), serviceName, componentName);
 
     assertEquals(1, serviceComponentDesiredStateEntity.getHistory().size());
 
@@ -546,7 +564,7 @@ public class ServiceComponentTest {
 
     // verify history is gone, too
     serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        cluster.getClusterId(), serviceName, componentName);
+        cluster2.getClusterId(), serviceName, componentName);
 
     Assert.assertNull(serviceComponentDesiredStateEntity);
 
@@ -677,10 +695,10 @@ public class ServiceComponentTest {
    * @param toVersion
    * @return
    */
-  private UpgradeEntity createUpgradeEntity(String fromVersion, String toVersion) {
+  private UpgradeEntity createUpgradeEntity(Cluster cl, String fromVersion, String toVersion) {
     UpgradeDAO upgradeDao = injector.getInstance(UpgradeDAO.class);
     UpgradeEntity upgradeEntity = new UpgradeEntity();
-    upgradeEntity.setClusterId(cluster.getClusterId());
+    upgradeEntity.setClusterId(cl.getClusterId());
     upgradeEntity.setDirection(Direction.UPGRADE);
     upgradeEntity.setFromVersion(fromVersion);
     upgradeEntity.setToVersion(toVersion);
@@ -689,7 +707,7 @@ public class ServiceComponentTest {
     upgradeEntity.setRequestId(1L);
 
     upgradeDao.create(upgradeEntity);
-    List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
+    List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cl.getClusterId());
     assertEquals(1, upgrades.size());
     return upgradeEntity;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/71cf8c25/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 7c1605d..2b3764b 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,8 +77,10 @@ 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;
@@ -114,11 +116,11 @@ import static org.junit.Assert.assertTrue;
  * {@link org.apache.ambari.server.upgrade.UpgradeCatalog220} unit tests.
  */
 public class UpgradeCatalog220Test {
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-  private StackEntity desiredStackEntity;
+  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 AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
   private AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
   private StackDAO stackDAO = createNiceMock(StackDAO.class);
@@ -129,8 +131,8 @@ public class UpgradeCatalog220Test {
 
   private IMocksControl mocksControl = EasyMock.createControl();
 
-  @Before
-  public void init() {
+  @BeforeClass
+  public static void init() {
     reset(entityManagerProvider);
     expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
     replay(entityManagerProvider);
@@ -145,8 +147,8 @@ public class UpgradeCatalog220Test {
     desiredStackEntity = stackDAO.find("HDP", "2.2.0");
   }
 
-  @After
-  public void tearDown() {
+  @AfterClass
+  public static void tearDown() {
     injector.getInstance(PersistService.class).stop();
   }