http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostComponentResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostComponentResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostComponentResourceProviderTest.java index 8d1d20d..03ed000 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostComponentResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostComponentResourceProviderTest.java @@ -332,131 +332,13 @@ public class HostComponentResourceProviderTest { Resource responseResource = requestStatus.getRequestResource(); assertEquals("response msg", responseResource.getPropertyValue(PropertyHelper.getPropertyId("Requests", "message"))); assertEquals(1000L, responseResource.getPropertyValue(PropertyHelper.getPropertyId("Requests", "id"))); - assertEquals("InProgress", responseResource.getPropertyValue(PropertyHelper.getPropertyId("Requests", "status"))); + assertEquals("Accepted", responseResource.getPropertyValue(PropertyHelper.getPropertyId("Requests", "status"))); assertTrue(requestStatus.getAssociatedResources().isEmpty()); // verify verify(managementController, response, resourceProviderFactory, stageContainer); } - @Test - public void testInstallAndStart() throws Exception { - Resource.Type type = Resource.Type.HostComponent; - - AmbariManagementController managementController = createMock(AmbariManagementController.class); - RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); - ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); - Injector injector = createNiceMock(Injector.class); - Clusters clusters = createNiceMock(Clusters.class); - Cluster cluster = createNiceMock(Cluster.class); - Service service = createNiceMock(Service.class); - ServiceComponent component = createNiceMock(ServiceComponent.class); - ServiceComponent clientComponent = createNiceMock(ServiceComponent.class); - ServiceComponentHost componentHost = createNiceMock(ServiceComponentHost.class); - ServiceComponentHost clientComponentHost = createNiceMock(ServiceComponentHost.class); - RequestStageContainer stageContainer = createNiceMock(RequestStageContainer.class); - MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class); - HostVersionDAO hostVersionDAO = createMock(HostVersionDAO.class); - - Collection<String> hosts = new HashSet<String>(); - hosts.add("Host100"); - - Map<String, String> mapRequestProps = new HashMap<String, String>(); - mapRequestProps.put("context", "Install and start components on added hosts"); - - Set<ServiceComponentHostResponse> nameResponse = new HashSet<ServiceComponentHostResponse>(); - nameResponse.add(new ServiceComponentHostResponse( - "Cluster102", "Service100", "Component100", "Host100", "INIT", "", "INIT", "", null)); - nameResponse.add(new ServiceComponentHostResponse( - "Cluster102", "Service100", "some-client", "Host100", "INIT", "", "INIT", "", null)); - Set<ServiceComponentHostResponse> nameResponse2 = new HashSet<ServiceComponentHostResponse>(); - nameResponse2.add(new ServiceComponentHostResponse( - "Cluster102", "Service100", "Component100", "Host100", "INIT", "", "INSTALLED", "", null)); - nameResponse2.add(new ServiceComponentHostResponse( - "Cluster102", "Service100", "some-client", "Host100", "INIT", "", "INSTALLED", "", null)); - - - // set expectations - expect(managementController.getClusters()).andReturn(clusters).anyTimes(); - expect(managementController.findServiceName(cluster, "Component100")).andReturn("Service100").anyTimes(); - expect(managementController.findServiceName(cluster, "some-client")).andReturn("Service100").anyTimes(); - expect(clusters.getCluster("Cluster102")).andReturn(cluster).anyTimes(); - expect(cluster.getService("Service100")).andReturn(service).anyTimes(); - expect(service.getServiceComponent("Component100")).andReturn(component).anyTimes(); - expect(service.getServiceComponent("some-client")).andReturn(clientComponent).anyTimes(); - expect(component.getServiceComponentHost("Host100")).andReturn(componentHost).anyTimes(); - expect(component.getName()).andReturn("Component100").anyTimes(); - expect(clientComponent.getServiceComponentHost("Host100")).andReturn(clientComponentHost).anyTimes(); - expect(clientComponent.getName()).andReturn("some-client").anyTimes(); - expect(clientComponent.isClientComponent()).andReturn(true).anyTimes(); - // actual state is always INIT until stages actually execute - expect(componentHost.getState()).andReturn(State.INIT).anyTimes(); - expect(componentHost.getHostName()).andReturn("Host100").anyTimes(); - expect(componentHost.getServiceComponentName()).andReturn("Component100").anyTimes(); - expect(clientComponentHost.getState()).andReturn(State.INIT).anyTimes(); - expect(clientComponentHost.getHostName()).andReturn("Host100").anyTimes(); - expect(clientComponentHost.getServiceComponentName()).andReturn("some-client").anyTimes(); - expect(response.getMessage()).andReturn("response msg").anyTimes(); - expect(hostVersionDAO.findByHostAndStateCurrent(anyObject(String.class), anyObject(String.class))).andReturn(null).anyTimes(); - - //Cluster is default type. Maintenance mode is not being tested here so the default is returned. - expect(maintenanceStateHelper.isOperationAllowed(Resource.Type.Cluster, componentHost)).andReturn(true).anyTimes(); - expect(maintenanceStateHelper.isOperationAllowed(Resource.Type.Cluster, clientComponentHost)).andReturn(true).anyTimes(); - - //todo: can we change to prevent having to call twice? - expect(managementController.getHostComponents( - EasyMock.<Set<ServiceComponentHostRequest>>anyObject())).andReturn(nameResponse); - expect(managementController.getHostComponents( - EasyMock.<Set<ServiceComponentHostRequest>>anyObject())).andReturn(nameResponse2); - - Map<String, Map<State, List<ServiceComponentHost>>> changedHosts = - new HashMap<String, Map<State, List<ServiceComponentHost>>>(); - - changedHosts.put("Component100", Collections.singletonMap(State.INSTALLED, Collections.singletonList(componentHost))); - changedHosts.put("some-client", Collections.singletonMap(State.INSTALLED, Collections.singletonList(clientComponentHost))); - - Map<String, Map<State, List<ServiceComponentHost>>> changedHosts2 = - new HashMap<String, Map<State, List<ServiceComponentHost>>>(); - List<ServiceComponentHost> changedComponentHosts2 = Collections.singletonList(componentHost); - changedHosts2.put("Component100", Collections.singletonMap(State.STARTED, changedComponentHosts2)); - - expect(managementController.addStages(null, cluster, mapRequestProps, null, null, null, changedHosts, - Collections.<ServiceComponentHost>emptyList(), false, false)).andReturn(stageContainer).once(); - - expect(managementController.addStages(stageContainer, cluster, mapRequestProps, null, null, null, changedHosts2, - Collections.<ServiceComponentHost>emptyList(), false, false)).andReturn(stageContainer).once(); - - stageContainer.persist(); - expect(stageContainer.getProjectedState("Host100", "Component100")).andReturn(State.INSTALLED).once(); - expect(stageContainer.getRequestStatusResponse()).andReturn(response).once(); - - TestHostComponentResourceProvider provider = - new TestHostComponentResourceProvider(PropertyHelper.getPropertyIds(type), - PropertyHelper.getKeyPropertyIds(type), - managementController, injector); - provider.setFieldValue("maintenanceStateHelper", maintenanceStateHelper); - provider.setFieldValue("hostVersionDAO", hostVersionDAO); - - expect(resourceProviderFactory.getHostComponentResourceProvider(anyObject(Set.class), - anyObject(Map.class), - eq(managementController))). - andReturn(provider).anyTimes(); - - // replay - replay(managementController, response, resourceProviderFactory, clusters, cluster, service, - component, componentHost, stageContainer, maintenanceStateHelper, clientComponent, - clientComponentHost, hostVersionDAO); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put(HostComponentResourceProvider.HOST_COMPONENT_STATE_PROPERTY_ID, "STARTED"); - - RequestStatusResponse requestResponse = provider.installAndStart("Cluster102", hosts); - - assertSame(response, requestResponse); - // verify - verify(managementController, response, resourceProviderFactory, stageContainer, - clientComponent, clientComponentHost); - } @Test public void testDeleteResources() throws Exception { @@ -465,8 +347,8 @@ public class HostComponentResourceProviderTest { AmbariManagementController managementController = createMock(AmbariManagementController.class); RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); Injector injector = createNiceMock(Injector.class); - - HostComponentResourceProvider provider = + + HostComponentResourceProvider provider = new HostComponentResourceProvider(PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController, injector);
http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequestTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequestTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequestTest.java new file mode 100644 index 0000000..acfd426 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequestTest.java @@ -0,0 +1,282 @@ +/** + * 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.controller.internal; + +import org.apache.ambari.server.topology.Blueprint; +import org.apache.ambari.server.topology.BlueprintFactory; +import org.apache.ambari.server.topology.Configuration; +import org.apache.ambari.server.topology.HostGroupInfo; +import org.apache.ambari.server.topology.InvalidTopologyTemplateException; +import org.apache.ambari.server.topology.RequiredPasswordValidator; +import org.apache.ambari.server.topology.TopologyRequest; +import org.apache.ambari.server.topology.TopologyValidator; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.powermock.api.easymock.PowerMock.createStrictMock; +import static org.powermock.api.easymock.PowerMock.replay; +import static org.powermock.api.easymock.PowerMock.reset; + +/** + * Unit tests for ProvisionClusterRequest. + */ +@SuppressWarnings("unchecked") +public class ProvisionClusterRequestTest { + + private static final String CLUSTER_NAME = "cluster_name"; + private static final String BLUEPRINT_NAME = "blueprint_name"; + + private static final BlueprintFactory blueprintFactory = createStrictMock(BlueprintFactory.class); + private static final Blueprint blueprint = createNiceMock(Blueprint.class); + private static final Configuration blueprintConfig = new Configuration( + Collections.<String, Map<String, String>>emptyMap(), + Collections.<String, Map<String, Map<String, String>>>emptyMap()); + + @Before + public void setUp() throws Exception { + ProvisionClusterRequest.init(blueprintFactory); + + expect(blueprintFactory.getBlueprint(BLUEPRINT_NAME)).andReturn(blueprint).once(); + expect(blueprint.getConfiguration()).andReturn(blueprintConfig).anyTimes(); + + replay(blueprintFactory, blueprint); + } + + @After + public void tearDown() { + reset(blueprintFactory, blueprint); + } + + @Test + public void test_basic() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + TopologyRequest provisionClusterRequest = new ProvisionClusterRequest(properties); + + assertEquals(CLUSTER_NAME, provisionClusterRequest.getClusterName()); + assertSame(blueprint, provisionClusterRequest.getBlueprint()); + Map<String, HostGroupInfo> hostGroupInfo = provisionClusterRequest.getHostGroupInfo(); + assertEquals(2, hostGroupInfo.size()); + + // group1 + // host info + HostGroupInfo group1Info = hostGroupInfo.get("group1"); + assertEquals("group1", group1Info.getHostGroupName()); + assertEquals(1, group1Info.getHostNames().size()); + assertTrue(group1Info.getHostNames().contains("host1.myDomain.com")); + assertEquals(1, group1Info.getRequestedHostCount()); + assertNull(group1Info.getPredicate()); + // configuration + Configuration group1Configuration = group1Info.getConfiguration(); + assertNull(group1Configuration.getParentConfiguration()); + assertEquals(1, group1Configuration.getProperties().size()); + Map<String, String> group1TypeProperties = group1Configuration.getProperties().get("foo-type"); + assertEquals(2, group1TypeProperties.size()); + assertEquals("prop1Value", group1TypeProperties.get("hostGroup1Prop1")); + assertEquals("prop2Value", group1TypeProperties.get("hostGroup1Prop2")); + assertTrue(group1Configuration.getAttributes().isEmpty()); + + // group2 + HostGroupInfo group2Info = hostGroupInfo.get("group2"); + assertEquals("group2", group2Info.getHostGroupName()); + assertTrue(group2Info.getHostNames().isEmpty()); + assertEquals(5, group2Info.getRequestedHostCount()); + assertNotNull(group2Info.getPredicate()); + // configuration + Configuration group2Configuration = group2Info.getConfiguration(); + assertNull(group2Configuration.getParentConfiguration()); + assertEquals(1, group2Configuration.getProperties().size()); + Map<String, String> group2TypeProperties = group2Configuration.getProperties().get("foo-type"); + assertEquals(1, group2TypeProperties.size()); + assertEquals("prop1Value", group2TypeProperties.get("hostGroup2Prop1")); + //attributes + Map<String, Map<String, Map<String, String>>> group2Attributes = group2Configuration.getAttributes(); + assertEquals(1, group2Attributes.size()); + Map<String, Map<String, String>> group2Type1Attributes = group2Attributes.get("foo-type"); + assertEquals(1, group2Type1Attributes.size()); + Map<String, String> group2Type1Prop1Attributes = group2Type1Attributes.get("hostGroup2Prop10"); + assertEquals(1, group2Type1Prop1Attributes.size()); + assertEquals("attribute1Prop10-value", group2Type1Prop1Attributes.get("attribute1")); + + // cluster scoped configuration + Configuration clusterScopeConfiguration = provisionClusterRequest.getConfiguration(); + assertSame(blueprintConfig, clusterScopeConfiguration.getParentConfiguration()); + assertEquals(1, clusterScopeConfiguration.getProperties().size()); + Map<String, String> clusterScopedProperties = clusterScopeConfiguration.getProperties().get("someType"); + assertEquals(1, clusterScopedProperties.size()); + assertEquals("someValue", clusterScopedProperties.get("property1")); + // attributes + Map<String, Map<String, Map<String, String>>> clusterScopedAttributes = clusterScopeConfiguration.getAttributes(); + assertEquals(1, clusterScopedAttributes.size()); + Map<String, Map<String, String>> clusterScopedTypeAttributes = clusterScopedAttributes.get("someType"); + assertEquals(1, clusterScopedTypeAttributes.size()); + Map<String, String> clusterScopedTypePropertyAttributes = clusterScopedTypeAttributes.get("property1"); + assertEquals(1, clusterScopedTypePropertyAttributes.size()); + assertEquals("someAttributePropValue", clusterScopedTypePropertyAttributes.get("attribute1")); + + verify(blueprintFactory, blueprint); + } + + @Test(expected= InvalidTopologyTemplateException.class) + public void test_NoHostGroupInfo() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + ((Collection)properties.get("host_groups")).clear(); + + // should result in an exception + new ProvisionClusterRequest(properties); + } + + @Test(expected= InvalidTopologyTemplateException.class) + public void test_GroupInfoMissingName() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + ((Collection<Map<String, Object>>)properties.get("host_groups")).iterator().next().remove("name"); + + // should result in an exception + new ProvisionClusterRequest(properties); + } + + @Test(expected= InvalidTopologyTemplateException.class) + public void test_NoHostsInfo() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + ((Collection<Map<String, Object>>)properties.get("host_groups")).iterator().next().remove("hosts"); + + // should result in an exception + new ProvisionClusterRequest(properties); + } + + @Test(expected = InvalidTopologyTemplateException.class) + public void test_NoHostNameOrHostCount() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + // remove fqdn property for a group that contains fqdn not host_count + for (Map<String, Object> groupProps : (Collection<Map<String, Object>>) properties.get("host_groups")) { + Collection<Map<String, Object>> hostInfo = (Collection<Map<String, Object>>) groupProps.get("hosts"); + Map<String, Object> next = hostInfo.iterator().next(); + if (next.containsKey("fqdn")) { + next.remove("fqdn"); + break; + } + } + + // should result in an exception + new ProvisionClusterRequest(properties); + } + + @Test + public void testGetValidators_noDefaultPassword() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + //properties.put("default_password", "pwd"); + TopologyRequest request = new ProvisionClusterRequest(properties); + List<TopologyValidator> validators = request.getTopologyValidators(); + + assertEquals(1, validators.size()); + TopologyValidator pwdValidator = validators.get(0); + + TopologyValidator noDefaultPwdValidator = new RequiredPasswordValidator(null); + assertEquals(pwdValidator, noDefaultPwdValidator); + } + + @Test + public void testGetValidators_defaultPassword() throws Exception { + Map<String, Object> properties = createBlueprintRequestProperties(CLUSTER_NAME, BLUEPRINT_NAME); + properties.put("default_password", "pwd"); + TopologyRequest request = new ProvisionClusterRequest(properties); + List<TopologyValidator> validators = request.getTopologyValidators(); + + assertEquals(1, validators.size()); + TopologyValidator pwdValidator = validators.get(0); + + TopologyValidator defaultPwdValidator = new RequiredPasswordValidator("pwd"); + assertEquals(pwdValidator, defaultPwdValidator); + } + + + public static Map<String, Object> createBlueprintRequestProperties(String clusterName, String blueprintName) { + Map<String, Object> properties = new LinkedHashMap<String, Object>(); + + properties.put(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID, clusterName); + properties.put(ClusterResourceProvider.BLUEPRINT_PROPERTY_ID, blueprintName); + + Collection<Map<String, Object>> hostGroups = new ArrayList<Map<String, Object>>(); + properties.put("host_groups", hostGroups); + + // host group 1 + Map<String, Object> hostGroup1Properties = new HashMap<String, Object>(); + hostGroups.add(hostGroup1Properties); + hostGroup1Properties.put("name", "group1"); + Collection<Map<String, String>> hostGroup1Hosts = new ArrayList<Map<String, String>>(); + hostGroup1Properties.put("hosts", hostGroup1Hosts); + Map<String, String> hostGroup1HostProperties = new HashMap<String, String>(); + hostGroup1HostProperties.put("fqdn", "host1.myDomain.com"); + hostGroup1Hosts.add(hostGroup1HostProperties); + // host group 1 scoped configuration + // version 1 configuration syntax + Collection<Map<String, String>> hostGroup1Configurations = new ArrayList<Map<String, String>>(); + hostGroup1Properties.put("configurations", hostGroup1Configurations); + Map<String, String> hostGroup1Configuration1 = new HashMap<String, String>(); + hostGroup1Configuration1.put("foo-type/hostGroup1Prop1", "prop1Value"); + hostGroup1Configuration1.put("foo-type/hostGroup1Prop2", "prop2Value"); + hostGroup1Configurations.add(hostGroup1Configuration1); + + // host group 2 + Map<String, Object> hostGroup2Properties = new HashMap<String, Object>(); + hostGroups.add(hostGroup2Properties); + hostGroup2Properties.put("name", "group2"); + Collection<Map<String, String>> hostGroup2Hosts = new ArrayList<Map<String, String>>(); + hostGroup2Properties.put("hosts", hostGroup2Hosts); + Map<String, String> hostGroup2HostProperties = new HashMap<String, String>(); + hostGroup2HostProperties.put("host_count", "5"); + hostGroup2HostProperties.put("host_predicate", "Hosts/host_name=myTestHost"); + hostGroup2Hosts.add(hostGroup2HostProperties); + // host group 2 scoped configuration + // version 2 configuration syntax + Collection<Map<String, String>> hostGroup2Configurations = new ArrayList<Map<String, String>>(); + hostGroup2Properties.put("configurations", hostGroup2Configurations); + Map<String, String> hostGroup2Configuration1 = new HashMap<String, String>(); + hostGroup2Configuration1.put("foo-type/properties/hostGroup2Prop1", "prop1Value"); + hostGroup2Configuration1.put("foo-type/properties_attributes/attribute1/hostGroup2Prop10", "attribute1Prop10-value"); + hostGroup2Configurations.add(hostGroup2Configuration1); + + // cluster scoped configuration + Collection<Map<String, String>> clusterConfigurations = new ArrayList<Map<String, String>>(); + properties.put("configurations", clusterConfigurations); + + Map<String, String> clusterConfigurationProperties = new HashMap<String, String>(); + clusterConfigurations.add(clusterConfigurationProperties); + clusterConfigurationProperties.put("someType/properties/property1", "someValue"); + clusterConfigurationProperties.put("someType/properties_attributes/attribute1/property1", "someAttributePropValue"); + + return properties; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java index 64210db..ed8336e 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java @@ -18,6 +18,8 @@ package org.apache.ambari.server.controller.internal; +import static org.easymock.EasyMock.and; +import static org.easymock.EasyMock.anyLong; import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.capture; import static org.easymock.EasyMock.createMock; @@ -63,7 +65,10 @@ import org.apache.ambari.server.orm.dao.RequestDAO; import org.apache.ambari.server.orm.entities.RequestEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.topology.LogicalRequest; +import org.apache.ambari.server.topology.TopologyManager; import org.easymock.Capture; +import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -75,14 +80,26 @@ public class RequestResourceProviderTest { private RequestDAO requestDAO; private HostRoleCommandDAO hrcDAO; + private TopologyManager topologyManager; @Before public void before() throws Exception { requestDAO = createNiceMock(RequestDAO.class); hrcDAO = createNiceMock(HostRoleCommandDAO.class); + topologyManager = createNiceMock(TopologyManager.class); - // !!! don't mess with injectors for this test + //todo: add assertions for topology manager interactions + expect(topologyManager.getStageSummaries(EasyMock.<Long>anyObject())).andReturn( + Collections.<Long, HostRoleCommandStatusSummaryDTO>emptyMap()).anyTimes(); + + expect(topologyManager.getRequests(EasyMock.<Collection<Long>>anyObject())).andReturn( + Collections.<LogicalRequest>emptyList()).anyTimes(); + + replay(topologyManager); + + + // !!! don't mess with injectors for this test Field field = RequestResourceProvider.class.getDeclaredField("s_requestDAO"); field.setAccessible(true); field.set(null, requestDAO); @@ -90,6 +107,10 @@ public class RequestResourceProviderTest { field = RequestResourceProvider.class.getDeclaredField("s_hostRoleCommandDAO"); field.setAccessible(true); field.set(null, hrcDAO); + + field = RequestResourceProvider.class.getDeclaredField("topologyManager"); + field.setAccessible(true); + field.set(null, topologyManager); } @@ -100,6 +121,7 @@ public class RequestResourceProviderTest { AmbariManagementController managementController = createMock(AmbariManagementController.class); RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + // replay replay(managementController, response); http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java index d79f809..6ae51da 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java @@ -76,6 +76,8 @@ import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; import org.apache.ambari.server.state.StackId; import org.apache.ambari.server.state.stack.upgrade.Direction; +import org.apache.ambari.server.topology.TopologyManager; +import org.apache.ambari.server.utils.StageUtils; import org.apache.ambari.server.view.ViewRegistry; import org.easymock.EasyMock; import org.junit.After; @@ -198,6 +200,10 @@ public class UpgradeResourceProviderTest { component = service.addServiceComponent("ZOOKEEPER_CLIENT"); sch = component.addServiceComponentHost("h1"); sch.setVersion("2.1.1.0"); + + TopologyManager topologyManager = new TopologyManager(); + StageUtils.setTopologyManager(topologyManager); + ActionManager.setTopologyManager(topologyManager); } @After http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/BlueprintEntityTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/BlueprintEntityTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/BlueprintEntityTest.java index 2587f6d..c660d19 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/BlueprintEntityTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/BlueprintEntityTest.java @@ -18,30 +18,16 @@ package org.apache.ambari.server.orm.entities; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import java.util.ArrayList; + import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.apache.ambari.server.api.services.AmbariMetaInfo; -import org.apache.ambari.server.state.PropertyInfo; -import org.apache.ambari.server.state.ServiceInfo; import org.junit.Before; import org.junit.Test; -import com.google.gson.Gson; /** * BlueprintEntity unit tests @@ -86,219 +72,4 @@ public class BlueprintEntityTest { entity.setConfigurations(configurations); assertSame(configurations, entity.getConfigurations()); } - - @Test - public void testValidateConfigurations_clusterConfig() throws Exception { - AmbariMetaInfo metaInfo = createMock(AmbariMetaInfo.class); - ServiceInfo service = new ServiceInfo(); - service.setName("service1"); - - List<PropertyInfo> serviceProperties = new ArrayList<PropertyInfo>(); - - PropertyInfo prop = new PropertyInfo(); - prop.setFilename("core-site.xml"); - prop.setName("super.secret.password"); - prop.setRequireInput(true); - Set<PropertyInfo.PropertyType> propertyTypes = new HashSet<PropertyInfo.PropertyType>(); - propertyTypes.add(PropertyInfo.PropertyType.PASSWORD); - prop.setPropertyTypes(propertyTypes); - prop.setValue(null); - serviceProperties.add(prop); - service.getProperties().addAll(serviceProperties); - service.getProperties().addAll(serviceProperties); - - BlueprintEntity entity = new BlueprintEntity(); - entity.setStack(stackEntity); - - Collection<BlueprintConfigEntity> configurations = new HashSet<BlueprintConfigEntity>(); - BlueprintConfigEntity configEntity = new BlueprintConfigEntity(); - configEntity.setBlueprintEntity(entity); - configEntity.setBlueprintName("blueprint"); - configEntity.setType("core-site"); - - Map<String, String> configData = new HashMap<String, String>(); - configData.put("foo", "val1"); - configData.put("bar", "val2"); - configData.put("super.secret.password", "password"); - configEntity.setConfigData(new Gson().toJson(configData)); - - configurations.add(configEntity); - entity.setConfigurations(configurations); - - Collection<HostGroupEntity> hostGroupEntities = new HashSet<HostGroupEntity>(); - HostGroupEntity hostGroupEntity = new HostGroupEntity(); - hostGroupEntity.setName("group1"); - Collection<HostGroupComponentEntity> hostGroupComponents = new HashSet<HostGroupComponentEntity>(); - HostGroupComponentEntity componentEntity = new HostGroupComponentEntity(); - componentEntity.setName("component1"); - componentEntity.setBlueprintName("blueprint"); - componentEntity.setHostGroupEntity(hostGroupEntity); - componentEntity.setHostGroupName("group1"); - hostGroupComponents.add(componentEntity); - hostGroupEntity.setComponents(hostGroupComponents); - hostGroupEntity.setConfigurations(Collections.<HostGroupConfigEntity>emptyList()); - hostGroupEntities.add(hostGroupEntity); - entity.setHostGroups(hostGroupEntities); - - expect(metaInfo.getComponentToService("HDP", "2.0.6", "component1")).andReturn("service1"); - expect(metaInfo.getService("HDP", "2.0.6", "service1")).andReturn(service); - - replay(metaInfo); - - Map<String, Map<String, Collection<String>>> missingProps = entity.validateConfigurations( - metaInfo, true); - - assertTrue(missingProps.isEmpty()); - - verify(metaInfo); - } - - @Test - public void testValidateConfigurations_hostGroupConfig() throws Exception { - AmbariMetaInfo metaInfo = createMock(AmbariMetaInfo.class); - ServiceInfo service = new ServiceInfo(); - service.setName("service1"); - - List<PropertyInfo> serviceProperties = new ArrayList<PropertyInfo>(); - PropertyInfo prop1 = new PropertyInfo(); - prop1.setFilename("core-site.xml"); - prop1.setName("super.secret.password"); - prop1.setRequireInput(true); - Set<PropertyInfo.PropertyType> propertyTypes = new HashSet<PropertyInfo.PropertyType>(); - propertyTypes.add(PropertyInfo.PropertyType.PASSWORD); - prop1.setPropertyTypes(propertyTypes); - prop1.setValue(null); - serviceProperties.add(prop1); - service.getProperties().addAll(serviceProperties); - - BlueprintEntity entity = new BlueprintEntity(); - entity.setStack(stackEntity); - - entity.setConfigurations(Collections.<BlueprintConfigEntity>emptyList()); - - Collection<HostGroupEntity> hostGroupEntities = new HashSet<HostGroupEntity>(); - HostGroupEntity hostGroupEntity = new HostGroupEntity(); - hostGroupEntity.setName("group1"); - Collection<HostGroupComponentEntity> hostGroupComponents = new HashSet<HostGroupComponentEntity>(); - HostGroupComponentEntity componentEntity = new HostGroupComponentEntity(); - componentEntity.setName("component1"); - componentEntity.setBlueprintName("blueprint"); - componentEntity.setHostGroupEntity(hostGroupEntity); - componentEntity.setHostGroupName("group1"); - hostGroupComponents.add(componentEntity); - hostGroupEntity.setComponents(hostGroupComponents); - - Collection<HostGroupConfigEntity> configurations = new HashSet<HostGroupConfigEntity>(); - HostGroupConfigEntity configEntity = new HostGroupConfigEntity(); - configEntity.setHostGroupEntity(hostGroupEntity); - configEntity.setBlueprintName("blueprint"); - configEntity.setType("core-site"); - - Map<String, String> configData = new HashMap<String, String>(); - configData.put("foo", "val1"); - configData.put("bar", "val2"); - configData.put("super.secret.password", "password"); - configEntity.setConfigData(new Gson().toJson(configData)); - configurations.add(configEntity); - - hostGroupEntity.setConfigurations(configurations); - hostGroupEntities.add(hostGroupEntity); - entity.setHostGroups(hostGroupEntities); - - expect(metaInfo.getComponentToService("HDP", "2.0.6", "component1")).andReturn("service1"); - expect(metaInfo.getService("HDP", "2.0.6", "service1")).andReturn(service); - - replay(metaInfo); - - Map<String, Map<String, Collection<String>>> missingProps = entity.validateConfigurations( - metaInfo, true); - - assertTrue(missingProps.isEmpty()); - - verify(metaInfo); - } - - @Test - public void testValidateConfigurations_negative() throws Exception { - AmbariMetaInfo metaInfo = createMock(AmbariMetaInfo.class); - ServiceInfo service = new ServiceInfo(); - service.setName("service1"); - - List<PropertyInfo> serviceProperties = new ArrayList<PropertyInfo>(); - - PropertyInfo prop1 = new PropertyInfo(); - prop1.setFilename("core-site.xml"); - prop1.setName("super.secret.password"); - prop1.setRequireInput(true); - Set<PropertyInfo.PropertyType> propertyTypes = new HashSet<PropertyInfo.PropertyType>(); - propertyTypes.add(PropertyInfo.PropertyType.PASSWORD); - prop1.setPropertyTypes(propertyTypes); - prop1.setValue(null); - serviceProperties.add(prop1); - - PropertyInfo prop2 = new PropertyInfo(); - prop2.setFilename("global.xml"); - prop2.setName("another.super.secret.password"); - prop2.setRequireInput(true); - Set<PropertyInfo.PropertyType> propertyTypes2 = new HashSet<PropertyInfo.PropertyType>(); - propertyTypes2.add(PropertyInfo.PropertyType.PASSWORD); - prop2.setPropertyTypes(propertyTypes2); - prop2.setValue(" "); - serviceProperties.add(prop2); - - service.getProperties().addAll(serviceProperties); - - BlueprintEntity entity = new BlueprintEntity(); - entity.setStack(stackEntity); - - Collection<BlueprintConfigEntity> configurations = new HashSet<BlueprintConfigEntity>(); - BlueprintConfigEntity configEntity = new BlueprintConfigEntity(); - configEntity.setBlueprintEntity(entity); - configEntity.setBlueprintName("blueprint"); - configEntity.setType("core-site"); - - Map<String, String> configData = new HashMap<String, String>(); - configData.put("foo", "val1"); - configData.put("bar", "val2"); - configData.put("some.other.secret.password", "password"); - configEntity.setConfigData(new Gson().toJson(configData)); - - configurations.add(configEntity); - entity.setConfigurations(configurations); - - Collection<HostGroupEntity> hostGroupEntities = new HashSet<HostGroupEntity>(); - HostGroupEntity hostGroupEntity = new HostGroupEntity(); - hostGroupEntity.setName("hg1"); - Collection<HostGroupComponentEntity> hostGroupComponents = new HashSet<HostGroupComponentEntity>(); - HostGroupComponentEntity componentEntity = new HostGroupComponentEntity(); - componentEntity.setName("component1"); - componentEntity.setBlueprintName("blueprint"); - componentEntity.setHostGroupEntity(hostGroupEntity); - componentEntity.setHostGroupName("hg1"); - hostGroupComponents.add(componentEntity); - hostGroupEntity.setComponents(hostGroupComponents); - hostGroupEntity.setConfigurations(Collections.<HostGroupConfigEntity>emptyList()); - hostGroupEntities.add(hostGroupEntity); - entity.setHostGroups(hostGroupEntities); - - expect(metaInfo.getComponentToService("HDP", "2.0.6", "component1")).andReturn( - "service1"); - expect(metaInfo.getService("HDP", "2.0.6", "service1")).andReturn(service); - - replay(metaInfo); - - Map<String, Map<String, Collection<String>>> missingProps = entity.validateConfigurations( - metaInfo, true); - - assertEquals(1, missingProps.size()); - Map<String, Collection<String>> typeProps = missingProps.get("hg1"); - assertEquals(2, typeProps.size()); - assertEquals(1, typeProps.get("global").size()); - assertEquals(1, typeProps.get("core-site").size()); - - assertTrue(typeProps.get("core-site").contains("super.secret.password")); - assertTrue(typeProps.get("global").contains("another.super.secret.password")); - - verify(metaInfo); - } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c9f0dd0b/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java new file mode 100644 index 0000000..cd465cf --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java @@ -0,0 +1,235 @@ +/** + * 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.topology; + +import org.apache.ambari.server.controller.internal.BlueprintResourceProvider; +import org.apache.ambari.server.controller.internal.BlueprintResourceProviderTest; +import org.apache.ambari.server.controller.internal.Stack; +import org.apache.ambari.server.orm.dao.BlueprintDAO; +import org.apache.ambari.server.orm.entities.BlueprintConfigEntity; +import org.apache.ambari.server.orm.entities.BlueprintEntity; +import org.apache.ambari.server.stack.NoSuchStackException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.powermock.api.easymock.PowerMock.createStrictMock; +import static org.powermock.api.easymock.PowerMock.replay; +import static org.powermock.api.easymock.PowerMock.reset; +import static org.powermock.api.easymock.PowerMock.verify; + +/** + * BlueprintFactory unit tests. + */ +@SuppressWarnings("unchecked") +public class BlueprintFactoryTest { + + private static final String BLUEPRINT_NAME = "test-blueprint"; + + BlueprintFactory factory = new BlueprintFactory(); + Stack stack = createNiceMock(Stack.class); + BlueprintFactory testFactory = new TestBlueprintFactory(stack); + BlueprintDAO dao = createStrictMock(BlueprintDAO.class); + BlueprintEntity entity = createStrictMock(BlueprintEntity.class); + BlueprintConfigEntity configEntity = createStrictMock(BlueprintConfigEntity.class); + + + @Before + public void init() throws Exception { + setPrivateField(factory, "blueprintDAO", dao); + + Map<String, Collection<String>> componentMap = new HashMap<String, Collection<String>>(); + Collection<String> components1 = new HashSet<String>(); + componentMap.put("test-service1", components1); + components1.add("component1"); + Collection<String> components2 = new HashSet<String>(); + componentMap.put("test-service2", components2); + components2.add("component2"); + + expect(stack.getComponents()).andReturn(componentMap).anyTimes(); + expect(stack.isMasterComponent("component1")).andReturn(true).anyTimes(); + expect(stack.isMasterComponent("component2")).andReturn(false).anyTimes(); + expect(stack.getServiceForComponent("component1")).andReturn("test-service1").anyTimes(); + expect(stack.getServiceForComponent("component2")).andReturn("test-service2").anyTimes(); + } + + @After + public void tearDown() { + reset(stack, dao, entity, configEntity); + } + + //todo: implement +// @Test +// public void testGetBlueprint() throws Exception { +// +// Collection<BlueprintConfigEntity> configs = new ArrayList<BlueprintConfigEntity>(); +// configs.add(configEntity); +// +// expect(dao.findByName(BLUEPRINT_NAME)).andReturn(entity).once(); +// expect(entity.getBlueprintName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); +// expect(entity.getConfigurations()).andReturn(configs).atLeastOnce(); +// +// replay(dao, entity); +// +// Blueprint blueprint = factory.getBlueprint(BLUEPRINT_NAME); +// +// +// } + + @Test + public void testGetBlueprint_NotFound() throws Exception { + expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null).once(); + replay(dao, entity, configEntity); + + assertNull(factory.getBlueprint(BLUEPRINT_NAME)); + } + + @Test + public void testCreateBlueprint() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + + replay(stack, dao, entity, configEntity); + Blueprint blueprint = testFactory.createBlueprint(props); + + assertEquals(BLUEPRINT_NAME, blueprint.getName()); + assertSame(stack, blueprint.getStack()); + assertEquals(2, blueprint.getHostGroups().size()); + + Map<String, HostGroup> hostGroups = blueprint.getHostGroups(); + HostGroup group1 = hostGroups.get("group1"); + assertEquals("group1", group1.getName()); + assertEquals("1", group1.getCardinality()); + Collection<String> components = group1.getComponents(); + assertEquals(2, components.size()); + assertTrue(components.contains("component1")); + assertTrue(components.contains("component2")); + Collection<String> services = group1.getServices(); + assertEquals(2, services.size()); + assertTrue(services.contains("test-service1")); + assertTrue(services.contains("test-service2")); + assertTrue(group1.containsMasterComponent()); + //todo: add configurations/attributes to properties + Configuration configuration = group1.getConfiguration(); + assertTrue(configuration.getProperties().isEmpty()); + assertTrue(configuration.getAttributes().isEmpty()); + + HostGroup group2 = hostGroups.get("group2"); + assertEquals("group2", group2.getName()); + assertEquals("2", group2.getCardinality()); + components = group2.getComponents(); + assertEquals(1, components.size()); + assertTrue(components.contains("component1")); + services = group2.getServices(); + assertEquals(1, services.size()); + assertTrue(services.contains("test-service1")); + assertTrue(group2.containsMasterComponent()); + //todo: add configurations/attributes to properties + //todo: test both v1 and v2 config syntax + configuration = group2.getConfiguration(); + assertTrue(configuration.getProperties().isEmpty()); + assertTrue(configuration.getAttributes().isEmpty()); + + verify(dao, entity, configEntity); + } + + @Test(expected=IllegalArgumentException.class) + public void testCreate_NoBlueprintName() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + props.remove(BlueprintResourceProvider.BLUEPRINT_NAME_PROPERTY_ID); + + replay(stack, dao, entity, configEntity); + testFactory.createBlueprint(props); + } + + @Test(expected=IllegalArgumentException.class) + public void testCreate_NoHostGroups() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + // remove all host groups + ((Set<Map<String, Object>>) props.get(BlueprintResourceProvider.HOST_GROUP_PROPERTY_ID)).clear(); + + replay(stack, dao, entity, configEntity); + testFactory.createBlueprint(props); + } + + @Test(expected=IllegalArgumentException.class) + public void testCreate_MissingHostGroupName() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + // remove the name property for one of the host groups + ((Set<Map<String, Object>>) props.get(BlueprintResourceProvider.HOST_GROUP_PROPERTY_ID)).iterator().next().remove("name"); + + replay(stack, dao, entity, configEntity); + testFactory.createBlueprint(props); + } + + @Test(expected=IllegalArgumentException.class) + public void testCreate_HostGroupWithNoComponents() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + // remove the components for one of the host groups + ((Set<Map<String, Object>>) props.get(BlueprintResourceProvider.HOST_GROUP_PROPERTY_ID)). + iterator().next().remove(BlueprintResourceProvider.COMPONENT_PROPERTY_ID); + + replay(stack, dao, entity, configEntity); + testFactory.createBlueprint(props); + } + + @Test(expected=IllegalArgumentException.class) + public void testCreate_HostGroupWithInvalidComponent() throws Exception { + Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next(); + // change a component name to an invalid name + ((Set<Map<String, Object>>) ((Set<Map<String, Object>>) props.get(BlueprintResourceProvider.HOST_GROUP_PROPERTY_ID)). + iterator().next().get(BlueprintResourceProvider.COMPONENT_PROPERTY_ID)).iterator().next().put("name", "INVALID_COMPONENT"); + + replay(stack, dao, entity, configEntity); + testFactory.createBlueprint(props); + } + + private class TestBlueprintFactory extends BlueprintFactory { + private Stack stack; + + public TestBlueprintFactory(Stack stack) { + this.stack = stack; + } + + @Override + protected Stack createStack(Map<String, Object> properties) throws NoSuchStackException { + return stack; + } + } + + + private void setPrivateField(Object o, String field, Object value) throws Exception { + Class<?> c = o.getClass(); + Field f = c.getDeclaredField(field); + f.setAccessible(true); + f.set(o, value); + } +}