diff --git a/hqu/hqapi1/app/AlertController.groovy b/hqu/hqapi1/app/AlertController.groovy index c7b3bda8..d94a1343 100644 --- a/hqu/hqapi1/app/AlertController.groovy +++ b/hqu/hqapi1/app/AlertController.groovy @@ -13,6 +13,8 @@ public class AlertController extends ApiController { private aMan = AlertMan.one private escMan = EscMan.one + private static final int ROUNDING_VOODOO = 60000 + private getEscalationState(alert) { // TODO: Move to AlertCategory if (alert.stateId) { @@ -71,11 +73,14 @@ public class AlertController extends ApiController { try { Integer groupId = null AlertSeverity severity = AlertSeverity.findByCode(sev) - long timerange = end - begin PageInfo pInfo = PageInfo.create(0, count, AlertSortField.DATE, false); - alerts = alertHelper.findAlerts(severity, timerange, end, + // TODO: Work around incorrect TimingVoodoo in AlertManagerEJBImpl + long roundedEnd = end + (ROUNDING_VOODOO - (end % ROUNDING_VOODOO)) + long timerange = roundedEnd - begin + + alerts = alertHelper.findAlerts(severity, timerange, roundedEnd, inEsc, notFixed, groupId, pInfo) } catch (IllegalStateException e) { failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS, @@ -93,7 +98,8 @@ public class AlertController extends ApiController { out << failureXml } else { out << getSuccessXML() - for (a in alerts) { + // TODO: See above, re-apply the actual end time + for (a in alerts.findAll { it.ctime <= end }) { out << getAlertXML(a) } } @@ -130,7 +136,9 @@ public class AlertController extends ApiController { "Unable to find resource with " + "id=" + rid) } else { - alerts = resource.getAlerts(user, begin, end, + // TODO: Work around incorrect TimingVoodoo in AlertManagerEJBImpl + long roundedEnd = end + (ROUNDING_VOODOO - (end % ROUNDING_VOODOO)) + alerts = resource.getAlerts(user, begin, roundedEnd, count, severity) //TODO: Move these to ResourceCategory or AlertManager if (inEsc) { @@ -157,7 +165,8 @@ public class AlertController extends ApiController { out << failureXml } else { out << getSuccessXML() - for (a in alerts) { + // TODO: See above, re-apply the actual end time + for (a in alerts.findAll { it.ctime <= end }) { out << getAlertXML(a) } } diff --git a/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java b/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java index 5f60a4b5..7ebef370 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java @@ -5,6 +5,7 @@ import org.hyperic.hq.hqapi1.types.Alert; import org.hyperic.hq.hqapi1.types.StatusResponse; import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.types.Escalation; import org.hyperic.hq.hqapi1.AlertApi; public class AlertAck_test extends AlertTestBase { @@ -14,32 +15,39 @@ public AlertAck_test(String name) { } public void testAckAlert() throws Exception { - Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); AlertApi api = getAlertApi(); + Resource platform = getLocalPlatformResource(false, false); + Escalation e = createEscalation(); + Alert a = generateAlerts(platform); + + validateAlert(a); + + // Test ack - alert will be in Escalation + StatusResponse ackResponse = api.ackAlert(a.getId(), "Test ACK", 60000l); + hqAssertSuccess(ackResponse); - AlertsResponse response = api.findAlerts(platform, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); + // TODO: Valididate ack? Will require a getById API. + + // Cleanup + deleteAlertDefinitionByAlert(a); + deleteEscalation(e); + } - for (Alert a : response.getAlert()) { - validateAlert(a); - } + public void testAckUnacknowledableAlert() throws Exception { + Resource platform = getLocalPlatformResource(false, false); + AlertApi api = getAlertApi(); + Alert a = generateAlerts(platform); - // Test ack - Alert a = response.getAlert().get(0); + validateAlert(a); + // Test ack - alert is not in escalation StatusResponse ackResponse = api.ackAlert(a.getId(), "Test ACK", 60000l); hqAssertSuccess(ackResponse); // TODO: Valididate ack? Will require a getById API. // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); } public void testAckInvalidAlert() throws Exception { diff --git a/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java b/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java index 9addba31..2f4d623c 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java @@ -17,48 +17,28 @@ public AlertDelete_test(String name) { } public void testDeleteAlert() throws Exception { - Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); AlertApi api = getAlertApi(); + Resource platform = getLocalPlatformResource(false, false); + Alert a = generateAlerts(platform); - AlertsResponse response = api.findAlerts(platform, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); + validateAlert(a); - for (Alert a : response.getAlert()) { - validateAlert(a); - } - // Test delete - Alert a = response.getAlert().get(0); - StatusResponse deleteResponse = api.delete(a.getId()); hqAssertSuccess(deleteResponse); // TODO: Valididate alert was deleted? Will require a getById API. // Cleanup - StatusResponse deleteDefResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteDefResponse); + deleteAlertDefinitionByAlert(a); } public void testDeleteAlertNoPermission() throws Exception { - Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); AlertApi api = getAlertApi(); + Resource platform = getLocalPlatformResource(false, false); + Alert a = generateAlerts(platform); - AlertsResponse response = api.findAlerts(platform, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); - - for (Alert a : response.getAlert()) { - validateAlert(a); - } + validateAlert(a); // Test delete with an unprivledged user @@ -66,17 +46,13 @@ public void testDeleteAlertNoPermission() throws Exception { User unprivUser = users.get(0); AlertApi apiUnpriv = getApi(unprivUser.getName(), TESTUSER_PASSWORD).getAlertApi(); - Alert a = response.getAlert().get(0); StatusResponse deleteResponse = apiUnpriv.delete(a.getId()); hqAssertFailurePermissionDenied(deleteResponse); // TODO: Valididate alert was deleted? Will require a getById API. // Cleanup - StatusResponse deleteDefResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteDefResponse); - + deleteAlertDefinitionByAlert(a); deleteTestUsers(users); } diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java index a03ae171..2f273d14 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java @@ -15,7 +15,7 @@ public AlertFindByResource_test(String name) { public void testFindValid() throws Exception { Resource r = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(r); + Alert a = generateAlerts(r); AlertApi api = getAlertApi(); @@ -25,14 +25,12 @@ public void testFindValid() throws Exception { assertTrue(response.getAlert().size() <= 10); assertTrue(response.getAlert().size() > 0); - for (Alert a : response.getAlert()) { - validateAlert(a); + for (Alert alerts : response.getAlert()) { + validateAlert(alerts); } // Cleanup - StatusResponse deleteResponse = getApi().getAlertDefinitionApi(). - deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); } public void testFindInvalidSeverity() throws Exception { diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java index fa32fde3..6885a563 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java @@ -14,9 +14,9 @@ public AlertFind_test(String name) { } public void testFindValid() throws Exception { - Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); AlertApi api = getAlertApi(); + Resource platform = getLocalPlatformResource(false, false); + Alert a = generateAlerts(platform); AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), 10, 1, false, false); @@ -24,14 +24,12 @@ public void testFindValid() throws Exception { assertTrue(response.getAlert().size() <= 10); assertTrue(response.getAlert().size() > 0); - for (Alert a : response.getAlert()) { - validateAlert(a); + for (Alert alerts : response.getAlert()) { + validateAlert(alerts); } // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); } public void testFindInvalidSeverity() throws Exception { diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java index 7d6c5310..a3ae2f9e 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java @@ -20,71 +20,46 @@ public AlertFix_test(String name) { } public void testFixAlert() throws Exception { - Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); AlertApi api = getAlertApi(); + Resource platform = getLocalPlatformResource(false, false); + Alert a = generateAlerts(platform); - AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); + validateAlert(a); - for (Alert a : response.getAlert()) { - validateAlert(a); - } - // Test marking fixed - Alert a = response.getAlert().get(0); - StatusResponse fixResponse = api.fixAlert(a.getId()); hqAssertSuccess(fixResponse); // TODO: Valididate fix flag was set? Will require a getById API. // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); } public void testFixPlatformAlertNoPermission() throws Exception { Resource platform = getLocalPlatformResource(false, false); - AlertDefinition d = generateAlerts(platform); - AlertApi api = getAlertApi(); + Alert a = generateAlerts(platform); - AlertsResponse response = api.findAlerts(platform, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); - - for (Alert a : response.getAlert()) { - validateAlert(a); - } + validateAlert(a); List users = createTestUsers(1); User unprivUser = users.get(0); AlertApi apiUnpriv = getApi(unprivUser.getName(), TESTUSER_PASSWORD).getAlertApi(); // Test marking fixed with an unprivlidged user - Alert a = response.getAlert().get(0); - StatusResponse fixResponse = apiUnpriv.fixAlert(a.getId()); hqAssertFailurePermissionDenied(fixResponse); // TODO: Valididate fix flag was set? Will require a getById API. // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); deleteTestUsers(users); } public void testFixServerAlertNoPermission() throws Exception { - ResourceApi rApi = getApi().getResourceApi(); AlertApi api = getAlertApi(); + ResourceApi rApi = getApi().getResourceApi(); ResourcePrototypeResponse protoResponse = rApi.getResourcePrototype("HQ Agent"); @@ -101,40 +76,28 @@ public void testFixServerAlertNoPermission() throws Exception { Resource server = resourcesResponse.getResource().get(0); - AlertDefinition d = generateAlerts(server); + Alert a = generateAlerts(server); - AlertsResponse response = api.findAlerts(server, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); - - for (Alert a : response.getAlert()) { - validateAlert(a); - } + validateAlert(a); List users = createTestUsers(1); User unprivUser = users.get(0); AlertApi apiUnpriv = getApi(unprivUser.getName(), TESTUSER_PASSWORD).getAlertApi(); // Test marking fixed with an unprivlidged user - Alert a = response.getAlert().get(0); - StatusResponse fixResponse = apiUnpriv.fixAlert(a.getId()); hqAssertFailurePermissionDenied(fixResponse); // TODO: Valididate fix flag was set? Will require a getById API. // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); deleteTestUsers(users); } public void testFixServiceAlertNoPermission() throws Exception { - ResourceApi rApi = getApi().getResourceApi(); AlertApi api = getAlertApi(); + ResourceApi rApi = getApi().getResourceApi(); ResourcePrototypeResponse protoResponse = rApi.getResourcePrototype("FileServer Mount"); @@ -151,34 +114,21 @@ public void testFixServiceAlertNoPermission() throws Exception { Resource service = resourcesResponse.getResource().get(0); - AlertDefinition d = generateAlerts(service); - - AlertsResponse response = api.findAlerts(service, 0, System.currentTimeMillis(), - 10, 1, false, false); - hqAssertSuccess(response); - assertTrue(response.getAlert().size() <= 10); - assertTrue(response.getAlert().size() > 0); - - for (Alert a : response.getAlert()) { - validateAlert(a); - } + Alert a = generateAlerts(service); + validateAlert(a); List users = createTestUsers(1); User unprivUser = users.get(0); AlertApi apiUnpriv = getApi(unprivUser.getName(), TESTUSER_PASSWORD).getAlertApi(); // Test marking fixed with an unprivlidged user - Alert a = response.getAlert().get(0); - StatusResponse fixResponse = apiUnpriv.fixAlert(a.getId()); hqAssertFailurePermissionDenied(fixResponse); // TODO: Valididate fix flag was set? Will require a getById API. // Cleanup - StatusResponse deleteResponse = getApi(). - getAlertDefinitionApi().deleteAlertDefinition(d.getId()); - hqAssertSuccess(deleteResponse); + deleteAlertDefinitionByAlert(a); deleteTestUsers(users); } diff --git a/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java b/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java index fda9db76..33129ff6 100644 --- a/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java +++ b/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java @@ -6,6 +6,7 @@ import org.hyperic.hq.hqapi1.MetricDataApi; import org.hyperic.hq.hqapi1.HQApi; import org.hyperic.hq.hqapi1.MetricApi; +import org.hyperic.hq.hqapi1.EscalationApi; import org.hyperic.hq.hqapi1.types.AlertDefinition; import org.hyperic.hq.hqapi1.types.Resource; import org.hyperic.hq.hqapi1.types.StatusResponse; @@ -15,6 +16,8 @@ import org.hyperic.hq.hqapi1.types.MetricsResponse; import org.hyperic.hq.hqapi1.types.Metric; import org.hyperic.hq.hqapi1.types.DataPoint; +import org.hyperic.hq.hqapi1.types.Escalation; +import org.hyperic.hq.hqapi1.types.EscalationResponse; import java.util.Random; import java.util.List; @@ -40,18 +43,51 @@ protected void validateAlert(Alert a) throws Exception { assertTrue("Empty long reason", a.getReason().length() > 0); } + protected Escalation createEscalation() throws Exception { + EscalationApi escalationApi = getApi().getEscalationApi(); + + Escalation e = new Escalation(); + Random r = new Random(); + e.setName("Test Escalation" + r.nextInt()); + e.setMaxPauseTime(24 * 60 * 60 * 1000); + e.setRepeat(true); + e.setDescription("Test escalation for Alert tests"); + e.setPauseAllowed(true); + + EscalationResponse response = escalationApi.createEscalation(e); + hqAssertSuccess(response); + return response.getEscalation(); + } + + protected void deleteEscalation(Escalation e) throws Exception { + EscalationApi api = getApi().getEscalationApi(); + StatusResponse response = api.deleteEscalation(e.getId()); + hqAssertSuccess(response); + } + + protected Alert generateAlerts(Resource resource) throws Exception { + return generateAlerts(resource, null); + } + /** * Setup an AlertDefinition that will fire Alert instances waiting for * at least 1 alert to be generated. * - * @return The AlertDefinition that was created to generate the alerts. + * @param resource The resource to generate Alerts on + * @param e Optional escalation to assign to the AlertDefiniton that is created + * + * @return The Alert that was created. + * @throws Exception If an error occurs generating the alerts */ - protected AlertDefinition generateAlerts(Resource resource) throws Exception { + protected Alert generateAlerts(Resource resource, + Escalation e) throws Exception { HQApi api = getApi(); AlertDefinitionApi defApi = api.getAlertDefinitionApi(); MetricApi metricApi = api.getMetricApi(); MetricDataApi dataApi = api.getMetricDataApi(); + long start = System.currentTimeMillis(); + // Find availability metric for the passed in resource MetricsResponse metricsResponse = metricApi.getMetrics(resource, true); hqAssertSuccess(metricsResponse); @@ -74,6 +110,9 @@ protected AlertDefinition generateAlerts(Resource resource) throws Exception { d.setPriority(AlertDefinitionBuilder.AlertPriority.MEDIUM.getPriority()); d.setActive(true); d.setResource(resource); + if (e != null) { + d.setEscalation(e); + } d.getAlertCondition().add(AlertDefinitionBuilder. createThresholdCondition(true, availMetric.getName(), AlertDefinitionBuilder.AlertComparator.GREATER_THAN, -1)); @@ -82,6 +121,9 @@ protected AlertDefinition generateAlerts(Resource resource) throws Exception { definitions.add(d); AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions); hqAssertSuccess(response); + assertEquals("Should have found only one Definition from sync", + 1, response.getAlertDefinition().size()); + AlertDefinition def = response.getAlertDefinition().get(0); // Insert a fake 'up' measurement List dataPoints = new ArrayList(); @@ -92,26 +134,37 @@ protected AlertDefinition generateAlerts(Resource resource) throws Exception { StatusResponse dataResponse = dataApi.addData(availMetric, dataPoints); hqAssertSuccess(dataResponse); - // Now we wait.. - System.out.println("Waiting for alerts on " + resource.getName() + "..."); - for (int i = 0; i < 120; i++) { + final int TIMEOUT = 120; + for (int i = 0; i < TIMEOUT; i++) { // Wait for alerts - AlertsResponse alerts = getAlertApi().findAlerts(resource, 0, + AlertsResponse alerts = getAlertApi().findAlerts(resource, start, System.currentTimeMillis(), - 10, 1, false, false); + 10, 1, (e != null), false); hqAssertSuccess(alerts); - if (alerts.getAlert().size() > 0) { - System.out.println("Found " + alerts.getAlert().size() + " alerts!"); - return response.getAlertDefinition().get(0); + + for (Alert a : alerts.getAlert()) { + // Verify this alert comes from the definition we just created + if (a.getAlertDefinitionId() == def.getId()) { + return a; + } } try { - Thread.sleep(1000); - } catch (InterruptedException e) { + Thread.sleep(500); + } catch (InterruptedException ex) { // Ignore } } - throw new Exception("Unable to find generated alerts!"); + throw new Exception("Unable to find generated alerts for " + + resource.getName() + " under alert definition " + + def.getName()); + } + + protected void deleteAlertDefinitionByAlert(Alert a) throws Exception { + AlertDefinitionApi api = getApi().getAlertDefinitionApi(); + + StatusResponse response = api.deleteAlertDefinition(a.getAlertDefinitionId()); + hqAssertSuccess(response); } }