Skip to content

Commit 73a8006

Browse files
committed
tests/driver_methods_utest.c: adapt for legacy and modern alarms [networkupstools#2931]
Signed-off-by: desertwitch <[email protected]>
1 parent 92b73d7 commit 73a8006

File tree

1 file changed

+147
-54
lines changed

1 file changed

+147
-54
lines changed

tests/driver_methods_utest.c

Lines changed: 147 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,51 +75,81 @@ int main(int argc, char **argv) {
7575
cases_passed = 0;
7676
cases_failed = 0;
7777

78-
/* test case #1 */
78+
/* test case #1 (from scratch) */
79+
/* we test for duplicate status tokens (no alarms) */
80+
/* expectation: no duplicates among status tokens */
7981
status_init();
80-
nut_debug_level = 6;
8182
status_set(" OL ");
8283
status_set("OL BOOST");
8384
status_set("OB ");
8485
status_set(" BOOST");
8586
status_commit();
87+
8688
valueStr = dstate_getinfo("ups.status");
87-
nut_debug_level = 0;
8889
report_0_means_pass(strcmp(valueStr, "OL BOOST OB"));
89-
printf(" test for ups.status: '%s'; any duplicates?\n", NUT_STRARG(valueStr));
90+
printf(" test for ups.status: '%s'; got no duplicates?\n", NUT_STRARG(valueStr));
9091

91-
/* test case #2, build on top of #1 */
92+
/* test case #2 (built on top of #1) */
93+
/* we raise an alarm using the modern alarm functions */
94+
/* expectation: alarm status token is present */
95+
/* note: normally we would re-init and re-set the values */
9296
alarm_init();
9397
alarm_set("Test alarm 1");
9498
alarm_set("[Test alarm 2]");
9599
alarm_set("Test alarm 1");
96100
alarm_commit();
97-
/* Note: normally we re-init and re-set the values */
98-
status_commit();
101+
status_commit(); /* to register ALARM status */
102+
99103
valueStr = dstate_getinfo("ups.status");
100104
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB"));
101-
printf(" test for ups.status: '%s'; got alarm?\n", NUT_STRARG(valueStr));
105+
printf(" test for ups.status: '%s'; got alarm, no duplicates?\n", NUT_STRARG(valueStr));
102106

103-
/* test case #3, build on top of #2 */
107+
/* test case #3 (built on top of #2) */
108+
/* we raise an alarm using the modern alarm functions */
109+
/* expectation: three alarm messages stored in ups.alarm */
110+
/* note: normally we would re-init and re-set the values */
104111
valueStr = dstate_getinfo("ups.alarm");
105-
/* NOTE: no dedup here! */
106112
report_0_means_pass(strcmp(valueStr, "Test alarm 1 [Test alarm 2] Test alarm 1"));
107113
printf(" test for ups.alarm: '%s'; got 3 alarms?\n", NUT_STRARG(valueStr));
108114

109-
/* test case #4, build on top of #1 and #2 */
110-
/* Note: normally we re-init and re-set the values */
115+
/* test case #4 (built on top of #3) */
116+
/* reset the status, see if the raised modern alarm persists */
117+
/* expectation: alarm remains, no duplicate tokens reported */
118+
/* note: normally we would re-init and re-set the values */
119+
status_init();
120+
status_set(" OL ");
121+
status_set("OL BOOST");
122+
status_set("OB ");
123+
status_set(" BOOST");
111124
status_set("BOO");
112125
status_set("BOO");
113126
status_set("OST");
114127
status_set("OST");
115128
status_set("OOS");
116129
status_set("OOS");
117130
status_commit();
131+
118132
valueStr = dstate_getinfo("ups.status");
119133
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB BOO OST OOS"));
120-
printf(" test for ups.status: '%s'; any duplicates?\n", NUT_STRARG(valueStr));
134+
printf(" test for ups.status: '%s'; got alarm, no duplicates?\n", NUT_STRARG(valueStr));
135+
136+
/* test case #5 (built on top of #4) */
137+
/* reset the status, see if the raised modern alarm persists */
138+
/* expectation: three alarm messages still stored in ups.alarm */
139+
/* note: normally we would re-init and re-set the values */
140+
valueStr = dstate_getinfo("ups.alarm");
141+
report_0_means_pass(strcmp(valueStr, "Test alarm 1 [Test alarm 2] Test alarm 1"));
142+
printf(" test for ups.alarm: '%s'; got 3 alarms?\n", NUT_STRARG(valueStr));
143+
144+
/* clear testing state for next from-scratch test */
145+
alarm_init();
146+
alarm_commit();
147+
status_init();
148+
status_commit();
121149

122-
/* test case #5+#6 from scratch */
150+
/* test case #6 (from scratch) */
151+
/* mix legacy alarm status and modern alarm functions */
152+
/* expectation: alarm status token is present, no duplicates */
123153
status_init();
124154
alarm_init();
125155
status_set("OL BOOST");
@@ -131,13 +161,25 @@ int main(int argc, char **argv) {
131161

132162
valueStr = dstate_getinfo("ups.status");
133163
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB"));
134-
printf(" test for ups.status with explicit ALARM set via status_set() and alarm_set() was not used first: '%s'; any duplicate ALARM?\n", NUT_STRARG(valueStr));
164+
printf(" test for ups.status with explicit ALARM set via status_set() and alarm_set() was not used first: '%s'; got alarm, no duplicates?\n", NUT_STRARG(valueStr));
135165

166+
/* test case #7 (built on top of #6) */
167+
/* mix legacy alarm status and modern alarm functions */
168+
/* expectation: 1 alarm message is recorded in ups.alarm */
169+
/* note: normally we would re-init and re-set the values */
136170
valueStr = dstate_getinfo("ups.alarm");
137171
report_0_means_pass(strcmp(valueStr, "[Test alarm 2]"));
138-
printf(" test for ups.alarm with explicit ALARM set via status_set() and alarm_set() was not used first: '%s'; got 1 alarm (injected N/A replaced by later explicit text)\n", NUT_STRARG(valueStr));
172+
printf(" test for ups.alarm with explicit ALARM set via status_set() and alarm_set() was not used first: '%s'; got 1 alarm?\n", NUT_STRARG(valueStr));
139173

140-
/* test case #7+#8 from scratch */
174+
/* clear testing state for next from-scratch test */
175+
alarm_init();
176+
alarm_commit();
177+
status_init();
178+
status_commit();
179+
180+
/* test case #8 (from scratch) */
181+
/* mix legacy alarm status and modern alarm functions (reverse) */
182+
/* expectation: alarm status token is present, no duplicates */
141183
status_init();
142184
alarm_init();
143185
status_set("OL BOOST");
@@ -149,78 +191,129 @@ int main(int argc, char **argv) {
149191

150192
valueStr = dstate_getinfo("ups.status");
151193
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB"));
152-
printf(" test for ups.status with explicit ALARM set via status_set() and alarm_set() was used first: '%s'; any duplicate ALARM?\n", NUT_STRARG(valueStr));
194+
printf(" test for ups.status with explicit ALARM set via status_set() and alarm_set() was used first: '%s'; got alarm, no duplicates?\n", NUT_STRARG(valueStr));
153195

196+
/* test case #9 (built on top of #8) */
197+
/* mix legacy alarm status and modern alarm functions (reverse) */
198+
/* expectation: 1 alarm message is recorded in ups.alarm */
199+
/* note: normally we would re-init and re-set the values */
154200
valueStr = dstate_getinfo("ups.alarm");
155201
report_0_means_pass(strcmp(valueStr, "[Test alarm 2]"));
156-
printf(" test for ups.alarm with explicit ALARM set via status_set() and alarm_set() was used first: '%s'; got 1 alarm (none injected)\n", NUT_STRARG(valueStr));
202+
printf(" test for ups.alarm with explicit ALARM set via status_set() and alarm_set() was used first: '%s'; got 1 alarm?\n", NUT_STRARG(valueStr));
157203

158-
/* test case #9+#10 from scratch */
159-
status_init();
204+
/* clear testing state for next from-scratch test */
160205
alarm_init();
206+
alarm_commit();
207+
status_init();
208+
status_commit();
209+
210+
/* test case #10 (from scratch) */
211+
/* legacy alarm status and no modern alarm functions */
212+
/* expectation: alarm status token is present, no duplicates */
213+
status_init();
161214
status_set("OL BOOST");
162215
status_set("ALARM");
163216
status_set("OB");
164-
alarm_commit();
165217
status_commit();
166218

167219
valueStr = dstate_getinfo("ups.status");
168220
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB"));
169-
printf(" test for ups.status with explicit ALARM set via status_set() and no extra alarm_set(): '%s'; any duplicate ALARM?\n", NUT_STRARG(valueStr));
221+
printf(" test for ups.status with explicit ALARM set via status_set() and no extra alarm_ funcs: '%s'; got alarm, no duplicates?\n", NUT_STRARG(valueStr));
170222

223+
/* test case #11 (built on top of #10) */
224+
/* legacy alarm status and no modern alarm functions */
225+
/* expectation: no alarm message is recorded in ups.alarm */
226+
/* note: normally we would re-init and re-set the values */
171227
valueStr = dstate_getinfo("ups.alarm");
172-
report_0_means_pass(strcmp(NUT_STRARG(valueStr), "[N/A]"));
173-
printf(" test for ups.alarm with explicit ALARM set via status_set() and no extra alarm_set(): '%s'; got 1 (injected) alarm\n", NUT_STRARG(valueStr));
228+
report_0_means_pass(valueStr != NULL); /* should be NULL */
229+
printf(" test for ups.alarm with explicit ALARM set via status_set() and no extra alarm_ funcs: '%s'; got NULL?\n", NUT_STRARG(valueStr));
174230

175-
/* test case #11+#12 from scratch */
176-
/* flush ups.alarm report */
177-
alarm_init();
178-
alarm_commit();
231+
/* clear testing state for next from-scratch test */
232+
/* alarm_ was not used, should be cleared from before */
233+
status_init();
234+
status_commit();
235+
236+
/* test case #12 (from scratch) */
237+
/* ignored LB */
238+
/* expectation: previous legacy alarm is cleared, LB ignored */
239+
dstate_setinfo("driver.flag.ignorelb", "enabled");
179240

180241
status_init();
181-
alarm_init();
182242
status_set("OL BOOST");
183-
status_set("ALARM");
184243
status_set("OB");
185244
status_set("LB"); /* Should be honoured */
186245
status_commit();
187246

188247
valueStr = dstate_getinfo("ups.status");
189-
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB LB"));
190-
printf(" test for ups.status with explicit ALARM set via status_set() and no extra alarm_set() nor alarm_commit(): '%s'; is ALARM reported?\n", NUT_STRARG(valueStr));
248+
report_0_means_pass(strcmp(valueStr, "OL BOOST OB"));
249+
printf(" test for ups.status with ignored LB: '%s'; got no alarm, ignored LB, no duplicates?\n", NUT_STRARG(valueStr));
191250

192-
valueStr = dstate_getinfo("ups.alarm");
193-
/*
194-
report_0_means_pass(valueStr != NULL); // pass if valueStr is NULL
195-
printf(" test for ups.alarm with explicit ALARM set via status_set() and no extra alarm_set() nor alarm_commit(): '%s'; got no alarms spelled out\n", NUT_STRARG(valueStr));
196-
*/
197-
report_0_means_pass(strcmp(NUT_STRARG(valueStr), "[N/A]"));
198-
printf(" test for ups.alarm with explicit ALARM set via status_set() and no extra alarm_set() nor alarm_commit(): '%s'; got 1 (injected) alarm\n", NUT_STRARG(valueStr));
199-
200-
/* test case #13+#14 from scratch */
201-
/* flush ups.alarm report */
251+
/* clear testing state for next from-scratch test */
202252
alarm_init();
203253
alarm_commit();
254+
status_init();
255+
status_commit();
204256

205-
dstate_setinfo("driver.flag.ignorelb", "enabled");
257+
/* test case #13 (from scratch) */
258+
/* alarm raised with modern alarm functions, no status, no status commit */
259+
/* expectation: empty status, no whitespace */
260+
alarm_init();
261+
alarm_set("[Test alarm]");
262+
alarm_commit();
206263

264+
valueStr = dstate_getinfo("ups.status");
265+
report_0_means_pass(strcmp(valueStr, "\0"));
266+
printf(" test for ups.status with alarm_ set and no status_commit(): '%s'; got empty, no whitespace?\n", NUT_STRARG(valueStr));
267+
268+
/* test case #14 (built on top of #13) */
269+
/* alarm raised with modern alarm functions, no status, no status commit */
270+
/* expectation: 1 alarm message is recorded in ups.alarm */
271+
/* note: normally we would re-init and re-set the values */
272+
valueStr = dstate_getinfo("ups.alarm");
273+
report_0_means_pass(strcmp(valueStr, "[Test alarm]"));
274+
printf(" test for ups.status with alarm_ set and no status_commit(): '%s'; got 1 alarm?\n", NUT_STRARG(valueStr));
275+
276+
/* clear testing state for next from-scratch test */
277+
alarm_init();
278+
alarm_commit();
207279
status_init();
280+
status_commit();
281+
282+
/* test case #15 (from scratch) */
283+
/* alarm raised with modern alarm functions, no status, but status commit */
284+
/* expectation: alarm status token is present, no leading/trailing spaces */
208285
alarm_init();
209-
status_set("OL BOOST");
210-
alarm_set("[N/A]");
211-
alarm_set("[Test alarm 2]");
212-
status_set("OB");
213-
status_set("LB"); /* Should be ignored */
286+
alarm_set("alarm");
214287
alarm_commit();
288+
status_commit(); /* to register ALARM status */
289+
290+
valueStr = dstate_getinfo("ups.status");
291+
report_0_means_pass(strcmp(valueStr, "ALARM"));
292+
printf(" test for ups.status with alarm_ set, no status and status_commit(): '%s'; got alarm, no whitespace?\n", NUT_STRARG(valueStr));
293+
294+
/* clear testing state for next from-scratch test */
295+
alarm_init();
296+
alarm_commit();
297+
status_init();
215298
status_commit();
216299

300+
/* test case #16 (from scratch) */
301+
/* alarm raised with modern alarm functions, status commit before alarm commit */
302+
/* expectation: empty status, no whitespace */
303+
alarm_init();
304+
alarm_set("alarm");
305+
status_commit();
306+
alarm_commit(); /* too late? */
307+
217308
valueStr = dstate_getinfo("ups.status");
218-
report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB"));
219-
printf(" test for ups.status with explicit alarm_set(N/A) and another alarm_set(): '%s'; is ALARM reported?\n", NUT_STRARG(valueStr));
309+
report_0_means_pass(strcmp(valueStr, "\0"));
310+
printf(" test for ups.status with alarm_ set, status_commit() before alarm_commit(): '%s'; got empty, no whitespace?\n", NUT_STRARG(valueStr));
220311

221-
valueStr = dstate_getinfo("ups.alarm");
222-
report_0_means_pass(strcmp(valueStr, "[N/A] [Test alarm 2]"));
223-
printf(" test for ups.alarm with explicit alarm_set(N/A) and another alarm_set(): '%s'; got both alarms, namesake of not-injected is not overwritten\n", NUT_STRARG(valueStr));
312+
/* clear testing state before finish */
313+
alarm_init();
314+
alarm_commit();
315+
status_init();
316+
status_commit();
224317

225318
/* finish */
226319
printf("test_rules completed. Total cases %d, passed %d, failed %d\n",

0 commit comments

Comments
 (0)