ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jspei...@apache.org
Subject [1/2] AMBARI-5077. Provision a cluster via a single REST API invocation by specifying a blueprint.
Date Fri, 14 Mar 2014 22:44:18 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 08c7610d4 -> 4ccf720aa


http://git-wip-us.apache.org/repos/asf/ambari/blob/4ccf720a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
index 96a9814..a9ccd40 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
@@ -18,25 +18,46 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.createStrictMock;
 import static org.easymock.EasyMock.eq;
 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.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.*;
 
+import org.apache.ambari.server.api.services.PersistKeyValueImpl;
+import org.apache.ambari.server.api.services.PersistKeyValueService;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.ClusterRequest;
 import org.apache.ambari.server.controller.ClusterResponse;
+import org.apache.ambari.server.controller.ConfigurationRequest;
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.StackConfigurationRequest;
+import org.apache.ambari.server.controller.StackConfigurationResponse;
+import org.apache.ambari.server.controller.StackServiceComponentRequest;
+import org.apache.ambari.server.controller.StackServiceComponentResponse;
+import org.apache.ambari.server.controller.StackServiceRequest;
+import org.apache.ambari.server.controller.StackServiceResponse;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
+import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.orm.dao.BlueprintDAO;
+import org.apache.ambari.server.orm.entities.BlueprintEntity;
+import org.apache.ambari.server.orm.entities.HostGroupComponentEntity;
+import org.apache.ambari.server.orm.entities.HostGroupEntity;
+import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.junit.Assert;
 import org.junit.Test;
@@ -112,6 +133,305 @@ public class ClusterResourceProviderTest {
   }
 
   @Test
+  public void testCreateResource_blueprint() throws Exception {
+    String blueprintName = "test-blueprint";
+    String stackName = "test";
+    String stackVersion = "1.23";
+
+    BlueprintDAO blueprintDAO = createStrictMock(BlueprintDAO.class);
+    AmbariManagementController managementController = createStrictMock(AmbariManagementController.class);
+    Request request = createNiceMock(Request.class);
+    RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
+    BlueprintEntity blueprint = createNiceMock(BlueprintEntity.class);
+    StackServiceResponse stackServiceResponse1 = createNiceMock(StackServiceResponse.class);
+    StackServiceResponse stackServiceResponse2 = createNiceMock(StackServiceResponse.class);
+    Capture<Set<StackServiceRequest>> stackServiceRequestCapture = new Capture<Set<StackServiceRequest>>();
+
+    StackServiceComponentResponse stackServiceComponentResponse1 = createNiceMock(StackServiceComponentResponse.class);
+    StackServiceComponentResponse stackServiceComponentResponse2 = createNiceMock(StackServiceComponentResponse.class);
+    StackServiceComponentResponse stackServiceComponentResponse3 = createNiceMock(StackServiceComponentResponse.class);
+    Capture<Set<StackServiceComponentRequest>> serviceComponentRequestCapture1
= new Capture<Set<StackServiceComponentRequest>>();
+    Capture<Set<StackServiceComponentRequest>> serviceComponentRequestCapture2
= new Capture<Set<StackServiceComponentRequest>>();
+
+    StackConfigurationResponse stackConfigurationResponse1 = createNiceMock(StackConfigurationResponse.class);
+    StackConfigurationResponse stackConfigurationResponse2 = createNiceMock(StackConfigurationResponse.class);
+    StackConfigurationResponse stackConfigurationResponse3 = createNiceMock(StackConfigurationResponse.class);
+    Capture<Set<StackConfigurationRequest>> serviceConfigurationRequestCapture1
= new Capture<Set<StackConfigurationRequest>>();
+    Capture<Set<StackConfigurationRequest>> serviceConfigurationRequestCapture2
= new Capture<Set<StackConfigurationRequest>>();
+
+    HostGroupEntity hostGroup = createNiceMock(HostGroupEntity.class);
+    HostGroupComponentEntity hostGroupComponent1 = createNiceMock(HostGroupComponentEntity.class);
+    HostGroupComponentEntity hostGroupComponent2 = createNiceMock(HostGroupComponentEntity.class);
+    HostGroupComponentEntity hostGroupComponent3 = createNiceMock(HostGroupComponentEntity.class);
+
+    ServiceResourceProvider serviceResourceProvider = createStrictMock(ServiceResourceProvider.class);
+    ResourceProvider componentResourceProvider = createStrictMock(ResourceProvider.class);
+    ResourceProvider hostResourceProvider = createStrictMock(ResourceProvider.class);
+    ResourceProvider hostComponentResourceProvider = createStrictMock(ResourceProvider.class);
+    PersistKeyValueImpl persistKeyValue = createNiceMock(PersistKeyValueImpl.class);
+
+    Capture<ClusterRequest> createClusterRequestCapture = new Capture<ClusterRequest>();
+    Capture<Set<ClusterRequest>> updateClusterRequestCapture = new Capture<Set<ClusterRequest>>();
+    Capture<Map<String, String>> updateClusterPropertyMapCapture = new Capture<Map<String,
String>>();
+    Capture<Set<ClusterRequest>> updateClusterRequestCapture2 = new Capture<Set<ClusterRequest>>();
+    Capture<Map<String, String>> updateClusterPropertyMapCapture2 = new Capture<Map<String,
String>>();
+    Capture<Set<ClusterRequest>> updateClusterRequestCapture3 = new Capture<Set<ClusterRequest>>();
+    Capture<Map<String, String>> updateClusterPropertyMapCapture3 = new Capture<Map<String,
String>>();
+
+
+    Capture<Request> serviceRequestCapture = new Capture<Request>();
+    Capture<Request> componentRequestCapture = new Capture<Request>();
+    Capture<Request> componentRequestCapture2 = new Capture<Request>();
+    Capture<Request> hostRequestCapture = new Capture<Request>();
+    Capture<Request> hostComponentRequestCapture = new Capture<Request>();
+
+    Set<StackServiceResponse> stackServiceResponses = new LinkedHashSet<StackServiceResponse>();
+    stackServiceResponses.add(stackServiceResponse1);
+    stackServiceResponses.add(stackServiceResponse2);
+
+    // service1 has 2 components
+    Set<StackServiceComponentResponse> stackServiceComponentResponses1 = new LinkedHashSet<StackServiceComponentResponse>();
+    stackServiceComponentResponses1.add(stackServiceComponentResponse1);
+    stackServiceComponentResponses1.add(stackServiceComponentResponse2);
+
+    // service2 has 1 components
+    Set<StackServiceComponentResponse> stackServiceComponentResponses2 = new LinkedHashSet<StackServiceComponentResponse>();
+    stackServiceComponentResponses2.add(stackServiceComponentResponse3);
+
+    // service1 has 1 config
+    Set<StackConfigurationResponse> stackConfigurationResponses1 = new LinkedHashSet<StackConfigurationResponse>();
+    stackConfigurationResponses1.add(stackConfigurationResponse1);
+
+    // service2 has 2 config
+    Set<StackConfigurationResponse> stackConfigurationResponses2 = new LinkedHashSet<StackConfigurationResponse>();
+    stackConfigurationResponses2.add(stackConfigurationResponse2);
+    stackConfigurationResponses2.add(stackConfigurationResponse3);
+
+    Collection<HostGroupComponentEntity> hostGroupComponents = new LinkedHashSet<HostGroupComponentEntity>();
+    hostGroupComponents.add(hostGroupComponent1);
+    hostGroupComponents.add(hostGroupComponent2);
+    hostGroupComponents.add(hostGroupComponent3);
+
+    // request properties
+    Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
+    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+
+    properties.put(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID, "c1");
+    properties.put(ClusterResourceProvider.BLUEPRINT_PROPERTY_ID, blueprintName);
+    propertySet.add(properties);
+
+    Collection<Map<String, Object>> hostGroups = new ArrayList<Map<String,
Object>>();
+    Map<String, Object> hostGroupProperties = new HashMap<String, Object>();
+    hostGroups.add(hostGroupProperties);
+    hostGroupProperties.put("name", "group1");
+    Collection<Map<String, String>> hostGroupHosts = new ArrayList<Map<String,
String>>();
+    hostGroupProperties.put("hosts", hostGroupHosts);
+    Map<String, String> hostGroupHostProperties = new HashMap<String, String>();
+    hostGroupHostProperties.put("fqdn", "host.domain");
+    hostGroupHosts.add(hostGroupHostProperties);
+    properties.put("host-groups", hostGroups);
+
+    // expectations
+    expect(request.getProperties()).andReturn(propertySet).anyTimes();
+    expect(blueprintDAO.findByName(blueprintName)).andReturn(blueprint);
+    expect(blueprint.getStackName()).andReturn(stackName);
+    expect(blueprint.getStackVersion()).andReturn(stackVersion);
+
+    expect(managementController.getStackServices(capture(stackServiceRequestCapture))).andReturn(stackServiceResponses);
+    expect(stackServiceResponse1.getServiceName()).andReturn("service1");
+    expect(stackServiceResponse2.getServiceName()).andReturn("service2");
+
+    expect(managementController.getStackComponents(capture(serviceComponentRequestCapture1))).
+        andReturn(stackServiceComponentResponses1);
+    expect(stackServiceComponentResponse1.getComponentName()).andReturn("component1");
+    expect(stackServiceComponentResponse2.getComponentName()).andReturn("component2");
+
+    expect(managementController.getStackConfigurations(capture(serviceConfigurationRequestCapture1))).
+        andReturn(stackConfigurationResponses1);
+    expect(stackConfigurationResponse1.getType()).andReturn("core-site.xml");
+    expect(stackConfigurationResponse1.getPropertyName()).andReturn("property1");
+    expect(stackConfigurationResponse1.getPropertyValue()).andReturn("value1");
+
+    expect(managementController.getStackComponents(capture(serviceComponentRequestCapture2))).
+        andReturn(stackServiceComponentResponses2);
+    expect(stackServiceComponentResponse3.getComponentName()).andReturn("component3");
+
+    expect(managementController.getStackConfigurations(capture(serviceConfigurationRequestCapture2))).
+        andReturn(stackConfigurationResponses2);
+    expect(stackConfigurationResponse2.getType()).andReturn("hdfs-site.xml");
+    expect(stackConfigurationResponse2.getPropertyName()).andReturn("property2");
+    expect(stackConfigurationResponse2.getPropertyValue()).andReturn("value2");
+    expect(stackConfigurationResponse3.getType()).andReturn("core-site.xml");
+    expect(stackConfigurationResponse3.getPropertyName()).andReturn("property3");
+    expect(stackConfigurationResponse3.getPropertyValue()).andReturn("value3");
+
+    expect(blueprint.getHostGroups()).andReturn(Collections.singleton(hostGroup));
+    expect(hostGroup.getName()).andReturn("group1");
+    expect(hostGroup.getComponents()).andReturn(hostGroupComponents);
+    expect(hostGroupComponent1.getName()).andReturn("component1");
+    expect(hostGroupComponent2.getName()).andReturn("component2");
+    expect(hostGroupComponent3.getName()).andReturn("component3");
+
+    managementController.createCluster(capture(createClusterRequestCapture));
+    expect(managementController.updateClusters(capture(updateClusterRequestCapture),
+        capture(updateClusterPropertyMapCapture))).andReturn(null);
+    expect(managementController.updateClusters(capture(updateClusterRequestCapture2),
+        capture(updateClusterPropertyMapCapture2))).andReturn(null);
+    expect(managementController.updateClusters(capture(updateClusterRequestCapture3),
+        capture(updateClusterPropertyMapCapture3))).andReturn(null);
+
+    expect(serviceResourceProvider.createResources(capture(serviceRequestCapture))).andReturn(null);
+    expect(componentResourceProvider.createResources(capture(componentRequestCapture))).andReturn(null);
+    expect(componentResourceProvider.createResources(capture(componentRequestCapture2))).andReturn(null);
+    expect(hostResourceProvider.createResources(capture(hostRequestCapture))).andReturn(null);
+    expect(hostComponentResourceProvider.createResources(capture(hostComponentRequestCapture))).andReturn(null);
+
+    expect(serviceResourceProvider.installAndStart("c1")).andReturn(response);
+
+    persistKeyValue.put("CLUSTER_CURRENT_STATUS", "{\"clusterState\":\"CLUSTER_STARTED_5\"}");
+
+    replay(blueprintDAO, managementController, request, response, blueprint, stackServiceResponse1,
stackServiceResponse2,
+           stackServiceComponentResponse1, stackServiceComponentResponse2, stackServiceComponentResponse3,
+           stackConfigurationResponse1, stackConfigurationResponse2, stackConfigurationResponse3,
hostGroup,
+           hostGroupComponent1, hostGroupComponent2, hostGroupComponent3, serviceResourceProvider,
+           componentResourceProvider, hostResourceProvider, hostComponentResourceProvider,
persistKeyValue);
+
+    // test
+    ClusterResourceProvider.injectBlueprintDAO(blueprintDAO);
+    PersistKeyValueService.init(persistKeyValue);
+    ResourceProvider provider = new TestClusterResourceProvider(
+        managementController, serviceResourceProvider, componentResourceProvider,
+        hostResourceProvider, hostComponentResourceProvider);
+
+    RequestStatus requestStatus = provider.createResources(request);
+
+    assertEquals(RequestStatus.Status.InProgress, requestStatus.getStatus());
+
+    Set<StackServiceRequest> stackServiceRequests = stackServiceRequestCapture.getValue();
+    assertEquals(1, stackServiceRequests.size());
+    StackServiceRequest ssr = stackServiceRequests.iterator().next();
+    assertNull(ssr.getServiceName());
+    assertEquals("test", ssr.getStackName());
+    assertEquals("1.23", ssr.getStackVersion());
+
+    Set<StackServiceComponentRequest> stackServiceComponentRequests1 = serviceComponentRequestCapture1.getValue();
+    Set<StackServiceComponentRequest> stackServiceComponentRequests2 = serviceComponentRequestCapture2.getValue();
+    assertEquals(1, stackServiceComponentRequests1.size());
+    assertEquals(1, stackServiceComponentRequests2.size());
+    StackServiceComponentRequest scr1 = stackServiceComponentRequests1.iterator().next();
+    StackServiceComponentRequest scr2 = stackServiceComponentRequests2.iterator().next();
+    assertNull(scr1.getComponentName());
+    assertNull(scr2.getComponentName());
+    assertEquals("1.23", scr1.getStackVersion());
+    assertEquals("1.23", scr2.getStackVersion());
+    assertEquals("test", scr1.getStackName());
+    assertEquals("test", scr2.getStackName());
+    assertTrue(scr1.getServiceName().equals("service1") || scr1.getServiceName().equals("service2"));
+    assertTrue(scr2.getServiceName().equals("service1") || scr2.getServiceName().equals("service2")
&&
+        ! scr2.getServiceName().equals(scr1.getServiceName()));
+
+    Set<StackConfigurationRequest> serviceConfigurationRequest1 = serviceConfigurationRequestCapture1.getValue();
+    Set<StackConfigurationRequest> serviceConfigurationRequest2 = serviceConfigurationRequestCapture2.getValue();
+    assertEquals(1, serviceConfigurationRequest1.size());
+    assertEquals(1, serviceConfigurationRequest2.size());
+    StackConfigurationRequest configReq1 = serviceConfigurationRequest1.iterator().next();
+    StackConfigurationRequest configReq2 = serviceConfigurationRequest2.iterator().next();
+    assertNull(configReq1.getPropertyName());
+    assertNull(configReq2.getPropertyName());
+    assertEquals("1.23", configReq1.getStackVersion());
+    assertEquals("1.23", configReq2.getStackVersion());
+    assertEquals("test", configReq1.getStackName());
+    assertEquals("test", configReq2.getStackName());
+    assertTrue(configReq1.getServiceName().equals("service1") || configReq1.getServiceName().equals("service2"));
+    assertTrue(configReq2.getServiceName().equals("service1") || configReq2.getServiceName().equals("service2")
&&
+        ! configReq2.getServiceName().equals(configReq1.getServiceName()));
+
+    ClusterRequest clusterRequest = createClusterRequestCapture.getValue();
+    assertEquals("c1", clusterRequest.getClusterName());
+    assertEquals("test-1.23", clusterRequest.getStackVersion());
+
+    Set<ClusterRequest> updateClusterRequest1 = updateClusterRequestCapture.getValue();
+    Set<ClusterRequest> updateClusterRequest2 = updateClusterRequestCapture2.getValue();
+    Set<ClusterRequest> updateClusterRequest3 = updateClusterRequestCapture3.getValue();
+    assertEquals(1, updateClusterRequest1.size());
+    assertEquals(1, updateClusterRequest2.size());
+    assertEquals(1, updateClusterRequest3.size());
+    ClusterRequest ucr1 = updateClusterRequest1.iterator().next();
+    ClusterRequest ucr2 = updateClusterRequest2.iterator().next();
+    ClusterRequest ucr3 = updateClusterRequest3.iterator().next();
+    assertEquals("c1", ucr1.getClusterName());
+    assertEquals("c1", ucr2.getClusterName());
+    assertEquals("c1", ucr3.getClusterName());
+    ConfigurationRequest cr1 = ucr1.getDesiredConfig();
+    ConfigurationRequest cr2 = ucr2.getDesiredConfig();
+    ConfigurationRequest cr3 = ucr3.getDesiredConfig();
+    assertEquals("1", cr1.getVersionTag());
+    assertEquals("1", cr2.getVersionTag());
+    assertEquals("1", cr3.getVersionTag());
+    Map<String, ConfigurationRequest> mapConfigRequests = new HashMap<String, ConfigurationRequest>();
+    mapConfigRequests.put(cr1.getType(), cr1);
+    mapConfigRequests.put(cr2.getType(), cr2);
+    mapConfigRequests.put(cr3.getType(), cr3);
+    assertEquals(3, mapConfigRequests.size());
+    ConfigurationRequest globalConfigRequest = mapConfigRequests.get("global");
+    assertEquals(4, globalConfigRequest.getProperties().size());
+    assertEquals("hadoop", globalConfigRequest.getProperties().get("user_group"));
+    assertEquals("ambari-qa", globalConfigRequest.getProperties().get("smokeuser"));
+    assertEquals("default@REPLACEME.NOWHERE", globalConfigRequest.getProperties().get("nagios_contact"));
+    assertEquals("admin", globalConfigRequest.getProperties().get("nagios_web_password"));
+    ConfigurationRequest hdfsConfigRequest = mapConfigRequests.get("hdfs-site");
+    assertEquals(1, hdfsConfigRequest.getProperties().size());
+    assertEquals("value2", hdfsConfigRequest.getProperties().get("property2"));
+    ConfigurationRequest coreConfigRequest = mapConfigRequests.get("core-site");
+    assertEquals(2, coreConfigRequest.getProperties().size());
+    assertEquals("value1", coreConfigRequest.getProperties().get("property1"));
+    assertEquals("value3", coreConfigRequest.getProperties().get("property3"));
+    assertNull(updateClusterPropertyMapCapture.getValue());
+    assertNull(updateClusterPropertyMapCapture2.getValue());
+    assertNull(updateClusterPropertyMapCapture3.getValue());
+
+    Request serviceRequest = serviceRequestCapture.getValue();
+    assertEquals(2, serviceRequest.getProperties().size());
+    Request componentRequest = componentRequestCapture.getValue();
+    Request componentRequest2 = componentRequestCapture2.getValue();
+    assertEquals(2, componentRequest.getProperties().size());
+    Set<String> componentRequest1Names = new HashSet<String>();
+    for (Map<String, Object> componentRequest1Properties : componentRequest.getProperties())
{
+      assertEquals(3, componentRequest1Properties.size());
+      assertEquals("c1", componentRequest1Properties.get("ServiceComponentInfo/cluster_name"));
+      assertEquals("service1", componentRequest1Properties.get("ServiceComponentInfo/service_name"));
+      componentRequest1Names.add((String) componentRequest1Properties.get("ServiceComponentInfo/component_name"));
+    }
+    assertTrue(componentRequest1Names.contains("component1") && componentRequest1Names.contains("component2"));
+    assertEquals(1, componentRequest2.getProperties().size());
+    Map<String, Object> componentRequest2Properties = componentRequest2.getProperties().iterator().next();
+    assertEquals("c1", componentRequest2Properties.get("ServiceComponentInfo/cluster_name"));
+    assertEquals("service2", componentRequest2Properties.get("ServiceComponentInfo/service_name"));
+    assertEquals("component3", componentRequest2Properties.get("ServiceComponentInfo/component_name"));
+    Request hostRequest = hostRequestCapture.getValue();
+    assertEquals(1, hostRequest.getProperties().size());
+    assertEquals("c1", hostRequest.getProperties().iterator().next().get("Hosts/cluster_name"));
+    assertEquals("host.domain", hostRequest.getProperties().iterator().next().get("Hosts/host_name"));
+    Request hostComponentRequest = hostComponentRequestCapture.getValue();
+    assertEquals(3, hostComponentRequest.getProperties().size());
+    Set<String> componentNames = new HashSet<String>();
+    for (Map<String, Object> hostComponentProperties : hostComponentRequest.getProperties())
{
+      assertEquals(3, hostComponentProperties.size());
+      assertEquals("c1", hostComponentProperties.get("HostRoles/cluster_name"));
+      assertEquals("host.domain", hostComponentProperties.get("HostRoles/host_name"));
+      componentNames.add((String) hostComponentProperties.get("HostRoles/component_name"));
+    }
+    assertTrue(componentNames.contains("component1") && componentNames.contains("component2")
&&
+        componentNames.contains("component3"));
+
+    verify(blueprintDAO, managementController, request, response, blueprint, stackServiceResponse1,
stackServiceResponse2,
+        stackServiceComponentResponse1, stackServiceComponentResponse2, stackServiceComponentResponse3,
+        stackConfigurationResponse1, stackConfigurationResponse2, stackConfigurationResponse3,
hostGroup,
+        hostGroupComponent1, hostGroupComponent2, hostGroupComponent3, serviceResourceProvider,
+        componentResourceProvider, hostResourceProvider, hostComponentResourceProvider, persistKeyValue);
+  }
+
+  @Test
   public void testGetResources() throws Exception{
     Resource.Type type = Resource.Type.Cluster;
 
@@ -251,7 +571,7 @@ public class ClusterResourceProviderTest {
     // verify
     verify(managementController, response);
   }
-  
+
   @Test
   public void testUpdateWithConfiguration() throws Exception {
     AmbariManagementController managementController = createMock(AmbariManagementController.class);
@@ -368,4 +688,44 @@ public class ClusterResourceProviderTest {
     // verify
     verify(managementController, response);
   }
+
+  private class TestClusterResourceProvider extends ClusterResourceProvider {
+
+    private ResourceProvider serviceResourceProvider;
+    private ResourceProvider componentResourceProvider;
+    private ResourceProvider hostResourceProvider;
+    private ResourceProvider hostComponentResourceProvider;
+
+    TestClusterResourceProvider(AmbariManagementController managementController,
+                                ResourceProvider serviceResourceProvider,
+                                ResourceProvider componentResourceProvider,
+                                ResourceProvider hostResourceProvider,
+                                ResourceProvider hostComponentResourceProvider) {
+
+      super(PropertyHelper.getPropertyIds(Resource.Type.Cluster),
+            PropertyHelper.getKeyPropertyIds(Resource.Type.Cluster),
+            managementController);
+
+      this.serviceResourceProvider = serviceResourceProvider;
+      this.componentResourceProvider = componentResourceProvider;
+      this.hostResourceProvider = hostResourceProvider;
+      this.hostComponentResourceProvider = hostComponentResourceProvider;
+    }
+
+    @Override
+    ResourceProvider getResourceProviderByType(Resource.Type type) {
+      if (type == Resource.Type.Service) {
+        return this.serviceResourceProvider;
+      } else if (type == Resource.Type.Component) {
+        return this.componentResourceProvider;
+      } else if (type == Resource.Type.Host) {
+        return this.hostResourceProvider;
+      } else if (type == Resource.Type.HostComponent) {
+        return this.hostComponentResourceProvider;
+      } else {
+        fail("Unexpected resource provider type requested");
+      }
+      return null;
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ccf720a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java
index 76c6ae3..c793f29 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java
@@ -290,7 +290,7 @@ public class ComponentResourceProviderTest {
     Capture<Collection<ServiceComponentHost>> ignoredScHostsCapture = new Capture<Collection<ServiceComponentHost>>();
     Capture<Cluster> clusterCapture = new Capture<Cluster>();
 
-    expect(managementController.createStages(capture(clusterCapture), capture(requestPropertiesCapture),
capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
+    expect(managementController.createAndPersistStages(capture(clusterCapture), capture(requestPropertiesCapture),
capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
     )).andReturn(requestStatusResponse);
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ccf720a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStageContainerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStageContainerTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStageContainerTest.java
new file mode 100644
index 0000000..5201653
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStageContainerTest.java
@@ -0,0 +1,209 @@
+/**
+ * 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.Role;
+import org.apache.ambari.server.RoleCommand;
+import org.apache.ambari.server.actionmanager.ActionManager;
+import org.apache.ambari.server.actionmanager.HostRoleCommand;
+import org.apache.ambari.server.actionmanager.HostRoleStatus;
+import org.apache.ambari.server.actionmanager.Request;
+import org.apache.ambari.server.actionmanager.RequestFactory;
+import org.apache.ambari.server.actionmanager.Stage;
+import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.ShortTaskStatus;
+import org.apache.ambari.server.state.State;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.createStrictMock;
+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.assertTrue;
+
+/**
+ * RequestStageContainer unit tests.
+ */
+public class RequestStageContainerTest {
+  @Test
+  public void testGetId() {
+    RequestStageContainer requestStages = new RequestStageContainer(500L, null, null, null);
+    assertEquals(500, requestStages.getId().longValue());
+  }
+
+  @Test
+  public void testGetAddStages() {
+    RequestStageContainer requestStages = new RequestStageContainer(500L, null, null, null);
+    assertTrue(requestStages.getStages().isEmpty());
+
+    Stage stage = createNiceMock(Stage.class);
+    requestStages.addStages(Collections.singletonList(stage));
+    assertEquals(1, requestStages.getStages().size());
+    assertTrue(requestStages.getStages().contains(stage));
+
+    Stage stage2 = createNiceMock(Stage.class);
+    Stage stage3 = createNiceMock(Stage.class);
+    List<Stage> listStages = new ArrayList<Stage>();
+    listStages.add(stage2);
+    listStages.add(stage3);
+    requestStages.addStages(listStages);
+    assertEquals(3, requestStages.getStages().size());
+    listStages = requestStages.getStages();
+    assertEquals(stage, listStages.get(0));
+    assertEquals(stage2, listStages.get(1));
+    assertEquals(stage3, listStages.get(2));
+  }
+
+  @Test
+  public void testGetLastStageId() {
+    RequestStageContainer requestStages = new RequestStageContainer(1L, null, null, null);
+    assertEquals(-1, requestStages.getLastStageId());
+
+    Stage stage1 = createNiceMock(Stage.class);
+    Stage stage2 = createNiceMock(Stage.class);
+    List<Stage> listStages = new ArrayList<Stage>();
+    listStages.add(stage1);
+    listStages.add(stage2);
+
+    expect(stage2.getStageId()).andReturn(22L);
+    replay(stage1, stage2);
+
+    requestStages = new RequestStageContainer(1L, listStages, null, null);
+    assertEquals(22, requestStages.getLastStageId());
+  }
+
+  @Test
+  public void testGetProjectedState() {
+    String hostname = "host";
+    String componentName = "component";
+
+    Stage stage1 = createNiceMock(Stage.class);
+    Stage stage2 = createNiceMock(Stage.class);
+    Stage stage3 = createNiceMock(Stage.class);
+    Stage stage4 = createNiceMock(Stage.class);
+    HostRoleCommand command1 = createNiceMock(HostRoleCommand.class);
+    HostRoleCommand command2 = createNiceMock(HostRoleCommand.class);
+    HostRoleCommand command3 = createNiceMock(HostRoleCommand.class);
+
+    List<Stage> stages = new ArrayList<Stage>();
+    stages.add(stage1);
+    stages.add(stage2);
+    stages.add(stage3);
+    stages.add(stage4);
+
+    //expectations
+    expect(stage1.getHostRoleCommand(hostname, componentName)).andReturn(command1).anyTimes();
+    expect(stage2.getHostRoleCommand(hostname, componentName)).andReturn(command2).anyTimes();
+    expect(stage3.getHostRoleCommand(hostname, componentName)).andReturn(command3).anyTimes();
+    expect(stage4.getHostRoleCommand(hostname, componentName)).andReturn(null).anyTimes();
+
+    expect(command3.getRoleCommand()).andReturn(RoleCommand.SERVICE_CHECK).anyTimes();
+    expect(command2.getRoleCommand()).andReturn(RoleCommand.INSTALL).anyTimes();
+    replay(stage1, stage2, stage3, stage4, command1, command2, command3);
+
+    RequestStageContainer requestStages = new RequestStageContainer(1L, stages, null, null);
+    assertEquals(State.INSTALLED, requestStages.getProjectedState(hostname, componentName));
+
+    verify(stage1, stage2, stage3, stage4, command1, command2, command3);
+  }
+
+  @Test
+  public void testPersist() {
+    ActionManager actionManager = createStrictMock(ActionManager.class);
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    Request request = createStrictMock(Request.class);
+    Stage stage1 = createNiceMock(Stage.class);
+    Stage stage2 = createNiceMock(Stage.class);
+    List<Stage> stages = new ArrayList<Stage>();
+    stages.add(stage1);
+    stages.add(stage2);
+
+    //expectations
+    expect(requestFactory.createNewFromStages(stages)).andReturn(request);
+    expect(request.getStages()).andReturn(stages).anyTimes();
+    actionManager.sendActions(request, null);
+
+    replay(actionManager, requestFactory, request, stage1, stage2);
+
+    RequestStageContainer requestStages = new RequestStageContainer(1L, stages, requestFactory,
actionManager);
+    requestStages.persist();
+
+    verify(actionManager, requestFactory, request, stage1, stage2);
+  }
+
+  @Test
+  public void testPersist_noStages() {
+    ActionManager actionManager = createStrictMock(ActionManager.class);
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+
+    // no expectations due to empty stage list
+    replay(actionManager, requestFactory);
+
+    RequestStageContainer requestStages = new RequestStageContainer(1L, null, requestFactory,
actionManager);
+    requestStages.persist();
+
+    verify(actionManager, requestFactory);
+  }
+
+  @Test
+  public void testGetRequestStatusResponse() {
+    ActionManager actionManager = createStrictMock(ActionManager.class);
+    Stage stage1 = createNiceMock(Stage.class);
+    Stage stage2 = createNiceMock(Stage.class);
+    HostRoleCommand command1 = createNiceMock(HostRoleCommand.class);
+    Role role = createNiceMock(Role.class);
+    List<Stage> stages = new ArrayList<Stage>();
+    RoleCommand roleCommand = RoleCommand.INSTALL;
+    HostRoleStatus status = HostRoleStatus.IN_PROGRESS;
+    stages.add(stage1);
+    stages.add(stage2);
+    List<HostRoleCommand> hostRoleCommands = new ArrayList<HostRoleCommand>();
+    hostRoleCommands.add(command1);
+
+    expect(actionManager.getRequestTasks(100)).andReturn(hostRoleCommands);
+    expect(actionManager.getRequestContext(100)).andReturn("test");
+    expect(command1.getTaskId()).andReturn(1L);
+    expect(command1.getRoleCommand()).andReturn(roleCommand);
+    expect(command1.getRole()).andReturn(role);
+    expect(command1.getStatus()).andReturn(status);
+
+    replay(actionManager, stage1, stage2, command1, role);
+
+    RequestStageContainer requestStages = new RequestStageContainer(100L, stages, null, actionManager);
+    RequestStatusResponse response = requestStages.getRequestStatusResponse();
+
+    assertEquals(100, response.getRequestId());
+    List<ShortTaskStatus> tasks = response.getTasks();
+    assertEquals(1, tasks.size());
+    ShortTaskStatus task = tasks.get(0);
+    assertEquals(1, task.getTaskId());
+    assertEquals(roleCommand.toString(), task.getCommand());
+    assertEquals(status.toString(), task.getStatus());
+
+    assertEquals("test", response.getRequestContext());
+
+    verify(actionManager, stage1, stage2, command1, role);
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ccf720a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
index 8bc97cd..2adac34 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
@@ -24,6 +24,7 @@ import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.isNull;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
 
@@ -256,6 +257,7 @@ public class ServiceResourceProviderTest {
     Service service0 = createNiceMock(Service.class);
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    RequestStageContainer requestStages = createNiceMock(RequestStageContainer.class);
     RequestStatusResponse requestStatusResponse = createNiceMock(RequestStatusResponse.class);
 
     Map<String, String> mapRequestProps = new HashMap<String, String>();
@@ -281,12 +283,16 @@ public class ServiceResourceProviderTest {
     Capture<Collection<ServiceComponentHost>> ignoredScHostsCapture = new Capture<Collection<ServiceComponentHost>>();
     Capture<Cluster> clusterCapture = new Capture<Cluster>();
 
-    expect(managementController.createStages(capture(clusterCapture), capture(requestPropertiesCapture),
capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
-    )).andReturn(requestStatusResponse);
+    expect(managementController.addStages((RequestStageContainer) isNull(), capture(clusterCapture),
capture(requestPropertiesCapture),
+        capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
+        capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
+    )).andReturn(requestStages);
+    requestStages.persist();
+    expect(requestStages.getRequestStatusResponse()).andReturn(requestStatusResponse);
 
     // replay
     replay(managementController, clusters, cluster,
-        service0, serviceFactory, ambariMetaInfo, requestStatusResponse);
+        service0, serviceFactory, ambariMetaInfo, requestStages, requestStatusResponse);
 
     ResourceProvider provider = getServiceProvider(managementController);
 
@@ -305,7 +311,7 @@ public class ServiceResourceProviderTest {
 
     // verify
     verify(managementController, clusters, cluster,
-        service0, serviceFactory, ambariMetaInfo, requestStatusResponse);
+        service0, serviceFactory, ambariMetaInfo, requestStages, requestStatusResponse);
   }
 
   @Test
@@ -318,6 +324,8 @@ public class ServiceResourceProviderTest {
     Service service0 = createNiceMock(Service.class);
     ServiceResponse serviceResponse0 = createNiceMock(ServiceResponse.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
+    RequestStageContainer requestStages1 = createNiceMock(RequestStageContainer.class);
+    RequestStageContainer requestStages2 = createNiceMock(RequestStageContainer.class);
 
     RequestStatusResponse response1 = createNiceMock(RequestStatusResponse.class);
     RequestStatusResponse response2 = createNiceMock(RequestStatusResponse
@@ -357,14 +365,24 @@ public class ServiceResourceProviderTest {
     Capture<Collection<ServiceComponentHost>> ignoredScHostsCapture = new Capture<Collection<ServiceComponentHost>>();
     Capture<Cluster> clusterCapture = new Capture<Cluster>();
 
-    expect(managementController1.createStages(capture(clusterCapture), capture(requestPropertiesCapture),
capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
-    )).andReturn(response1);
+    expect(managementController1.addStages((RequestStageContainer) isNull(), capture(clusterCapture),
capture(requestPropertiesCapture),
+        capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
+        capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
+    )).andReturn(requestStages1);
 
-    expect(managementController2.createStages(capture(clusterCapture), capture(requestPropertiesCapture),
capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
-    )).andReturn(response2);
+    expect(managementController2.addStages((RequestStageContainer) isNull(), capture(clusterCapture),
capture(requestPropertiesCapture),
+        capture(requestParametersCapture), capture(changedServicesCapture), capture(changedCompsCapture),
+        capture(changedScHostsCapture), capture(ignoredScHostsCapture), anyBoolean(), anyBoolean()
+    )).andReturn(requestStages2);
+
+    requestStages1.persist();
+    expect(requestStages1.getRequestStatusResponse()).andReturn(response1);
+
+    requestStages2.persist();
+    expect(requestStages2.getRequestStatusResponse()).andReturn(response2);
 
     // replay
-    replay(managementController1, response1, managementController2, response2,
+    replay(managementController1, response1, managementController2, requestStages1, requestStages2,
response2,
         clusters, cluster, service0, serviceResponse0, ambariMetaInfo);
 
     ResourceProvider provider1 = getServiceProvider(managementController1);
@@ -397,7 +415,7 @@ public class ServiceResourceProviderTest {
     provider2.updateResources(request, predicate2);
 
     // verify
-    verify(managementController1, response1, managementController2, response2,
+    verify(managementController1, response1, managementController2, requestStages1, requestStages2,
response2,
         clusters, cluster, service0, serviceResponse0, ambariMetaInfo);
   }
 
@@ -1105,7 +1123,10 @@ public class ServiceResourceProviderTest {
                                                      boolean reconfigureClients) throws AmbariException
   {
     ServiceResourceProvider provider = getServiceProvider(controller);
-    return provider.updateServices(requests, requestProperties, runSmokeTest, reconfigureClients);
+
+    RequestStageContainer request = provider.updateServices(null, requests, requestProperties,
runSmokeTest, reconfigureClients);
+    request.persist();
+    return request.getRequestStatusResponse();
   }
 
   public static RequestStatusResponse deleteServices(AmbariManagementController controller,
Set<ServiceRequest> requests)


Mime
View raw message