Issue #21 - Added flag to outages, if set it will automatically start the maintenance mode once the outage starts.

This commit is contained in:
Daniel Thee Roperto
2016-09-22 14:26:52 +10:00
parent 708622bbcd
commit b302d9dfa5
19 changed files with 341 additions and 110 deletions

View File

@@ -114,6 +114,7 @@ class create_test extends cli_testcase {
public function test_create_withoptions() {
$this->set_parameters([
'--autostart=true',
'--warn=10',
'--start=0',
'--duration=30',
@@ -140,6 +141,7 @@ class create_test extends cli_testcase {
public function test_create_onlyid() {
$this->set_parameters([
'--onlyid',
'--autostart=N',
'--warn=10',
'--start=0',
'--duration=30',
@@ -173,6 +175,7 @@ class create_test extends cli_testcase {
$cli = new create();
$cli->set_referencetime($now);
$cli->set_defaults([
'autostart' => false,
'warn' => 50,
'start' => 200,
'duration' => 300,
@@ -194,10 +197,11 @@ class create_test extends cli_testcase {
}
public function test_create_withclone() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
// Create the outage to clone.
$original = new outage([
'autostart' => false,
'warntime' => $now - 120,
'starttime' => $now,
'stoptime' => $now + 120,
@@ -236,6 +240,7 @@ class create_test extends cli_testcase {
public function test_create_withblock() {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--autostart=N',
'--block',
'--warn=60',
'--start=0',

View File

@@ -66,9 +66,10 @@ class finish_test extends cli_testcase {
}
public function test_endedoutage() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -83,9 +84,10 @@ class finish_test extends cli_testcase {
}
public function test_finish() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 100,
@@ -99,7 +101,7 @@ class finish_test extends cli_testcase {
}
public function test_activenotfound() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
@@ -107,7 +109,7 @@ class finish_test extends cli_testcase {
}
public function test_invalidid() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
@@ -115,7 +117,7 @@ class finish_test extends cli_testcase {
}
public function test_idnotfound() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);

View File

@@ -85,9 +85,10 @@ class waitforit_test extends cli_testcase {
}
public function test_endedoutage() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -102,9 +103,10 @@ class waitforit_test extends cli_testcase {
}
public function test_activeverbose() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 10,
'starttime' => $now + 1,
'stoptime' => $now + 10,
@@ -121,9 +123,10 @@ class waitforit_test extends cli_testcase {
}
public function test_countdown() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + 45,
'stoptime' => $now + (60 * 60),
@@ -146,9 +149,10 @@ class waitforit_test extends cli_testcase {
}
public function test_outagechanged() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (60 * 60),

View File

@@ -39,6 +39,7 @@ class events_test extends advanced_testcase {
// Save new outage.
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,

View File

@@ -44,6 +44,7 @@ class outagedb_test extends advanced_testcase {
/**
* Helper function to create an outage then save it to the database.
*
* @param bool $autostart If outage should automatically start.
* @param int $now Timestamp for now, such as time().
* @param int $warning In how many hours the warning starts. Can be negative.
* @param int $start In how many hours this outage starts. Can be negative.
@@ -52,8 +53,9 @@ class outagedb_test extends advanced_testcase {
* @param int|null $finished In how many hours this outage is marked as finished. Can be negative or null.
* @return int Id the of created outage.
*/
private static function saveoutage($now, $warning, $start, $stop, $title, $finished = null) {
private static function saveoutage($autostart, $now, $warning, $start, $stop, $title, $finished = null) {
return outagedb::save(new outage([
'autostart' => $autostart,
'warntime' => $now + ($warning * 60 * 60),
'starttime' => $now + ($start * 60 * 60),
'stoptime' => $now + ($stop * 60 * 60),
@@ -84,6 +86,21 @@ class outagedb_test extends advanced_testcase {
outagedb::save($outage);
}
public function test_saved_fields() {
$this->resetAfterTest(true);
for ($i = 0; $i < 4; $i++) {
$expected = $this->createoutage($i);
$expected->id = outagedb::save($expected);
$actual = outagedb::get_by_id($expected->id);
// Ignore the following fields.
$expected->lastmodified = $actual->lastmodified;
$expected->createdby = $actual->createdby;
$expected->modifiedby = $actual->modifiedby;
// Check if fields are the same.
self::assertEquals($expected, $actual, 'Failed for $i='.$i);
}
}
/**
* Make sure we can get existing entries and null if not found.
*/
@@ -121,7 +138,7 @@ class outagedb_test extends advanced_testcase {
$now = time();
$this->resetAfterTest(true);
// Create it.
$id = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$id = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$outage = outagedb::get_by_id($id);
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
@@ -202,34 +219,29 @@ class outagedb_test extends advanced_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertNull(outagedb::get_active($now), 'There should be no active outage at this point.');
self::saveoutage($now, 1, 2, 3,
'An outage that starts in the future and is not in warning period.');
self::saveoutage(false, $now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
self::saveoutage($now, -3, -2, -1,
'An outage that is already in the past.');
self::saveoutage(false, $now, -3, -2, -1, 'An outage that is already in the past.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
$activeid = self::saveoutage($now, -2, 1, 2,
'An outage in warning period.');
$activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -1, 2, 3,
self::saveoutage(false, $now, -1, 2, 3,
'Another outage in warning period, but ignored as it starts after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -3, -2, 2, 'An finished outage.', -1);
self::saveoutage(false, $now, -3, -2, 2, 'An finished outage.', -1);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage($now, -3, -2, 2,
'An ongoing outage.');
$activeid = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -3, -1, 1,
'Another ongoing outage but ignored because it started after the previous one.');
self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -3, -2, 1,
self::saveoutage(false, $now, -3, -2, 1,
'Another ongoing outage starting at the same time, but ignored as it stops before the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
}
@@ -243,41 +255,41 @@ class outagedb_test extends advanced_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_unended($now), 'There should be no future outages at this point.');
self::saveoutage($now, -3, -2, -1, 'A past outage.');
self::saveoutage(false, $now, -3, -2, -1, 'A past outage.');
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
$id1 = self::saveoutage($now, 2, 3, 4, 'A future outage.');
$id1 = self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id2 = self::saveoutage($now, 1, 4, 5, 'Another future outage.');
$id2 = self::saveoutage(false, $now, 1, 4, 5, 'Another future outage.');
self::assertEquals([$id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id3 = self::saveoutage($now, 1, 3, 5, 'Yet another future outage.');
$id3 = self::saveoutage(false, $now, 1, 3, 5, 'Yet another future outage.');
self::assertEquals([$id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id4 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
$id4 = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id5 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.');
$id5 = self::saveoutage(false, $now, -1, 2, 3, 'Another outage in warning period.');
self::assertEquals([$id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id6 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$id6 = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([$id6, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id7 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.');
$id7 = self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage.');
self::assertEquals([$id6, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id8 = self::saveoutage($now, -3, -2, 1, 'Yet another ongoing outage.');
$id8 = self::saveoutage(false, $now, -3, -2, 1, 'Yet another ongoing outage.');
self::assertEquals([$id6, $id8, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
}
@@ -291,28 +303,28 @@ class outagedb_test extends advanced_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_ended($now), 'There should be no future outages at this point.');
self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage($now, 2, 3, 4, 'A future outage.');
self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
$id1 = self::saveoutage($now, -8, -6, -4, 'A past outage.');
$id1 = self::saveoutage(false, $now, -8, -6, -4, 'A past outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id2 = self::saveoutage($now, -8, -7, -5, 'Another past outage.');
$id2 = self::saveoutage(false, $now, -8, -7, -5, 'Another past outage.');
self::assertEquals([$id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id3 = self::saveoutage($now, -8, -5, -3, 'Yet another past outage.');
$id3 = self::saveoutage(false, $now, -8, -5, -3, 'Yet another past outage.');
self::assertEquals([$id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id4 = self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
$id4 = self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
}
@@ -325,6 +337,7 @@ class outagedb_test extends advanced_testcase {
*/
private function createoutage($i) {
return new outage([
'autostart' => ($i % 2 == 0),
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warntime' => $i * 60,