http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java index 7cb9a74..1ad8cef 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -27,12 +27,13 @@ import java.util.Map; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.H2DatabaseCleaner; -import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.controller.ServiceResponse; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.OrmTestHelper; import org.apache.ambari.server.orm.dao.ClusterServiceDAO; import org.apache.ambari.server.orm.entities.ClusterServiceEntity; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -51,7 +52,12 @@ public class ServiceTest { private ServiceFactory serviceFactory; private ServiceComponentFactory serviceComponentFactory; private ServiceComponentHostFactory serviceComponentHostFactory; - private AmbariMetaInfo metaInfo; + private OrmTestHelper ormTestHelper; + + private final String STACK_VERSION = "0.1"; + private final String REPO_VERSION = "0.1-1234"; + private final StackId STACK_ID = new StackId("HDP", STACK_VERSION); + private RepositoryVersionEntity repositoryVersion; @Before public void setup() throws Exception { @@ -59,13 +65,14 @@ public class ServiceTest { injector.getInstance(GuiceJpaInitializer.class); clusters = injector.getInstance(Clusters.class); serviceFactory = injector.getInstance(ServiceFactory.class); - serviceComponentFactory = injector.getInstance( - ServiceComponentFactory.class); - serviceComponentHostFactory = injector.getInstance( - ServiceComponentHostFactory.class); - metaInfo = injector.getInstance(AmbariMetaInfo.class); + serviceComponentFactory = injector.getInstance(ServiceComponentFactory.class); + serviceComponentHostFactory = injector.getInstance(ServiceComponentHostFactory.class); + + ormTestHelper = injector.getInstance(OrmTestHelper.class); + repositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(STACK_ID, REPO_VERSION); + clusterName = "foo"; - clusters.addCluster(clusterName, new StackId("HDP-0.1")); + clusters.addCluster(clusterName, STACK_ID); cluster = clusters.getCluster(clusterName); Assert.assertNotNull(cluster); } @@ -77,7 +84,7 @@ public class ServiceTest { @Test public void testCanBeRemoved() throws Exception{ - Service service = cluster.addService("HDFS"); + Service service = cluster.addService("HDFS", repositoryVersion); for (State state : State.values()) { service.setDesiredState(state); @@ -119,15 +126,20 @@ public class ServiceTest { @Test public void testGetAndSetServiceInfo() throws AmbariException { String serviceName = "HDFS"; - Service s = serviceFactory.createNew(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(s); Service service = cluster.getService(serviceName); Assert.assertNotNull(service); - service.setDesiredStackVersion(new StackId("HDP-1.2.0")); - Assert.assertEquals("HDP-1.2.0", - service.getDesiredStackVersion().getStackId()); + StackId desiredStackId = new StackId("HDP-1.2.0"); + String desiredVersion = "1.2.0-1234"; + + RepositoryVersionEntity desiredRepositoryVersion = ormTestHelper.getOrCreateRepositoryVersion( + desiredStackId, desiredVersion); + + service.setDesiredRepositoryVersion(desiredRepositoryVersion); + Assert.assertEquals(desiredStackId, service.getDesiredStackId()); service.setDesiredState(State.INSTALLING); Assert.assertEquals(State.INSTALLING, service.getDesiredState()); @@ -140,7 +152,7 @@ public class ServiceTest { @Test public void testAddGetDeleteServiceComponents() throws AmbariException { String serviceName = "HDFS"; - Service s = serviceFactory.createNew(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(s); Service service = cluster.getService(serviceName); @@ -154,7 +166,7 @@ public class ServiceTest { Assert.assertEquals(State.INIT, service.getDesiredState()); Assert.assertEquals(SecurityState.UNSECURED, service.getSecurityState()); Assert.assertFalse( - service.getDesiredStackVersion().getStackId().isEmpty()); + service.getDesiredStackId().getStackId().isEmpty()); Assert.assertTrue(s.getServiceComponents().isEmpty()); @@ -166,7 +178,7 @@ public class ServiceTest { serviceComponentFactory.createNew(s, "DATANODE2"); Map<String, ServiceComponent> comps = new - HashMap<String, ServiceComponent>(); + HashMap<>(); comps.put(sc1.getName(), sc1); comps.put(sc2.getName(), sc2); @@ -223,30 +235,30 @@ public class ServiceTest { @Test public void testConvertToResponse() throws AmbariException { String serviceName = "HDFS"; - Service s = serviceFactory.createNew(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(s); Service service = cluster.getService(serviceName); Assert.assertNotNull(service); ServiceResponse r = s.convertToResponse(); Assert.assertEquals(s.getName(), r.getServiceName()); - Assert.assertEquals(s.getCluster().getClusterName(), - r.getClusterName()); - Assert.assertEquals(s.getDesiredStackVersion().getStackId(), - r.getDesiredStackVersion()); - Assert.assertEquals(s.getDesiredState().toString(), - r.getDesiredState()); - - service.setDesiredStackVersion(new StackId("HDP-1.2.0")); + Assert.assertEquals(s.getCluster().getClusterName(), r.getClusterName()); + Assert.assertEquals(s.getDesiredStackId().getStackId(), r.getDesiredStackId()); + Assert.assertEquals(s.getDesiredState().toString(), r.getDesiredState()); + + StackId desiredStackId = new StackId("HDP-1.2.0"); + String desiredVersion = "1.2.0-1234"; + + RepositoryVersionEntity desiredRepositoryVersion = ormTestHelper.getOrCreateRepositoryVersion( + desiredStackId, desiredVersion); + + service.setDesiredRepositoryVersion(desiredRepositoryVersion); service.setDesiredState(State.INSTALLING); r = s.convertToResponse(); Assert.assertEquals(s.getName(), r.getServiceName()); - Assert.assertEquals(s.getCluster().getClusterName(), - r.getClusterName()); - Assert.assertEquals(s.getDesiredStackVersion().getStackId(), - r.getDesiredStackVersion()); - Assert.assertEquals(s.getDesiredState().toString(), - r.getDesiredState()); + Assert.assertEquals(s.getCluster().getClusterName(), r.getClusterName()); + Assert.assertEquals(s.getDesiredStackId().getStackId(), r.getDesiredStackId()); + Assert.assertEquals(s.getDesiredState().toString(), r.getDesiredState()); // FIXME add checks for configs StringBuilder sb = new StringBuilder(); @@ -259,7 +271,7 @@ public class ServiceTest { @Test public void testServiceMaintenance() throws Exception { String serviceName = "HDFS"; - Service s = serviceFactory.createNew(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(s); Service service = cluster.getService(serviceName); @@ -282,7 +294,7 @@ public class ServiceTest { @Test public void testSecurityState() throws Exception { String serviceName = "HDFS"; - Service s = serviceFactory.createNew(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(s); Service service = cluster.getService(serviceName); @@ -326,11 +338,11 @@ public class ServiceTest { h.setIPv4(hostname + "ipv4"); h.setIPv6(hostname + "ipv6"); - Map<String, String> hostAttributes = new HashMap<String, String>(); + Map<String, String> hostAttributes = new HashMap<>(); hostAttributes.put("os_family", "redhat"); hostAttributes.put("os_release_version", "6.3"); h.setHostAttributes(hostAttributes); clusters.mapHostToCluster(hostname, clusterName); } -} +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java index e50dba5..63baba7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertEventPublisherTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -33,6 +33,7 @@ import org.apache.ambari.server.orm.entities.AlertCurrentEntity; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.AlertGroupEntity; import org.apache.ambari.server.orm.entities.AlertHistoryEntity; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.AlertState; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; @@ -76,6 +77,9 @@ public class AlertEventPublisherTest { private OrmTestHelper ormHelper; private AggregateDefinitionMapping aggregateMapping; + private final String STACK_VERSION = "2.0.6"; + private final String REPO_VERSION = "2.0.6-1234"; + /** * */ @@ -95,7 +99,10 @@ public class AlertEventPublisherTest { aggregateMapping = injector.getInstance(AggregateDefinitionMapping.class); clusterName = "foo"; - clusters.addCluster(clusterName, new StackId("HDP", "2.0.6")); + StackId stackId = new StackId("HDP", STACK_VERSION); + ormHelper.createStack(stackId); + + clusters.addCluster(clusterName, stackId); cluster = clusters.getCluster(clusterName); Assert.assertNotNull(cluster); } @@ -300,14 +307,14 @@ public class AlertEventPublisherTest { source.getAlertName())); } - /** - * Calls {@link Service#persist()} to mock a service install. - */ private void installHdfsService() throws Exception { + RepositoryVersionEntity repositoryVersion = ormHelper.getOrCreateRepositoryVersion( + cluster.getCurrentStackVersion(), REPO_VERSION); + String serviceName = "HDFS"; - Service service = serviceFactory.createNew(cluster, serviceName); + Service service = serviceFactory.createNew(cluster, serviceName, repositoryVersion); service = cluster.getService(serviceName); Assert.assertNotNull(service); } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java index 483aac5..548c13e 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/InitialAlertEventTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -28,10 +28,12 @@ import org.apache.ambari.server.events.MockEventListener; import org.apache.ambari.server.events.publishers.AlertEventPublisher; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.OrmTestHelper; import org.apache.ambari.server.orm.dao.AlertDefinitionDAO; import org.apache.ambari.server.orm.dao.AlertsDAO; import org.apache.ambari.server.orm.entities.AlertCurrentEntity; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.state.Alert; import org.apache.ambari.server.state.AlertFirmness; import org.apache.ambari.server.state.AlertState; @@ -72,6 +74,13 @@ public class InitialAlertEventTest { private String m_clusterName; private ServiceFactory m_serviceFactory; + private OrmTestHelper m_helper; + + private final String STACK_VERSION = "2.0.6"; + private final String REPO_VERSION = "2.0.6-1234"; + private final StackId STACK_ID = new StackId("HDP", STACK_VERSION); + private RepositoryVersionEntity m_repositoryVersion; + /** * */ @@ -97,9 +106,12 @@ public class InitialAlertEventTest { m_serviceFactory = m_injector.getInstance(ServiceFactory.class); m_alertsDao = m_injector.getInstance(AlertsDAO.class); + m_helper = m_injector.getInstance(OrmTestHelper.class); + + m_repositoryVersion = m_helper.getOrCreateRepositoryVersion(STACK_ID, REPO_VERSION); m_clusterName = "c1"; - m_clusters.addCluster(m_clusterName, new StackId("HDP", "2.0.6")); + m_clusters.addCluster(m_clusterName, STACK_ID); m_cluster = m_clusters.getCluster(m_clusterName); Assert.assertNotNull(m_cluster); @@ -173,12 +185,9 @@ public class InitialAlertEventTest { m_listener.getAlertEventReceivedCount(InitialAlertEvent.class)); } - /** - * Calls {@link Service#persist()} to mock a service install. - */ private void installHdfsService() throws Exception { String serviceName = "HDFS"; - Service service = m_serviceFactory.createNew(m_cluster, serviceName); + Service service = m_serviceFactory.createNew(m_cluster, serviceName, m_repositoryVersion); service = m_cluster.getService(serviceName); Assert.assertNotNull(service); @@ -197,4 +206,4 @@ public class InitialAlertEventTest { EventBusSynchronizer.synchronizeAmbariEventPublisher(binder); } } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java index 76146b5..caf6bf4 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterDeadlockTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -35,13 +35,13 @@ import org.apache.ambari.server.events.listeners.upgrade.HostVersionOutOfSyncLis import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.OrmTestHelper; +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.ConfigFactory; 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.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentFactory; @@ -96,6 +96,7 @@ public class ClusterDeadlockTest { private OrmTestHelper helper; private StackId stackId = new StackId("HDP-0.1"); + private String REPO_VERSION = "0.1-1234"; /** * The cluster. @@ -105,7 +106,7 @@ public class ClusterDeadlockTest { /** * */ - private List<String> hostNames = new ArrayList<String>(NUMBER_OF_HOSTS); + private List<String> hostNames = new ArrayList<>(NUMBER_OF_HOSTS); /** * Creates 100 hosts and adds them to the cluster. @@ -119,18 +120,19 @@ public class ClusterDeadlockTest { injector.getInstance(GuiceJpaInitializer.class); injector.injectMembers(this); + + helper.createStack(stackId); + clusters.addCluster("c1", stackId); cluster = clusters.getCluster("c1"); helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion()); - cluster.createClusterVersion(stackId, - stackId.getStackVersion(), "admin", RepositoryVersionState.INSTALLING); Config config1 = configFactory.createNew(cluster, "test-type1", "version1", new HashMap<String, String>(), new HashMap<String, Map<String, String>>()); Config config2 = configFactory.createNew(cluster, "test-type2", "version1", new HashMap<String, String>(), new HashMap<String, Map<String, String>>()); - cluster.addDesiredConfig("test user", new HashSet<Config>(Arrays.asList(config1, config2))); + cluster.addDesiredConfig("test user", new HashSet<>(Arrays.asList(config1, config2))); // 100 hosts for (int i = 0; i < NUMBER_OF_HOSTS; i++) { @@ -170,7 +172,7 @@ public class ClusterDeadlockTest { ServiceComponentHost dataNodeSCH = createNewServiceComponentHost("HDFS", "DATANODE", "c64-0"); - List<Thread> threads = new ArrayList<Thread>(); + List<Thread> threads = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_THREADS; i++) { DeadlockExerciserThread thread = new DeadlockExerciserThread(); thread.setCluster(cluster); @@ -209,7 +211,7 @@ public class ClusterDeadlockTest { ServiceComponent nameNodeComponent = service.getServiceComponent("NAMENODE"); ServiceComponent dataNodeComponent = service.getServiceComponent("DATANODE"); - List<Thread> threads = new ArrayList<Thread>(); + List<Thread> threads = new ArrayList<>(); for (int i = 0; i < 5; i++) { ServiceComponentReaderWriterThread thread = new ServiceComponentReaderWriterThread(); thread.setDataNodeComponent(dataNodeComponent); @@ -241,7 +243,7 @@ public class ClusterDeadlockTest { @Test() public void testDeadlockWhileRestartingComponents() throws Exception { // for each host, install both components - List<ServiceComponentHost> serviceComponentHosts = new ArrayList<ServiceComponentHost>(); + List<ServiceComponentHost> serviceComponentHosts = new ArrayList<>(); for (String hostName : hostNames) { serviceComponentHosts.add(createNewServiceComponentHost("HDFS", "NAMENODE", hostName)); @@ -250,7 +252,7 @@ public class ClusterDeadlockTest { "DATANODE", hostName)); } - List<Thread> threads = new ArrayList<Thread>(); + List<Thread> threads = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_THREADS; i++) { ClusterReaderThread clusterReaderThread = new ClusterReaderThread(); ClusterWriterThread clusterWriterThread = new ClusterWriterThread(); @@ -281,7 +283,7 @@ public class ClusterDeadlockTest { @Test public void testDeadlockWithConfigsUpdate() throws Exception { - List<Thread> threads = new ArrayList<Thread>(); + List<Thread> threads = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_THREADS; i++) { ClusterDesiredConfigsReaderThread readerThread = null; for (int j = 0; j < NUMBER_OF_THREADS; j++) { @@ -559,7 +561,7 @@ public class ClusterDeadlockTest { } private void setOsFamily(Host host, String osFamily, String osVersion) { - Map<String, String> hostAttributes = new HashMap<String, String>(2); + Map<String, String> hostAttributes = new HashMap<>(2); hostAttributes.put("os_family", osFamily); hostAttributes.put("os_release_version", osVersion); host.setHostAttributes(hostAttributes); @@ -577,8 +579,6 @@ public class ClusterDeadlockTest { sc.addServiceComponentHost(sch); sch.setDesiredState(State.INSTALLED); sch.setState(State.INSTALLED); - sch.setDesiredStackVersion(stackId); - sch.setStackVersion(stackId); return sch; } @@ -586,10 +586,13 @@ public class ClusterDeadlockTest { private Service installService(String serviceName) throws AmbariException { Service service = null; + RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion( + stackId, REPO_VERSION); + try { service = cluster.getService(serviceName); } catch (ServiceNotFoundException e) { - service = serviceFactory.createNew(cluster, serviceName); + service = serviceFactory.createNew(cluster, serviceName, repositoryVersion); cluster.addService(service); } @@ -626,4 +629,4 @@ public class ClusterDeadlockTest { EasyMock.createNiceMock(HostVersionOutOfSyncListener.class)); } } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterEffectiveVersionTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterEffectiveVersionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterEffectiveVersionTest.java deleted file mode 100644 index 05f31d4..0000000 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterEffectiveVersionTest.java +++ /dev/null @@ -1,326 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.server.state.cluster; - -import java.util.ArrayList; -import java.util.HashMap; - -import javax.persistence.EntityManager; - -import org.apache.ambari.server.actionmanager.ActionManager; -import org.apache.ambari.server.actionmanager.RequestFactory; -import org.apache.ambari.server.actionmanager.StageFactory; -import org.apache.ambari.server.api.services.AmbariMetaInfo; -import org.apache.ambari.server.controller.AbstractRootServiceResponseFactory; -import org.apache.ambari.server.controller.AmbariManagementController; -import org.apache.ambari.server.controller.KerberosHelper; -import org.apache.ambari.server.controller.spi.ClusterController; -import org.apache.ambari.server.events.publishers.AmbariEventPublisher; -import org.apache.ambari.server.hooks.HookContextFactory; -import org.apache.ambari.server.hooks.HookService; -import org.apache.ambari.server.metadata.CachedRoleCommandOrderProvider; -import org.apache.ambari.server.metadata.RoleCommandOrderProvider; -import org.apache.ambari.server.orm.DBAccessor; -import org.apache.ambari.server.orm.dao.ClusterDAO; -import org.apache.ambari.server.orm.dao.ClusterVersionDAO; -import org.apache.ambari.server.orm.dao.HostRoleCommandDAO; -import org.apache.ambari.server.orm.entities.ClusterConfigEntity; -import org.apache.ambari.server.orm.entities.ClusterEntity; -import org.apache.ambari.server.orm.entities.ClusterServiceEntity; -import org.apache.ambari.server.orm.entities.ClusterVersionEntity; -import org.apache.ambari.server.orm.entities.ConfigGroupEntity; -import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; -import org.apache.ambari.server.orm.entities.RequestScheduleEntity; -import org.apache.ambari.server.orm.entities.StackEntity; -import org.apache.ambari.server.orm.entities.UpgradeEntity; -import org.apache.ambari.server.scheduler.ExecutionScheduler; -import org.apache.ambari.server.security.authorization.Users; -import org.apache.ambari.server.stack.StackManagerFactory; -import org.apache.ambari.server.stageplanner.RoleGraphFactory; -import org.apache.ambari.server.state.Cluster; -import org.apache.ambari.server.state.Clusters; -import org.apache.ambari.server.state.ConfigFactory; -import org.apache.ambari.server.state.ServiceComponentFactory; -import org.apache.ambari.server.state.ServiceComponentHostFactory; -import org.apache.ambari.server.state.ServiceFactory; -import org.apache.ambari.server.state.ServiceInfo; -import org.apache.ambari.server.state.StackId; -import org.apache.ambari.server.state.UpgradeContextFactory; -import org.apache.ambari.server.state.configgroup.ConfigGroupFactory; -import org.apache.ambari.server.state.scheduler.RequestExecutionFactory; -import org.apache.ambari.server.state.stack.OsFamily; -import org.apache.ambari.server.state.stack.upgrade.Direction; -import org.apache.ambari.server.state.stack.upgrade.UpgradeType; -import org.easymock.EasyMock; -import org.easymock.EasyMockSupport; -import org.eclipse.jetty.server.SessionManager; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.springframework.security.crypto.password.PasswordEncoder; - -import com.google.inject.Binder; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Module; -import com.google.inject.assistedinject.FactoryModuleBuilder; - -import junit.framework.Assert; - -/** - * Tests that cluster effective version is calcualted correctly during upgrades. - */ -@RunWith(value = PowerMockRunner.class) -@PrepareForTest({ ClusterImpl.class }) -public class ClusterEffectiveVersionTest extends EasyMockSupport { - - private static final String CLUSTER_NAME = "c1"; - - private Injector m_injector; - private ClusterEntity m_clusterEntity; - private Cluster m_cluster; - - /** - * @throws Exception - */ - @Before - public void setup() throws Exception { - m_injector = Guice.createInjector(new MockModule()); - m_clusterEntity = createNiceMock(ClusterEntity.class); - - expectClusterEntityMocks(); - - replayAll(); - - ClusterFactory clusterFactory = m_injector.getInstance(ClusterFactory.class); - m_cluster = clusterFactory.create(m_clusterEntity); - - verifyAll(); - } - - /** - * Tests that {@link Cluster#getEffectiveClusterVersion()} returns the - * "current" version when there is no upgrade in progress. - */ - @Test - public void testEffectiveVersionWithNoUpgrade() throws Exception { - Cluster clusterSpy = Mockito.spy(m_cluster); - - Mockito.doReturn(null).when(clusterSpy).getUpgradeInProgress(); - - ClusterVersionEntity currentClusterVersion = new ClusterVersionEntity(); - Mockito.doReturn(currentClusterVersion).when(clusterSpy).getCurrentClusterVersion(); - - ClusterVersionEntity effectiveVersion = clusterSpy.getEffectiveClusterVersion(); - Assert.assertEquals(currentClusterVersion, effectiveVersion); - } - - /** - * Tests that {@link Cluster#getEffectiveClusterVersion()} returns the target - * version in an active rolling upgrade. - */ - @Test - public void testEffectiveVersionWithActiveRollingUpgrade() throws Exception { - resetAll(); - expectClusterEntityMocks(); - - Cluster clusterSpy = Mockito.spy(m_cluster); - - RepositoryVersionEntity repoVersion2300 = createNiceMock(RepositoryVersionEntity.class); - RepositoryVersionEntity repoVersion2400 = createNiceMock(RepositoryVersionEntity.class); - - StackId stackId23 = new StackId("HDP", "2.3"); - StackId stackId24 = new StackId("HDP", "2.4"); - - EasyMock.expect(repoVersion2300.getVersion()).andReturn("2.3.0.0-1234").anyTimes(); - EasyMock.expect(repoVersion2300.getStackId()).andReturn(stackId23).anyTimes(); - EasyMock.expect(repoVersion2400.getVersion()).andReturn("2.4.0.0-1234").anyTimes(); - EasyMock.expect(repoVersion2400.getStackId()).andReturn(stackId24).anyTimes(); - - UpgradeEntity upgradeEntity = createNiceMock(UpgradeEntity.class); - EasyMock.expect(upgradeEntity.getId()).andReturn(1L).atLeastOnce(); - EasyMock.expect(upgradeEntity.getUpgradeType()).andReturn(UpgradeType.ROLLING).atLeastOnce(); - EasyMock.expect(upgradeEntity.getFromRepositoryVersion()).andReturn(repoVersion2300).anyTimes(); - EasyMock.expect(upgradeEntity.getToRepositoryVersion()).andReturn(repoVersion2400).atLeastOnce(); - - ClusterVersionEntity clusterVersionUpgradingTo = createNiceMock(ClusterVersionEntity.class); - EasyMock.expect(clusterVersionUpgradingTo.getId()).andReturn(1L).atLeastOnce(); - - ClusterVersionDAO clusterVersionDAO = m_injector.getInstance(ClusterVersionDAO.class); - EasyMock.expect(clusterVersionDAO.findByClusterAndStackAndVersion(CLUSTER_NAME, stackId24, - "2.4.0.0-1234")).andReturn(clusterVersionUpgradingTo).once(); - - EasyMock.expect(clusterVersionDAO.findByPK(1L)).andReturn(clusterVersionUpgradingTo).once(); - - replayAll(); - - Mockito.doReturn(upgradeEntity).when(clusterSpy).getUpgradeInProgress(); - - // this shouldn't be returned since there is an upgrade in progress - ClusterVersionEntity currentClusterVersion = new ClusterVersionEntity(); - Mockito.doReturn(currentClusterVersion).when(clusterSpy).getCurrentClusterVersion(); - - ClusterVersionEntity effectiveVersion = clusterSpy.getEffectiveClusterVersion(); - Assert.assertEquals(clusterVersionUpgradingTo, effectiveVersion); - - verifyAll(); - } - - /** - * Tests that {@link Cluster#getEffectiveClusterVersion()} returns the target - * version in an active rolling upgrade. - */ - @Test - public void testEffectiveVersionWithActiveExpressDowngrade() throws Exception { - resetAll(); - expectClusterEntityMocks(); - - Cluster clusterSpy = Mockito.spy(m_cluster); - - RepositoryVersionEntity repoVersion2300 = createNiceMock(RepositoryVersionEntity.class); - RepositoryVersionEntity repoVersion2400 = createNiceMock(RepositoryVersionEntity.class); - - StackId stackId23 = new StackId("HDP", "2.3"); - StackId stackId24 = new StackId("HDP", "2.4"); - - EasyMock.expect(repoVersion2300.getVersion()).andReturn("2.3.0.0-1234").anyTimes(); - EasyMock.expect(repoVersion2300.getStackId()).andReturn(stackId23).anyTimes(); - EasyMock.expect(repoVersion2400.getVersion()).andReturn("2.4.0.0-1234").anyTimes(); - EasyMock.expect(repoVersion2400.getStackId()).andReturn(stackId24).anyTimes(); - - UpgradeEntity upgradeEntity = createNiceMock(UpgradeEntity.class); - EasyMock.expect(upgradeEntity.getId()).andReturn(1L).atLeastOnce(); - EasyMock.expect(upgradeEntity.getUpgradeType()).andReturn(UpgradeType.NON_ROLLING).atLeastOnce(); - EasyMock.expect(upgradeEntity.getDirection()).andReturn(Direction.DOWNGRADE).atLeastOnce(); - EasyMock.expect(upgradeEntity.getFromRepositoryVersion()).andReturn(repoVersion2400).anyTimes(); - EasyMock.expect(upgradeEntity.getToRepositoryVersion()).andReturn(repoVersion2300).atLeastOnce(); - - ClusterVersionEntity clusterVersionDowngradingTo = createNiceMock(ClusterVersionEntity.class); - EasyMock.expect(clusterVersionDowngradingTo.getId()).andReturn(1L).atLeastOnce(); - - ClusterVersionDAO clusterVersionDAO = m_injector.getInstance(ClusterVersionDAO.class); - EasyMock.expect(clusterVersionDAO.findByClusterAndStackAndVersion(CLUSTER_NAME, stackId23, - "2.3.0.0-1234")).andReturn(clusterVersionDowngradingTo).once(); - - EasyMock.expect(clusterVersionDAO.findByPK(1L)).andReturn(clusterVersionDowngradingTo).once(); - - replayAll(); - - Mockito.doReturn(upgradeEntity).when(clusterSpy).getUpgradeInProgress(); - - // this shouldn't be returned since there is an upgrade in progress - ClusterVersionEntity currentClusterVersion = new ClusterVersionEntity(); - Mockito.doReturn(currentClusterVersion).when(clusterSpy).getCurrentClusterVersion(); - - ClusterVersionEntity effectiveVersion = clusterSpy.getEffectiveClusterVersion(); - Assert.assertEquals(clusterVersionDowngradingTo, effectiveVersion); - - verifyAll(); - } - - /** - * Sets the expectations on the {@link ClusterEntity} mock. - */ - private void expectClusterEntityMocks() { - ClusterDAO clusterDAO = m_injector.getInstance(ClusterDAO.class); - StackEntity stackEntity = createNiceMock(StackEntity.class); - - EasyMock.expect(clusterDAO.findById(1L)).andReturn(m_clusterEntity).anyTimes(); - - EasyMock.expect(stackEntity.getStackId()).andReturn(1L).anyTimes(); - EasyMock.expect(stackEntity.getStackName()).andReturn("HDP").anyTimes(); - EasyMock.expect(stackEntity.getStackVersion()).andReturn("2.3").anyTimes(); - - EasyMock.expect(m_clusterEntity.getClusterId()).andReturn(1L).anyTimes(); - EasyMock.expect(m_clusterEntity.getClusterName()).andReturn(CLUSTER_NAME).anyTimes(); - EasyMock.expect(m_clusterEntity.getDesiredStack()).andReturn(stackEntity).anyTimes(); - EasyMock.expect(m_clusterEntity.getClusterServiceEntities()).andReturn( - new ArrayList<ClusterServiceEntity>()).anyTimes(); - EasyMock.expect(m_clusterEntity.getClusterConfigEntities()).andReturn( - new ArrayList<ClusterConfigEntity>()).anyTimes(); - - EasyMock.expect(m_clusterEntity.getConfigGroupEntities()).andReturn( - new ArrayList<ConfigGroupEntity>()).anyTimes(); - - EasyMock.expect(m_clusterEntity.getRequestScheduleEntities()).andReturn( - new ArrayList<RequestScheduleEntity>()).anyTimes(); - } - - /** - * - */ - private class MockModule implements Module { - /** - * - */ - @Override - public void configure(Binder binder) { - binder.bind(UpgradeContextFactory.class).toInstance(EasyMock.createNiceMock(UpgradeContextFactory.class)); - binder.bind(Clusters.class).toInstance(EasyMock.createNiceMock(Clusters.class)); - binder.bind(OsFamily.class).toInstance(EasyMock.createNiceMock(OsFamily.class)); - binder.bind(DBAccessor.class).toInstance(EasyMock.createNiceMock(DBAccessor.class)); - binder.bind(EntityManager.class).toInstance(EasyMock.createNiceMock(EntityManager.class)); - binder.bind(ActionManager.class).toInstance(EasyMock.createNiceMock(ActionManager.class)); - binder.bind(HostRoleCommandDAO.class).toInstance(EasyMock.createNiceMock(HostRoleCommandDAO.class)); - binder.bind(AmbariManagementController.class).toInstance(EasyMock.createNiceMock(AmbariManagementController.class)); - binder.bind(ClusterController.class).toInstance(EasyMock.createNiceMock(ClusterController.class)); - binder.bind(StackManagerFactory.class).toInstance(EasyMock.createNiceMock(StackManagerFactory.class)); - binder.bind(SessionManager.class).toInstance(EasyMock.createNiceMock(SessionManager.class)); - binder.bind(RequestExecutionFactory.class).toInstance(EasyMock.createNiceMock(RequestExecutionFactory.class)); - binder.bind(ExecutionScheduler.class).toInstance(EasyMock.createNiceMock(ExecutionScheduler.class)); - binder.bind(RequestFactory.class).toInstance(EasyMock.createNiceMock(RequestFactory.class)); - binder.bind(StageFactory.class).toInstance(EasyMock.createNiceMock(StageFactory.class)); - binder.bind(RoleGraphFactory.class).toInstance(EasyMock.createNiceMock(RoleGraphFactory.class)); - binder.bind(AbstractRootServiceResponseFactory.class).toInstance(EasyMock.createNiceMock(AbstractRootServiceResponseFactory.class)); - binder.bind(ConfigFactory.class).toInstance(EasyMock.createNiceMock(ConfigFactory.class)); - binder.bind(ConfigGroupFactory.class).toInstance(EasyMock.createNiceMock(ConfigGroupFactory.class)); - binder.bind(ServiceFactory.class).toInstance(EasyMock.createNiceMock(ServiceFactory.class)); - binder.bind(ServiceComponentFactory.class).toInstance(EasyMock.createNiceMock(ServiceComponentFactory.class)); - binder.bind(ServiceComponentHostFactory.class).toInstance(EasyMock.createNiceMock(ServiceComponentHostFactory.class)); - binder.bind(PasswordEncoder.class).toInstance(EasyMock.createNiceMock(PasswordEncoder.class)); - binder.bind(KerberosHelper.class).toInstance(EasyMock.createNiceMock(KerberosHelper.class)); - binder.bind(Users.class).toInstance(EasyMock.createNiceMock(Users.class)); - binder.bind(AmbariEventPublisher.class).toInstance(createNiceMock(AmbariEventPublisher.class)); - binder.bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class)); - binder.bind(HookService.class).toInstance(createMock(HookService.class)); - binder.install(new FactoryModuleBuilder().implement( - Cluster.class, ClusterImpl.class).build(ClusterFactory.class)); - - binder.bind(RoleCommandOrderProvider.class).to(CachedRoleCommandOrderProvider.class); - - try { - AmbariMetaInfo ambariMetaInfo = EasyMock.createNiceMock(AmbariMetaInfo.class); - EasyMock.expect( - ambariMetaInfo.getServices(EasyMock.anyString(), EasyMock.anyString())).andReturn( - new HashMap<String, ServiceInfo>()).anyTimes(); - - EasyMock.replay(ambariMetaInfo); - - binder.bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo); - } catch (Exception exception) { - Assert.fail(exception.toString()); - } - - binder.bind(ClusterDAO.class).toInstance(createNiceMock(ClusterDAO.class)); - binder.bind(ClusterVersionDAO.class).toInstance(createNiceMock(ClusterVersionDAO.class)); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/56362fd6/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java index 3172571..4dd52e0 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -40,6 +40,8 @@ import org.apache.ambari.server.HostNotFoundException; import org.apache.ambari.server.controller.AmbariSessionManager; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.OrmTestHelper; +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.Host; @@ -60,12 +62,14 @@ public class ClusterImplTest { private static Injector injector; private static Clusters clusters; + private static OrmTestHelper ormTestHelper; @BeforeClass public static void setUpClass() throws Exception { injector = Guice.createInjector(new InMemoryDefaultTestModule()); injector.getInstance(GuiceJpaInitializer.class); clusters = injector.getInstance(Clusters.class); + ormTestHelper = injector.getInstance(OrmTestHelper.class); } @AfterClass @@ -75,7 +79,7 @@ public class ClusterImplTest { @Test public void testAddSessionAttributes() throws Exception { - Map<String, Object> attributes = new HashMap<String, Object>(); + Map<String, Object> attributes = new HashMap<>(); attributes.put("foo", "bar"); AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class); @@ -101,14 +105,14 @@ public class ClusterImplTest { @Test public void testSetSessionAttribute() throws Exception { - Map<String, Object> attributes = new HashMap<String, Object>(); + Map<String, Object> attributes = new HashMap<>(); attributes.put("foo", "bar"); attributes.put("foo2", "bar2"); - Map<String, Object> updatedAttributes = new HashMap<String, Object>(attributes); + Map<String, Object> updatedAttributes = new HashMap<>(attributes); updatedAttributes.put("foo2", "updated value"); - Map<String, Object> addedAttributes = new HashMap<String, Object>(updatedAttributes); + Map<String, Object> addedAttributes = new HashMap<>(updatedAttributes); updatedAttributes.put("foo3", "added value"); AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class); @@ -144,11 +148,11 @@ public class ClusterImplTest { @Test public void testRemoveSessionAttribute() throws Exception { - Map<String, Object> attributes = new HashMap<String, Object>(); + Map<String, Object> attributes = new HashMap<>(); attributes.put("foo", "bar"); attributes.put("foo2", "bar2"); - Map<String, Object> trimmedAttributes = new HashMap<String, Object>(attributes); + Map<String, Object> trimmedAttributes = new HashMap<>(attributes); trimmedAttributes.remove("foo2"); AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class); @@ -175,7 +179,7 @@ public class ClusterImplTest { @Test public void testGetSessionAttributes() throws Exception { - Map<String, Object> attributes = new HashMap<String, Object>(); + Map<String, Object> attributes = new HashMap<>(); attributes.put("foo", "bar"); AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class); @@ -207,10 +211,17 @@ public class ClusterImplTest { String clusterName = "TEST_CLUSTER"; String hostName1 = "HOST1", hostName2 = "HOST2"; - clusters.addCluster(clusterName, new StackId("HDP-2.1.1")); + String stackVersion = "HDP-2.1.1"; + String repoVersion = "2.1.1-1234"; + StackId stackId = new StackId(stackVersion); + ormTestHelper.createStack(stackId); + clusters.addCluster(clusterName, stackId); Cluster cluster = clusters.getCluster(clusterName); + RepositoryVersionEntity repositoryVersion = ormTestHelper.getOrCreateRepositoryVersion( + new StackId(stackVersion), repoVersion); + clusters.addHost(hostName1); clusters.addHost(hostName2); @@ -222,7 +233,7 @@ public class ClusterImplTest { clusters.mapAndPublishHostsToCluster(Sets.newHashSet(hostName1, hostName2), clusterName); - Service hdfs = cluster.addService("HDFS"); + Service hdfs = cluster.addService("HDFS", repositoryVersion); ServiceComponent nameNode = hdfs.addServiceComponent("NAMENODE"); nameNode.addServiceComponentHost(hostName1); @@ -235,7 +246,7 @@ public class ClusterImplTest { hdfsClient.addServiceComponentHost(hostName1); hdfsClient.addServiceComponentHost(hostName2); - Service tez = cluster.addService(serviceToDelete); + Service tez = cluster.addService(serviceToDelete, repositoryVersion); ServiceComponent tezClient = tez.addServiceComponent("TEZ_CLIENT"); ServiceComponentHost tezClientHost1 = tezClient.addServiceComponentHost(hostName1); @@ -259,13 +270,13 @@ public class ClusterImplTest { @Test public void testDeleteHost() throws Exception { // Given - - String clusterName = "TEST_DELETE_HOST"; String hostName1 = "HOSTNAME1", hostName2 = "HOSTNAME2"; String hostToDelete = hostName2; + StackId stackId = new StackId("HDP-2.1.1"); - clusters.addCluster(clusterName, new StackId("HDP-2.1.1")); + ormTestHelper.createStack(stackId); + clusters.addCluster(clusterName, stackId); Cluster cluster = clusters.getCluster(clusterName); @@ -296,8 +307,6 @@ public class ClusterImplTest { catch(HostNotFoundException e){ } - - } @Test @@ -305,7 +314,9 @@ public class ClusterImplTest { // Given String clusterName = "TEST_CLUSTER_SIZE"; String hostName1 = "host1", hostName2 = "host2"; - clusters.addCluster(clusterName, new StackId("HDP-2.1.1")); + StackId stackId = new StackId("HDP", "2.1.1"); + ormTestHelper.createStack(stackId); + clusters.addCluster(clusterName, stackId); Cluster cluster = clusters.getCluster(clusterName); clusters.addHost(hostName1); @@ -326,4 +337,4 @@ public class ClusterImplTest { assertEquals(2, clusterSize); } -} +} \ No newline at end of file
