Skip to content

Commit

Permalink
Add support to execute control actions for compatible groups. Functio…
Browse files Browse the repository at this point in the history
…nality added primarily to do integration testing for HQ-2080.
  • Loading branch information
pnguyen committed Apr 9, 2010
1 parent 764af6a commit 4fb92c4
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 49 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

Changes in HQApi 3.2

*) Add support to execute control actions for compatible groups.
Functionality added primarily to do integration testing
for HQ-2080.

*) [HQ-1309] Updated GroupApi to list groups by role. Functionality
added primarily to do integration testing for HQ-1309. No
corresponding CLI command was added.
Expand Down
7 changes: 4 additions & 3 deletions hqu/hqapi1/app/ControlController.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.hyperic.hq.hqapi1.ErrorCode;
import org.hyperic.hq.authz.shared.PermissionException
import org.hyperic.hq.grouping.shared.GroupNotCompatibleException
import org.hyperic.util.pager.PageControl

class ControlController extends ApiController {
Expand Down Expand Up @@ -100,9 +101,6 @@ class ControlController extends ApiController {
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id " + resourceId + " not found")
} else if (resource.entityId.isGroup()) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Group control action execution is not supported")
} else if (!action) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No action given")
Expand All @@ -115,6 +113,9 @@ class ControlController extends ApiController {
" does not support action " + action)
} catch (PermissionException e) {
failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED)
} catch (GroupNotCompatibleException e) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Control actions not supported for mixed groups")
} catch (Exception e) {
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR,
"Unexpected error: " + e.getMessage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testAddRemoveControlAction() throws Exception {

// Cleanup
cleanup(response.getAlertDefinition());
cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}

public void testAddControlActionWrongAction() throws Exception {
Expand Down Expand Up @@ -106,7 +106,7 @@ public void testAddControlActionWrongAction() throws Exception {

// Cleanup
cleanup(response.getAlertDefinition());
cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}

public void testAddControlActionUncontrollableResource() throws Exception {
Expand Down
6 changes: 3 additions & 3 deletions src/org/hyperic/hq/hqapi1/test/ControlAction_test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -77,7 +77,7 @@ public void testControlActionValidResource() throws Exception {

assertEquals("run", response.getAction().get(0));

cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}

public void testControlActionNoPermission() throws Exception {
Expand All @@ -94,6 +94,6 @@ public void testControlActionNoPermission() throws Exception {
hqAssertFailurePermissionDenied(response);

deleteTestUsers(users);
cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}
}
6 changes: 3 additions & 3 deletions src/org/hyperic/hq/hqapi1/test/ControlExecute_test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -62,7 +62,7 @@ public void testExecuteValidResource() throws Exception {
"run", arguments);
hqAssertSuccess(executeResponse);

cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}

public void testExecuteNoPermission() throws Exception {
Expand All @@ -80,6 +80,6 @@ public void testExecuteNoPermission() throws Exception {
hqAssertFailurePermissionDenied(executeResponse);

deleteTestUsers(users);
cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}
}
35 changes: 16 additions & 19 deletions src/org/hyperic/hq/hqapi1/test/ControlHistory_test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand All @@ -29,6 +29,7 @@

import org.hyperic.hq.hqapi1.ControlApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.types.ControlHistory;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ControlHistoryResponse;
import org.hyperic.hq.hqapi1.types.User;
Expand Down Expand Up @@ -64,6 +65,11 @@ public void testControlHistoryNoControlPlugin() throws Exception {
response.getControlHistory().size());
}

/**
* To validate HQ-2080
*
* @throws Exception
*/
public void testControlHistoryValidResource() throws Exception {
HQApi api = getApi();
ControlApi cApi = getApi().getControlApi();
Expand All @@ -74,24 +80,15 @@ public void testControlHistoryValidResource() throws Exception {
"run", new String[0]);
hqAssertSuccess(executeResponse);

long start = System.currentTimeMillis();
while (System.currentTimeMillis() < (start + 60000)) {
ControlHistory controlHistory = findControlHistory(controllableResource, 5);

ControlHistoryResponse historyResponse = cApi.getHistory(controllableResource);
hqAssertSuccess(historyResponse);
if (historyResponse.getControlHistory().size() > 0) {
assertEquals("Wrong number of items in control history for " +
controllableResource.getName(), 1,
historyResponse.getControlHistory().size());
cleanupControllableResource(api, controllableResource);
return;

}
Thread.sleep(1000);
}

cleanupControllableResource(api, controllableResource);
fail("Unable to find control history in timeout of 60 seconds");
// HQ-2080 eliminated Quartz when running a control action on
// a resource (non-group) for immediate execution, so there
// should be no delays.
long timeToStart = controlHistory.getStartTime() - controlHistory.getDateScheduled();
assertTrue(timeToStart < SECOND);

cleanupResource(api, controllableResource);
}

public void testControlHistoryNoPermission() throws Exception {
Expand All @@ -109,6 +106,6 @@ public void testControlHistoryNoPermission() throws Exception {
hqAssertFailurePermissionDenied(response);

deleteTestUsers(users);
cleanupControllableResource(api, controllableResource);
cleanupResource(api, controllableResource);
}
}
65 changes: 62 additions & 3 deletions src/org/hyperic/hq/hqapi1/test/ControlTestBase.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.types.ControlHistory;
import org.hyperic.hq.hqapi1.types.ControlHistoryResponse;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.ControlApi;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.ResourceApi;

public class ControlTestBase extends GroupTestBase {

protected static final long SECOND = 1000;
protected static final String STATUS_COMPLETED = "Completed";

public ControlTestBase(String name) {
super(name);
}

public Group createControllableGroup(HQApi api)
throws Exception
{
return createControllableGroup(api, 1);
}

public Group createControllableGroup(HQApi api, int numOfResources)
throws Exception
{
GroupApi groupApi = api.getGroupApi();

Resource controllableResource = createControllableResource(api);
Group g = generateTestGroup();
g.setResourcePrototype(controllableResource.getResourcePrototype());
g.getResource().add(controllableResource);

for (int i=0; i< numOfResources; i++) {
Resource controllableResource = createControllableResource(api);
g.setResourcePrototype(controllableResource.getResourcePrototype());
g.getResource().add(controllableResource);
}

GroupResponse createGroupResponse = groupApi.createGroup(g);
hqAssertSuccess(createGroupResponse);

assertEquals(numOfResources,
createGroupResponse.getGroup().getResource().size());

return createGroupResponse.getGroup();
}
Expand All @@ -46,4 +64,45 @@ public void cleanupControllableGroup(HQApi api, Group g)
StatusResponse response = groupApi.deleteGroup(g.getId());
hqAssertSuccess(response);
}

protected ControlHistory findControlHistory(Object resource, int timeout)
throws Exception {

ControlApi cApi = getApi().getControlApi();
ControlHistory controlHistory = null;
long start = System.currentTimeMillis();

while (System.currentTimeMillis() < (start + (timeout*SECOND))) {

ControlHistoryResponse historyResponse = null;
if (resource instanceof Resource) {
historyResponse = cApi.getHistory((Resource) resource);
} else {
historyResponse = cApi.getHistory((Group) resource);
}
hqAssertSuccess(historyResponse);

if (historyResponse.getControlHistory().size() > 0) {
assertEquals("Wrong number of items in control history",
1, historyResponse.getControlHistory().size());

ControlHistory log = historyResponse.getControlHistory().get(0);

if (STATUS_COMPLETED.equals(log.getStatus())) {
controlHistory = log;
break;
}

}
Thread.sleep(SECOND);
}

if (controlHistory == null) {
throw new Exception (
"Unable to find a completed control history in timeout of "
+ timeout + " seconds");
}

return controlHistory;
}
}
38 changes: 29 additions & 9 deletions src/org/hyperic/hq/hqapi1/test/GroupControlExecute_test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand All @@ -30,9 +30,11 @@
import org.hyperic.hq.hqapi1.ControlApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.User;

import java.util.ArrayList;
import java.util.List;

public class GroupControlExecute_test extends ControlTestBase {
Expand All @@ -51,23 +53,43 @@ public void testExecuteInvalidGroup() throws Exception {
hqAssertFailureObjectNotFound(response);
}

public void testExecuteValidGroup() throws Exception {
public void testExecuteCompatibleGroup() throws Exception {
HQApi api = getApi();

Group controlGroup = createControllableGroup(api);
Group controlGroup = createControllableGroup(api, 5);
String[] arguments = new String[0];

ControlApi cApi = api.getControlApi();
StatusResponse executeResponse = cApi.executeAction(controlGroup,
"run", arguments);

// TODO Update this when group control action execution is supported
//hqAssertSuccess(executeResponse);
hqAssertFailureNotSupported(executeResponse);
hqAssertSuccess(executeResponse);

cleanupControllableGroup(api, controlGroup);
}

public void testExecuteMixedGroup() throws Exception {
HQApi api = getApi();

Resource platform = getLocalPlatformResource(false, false);
Resource service = createControllableResource(api);
List<Resource> resources = new ArrayList<Resource>();
resources.add(platform);
resources.add(service);

Group mixedGroup = createGroup(resources);
String[] arguments = new String[0];

ControlApi cApi = api.getControlApi();
StatusResponse executeResponse = cApi.executeAction(mixedGroup,
"run", arguments);

hqAssertFailureNotSupported(executeResponse);

cleanupResource(api, service);
cleanupGroup(mixedGroup);
}

public void testExecuteNoPermission() throws Exception {
HQApi api = getApi();

Expand All @@ -81,9 +103,7 @@ public void testExecuteNoPermission() throws Exception {
StatusResponse executeResponse = cApiUnpriv.executeAction(controlGroup,
"run", new String[0]);

// TODO Update this when group control action execution is supported
//hqAssertFailurePermissionDenied(executeResponse);
hqAssertFailureNotSupported(executeResponse);
hqAssertFailurePermissionDenied(executeResponse);

deleteTestUsers(users);
cleanupControllableGroup(api, controlGroup);
Expand Down
Loading

0 comments on commit 4fb92c4

Please sign in to comment.