ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From swa...@apache.org
Subject ambari git commit: AMBARI-14283. Provide ability to perform INSTALL only operation from blueprints. (swagle)
Date Wed, 09 Dec 2015 21:40:47 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 30424f72d -> 7430c094b


AMBARI-14283. Provide ability to perform INSTALL only operation from blueprints. (swagle)


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

Branch: refs/heads/trunk
Commit: 7430c094b7f9183c475585ba0ef6d552b20d682e
Parents: 30424f7
Author: Siddharth Wagle <swagle@hortonworks.com>
Authored: Wed Dec 9 13:40:32 2015 -0800
Committer: Siddharth Wagle <swagle@hortonworks.com>
Committed: Wed Dec 9 13:40:43 2015 -0800

----------------------------------------------------------------------
 .../internal/ClusterResourceProvider.java       |   7 +-
 .../internal/ProvisionClusterRequest.java       |  44 ++-
 .../ambari/server/topology/ClusterTopology.java |  10 +
 .../server/topology/ClusterTopologyImpl.java    |  13 +
 .../ambari/server/topology/HostRequest.java     |  46 ++-
 .../ambari/server/topology/TopologyManager.java |   2 +
 .../internal/ClusterResourceProviderTest.java   |  27 ++
 .../ClusterInstallWithoutStartTest.java         | 377 +++++++++++++++++++
 8 files changed, 506 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
index 2add289..f7d359c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
@@ -59,6 +59,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import static org.apache.ambari.server.controller.internal.ProvisionClusterRequest.PROVISION_ACTION_PROPERTY;
+
 
 /**
  * Resource provider for cluster resources.
@@ -188,6 +190,7 @@ public class ClusterResourceProvider extends AbstractControllerResourceProvider
     baseUnsupported.remove("configurations");
     baseUnsupported.remove("credentials");
     baseUnsupported.remove("config_recommendation_strategy");
+    baseUnsupported.remove("provision_action");
 
     return checkConfigPropertyIds(baseUnsupported, "Clusters");
   }
@@ -510,9 +513,9 @@ public class ClusterResourceProvider extends AbstractControllerResourceProvider
 
     String rawRequestBody = requestInfoProperties.get(Request.REQUEST_INFO_BODY_PROPERTY);
     Map<String, Object> rawBodyMap = jsonSerializer.<Map<String, Object>>fromJson(rawRequestBody,
Map.class);
+    SecurityConfiguration securityConfiguration =
+      securityConfigurationFactory.createSecurityConfigurationFromRequest(rawBodyMap, false);
 
-    SecurityConfiguration securityConfiguration = securityConfigurationFactory.createSecurityConfigurationFromRequest
-      (rawBodyMap, false);
     ProvisionClusterRequest createClusterRequest;
     try {
       createClusterRequest = topologyRequestFactory.createProvisionClusterRequest(properties,
securityConfiguration);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequest.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequest.java
index 56c4b76..7b1de26 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ProvisionClusterRequest.java
@@ -94,6 +94,17 @@ public class ProvisionClusterRequest extends BaseClusterRequest {
   public static final String CONFIG_RECOMMENDATION_STRATEGY = "config_recommendation_strategy";
 
   /**
+   * Support for controlling whether Install and Start tasks are created on
+   * blueprint deploy by default.
+   */
+  public static final String PROVISION_ACTION_PROPERTY = "provision_action";
+
+  public enum ProvisionAction {
+    INSTALL_ONLY,     // Skip Start
+    INSTALL_AND_START // Default action
+  }
+
+  /**
    * configuration factory
    */
   private static ConfigurationFactory configurationFactory = new ConfigurationFactory();
@@ -115,6 +126,8 @@ public class ProvisionClusterRequest extends BaseClusterRequest {
    */
   private final ConfigRecommendationStrategy configRecommendationStrategy;
 
+  private final ProvisionAction provisionAction;
+
   private final static Logger LOG = LoggerFactory.getLogger(ProvisionClusterRequest.class);
 
   /**
@@ -152,6 +165,8 @@ public class ProvisionClusterRequest extends BaseClusterRequest {
     this.credentialsMap = parseCredentials(properties);
 
     this.configRecommendationStrategy = parseConfigRecommendationStrategy(properties);
+
+    this.provisionAction = parseProvisionAction(properties);
   }
 
   private Map<String, Credential> parseCredentials(Map<String, Object> properties)
throws
@@ -377,7 +392,7 @@ public class ProvisionClusterRequest extends BaseClusterRequest {
         Enums.getIfPresent(ConfigRecommendationStrategy.class, configRecommendationStrategy);
       if (!configRecommendationStrategyOpt.isPresent()) {
         throw new InvalidTopologyTemplateException(String.format(
-          "Config recommendation stratagy is not supported: %s", configRecommendationStrategy));
+          "Config recommendation strategy is not supported: %s", configRecommendationStrategy));
       }
       return configRecommendationStrategyOpt.get();
     } else {
@@ -385,4 +400,31 @@ public class ProvisionClusterRequest extends BaseClusterRequest {
       return ConfigRecommendationStrategy.NEVER_APPLY;
     }
   }
+
+  /**
+   * Parse Provision Action specified in RequestInfo properties.
+   */
+  private ProvisionAction parseProvisionAction(Map<String, Object> properties) throws
InvalidTopologyTemplateException {
+    if (properties.containsKey(PROVISION_ACTION_PROPERTY)) {
+      String provisionActionStr = String.valueOf(properties.get(PROVISION_ACTION_PROPERTY));
+      Optional<ProvisionAction> provisionActionOptional =
+        Enums.getIfPresent(ProvisionAction.class, provisionActionStr);
+
+      if (!provisionActionOptional.isPresent()) {
+        throw new InvalidTopologyTemplateException(String.format(
+          "Invalid provision_action specified in the template: %s", provisionActionStr));
+      }
+      return provisionActionOptional.get();
+    } else {
+      return ProvisionAction.INSTALL_AND_START;
+    }
+  }
+
+  /**
+   * Get requested @ProvisionClusterRequest.ProvisionAction
+   */
+  public ProvisionAction getProvisionAction() {
+    return provisionAction;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopology.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopology.java
b/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopology.java
index 0ac2e2a..c3c04db 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopology.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopology.java
@@ -19,6 +19,8 @@
 package org.apache.ambari.server.topology;
 
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.internal.ProvisionClusterRequest;
+import org.apache.ambari.server.controller.internal.ProvisionClusterRequest.ProvisionAction;
 
 import java.util.Collection;
 import java.util.Map;
@@ -157,6 +159,14 @@ public interface ClusterTopology {
 
   public ConfigRecommendationStrategy getConfigRecommendationStrategy();
 
+  /**
+   * Set request provision action : INSTALL vs INSTALL_AND_START
+   * @param provisionAction @ProvisionAction
+   */
+  public void setProvisionAction(ProvisionAction provisionAction);
+
+  public ProvisionAction getProvisionAction();
+
   public Map<String, AdvisedConfiguration> getAdvisedConfigurations();
 
   //todo: don't expose ambari context from this class

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopologyImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopologyImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopologyImpl.java
index 687c2e1..e78300c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopologyImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/ClusterTopologyImpl.java
@@ -21,6 +21,8 @@ package org.apache.ambari.server.topology;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.internal.ProvisionClusterRequest;
+import org.apache.ambari.server.controller.internal.ProvisionClusterRequest.ProvisionAction;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -46,6 +48,7 @@ public class ClusterTopologyImpl implements ClusterTopology {
   private Blueprint blueprint;
   private Configuration configuration;
   private ConfigRecommendationStrategy configRecommendationStrategy;
+  private ProvisionAction provisionAction = ProvisionAction.INSTALL_AND_START;
   private Map<String, AdvisedConfiguration> advisedConfigurations = new HashMap<String,
AdvisedConfiguration>();
   private final Map<String, HostGroupInfo> hostGroupInfoMap = new HashMap<String,
HostGroupInfo>();
   private final AmbariContext ambariContext;
@@ -246,6 +249,16 @@ public class ClusterTopologyImpl implements ClusterTopology {
   }
 
   @Override
+  public ProvisionAction getProvisionAction() {
+    return provisionAction;
+  }
+
+  @Override
+  public void setProvisionAction(ProvisionAction provisionAction) {
+    this.provisionAction = provisionAction;
+  }
+
+  @Override
   public Map<String, AdvisedConfiguration> getAdvisedConfigurations() {
     return this.advisedConfigurations;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/topology/HostRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/HostRequest.java
b/ambari-server/src/main/java/org/apache/ambari/server/topology/HostRequest.java
index 518cbaf..7f178f4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/topology/HostRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/HostRequest.java
@@ -18,14 +18,6 @@
 
 package org.apache.ambari.server.topology;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.predicate.PredicateCompiler;
@@ -44,6 +36,15 @@ import org.apache.ambari.server.state.host.HostImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.ambari.server.controller.internal.ProvisionClusterRequest.ProvisionAction.INSTALL_ONLY;
+
 /**
  * Represents a set of requests to a single host such as install, start, etc.
  */
@@ -178,11 +179,18 @@ public class HostRequest implements Comparable<HostRequest> {
 
     InstallHostTask installTask = new InstallHostTask();
     topologyTasks.add(installTask);
-    StartHostTask startTask = new StartHostTask();
-    topologyTasks.add(startTask);
-
     logicalTaskMap.put(installTask, new HashMap<String, Long>());
-    logicalTaskMap.put(startTask, new HashMap<String, Long>());
+
+    boolean skipStartTaskCreate = topology.getProvisionAction().equals(INSTALL_ONLY);
+
+    StartHostTask startTask = null;
+    if (!skipStartTaskCreate) {
+      startTask = new StartHostTask();
+      topologyTasks.add(startTask);
+      logicalTaskMap.put(startTask, new HashMap<String, Long>());
+    } else {
+      LOG.info("Skipping Start task creation since provision action = " + topology.getProvisionAction());
+    }
 
     // lower level logical component level tasks which get mapped to physical tasks
     HostGroup hostGroup = getHostGroup();
@@ -204,7 +212,7 @@ public class HostRequest implements Comparable<HostRequest> {
 
       Stack stack = hostGroup.getStack();
       // if component isn't a client, add a start task
-      if (stack!=null && !stack.getComponentInfo(component).isClient()) {
+      if (!skipStartTaskCreate && stack != null && !stack.getComponentInfo(component).isClient())
{
         HostRoleCommand logicalStartTask = context.createAmbariTask(
             getRequestId(), id, component, hostName, AmbariContext.TaskType.START);
         logicalTasks.put(logicalStartTask.getTaskId(), logicalStartTask);
@@ -218,11 +226,15 @@ public class HostRequest implements Comparable<HostRequest> {
     topologyTasks.add(new RegisterWithConfigGroupTask());
     InstallHostTask installTask = new InstallHostTask();
     topologyTasks.add(installTask);
-    StartHostTask startTask = new StartHostTask();
-    topologyTasks.add(startTask);
-
     logicalTaskMap.put(installTask, new HashMap<String, Long>());
-    logicalTaskMap.put(startTask, new HashMap<String, Long>());
+
+    boolean skipStartTaskCreate = topology.getProvisionAction().equals(INSTALL_ONLY);
+
+    if (!skipStartTaskCreate) {
+      StartHostTask startTask = new StartHostTask();
+      topologyTasks.add(startTask);
+      logicalTaskMap.put(startTask, new HashMap<String, Long>());
+    }
 
     AmbariContext ambariContext = topology.getAmbariContext();
     // lower level logical component level tasks which get mapped to physical tasks

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/main/java/org/apache/ambari/server/topology/TopologyManager.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/TopologyManager.java
b/ambari-server/src/main/java/org/apache/ambari/server/topology/TopologyManager.java
index 4b45806..80d19d2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/topology/TopologyManager.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/TopologyManager.java
@@ -175,6 +175,8 @@ public class TopologyManager {
     request.setClusterId(clusterId);
     // set recommendation strategy
     topology.setConfigRecommendationStrategy(request.getConfigRecommendationStrategy());
+    // set provision action requested
+    topology.setProvisionAction(request.getProvisionAction());
     // persist request after it has successfully validated
     PersistedTopologyRequest persistedRequest = RetryHelper.executeWithRetry(new Callable<PersistedTopologyRequest>()
{
       @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/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 179a09e..0eb1e59 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
@@ -137,6 +137,33 @@ public class ClusterResourceProviderTest {
     testCreateResource_blueprint(TestAuthenticationFactory.createClusterAdministrator());
   }
 
+  @Test
+  public void testCreateResource_blueprint_With_ProvisionAction() throws Exception {
+    Set<Map<String, Object>> requestProperties = createBlueprintRequestProperties(CLUSTER_NAME,
BLUEPRINT_NAME);
+    Map<String, Object> properties = requestProperties.iterator().next();
+    properties.put(ProvisionClusterRequest.PROVISION_ACTION_PROPERTY, "INSTALL_ONLY");
+    Map<String, String> requestInfoProperties = new HashMap<String, String>();
+    requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, "{}");
+
+    // set expectations
+    expect(request.getProperties()).andReturn(requestProperties).anyTimes();
+    expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties).anyTimes();
+
+    expect(securityFactory.createSecurityConfigurationFromRequest(anyObject(HashMap.class),
anyBoolean())).andReturn(null)
+      .once();
+    expect(topologyFactory.createProvisionClusterRequest(properties, null)).andReturn(topologyRequest).once();
+    expect(topologyManager.provisionCluster(topologyRequest)).andReturn(requestStatusResponse).once();
+    expect(requestStatusResponse.getRequestId()).andReturn(5150L).anyTimes();
+
+    replayAll();
+    RequestStatus requestStatus = provider.createResources(request);
+    assertEquals(5150L, requestStatus.getRequestResource().getPropertyValue(PropertyHelper.getPropertyId("Requests",
"id")));
+    assertEquals(Resource.Type.Request, requestStatus.getRequestResource().getType());
+    assertEquals("Accepted", requestStatus.getRequestResource().getPropertyValue(PropertyHelper.getPropertyId("Requests",
"status")));
+
+    verifyAll();
+  }
+
   @Test(expected = IllegalArgumentException.class)
   public void testCreateResource_blueprint_withInvalidSecurityConfiguration() throws Exception
{
     Set<Map<String, Object>> requestProperties = createBlueprintRequestProperties(CLUSTER_NAME,
BLUEPRINT_NAME);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7430c094/ambari-server/src/test/java/org/apache/ambari/server/topology/ClusterInstallWithoutStartTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/topology/ClusterInstallWithoutStartTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/topology/ClusterInstallWithoutStartTest.java
new file mode 100644
index 0000000..1354a72
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/topology/ClusterInstallWithoutStartTest.java
@@ -0,0 +1,377 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.Role;
+import org.apache.ambari.server.RoleCommand;
+import org.apache.ambari.server.actionmanager.HostRoleCommand;
+import org.apache.ambari.server.actionmanager.HostRoleStatus;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.controller.AmbariServer;
+import org.apache.ambari.server.controller.ClusterRequest;
+import org.apache.ambari.server.controller.ConfigurationRequest;
+import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.internal.ProvisionClusterRequest;
+import org.apache.ambari.server.controller.internal.Stack;
+import org.apache.ambari.server.controller.spi.ClusterController;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.ResourceProvider;
+import org.apache.ambari.server.orm.entities.TopologyLogicalRequestEntity;
+import org.apache.ambari.server.security.encryption.CredentialStoreService;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.SecurityType;
+import org.easymock.Capture;
+import org.easymock.EasyMockRule;
+import org.easymock.EasyMockSupport;
+import org.easymock.Mock;
+import org.easymock.MockType;
+import org.easymock.TestSubject;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import static org.apache.ambari.server.controller.internal.ProvisionClusterRequest.ProvisionAction.INSTALL_ONLY;
+import static org.easymock.EasyMock.anyLong;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.isA;
+import static org.easymock.EasyMock.isNull;
+import static org.easymock.EasyMock.newCapture;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(AmbariServer.class)
+public class ClusterInstallWithoutStartTest {
+  private static final String CLUSTER_NAME = "test-cluster";
+  private static final long CLUSTER_ID = 1;
+  private static final String BLUEPRINT_NAME = "test-bp";
+  private static final String STACK_NAME = "test-stack";
+  private static final String STACK_VERSION = "test-stack-version";
+
+  @Rule
+  public EasyMockRule mocks = new EasyMockRule(this);
+
+  @TestSubject
+  private TopologyManager topologyManager = new TopologyManager();
+
+  @Mock(type = MockType.NICE)
+  private Blueprint blueprint;
+
+  @Mock(type = MockType.NICE)
+  private Stack stack;
+
+  @Mock(type = MockType.NICE)
+  private ProvisionClusterRequest request;
+  private PersistedTopologyRequest persistedTopologyRequest;
+//  @Mock(type = MockType.STRICT)
+  private LogicalRequestFactory logicalRequestFactory;
+  @Mock(type = MockType.DEFAULT)
+  private LogicalRequest logicalRequest;
+  @Mock(type = MockType.NICE)
+  private AmbariContext ambariContext;
+  @Mock(type = MockType.NICE)
+  private ConfigurationRequest configurationRequest;
+  @Mock(type = MockType.NICE)
+  private ConfigurationRequest configurationRequest2;
+  @Mock(type = MockType.NICE)
+  private ConfigurationRequest configurationRequest3;
+  @Mock(type = MockType.NICE)
+  private RequestStatusResponse requestStatusResponse;
+  @Mock(type = MockType.STRICT)
+  private ExecutorService executor;
+  @Mock(type = MockType.STRICT)
+  private PersistedState persistedState;
+  @Mock(type = MockType.NICE)
+  private HostGroup group1;
+  @Mock(type = MockType.NICE)
+  private HostGroup group2;
+  @Mock(type = MockType.STRICT)
+  private SecurityConfigurationFactory securityConfigurationFactory;
+  @Mock(type = MockType.STRICT)
+  private CredentialStoreService credentialStoreService;
+  @Mock(type = MockType.STRICT)
+  private ClusterController clusterController;
+  @Mock(type = MockType.STRICT)
+  private ResourceProvider resourceProvider;
+  @Mock(type = MockType.NICE)
+  private AmbariManagementController managementController;
+  @Mock(type = MockType.NICE)
+  private Clusters clusters;
+  @Mock(type = MockType.NICE)
+  private Cluster cluster;
+  @Mock(type = MockType.NICE)
+  private HostRoleCommand hostRoleCommand;
+
+  @Mock(type = MockType.STRICT)
+  private Future mockFuture;
+
+  private final Configuration stackConfig = new Configuration(new HashMap<String, Map<String,
String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>());
+  private final Configuration bpConfiguration = new Configuration(new HashMap<String,
Map<String, String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), stackConfig);
+  private final Configuration topoConfiguration = new Configuration(new HashMap<String,
Map<String, String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), bpConfiguration);
+  private final Configuration bpGroup1Config = new Configuration(new HashMap<String, Map<String,
String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), bpConfiguration);
+  private final Configuration bpGroup2Config = new Configuration(new HashMap<String, Map<String,
String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), bpConfiguration);
+
+  private final Configuration topoGroup1Config = new Configuration(new HashMap<String,
Map<String, String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), bpGroup1Config);
+  private final Configuration topoGroup2Config = new Configuration(new HashMap<String,
Map<String, String>>(),
+    new HashMap<String, Map<String, Map<String, String>>>(), bpGroup2Config);
+
+  private HostGroupInfo group1Info = new HostGroupInfo("group1");
+  private HostGroupInfo group2Info = new HostGroupInfo("group2");
+  private Map<String, HostGroupInfo> groupInfoMap = new HashMap<String, HostGroupInfo>();
+
+  private Collection<String> group1Components = Arrays.asList("component1", "component2",
"component3");
+  private Collection<String> group2Components = Arrays.asList("component3", "component4");
+
+  private Map<String, Collection<String>> group1ServiceComponents = new HashMap<String,
Collection<String>>();
+  private Map<String, Collection<String>> group2ServiceComponents = new HashMap<String,
Collection<String>>();
+
+  private Map<String, Collection<String>> serviceComponents = new HashMap<String,
Collection<String>>();
+
+  private String predicate = "Hosts/host_name=foo";
+
+  private List<TopologyValidator> topologyValidators = new ArrayList<TopologyValidator>();
+
+  private Capture<ClusterTopology> clusterTopologyCapture;
+  private Capture<Map<String, Object>> configRequestPropertiesCapture;
+  private Capture<Map<String, Object>> configRequestPropertiesCapture2;
+  private Capture<Map<String, Object>> configRequestPropertiesCapture3;
+  private Capture<ClusterRequest> updateClusterConfigRequestCapture;
+  private Capture<Runnable> updateConfigTaskCapture;
+
+  @Before
+  public void setup() throws Exception {
+    clusterTopologyCapture = newCapture();
+    configRequestPropertiesCapture = newCapture();
+    configRequestPropertiesCapture2 = newCapture();
+    configRequestPropertiesCapture3 = newCapture();
+    updateClusterConfigRequestCapture = newCapture();
+    updateConfigTaskCapture = newCapture();
+
+    topoConfiguration.setProperty("service1-site", "s1-prop", "s1-prop-value");
+    topoConfiguration.setProperty("service2-site", "s2-prop", "s2-prop-value");
+    topoConfiguration.setProperty("cluster-env", "g-prop", "g-prop-value");
+
+    //clusterRequestCapture = new Capture<ClusterRequest>();
+    // group 1 has fqdn specified
+    group1Info.addHost("host1");
+    group1Info.setConfiguration(topoGroup1Config);
+    // group 2 has host_count and host_predicate specified
+    group2Info.setRequestedCount(2);
+    group2Info.setPredicate(predicate);
+    group2Info.setConfiguration(topoGroup2Config);
+
+    groupInfoMap.put("group1", group1Info);
+    groupInfoMap.put("group2", group2Info);
+
+    Map<String, HostGroup> groupMap = new HashMap<String, HostGroup>();
+    groupMap.put("group1", group1);
+    groupMap.put("group2", group2);
+
+    serviceComponents.put("service1", Arrays.asList("component1", "component3"));
+    serviceComponents.put("service2", Arrays.asList("component2", "component4"));
+
+    group1ServiceComponents.put("service1", Arrays.asList("component1", "component3"));
+    group1ServiceComponents.put("service2", Collections.singleton("component2"));
+    group2ServiceComponents.put("service2", Collections.singleton("component3"));
+    group2ServiceComponents.put("service2", Collections.singleton("component4"));
+
+    expect(blueprint.getHostGroup("group1")).andReturn(group1).anyTimes();
+    expect(blueprint.getHostGroup("group2")).andReturn(group2).anyTimes();
+    expect(blueprint.getComponents("service1")).andReturn(Arrays.asList("component1", "component3")).anyTimes();
+    expect(blueprint.getComponents("service2")).andReturn(Arrays.asList("component2", "component4")).anyTimes();
+    expect(blueprint.getConfiguration()).andReturn(bpConfiguration).anyTimes();
+    expect(blueprint.getHostGroups()).andReturn(groupMap).anyTimes();
+    expect(blueprint.getHostGroupsForComponent("component1")).andReturn(Collections.singleton(group1)).anyTimes();
+    expect(blueprint.getHostGroupsForComponent("component2")).andReturn(Collections.singleton(group1)).anyTimes();
+    expect(blueprint.getHostGroupsForComponent("component3")).andReturn(Arrays.asList(group1,
group2)).anyTimes();
+    expect(blueprint.getHostGroupsForComponent("component4")).andReturn(Collections.singleton(group2)).anyTimes();
+    expect(blueprint.getHostGroupsForService("service1")).andReturn(Arrays.asList(group1,
group2)).anyTimes();
+    expect(blueprint.getHostGroupsForService("service2")).andReturn(Arrays.asList(group1,
group2)).anyTimes();
+    expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).anyTimes();
+    expect(blueprint.getServices()).andReturn(Arrays.asList("service1", "service2")).anyTimes();
+    expect(blueprint.getStack()).andReturn(stack).anyTimes();
+    // don't expect toEntity()
+
+    expect(stack.getAllConfigurationTypes("service1")).andReturn(Arrays.asList("service1-site",
"service1-env")).anyTimes();
+    expect(stack.getAllConfigurationTypes("service2")).andReturn(Arrays.asList("service2-site",
"service2-env")).anyTimes();
+    expect(stack.getAutoDeployInfo("component1")).andReturn(null).anyTimes();
+    expect(stack.getAutoDeployInfo("component2")).andReturn(null).anyTimes();
+    expect(stack.getAutoDeployInfo("component3")).andReturn(null).anyTimes();
+    expect(stack.getAutoDeployInfo("component4")).andReturn(null).anyTimes();
+    expect(stack.getCardinality("component1")).andReturn(new Cardinality("1")).anyTimes();
+    expect(stack.getCardinality("component2")).andReturn(new Cardinality("1")).anyTimes();
+    expect(stack.getCardinality("component3")).andReturn(new Cardinality("1+")).anyTimes();
+    expect(stack.getCardinality("component4")).andReturn(new Cardinality("1+")).anyTimes();
+    expect(stack.getComponents()).andReturn(serviceComponents).anyTimes();
+    expect(stack.getComponents("service1")).andReturn(serviceComponents.get("service1")).anyTimes();
+    expect(stack.getComponents("service2")).andReturn(serviceComponents.get("service2")).anyTimes();
+    expect(stack.getConfiguration()).andReturn(stackConfig).anyTimes();
+    expect(stack.getName()).andReturn(STACK_NAME).anyTimes();
+    expect(stack.getVersion()).andReturn(STACK_VERSION).anyTimes();
+    expect(stack.getExcludedConfigurationTypes("service1")).andReturn(Collections.<String>emptySet()).anyTimes();
+    expect(stack.getExcludedConfigurationTypes("service2")).andReturn(Collections.<String>emptySet()).anyTimes();
+
+    expect(request.getBlueprint()).andReturn(blueprint).anyTimes();
+    expect(request.getClusterId()).andReturn(CLUSTER_ID).anyTimes();
+    expect(request.getClusterName()).andReturn(CLUSTER_NAME).anyTimes();
+    expect(request.getDescription()).andReturn("Provision Cluster Test").anyTimes();
+    expect(request.getConfiguration()).andReturn(topoConfiguration).anyTimes();
+    expect(request.getHostGroupInfo()).andReturn(groupInfoMap).anyTimes();
+    expect(request.getTopologyValidators()).andReturn(topologyValidators).anyTimes();
+    expect(request.getConfigRecommendationStrategy()).andReturn(ConfigRecommendationStrategy.NEVER_APPLY);
+    expect(request.getProvisionAction()).andReturn(INSTALL_ONLY).anyTimes();
+    expect(request.getSecurityConfiguration()).andReturn(null).anyTimes();
+
+
+    expect(group1.getBlueprintName()).andReturn(BLUEPRINT_NAME).anyTimes();
+    expect(group1.getCardinality()).andReturn("test cardinality").anyTimes();
+    expect(group1.containsMasterComponent()).andReturn(true).anyTimes();
+    expect(group1.getComponents()).andReturn(group1Components).anyTimes();
+    expect(group1.getComponents("service1")).andReturn(group1ServiceComponents.get("service1")).anyTimes();
+    expect(group1.getComponents("service2")).andReturn(group1ServiceComponents.get("service1")).anyTimes();
+    expect(group1.getConfiguration()).andReturn(topoGroup1Config).anyTimes();
+    expect(group1.getName()).andReturn("group1").anyTimes();
+    expect(group1.getServices()).andReturn(Arrays.asList("service1", "service2")).anyTimes();
+    expect(group1.getStack()).andReturn(stack).anyTimes();
+
+    expect(group2.getBlueprintName()).andReturn(BLUEPRINT_NAME).anyTimes();
+    expect(group2.getCardinality()).andReturn("test cardinality").anyTimes();
+    expect(group2.containsMasterComponent()).andReturn(false).anyTimes();
+    expect(group2.getComponents()).andReturn(group2Components).anyTimes();
+    expect(group2.getComponents("service1")).andReturn(group2ServiceComponents.get("service1")).anyTimes();
+    expect(group2.getComponents("service2")).andReturn(group2ServiceComponents.get("service2")).anyTimes();
+    expect(group2.getConfiguration()).andReturn(topoGroup2Config).anyTimes();
+    expect(group2.getName()).andReturn("group2").anyTimes();
+    expect(group2.getServices()).andReturn(Arrays.asList("service1", "service2")).anyTimes();
+    expect(group2.getStack()).andReturn(stack).anyTimes();
+
+    // Create partial mock to allow actual logical request creation
+    logicalRequestFactory = createMockBuilder(LogicalRequestFactory.class).addMockedMethod(
+      LogicalRequestFactory.class.getMethod("createRequest",
+        Long.class, TopologyRequest.class, ClusterTopology.class,
+        TopologyLogicalRequestEntity.class)).createMock();
+    Field f = TopologyManager.class.getDeclaredField("logicalRequestFactory");
+    f.setAccessible(true);
+    f.set(topologyManager, logicalRequestFactory);
+
+    PowerMock.mockStatic(AmbariServer.class);
+    expect(AmbariServer.getController()).andReturn(managementController).anyTimes();
+    PowerMock.replay(AmbariServer.class);
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getClusterById(anyLong())).andReturn(cluster).anyTimes();
+    expect(cluster.getClusterName()).andReturn(CLUSTER_NAME).anyTimes();
+
+    expect(ambariContext.getPersistedTopologyState()).andReturn(persistedState).anyTimes();
+    //todo: don't ignore param
+    ambariContext.createAmbariResources(isA(ClusterTopology.class), eq(CLUSTER_NAME), (SecurityType)
isNull());
+    expectLastCall().once();
+    expect(ambariContext.getNextRequestId()).andReturn(1L).once();
+    expect(ambariContext.isClusterKerberosEnabled(CLUSTER_ID)).andReturn(false).anyTimes();
+    expect(ambariContext.getClusterId(CLUSTER_NAME)).andReturn(CLUSTER_ID).anyTimes();
+    expect(ambariContext.getClusterName(CLUSTER_ID)).andReturn(CLUSTER_NAME).anyTimes();
+    // cluster configuration task run() isn't executed by mock executor
+    // so only INITIAL config
+    expect(ambariContext.createConfigurationRequests(capture(configRequestPropertiesCapture))).
+      andReturn(Collections.singletonList(configurationRequest));
+    expect(ambariContext.createConfigurationRequests(capture(configRequestPropertiesCapture2))).
+      andReturn(Collections.singletonList(configurationRequest2)).once();
+    expect(ambariContext.createConfigurationRequests(capture(configRequestPropertiesCapture3))).
+      andReturn(Collections.singletonList(configurationRequest3)).once();
+    // INSTALL task expectation
+    expect(ambariContext.createAmbariTask(anyLong(), anyLong(), anyString(),
+      anyString(), eq(AmbariContext.TaskType.INSTALL))).andReturn(hostRoleCommand).atLeastOnce();
+    expect(hostRoleCommand.getTaskId()).andReturn(1L).atLeastOnce();
+    expect(hostRoleCommand.getRoleCommand()).andReturn(RoleCommand.INSTALL).atLeastOnce();
+    expect(hostRoleCommand.getRole()).andReturn(Role.INSTALL_PACKAGES).atLeastOnce();
+    expect(hostRoleCommand.getStatus()).andReturn(HostRoleStatus.COMPLETED).atLeastOnce();
+
+    ambariContext.setConfigurationOnCluster(capture(updateClusterConfigRequestCapture));
+    expectLastCall().times(3);
+    ambariContext.persistInstallStateForUI(CLUSTER_NAME, STACK_NAME, STACK_VERSION);
+    expectLastCall().once();
+
+    expect(clusterController.ensureResourceProvider(anyObject(Resource.Type.class))).andReturn(resourceProvider);
+    expect(executor.submit(anyObject(AsyncCallableService.class))).andReturn(mockFuture);
+
+    persistedTopologyRequest = new PersistedTopologyRequest(1, request);
+    expect(persistedState.getAllRequests()).andReturn(Collections.<ClusterTopology,
+      List<LogicalRequest>>emptyMap()).once();
+    expect(persistedState.persistTopologyRequest(request)).andReturn(persistedTopologyRequest).once();
+    persistedState.persistLogicalRequest((LogicalRequest) anyObject(), anyLong());
+    expectLastCall().once();
+
+    replay(blueprint, stack, request, group1, group2, ambariContext, logicalRequestFactory,
logicalRequest,
+      configurationRequest, configurationRequest2, configurationRequest3, requestStatusResponse,
executor,
+      persistedState, securityConfigurationFactory, credentialStoreService, clusterController,
resourceProvider,
+      mockFuture, managementController, clusters, cluster, hostRoleCommand);
+
+    Class clazz = TopologyManager.class;
+
+    f = clazz.getDeclaredField("executor");
+    f.setAccessible(true);
+    f.set(topologyManager, executor);
+
+    EasyMockSupport.injectMocks(topologyManager);
+  }
+
+  @After
+  public void tearDown() {
+    verify(blueprint, stack, request, group1, group2, ambariContext, logicalRequestFactory,
+      logicalRequest, configurationRequest, configurationRequest2, configurationRequest3,
+      requestStatusResponse, executor, persistedState, mockFuture,
+      managementController, clusters, cluster, hostRoleCommand);
+
+    reset(blueprint, stack, request, group1, group2, ambariContext, logicalRequestFactory,
+      logicalRequest, configurationRequest, configurationRequest2, configurationRequest3,
+      requestStatusResponse, executor, persistedState, mockFuture,
+      managementController, clusters, cluster, hostRoleCommand);
+  }
+
+  @Test
+  public void testProvisionCluster() throws Exception {
+    topologyManager.provisionCluster(request);
+  }
+}


Mime
View raw message