7 class Recurring_downtime_Test
extends PHPUnit_Framework_TestCase
{
8 var $scheduletimeofday = "12:00";
9 var $scheduleendtime = "15:00";
10 var $basecomment = 'Recurring Downtime Test Schedule For ';
13 * Set up prerequisities for this test
15 public function setUp() {
16 $this->auth
= Auth
::instance(array('session_key' => false))->force_user(new Op5User_AlwaysAuth());
18 $this->sd
= new ScheduleDate_Model();
20 $this->basictests
= array(
21 mktime(23, 50, 0, 11, 11, 2036) => 'Schedule on a monday',
22 mktime(23, 50, 0, 11, 12, 2036) => 'Schedule on a tuesday',
23 mktime(23, 50, 0, 11, 13, 2036) => 'Schedule on a wednesday',
24 mktime(23, 50, 0, 11, 14, 2036) => 'Schedule on a thursday',
25 mktime(23, 50, 0, 11, 15, 2036) => 'Schedule on a friday',
26 mktime(23, 50, 0, 11, 16, 2036) => 'Schedule on a saturday',
27 mktime(23, 50, 0, 11, 17, 2036) => 'Schedule on a sunday',
28 mktime(23, 50, 0, 11, 30, 2036) => 'Schedule the first of a new month',
29 mktime(23, 50, 0, 2, 28, 2036) => 'Schedule on a leap day',
30 mktime(23, 50, 0, 12, 31, 2036) => 'Schedule on new years day',
34 public function tearDown() {
35 $db = Database
::instance();
36 $db->query("TRUNCATE TABLE recurring_downtime");
37 $db->query("TRUNCATE TABLE recurring_downtime_objects");
41 public function resubmit_and_cleanup($tests, $type) {
42 $ls = Livestatus
::instance();
43 $comment = $this->basecomment
. $type;
44 $current_number = count($ls->getDowntimes(array('filter' => array('comment' => $comment))));
48 foreach ($tests as $time => $description) {
49 $old_count[$time] = count($ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduletimeofday} +1 day", $time)), 'columns' => array('id'))));
51 // Test that we cannot create duplicate schedules. Function should return skipping to STDERR if duplicate.
52 exec('/usr/bin/php index.php default/cron/downtime/'.$time.' 2>&1', $output, $status);
53 $this->assertEquals(0, $status, 'Return code should be zero');
54 $this->assertNotEmpty($output, "$description twice should give error");
58 foreach ($tests as $time => $description) {
59 $dt = $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduletimeofday} +1 day", $time)), 'columns' => array('id')));
60 $this->assertCount($old_count[$time], $dt, 'There should still be the same number of matching downtimes from '.$description);
61 foreach ($dt as $row) {
66 $this->assertCount($current_number, $ls->getDowntimes(array('filter' => array('comment' => $comment))), 'Still same number of downtimes in total with our comment');
68 // Remove downtimes when tests are done.
69 $cmd = (strpos($type, 'host') !== false) ?
'DEL_HOST_DOWNTIME;' : 'DEL_SVC_DOWNTIME;';
70 foreach ($ids as $id) {
71 $res = nagioscmd
::submit_to_nagios($cmd . $id);
72 $this->assertTrue($res, 'Host delete command was submitted');
75 $this->assertCount(0, $ls->getDowntimes(array('filter' => array('comment' => $comment))), "Downtimes are gone after deleting them");
78 public function cron($tests, $type, $expected_number) {
79 $comment = $this->basecomment
. $type;
80 foreach ($tests as $time => $description) {
82 exec('/usr/bin/php index.php default/cron/downtime/'.$time.' 2>&1', $output, $status);
83 $this->assertEmpty($output, $description ." returned output: ".implode("\n", $output));
84 $this->assertEquals(0, $status, 'Return code should be zero');
87 sleep(3); # Y U SO SLOW?
89 $ls = Livestatus
::instance();
90 foreach ($tests as $time => $description) {
91 $dt = $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduletimeofday} +1 day", $time)), 'columns' => array('comment'), 'auth' => false));
92 $this->assertCount($expected_number, $dt, "Unexpected number of downtimes created from $description");
93 foreach ($dt as $row) {
94 $this->assertEquals('AUTO: ' . $comment, $row['comment'], "Downtimes matching $description should have proper comment");
100 * Test if everyday schedule for hosts work
102 public function test_schedule_hosts() {
103 $comment = $this->basecomment
.'hosts';
106 "downtime_type" => "hosts",
107 "objects" => array("monitor"),
108 "comment" => $comment,
109 "start_time" => $this->scheduletimeofday
,
110 "end_time" => $this->scheduleendtime
,
111 "duration" => "2:00",
113 "weekdays" => array("1","2","3","4","5","6","0"),
114 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
116 $this->assertTrue($this->sd
->edit_schedule($data, $id));
117 $db = Database
::instance();
118 $sql = "SELECT id FROM recurring_downtime WHERE comment = {$db->escape($comment)} ORDER BY id DESC";
119 $result = $db->query($sql);
120 $this->assertCount(1, $result, "After creating a new schedule, there's only one with that name");
122 $this->cron($this->basictests
, 'hosts', 1);
123 $this->resubmit_and_cleanup($this->basictests
, 'hosts');
125 $sql = "DELETE FROM recurring_downtime WHERE comment = {$db->escape($comment)}";
126 $result = $db->query($sql);
127 $this->assertCount(1, $result, "One schedule was deleted.");
131 * Test if everyday schedule for hosts work
133 public function test_schedule_hostgroups() {
134 $comment = $this->basecomment
. 'hostgroups';
137 "downtime_type" => "hostgroups",
138 "objects" => array("hostgroup_up", "hostgroup_all"),
139 "comment" => $comment,
140 "start_time" => $this->scheduletimeofday
,
141 'end_time' => $this->scheduleendtime
,
142 "duration" => "2:00",
144 "weekdays" => array("1","2","3","4","5","6","0"),
145 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
147 # The number is wrong.
148 # Any overlapping hosts will be added twice.
149 # However, it's slightly better with two downtimes than none.
151 $ls = Livestatus
::instance();
152 foreach ($data['objects'] as $group) {
153 $number +
= count($ls->getHosts(array('columns' => array('name'), 'filter' => array('groups' => array('>=' => $group)))));
157 $this->assertTrue($this->sd
->edit_schedule($data, $id));
158 $db = Database
::instance();
159 $sql = "SELECT id FROM recurring_downtime WHERE comment = {$db->escape($comment)} ORDER BY id DESC";
160 $result = $db->query($sql);
161 $this->assertCount(1, $result, "After creating a new schedule, there's only one with that name");
163 $this->cron($this->basictests
, 'hostgroups', $number);
164 $this->resubmit_and_cleanup($this->basictests
, 'hostgroups');
166 $sql = "DELETE FROM recurring_downtime WHERE comment = {$db->escape($comment)}";
167 $result = $db->query($sql);
168 $this->assertCount(1, $result, "One schedule was deleted.");
172 *↦ Test if everyday schedule for services work
174 public function test_schedule_services() {
175 $comment = $this->basecomment
. 'services';
178 "downtime_type" => "services",
179 "objects" => array("host_down;service ok", "host_down;service critical"),
180 "comment" => $comment,
181 "start_time" => $this->scheduletimeofday
,
182 'end_time' => $this->scheduleendtime
,
183 "duration" => "2:00",
185 "weekdays" => array("1","2","3","4","5","6","0"),
186 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
188 $this->assertTrue($this->sd
->edit_schedule($data, $id));
189 $db = Database
::instance();
190 $sql = "SELECT id FROM recurring_downtime WHERE comment = {$db->escape($comment)} ORDER BY id DESC";
191 $result = $db->query($sql);
192 $this->assertCount(1, $result, "After creating a new schedule, there's only one with that name");
194 $this->cron($this->basictests
, 'services', 2);
195 $this->resubmit_and_cleanup($this->basictests
, 'services');
197 $db = Database
::instance();
198 $sql = "DELETE FROM recurring_downtime WHERE comment = {$db->escape($comment)}";
199 $result = $db->query($sql);
200 $this->assertCount(1, $result, "One schedule was deleted.");
204 *↦ Test if everyday schedule for services work
206 public function test_schedule_servicegroups() {
207 $comment = $this->basecomment
. 'servicegroups';
210 "downtime_type" => "servicegroups",
211 "objects" => array("servicegroup_ok", "servicegroup_critical"),
212 "comment" => $comment,
213 "start_time" => $this->scheduletimeofday
,
214 'end_time' => $this->scheduleendtime
,
215 "duration" => "2:00",
217 "weekdays" => array("1","2","3","4","5","6","0"),
218 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
220 $this->assertTrue($this->sd
->edit_schedule($data, $id));
221 $db = Database
::instance();
222 $sql = "SELECT id FROM recurring_downtime WHERE comment = {$db->escape($comment)} ORDER BY id DESC";
223 $result = $db->query($sql);
224 $this->assertCount(1 ,$result, "After creating a new schedule, there's only one with that name");
226 # The number is wrong.
227 # Any overlapping hosts will be added twice.
228 # However, it's slightly better with two downtimes than none.
230 $ls = Livestatus
::instance();
231 foreach ($data['objects'] as $group) {
232 $number +
= count($ls->getServices(array('filter' => array('groups' => array('>=' => $group)))));
235 $this->cron($this->basictests
, 'servicegroups', $number);
236 $this->resubmit_and_cleanup($this->basictests
, 'servicegroups');
238 $db = Database
::instance();
239 $sql = "DELETE FROM recurring_downtime WHERE comment = {$db->escape($comment)}";
240 $result = $db->query($sql);
241 $this->assertCount(1, $result, "One schedule was deleted.");
244 public function test_host_noschedule() {
245 $comment = $this->basecomment
. "hosts";
248 "downtime_type" => "hosts",
249 "objects" => array("monitor"),
250 "comment" => $comment,
251 "start_time" => $this->scheduletimeofday
,
252 'end_time' => $this->scheduleendtime
,
253 "duration" => "2:00",
255 "weekdays" => array("1","2","3","4","5","6"),
256 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
258 $this->assertTrue($this->sd
->edit_schedule($data, $id));
260 $tests_expected = array(
261 strtotime("2036-01-17 23:50") => "Schedule on thursday when sunday is excluded",
263 $tests_unexpected = array(
264 strtotime("2036-01-19 23:50") => "Schedule on saturday when sunday is excluded",
267 $this->cron($tests_expected, 'hosts', 1);
268 $this->resubmit_and_cleanup($tests_expected, 'hosts');
270 $this->cron($tests_unexpected, 'hosts', 0);
271 # Honey, I swear, Nothing Happened, so there's nothing to clean up!
273 $db = Database
::instance();
274 $sql = "DELETE FROM recurring_downtime WHERE comment = {$db->escape($comment)}";
275 $result = $db->query($sql);
276 $this->assertCount(1, $result, "One schedule should be deleted.");
279 public function test_host_acrossmidnight() {
280 $comment = $this->basecomment
. "hosts";
283 "downtime_type" => "hosts",
284 "objects" => array("monitor"),
285 "comment" => $comment,
286 "start_time" => $this->scheduleendtime
,
287 'end_time' => $this->scheduletimeofday
,
288 "duration" => "2:00",
290 "weekdays" => array("1","2","3","4","5","6","0"),
291 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
293 $this->assertTrue($this->sd
->edit_schedule($data, $id));
294 $time = mktime(23, 50, 0, 11, 11, 2036);
297 exec('/usr/bin/php index.php default/cron/downtime/'.$time.' 2>&1', $output, $status);
298 $this->assertEmpty($output, "acrossmidnight returned output: ".implode("\n", $output));
299 $this->assertEquals(0, $status, 'Return code should be zero');
301 sleep(3); # Y U SO SLOW?
303 $ls = Livestatus
::instance();
304 $dt = $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduleendtime} +1 day", $time), 'end_time' => strtotime("{$this->scheduletimeofday} +2 days", $time)), 'columns' => array('id'), 'auth' => false));
305 $this->assertCount(1, $dt, "Unexpected number of downtimes created from acrossmidnight");
307 // Remove downtimes when tests are done.
308 $cmd = 'DEL_HOST_DOWNTIME;';
309 foreach ($dt as $id) {
310 $res = nagioscmd
::submit_to_nagios($cmd . $id['id']);
311 $this->assertTrue($res, 'Host delete command was submitted');
314 $this->assertCount(0, $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduleendtime} +1 day", $time), 'end_time' => strtotime("{$this->scheduletimeofday} +2 days", $time)))), "Downtimes are gone after deleting them");
317 public function test_realistic_time() {
318 $comment = $this->basecomment
. "tomorrow";
321 "downtime_type" => "hosts",
322 "objects" => array("monitor"),
323 "comment" => $comment,
324 "start_time" => $this->scheduleendtime
,
325 'end_time' => $this->scheduletimeofday
,
326 "duration" => "2:00",
328 "weekdays" => array("1","2","3","4","5","6","0"),
329 "months" => array("1","2","3","4","5","6","7","8","9","10","11","12"));
331 $this->assertTrue($this->sd
->edit_schedule($data, $id));
334 exec('/usr/bin/php index.php default/cron/downtime 2>&1', $output, $status);
335 $this->assertEmpty($output, "acrossmidnight returned output: ".implode("\n", $output));
336 $this->assertEquals(0, $status, 'Return code should be zero');
338 sleep(3); # Y U SO SLOW?
340 $ls = Livestatus
::instance();
341 $dt = $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduleendtime} +1 day"), 'end_time' => strtotime("{$this->scheduletimeofday} +2 days")), 'columns' => array('id'), 'auth' => false));
342 $this->assertCount(1, $dt, "Unexpected number of downtimes created from acrossmidnight");
344 // Remove downtimes when tests are done.
345 $cmd = 'DEL_HOST_DOWNTIME;';
346 foreach ($dt as $id) {
347 $res = nagioscmd
::submit_to_nagios($cmd . $id['id']);
348 $this->assertTrue($res, 'Host delete command was submitted');
351 $this->assertCount(0, $ls->getDowntimes(array('filter' => array('start_time' => strtotime("{$this->scheduleendtime} +1 day"), 'end_time' => strtotime("{$this->scheduletimeofday} +2 days")))), "Downtimes are gone after deleting them");