incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject git commit: AMBARI-3706 - Absent cluster name validation on query for Requests
Date Fri, 08 Nov 2013 14:49:20 GMT
Updated Branches:
  refs/heads/trunk 327e4cc91 -> 79691bccd


AMBARI-3706 - Absent cluster name validation on query for Requests


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

Branch: refs/heads/trunk
Commit: 79691bccdcf02c737976ea6581fa532fcc1db19e
Parents: 327e4cc
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Wed Nov 6 14:54:54 2013 -0500
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Fri Nov 8 09:48:49 2013 -0500

----------------------------------------------------------------------
 .../internal/RequestResourceProvider.java       | 14 +++-
 .../internal/RequestResourceProviderTest.java   | 75 ++++++++++++++++++++
 2 files changed, 88 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/79691bcc/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
index f8c9065..d9f840c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java
@@ -33,6 +33,7 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
+import org.apache.ambari.server.state.Clusters;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -207,11 +208,22 @@ class RequestResourceProvider extends AbstractControllerResourceProvider
{
   private Set<Resource> getRequestResources(String clusterName,
                                             Long requestId,
                                             String requestStatus,
-                                            Set<String> requestedPropertyIds) throws
NoSuchResourceException {
+                                            Set<String> requestedPropertyIds)
+      throws NoSuchResourceException, NoSuchParentResourceException {
 
     Set<Resource> response = new HashSet<Resource>();
     ActionManager actionManager = getManagementController().getActionManager();
 
+    if (clusterName != null) {
+      Clusters clusters = getManagementController().getClusters();
+      //validate that cluster exists, throws exception if it doesn't.
+      try {
+        clusters.getCluster(clusterName);
+      } catch (AmbariException e) {
+        throw new NoSuchParentResourceException(e.getMessage(), e);
+      }
+    }
+
     if (requestId == null) {
       org.apache.ambari.server.actionmanager.RequestStatus status = null;
       if (requestStatus != null) {

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/79691bcc/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
index 49f49c6..016ba59 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
@@ -18,18 +18,22 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import org.apache.ambari.server.AmbariException;
 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.controller.ExecuteActionRequest;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.RequestStatusResponse;
+import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 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.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
 import org.easymock.Capture;
 import org.junit.Assert;
 import org.junit.Test;
@@ -149,6 +153,77 @@ public class RequestResourceProviderTest {
   }
 
   @Test
+  public void testGetResourcesWithCluster() throws Exception {
+    Resource.Type type = Resource.Type.Request;
+
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    ActionManager actionManager = createNiceMock(ActionManager.class);
+    HostRoleCommand hostRoleCommand = createNiceMock(HostRoleCommand.class);
+    Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+
+    List<HostRoleCommand> hostRoleCommands = new LinkedList<HostRoleCommand>();
+    hostRoleCommands.add(hostRoleCommand);
+
+    Capture<Collection<Long>> requestIdsCapture = new Capture<Collection<Long>>();
+    Capture<List<Long>> requestIdListCapture = new Capture<List<Long>>();
+
+    Map<Long, String> requestContexts = new HashMap<Long, String>();
+    requestContexts.put(100L, "this is a context");
+
+    // set expectations
+    expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
+    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes();
+    expect(clusters.getCluster("bad-cluster")).andThrow(new AmbariException("bad cluster!")).anyTimes();
+    expect(actionManager.getAllTasksByRequestIds(capture(requestIdsCapture))).andReturn(hostRoleCommands);
+    expect(actionManager.getRequestContext(capture(requestIdListCapture))).andReturn(requestContexts);
+    expect(hostRoleCommand.getRequestId()).andReturn(100L).anyTimes();
+    expect(hostRoleCommand.getStatus()).andReturn(HostRoleStatus.IN_PROGRESS);
+
+    // replay
+    replay(managementController, actionManager, hostRoleCommand, clusters, cluster);
+
+    ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
+        type,
+        PropertyHelper.getPropertyIds(type),
+        PropertyHelper.getKeyPropertyIds(type),
+        managementController);
+
+    Set<String> propertyIds = new HashSet<String>();
+
+    propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID);
+    propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID);
+
+    Predicate predicate = new PredicateBuilder().
+        property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("c1").and().
+        property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").
+        toPredicate();
+    Request request = PropertyHelper.getReadRequest(propertyIds);
+    Set<Resource> resources = provider.getResources(request, predicate);
+
+    Assert.assertEquals(1, resources.size());
+    for (Resource resource : resources) {
+      Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID));
+      Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID));
+    }
+
+    // try again with a bad cluster name
+    predicate = new PredicateBuilder().
+        property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("bad-cluster").and().
+        property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").
+        toPredicate();
+    try {
+      provider.getResources(request, predicate);
+    } catch (NoSuchParentResourceException e) {
+      e.printStackTrace();
+    }
+
+    // verify
+    verify(managementController, actionManager, hostRoleCommand, clusters, cluster);
+  }
+
+  @Test
   public void testGetResourcesOrPredicate() throws Exception {
     Resource.Type type = Resource.Type.Request;
 


Mime
View raw message