Punk-Observe

 view release on metacpan or  search on metacpan

t/0914-alert-editor.t  view on Meta::CPAN

    my $db = fresh();
    my $r = $C->can('save_alert')->($db, 'default', {
        name => 'error rate', query => 'spans | bucket(30s) count',
        op => '>', threshold => 5, for => '30s', every => '30s' });

    my $s = $C->can('save_silence')->($db, 'default',
        { pattern => 'error rate/', until => '1h',
          by => 'lnation', reason => 'deploying' });
    ok($s->{ok}, 'a silence saves');

    my $a = $C->can('alerts')->($db, 'default', {});
    is($a->{rules}[0]{silenced}, 1,
       'the rule row is badged - the reader used to never set this');
    is($a->{silences}[0]{by}, 'lnation',
       '  and the operator column is no longer permanently empty');
    ok($a->{silences}[0]{id}, '  and the row carries the id a revoke needs');

    # The trailing slash means the whole rule; a full key means one series.
    ok($C->can('silence_match')->('error rate/', 1, 'error rate/cards'),
       'a rule-wide pattern covers every series');
    ok(!$C->can('silence_match')->('error rate/cards', 0, 'error rate/shop'),
       '  a series pattern covers only its own');

    # REVOKED MEANS EXPIRED, NOT ERASED: suppression stops now, the record
    # of who silenced what - and why - survives for the incident's story.
    $C->can('delete_silence')->($db, 'default', $s->{id});
    my $a2 = $C->can('alerts')->($db, 'default', {});
    is(scalar @{ $a2->{silences} }, 0, 'a revoked silence stops suppressing');
    is($a2->{rules}[0]{silenced}, 0, '  immediately');
    my ($kept) = $db->dbh->selectrow_array(
        'SELECT COUNT(*) FROM silences', undef);
    is($kept, 1, '  and the row survives as the record of what happened');

    my $bad = $C->can('save_silence')->($db, 'default',
        { pattern => 'x/', until => 'whenever' });
    ok(!$bad->{ok} && $bad->{field} eq 'until',
       'a silence with no readable duration is refused, naming the field');
}

# --- deleting a rule takes its whole story with it ---------------------------

{
    my $db = fresh();
    my $r = $C->can('save_alert')->($db, 'default', {
        name => 'gone', query => 'spans | bucket(30s) count',
        op => '>', threshold => 1, for => '30s', every => '30s' });
    my $dbh = $db->dbh;
    $dbh->do("INSERT INTO alert_state (rule_id, series, state, last_seen)
              VALUES (?, 'all', 'firing', 1)", undef, $r->{id});
    $dbh->do("INSERT INTO alert_events
                  (rule_id, series, kind, from_state, to_state, at)
              VALUES (?, 'all', 1, 'ok', 'firing', 1)", undef, $r->{id});
    $dbh->do("INSERT INTO notifications
                  (rule_id, series, kind, at, created_at)
              VALUES (?, 'all', 1, 1, 1)", undef, $r->{id});

    my $d = $C->can('delete_alert')->($db, 'default', $r->{id});
    ok($d->{ok}, 'the rule deletes');
    for my $t (qw(alert_state alert_events notifications)) {
        my ($n) = $dbh->selectrow_array("SELECT COUNT(*) FROM $t");
        is($n, 0, "  and $t went with it - the cascade is real because the "
                . 'backend turns foreign keys on');
    }
}

done_testing();



( run in 1.447 second using v1.01-cache-2.11-cpan-e7c6538aa59 )