This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 6c9c06c  AMBARI-25040. Handle blueprint/VDF stack version mismatch 
(#2719)
6c9c06c is described below

commit 6c9c06c34bd31562962e5e956c201db3990fc8db
Author: Doroszlai, Attila <[email protected]>
AuthorDate: Wed Dec 12 20:20:31 2018 +0100

    AMBARI-25040. Handle blueprint/VDF stack version mismatch (#2719)
---
 .../ambari/server/topology/AmbariContext.java      | 43 +++++++----
 .../ambari/server/topology/AmbariContextTest.java  | 84 ++++++++++++++++++----
 .../topology/addservice/RequestValidatorTest.java  | 14 +---
 .../org/apache/ambari/server/utils/Assertions.java | 35 +++++++++
 4 files changed, 136 insertions(+), 40 deletions(-)

diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
index dcb4739..34268ca 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
@@ -25,6 +25,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -265,23 +266,35 @@ public class AmbariContext {
         repoVersion = stackRepoVersions.get(0);
         LOG.warn("Cluster is being provisioned using the single matching 
repository version {}", repoVersion.getVersion());
       }
-    } else if (null != repoVersionId){
-      repoVersion = repositoryVersionDAO.findByPK(repoVersionId);
-
-      if (null == repoVersion) {
-        throw new IllegalArgumentException(String.format(
-          "Could not identify repository version with repository version id %s 
for installing services. "
-            + "Specify a valid repository version id with '%s'",
-          repoVersionId, ProvisionClusterRequest.REPO_VERSION_ID_PROPERTY));
-      }
     } else {
-      repoVersion = repositoryVersionDAO.findByStackAndVersion(stackId, 
repoVersionString);
+      if (null != repoVersionId) {
+        repoVersion = repositoryVersionDAO.findByPK(repoVersionId);
+
+        if (null == repoVersion) {
+          throw new IllegalArgumentException(String.format(
+            "Could not identify repository version with repository version id 
%s for installing services. "
+              + "Specify a valid repository version id with '%s'",
+            repoVersionId, ProvisionClusterRequest.REPO_VERSION_ID_PROPERTY));
+        }
+      } else {
+        repoVersion = repositoryVersionDAO.findByStackAndVersion(stackId, 
repoVersionString);
+
+        if (null == repoVersion) {
+          throw new IllegalArgumentException(String.format(
+            "Could not identify repository version with stack %s and version 
%s for installing services. "
+              + "Specify a valid version with '%s'",
+            stackId, repoVersionString, 
ProvisionClusterRequest.REPO_VERSION_PROPERTY));
+        }
+      }
 
-      if (null == repoVersion) {
-        throw new IllegalArgumentException(String.format(
-          "Could not identify repository version with stack %s and version %s 
for installing services. "
-            + "Specify a valid version with '%s'",
-          stackId, repoVersionString, 
ProvisionClusterRequest.REPO_VERSION_PROPERTY));
+      if (!Objects.equals(repoVersion.getStackId(), stackId)) {
+        String repoVersionPair = repoVersionId != null
+          ? String.format("'%s' = %d", 
ProvisionClusterRequest.REPO_VERSION_ID_PROPERTY, repoVersionId)
+          : String.format("'%s' = '%s'", 
ProvisionClusterRequest.REPO_VERSION_PROPERTY, repoVersionString);
+        String msg = String.format(
+          "The stack specified in the blueprint (%s) and the repository 
version (%s for %s) should match",
+          stackId, repoVersion.getStackId(), repoVersionPair);
+        throw new IllegalArgumentException(msg);
       }
     }
 
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
index ab0c152..dbab576 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.topology;
 
 import static java.util.Collections.singletonList;
+import static org.apache.ambari.server.utils.Assertions.assertThrows;
 import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
@@ -103,7 +104,7 @@ public class AmbariContextTest {
   private static final String HOST_GROUP_2 = "group2";
   private static final String HOST1 = "host1";
   private static final String HOST2 = "host2";
-  StackId stackId = new StackId(STACK_NAME, STACK_VERSION);
+  private static final StackId STACK_ID = new StackId(STACK_NAME, 
STACK_VERSION);
 
   private static final AmbariContext context = new AmbariContext();
   private static final AmbariManagementController controller = 
createNiceMock(AmbariManagementController.class);
@@ -735,17 +736,7 @@ public class AmbariContextTest {
 
     context.repositoryVersionDAO = repositoryVersionDAO;
 
-    controller.createCluster(capture(Capture.<ClusterRequest>newInstance()));
-    expectLastCall().once();
-    expect(cluster.getServices()).andReturn(clusterServices).anyTimes();
-
-    
serviceResourceProvider.createServices(capture(Capture.<Set<ServiceRequest>>newInstance()));
-    expectLastCall().once();
-    
componentResourceProvider.createComponents(capture(Capture.<Set<ServiceComponentRequest>>newInstance()));
-    expectLastCall().once();
-
-    expect(serviceResourceProvider.updateResources(
-        capture(Capture.<Request>newInstance()), 
capture(Capture.<Predicate>newInstance()))).andReturn(null).atLeastOnce();
+    expectAmbariResourceCreation();
 
     replayAll();
 
@@ -753,6 +744,23 @@ public class AmbariContextTest {
     context.createAmbariResources(topology, CLUSTER_NAME, null, null, null);
   }
 
+  private void expectAmbariResourceCreation() {
+    try {
+      controller.createCluster(capture(Capture.newInstance()));
+      expectLastCall().once();
+      expect(cluster.getServices()).andReturn(clusterServices).anyTimes();
+
+      serviceResourceProvider.createServices(capture(Capture.newInstance()));
+      expectLastCall().once();
+      
componentResourceProvider.createComponents(capture(Capture.newInstance()));
+      expectLastCall().once();
+
+      
expect(serviceResourceProvider.updateResources(capture(Capture.newInstance()), 
capture(Capture.newInstance()))).andReturn(null).atLeastOnce();
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   @Test
   public void testCreateAmbariResourcesManyVersions() throws Exception {
 
@@ -807,4 +815,56 @@ public class AmbariContextTest {
     }
   }
 
+  @Test
+  public void testCreateAmbariResourcesVersionOK() {
+    RepositoryVersionEntity repoVersion = 
repositoryInClusterCreationRequest(1L, "3.0.1.2-345", STACK_ID);
+    expectAmbariResourceCreation();
+    replayAll();
+
+    context.createAmbariResources(topology, CLUSTER_NAME, null, 
repoVersion.getVersion(), null);
+  }
+
+  @Test
+  public void testCreateAmbariResourcesVersionByIdOK() {
+    RepositoryVersionEntity repoVersion = 
repositoryInClusterCreationRequest(1L, "3.0.1.2-345", STACK_ID);
+    expectAmbariResourceCreation();
+    replayAll();
+
+    context.createAmbariResources(topology, CLUSTER_NAME, null, null, 
repoVersion.getId());
+  }
+
+  @Test
+  public void testCreateAmbariResourcesVersionMismatch() {
+    RepositoryVersionEntity repoVersion = 
repositoryInClusterCreationRequest(1L, "3.0.1.2-345", new StackId("HDP", 
"3.0"));
+    replayAll();
+
+    IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+      () -> context.createAmbariResources(topology, CLUSTER_NAME, null, 
repoVersion.getVersion(), null));
+    assertTrue(e.getMessage(), e.getMessage().contains("should match"));
+  }
+
+  @Test
+  public void testCreateAmbariResourcesVersionMismatchById() {
+    RepositoryVersionEntity repoVersion = 
repositoryInClusterCreationRequest(1L, "3.0.1.2-345", new StackId("HDP", 
"3.0"));
+    replayAll();
+
+    IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+      () -> context.createAmbariResources(topology, CLUSTER_NAME, null, null, 
repoVersion.getId()));
+    assertTrue(e.getMessage(), e.getMessage().contains("should match"));
+  }
+
+  private RepositoryVersionEntity repositoryInClusterCreationRequest(Long id, 
String version, StackId stackId) {
+    RepositoryVersionEntity repoEntity = 
createNiceMock(RepositoryVersionEntity.class);
+    expect(repoEntity.getStackId()).andStubReturn(stackId);
+    expect(repoEntity.getId()).andStubReturn(id);
+    expect(repoEntity.getType()).andStubReturn(RepositoryType.STANDARD);
+    expect(repoEntity.getVersion()).andStubReturn(version);
+    RepositoryVersionDAO repositoryVersionDAO = 
createNiceMock(RepositoryVersionDAO.class);
+    expect(repositoryVersionDAO.findByPK(1L)).andStubReturn(repoEntity);
+    
expect(repositoryVersionDAO.findByStackAndVersion(EasyMock.anyObject(StackId.class),
 EasyMock.anyString())).andStubReturn(repoEntity);
+    replay(repositoryVersionDAO, repoEntity);
+    context.repositoryVersionDAO = repositoryVersionDAO;
+    return repoEntity;
+  }
+
 }
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/addservice/RequestValidatorTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/addservice/RequestValidatorTest.java
index 6b285dc..d0ab25c 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/addservice/RequestValidatorTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/addservice/RequestValidatorTest.java
@@ -20,6 +20,7 @@ package org.apache.ambari.server.topology.addservice;
 import static java.util.stream.Collectors.toList;
 import static java.util.stream.Collectors.toMap;
 import static java.util.stream.Collectors.toSet;
+import static org.apache.ambari.server.utils.Assertions.assertThrows;
 import static org.easymock.EasyMock.expect;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -604,17 +605,4 @@ public class RequestValidatorTest extends EasyMockSupport {
     ));
   }
 
-  private static <T extends Throwable> T assertThrows(Class<T> 
expectedException, Runnable code) {
-    try {
-      code.run();
-    } catch (Throwable t) {
-      if (expectedException.isInstance(t)) {
-        return expectedException.cast(t);
-      }
-      throw new AssertionError("Expected exception: " + expectedException + " 
but " + t.getClass() + " was thrown instead");
-    }
-
-    throw new AssertionError("Expected exception: " + expectedException + ", 
but was not thrown");
-  }
-
 }
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/utils/Assertions.java 
b/ambari-server/src/test/java/org/apache/ambari/server/utils/Assertions.java
new file mode 100644
index 0000000..8599185
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/utils/Assertions.java
@@ -0,0 +1,35 @@
+/*
+ * 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.utils;
+
+public class Assertions {
+
+  public static <T extends Exception> T assertThrows(Class<T> 
expectedException, Runnable code) {
+    try {
+      code.run();
+    } catch (Exception e) {
+      if (expectedException.isInstance(e)) {
+        return expectedException.cast(e);
+      }
+      throw new AssertionError("Expected exception: " + expectedException + " 
but " + e.getClass() + " was thrown instead", e);
+    }
+
+    throw new AssertionError("Expected exception: " + expectedException + ", 
but was not thrown");
+  }
+
+}

Reply via email to