/
AlertTestBase.java
196 lines (159 loc) · 7.32 KB
/
AlertTestBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.hyperic.hq.hqapi1.test;
import org.hyperic.hq.hqapi1.AlertApi;
import org.hyperic.hq.hqapi1.AlertDefinitionBuilder;
import org.hyperic.hq.hqapi1.AlertDefinitionApi;
import org.hyperic.hq.hqapi1.MetricDataApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.EscalationApi;
import org.hyperic.hq.hqapi1.EscalationActionBuilder;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
import org.hyperic.hq.hqapi1.types.AlertResponse;
import org.hyperic.hq.hqapi1.types.AlertsResponse;
import org.hyperic.hq.hqapi1.types.Alert;
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.io.IOException;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
public abstract class AlertTestBase extends AlertDefinitionTestBase {
public AlertTestBase(String name) {
super(name);
}
protected AlertApi getAlertApi() {
return getApi().getAlertApi();
}
protected Alert getAlert(Integer alertId) throws IOException {
AlertResponse response = getAlertApi().getAlert(alertId);
hqAssertSuccess(response);
Alert a = response.getAlert();
validateAlert(a);
return a;
}
protected void validateAlert(Alert a) {
assertNotNull("fixed was NULL", a.isFixed());
assertTrue("ctime is incorrect", a.getCtime() > 0);
assertTrue("resourceId is invalid", a.getResourceId() > 0);
assertTrue("alertDefinitionId is invalid", a.getAlertDefinitionId() > 0);
assertNotNull("Alert name was null", a.getName());
assertTrue("Alert id is incorrect", a.getId() > 0);
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);
e.getAction().add(EscalationActionBuilder.createNoOpAction(60000));
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.
*
* @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 Alert generateAlerts(Resource resource,
Escalation e) throws Exception {
long start = System.currentTimeMillis();
// Find availability metric for the passed in resource
Metric availMetric = findAvailabilityMetric(resource);
// Create alert definition
AlertDefinition d = generateTestDefinition();
d.setDescription("Definition that will always fire, allowing for testing of Alerts");
d.setResource(resource);
if (e != null) {
d.setEscalation(e);
}
d.getAlertCondition().add(AlertDefinitionBuilder.
createThresholdCondition(true, availMetric.getName(),
AlertDefinitionBuilder.AlertComparator.GREATER_THAN, -1));
AlertDefinition def = syncAlertDefinition(d);
// Insert a fake 'up' measurement
sendAvailabilityDataPoint(resource, 1.0);
return findAlert(def, start);
}
protected Alert fireAvailabilityAlert(AlertDefinition def,
boolean willRecover,
double availability)
throws Exception {
long start = System.currentTimeMillis();
// Insert a fake availability data point so that
// the alert definition will fire.
sendAvailabilityDataPoint(def.getResource(), availability);
Alert alert = findAlert(def, start);
assertFalse("The alert should not be fixed",
alert.isFixed());
// Get the updated alert definition
AlertDefinition updatedDef = getAlertDefinition(alert.getAlertDefinitionId());
validateAvailabilityAlertDefinition(updatedDef,
willRecover,
willRecover ? false : true);
return alert;
}
protected void sendAvailabilityDataPoint(Resource resource, double availability)
throws IOException {
MetricDataApi dataApi = getApi().getMetricDataApi();
// Find availability metric for the resource
Metric availMetric = findAvailabilityMetric(resource);
List<DataPoint> dataPoints = new ArrayList<DataPoint>();
DataPoint dp = new DataPoint();
dp.setTimestamp(System.currentTimeMillis());
dp.setValue(availability);
dataPoints.add(dp);
StatusResponse dataResponse = dataApi.addData(availMetric, dataPoints);
hqAssertSuccess(dataResponse);
}
protected Alert findAlert(AlertDefinition def, long start)
throws Exception {
final int TIMEOUT = 120;
for (int i = 0; i < TIMEOUT; i++) {
// Wait for alerts
AlertsResponse alerts = getAlertApi().findAlerts(def.getResource(), start,
System.currentTimeMillis(),
10, 1,
(def.getEscalation() != null),
false);
hqAssertSuccess(alerts);
for (Alert a : alerts.getAlert()) {
// Verify this alert comes from the definition we just created
if (a.getAlertDefinitionId() == def.getId()) {
validateAlert(a);
return a;
}
}
pauseTest(500);
}
throw new Exception("Unable to find generated alerts for " +
def.getResource().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);
}
}