Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up old jobs after the rr number changes for an incident #78

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Dashboard/Model/Incidents.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,27 @@ sub _update_openqa_jobs ($self, $inc) {
sub _update ($db, $incident) {
$db->query('INSERT INTO incidents (number, project) VALUES (?, ?) ON CONFLICT DO NOTHING',
$incident->{number}, $incident->{project});
my $id = $db->query('SELECT id FROM incidents WHERE number = ? LIMIT 1', $incident->{number})->hash->{id};
my $row = $db->query('SELECT id, rr_number FROM incidents WHERE number = ? LIMIT 1', $incident->{number})->hash;
my ($id, $rr_number) = ($row->{id}, $row->{rr_number} // 0);

$db->query(
'UPDATE incidents SET packages = ?, rr_number = ?, review = ?, review_qam = ?, approved = ?, emu = ?, active = ?
WHERE id = ?', $incident->{packages}, $incident->{rr_number}, $incident->{inReview}, $incident->{inReviewQAM},
$incident->{approved}, $incident->{emu}, $incident->{isActive}, $id
);

# Remove old jobs after release request number changed (because incidents might be reused)
if (defined $incident->{rr_number} && $rr_number ne '0' && $rr_number ne $incident->{rr_number}) {

# Individual jobs
$db->query('DELETE FROM incident_openqa_settings WHERE incident = ?', $id);

# Aggregate jobs
$db->query(
'DELETE FROM update_openqa_settings WHERE id IN (SELECT settings FROM incident_in_update WHERE incident = ?)',
$id);
}

# Add new channels
my $old_channels
= $db->query('SELECT name FROM channels c JOIN incident_channels ic ON ic.channel = c.id WHERE ic.incident = ?',
Expand Down
11 changes: 8 additions & 3 deletions migrations/dashboard.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ CREATE TABLE IF NOT EXISTS update_openqa_settings (
);
CREATE TABLE IF NOT EXISTS incident_in_update (
id SERIAL PRIMARY KEY,
settings INT REFERENCES update_openqa_settings(id),
settings INT REFERENCES update_openqa_settings(id) ON DELETE CASCADE,
incident INT REFERENCES incidents(id)
);
CREATE TYPE qa_status AS ENUM ('unknown', 'waiting', 'passed', 'failed', 'stopped');
CREATE TABLE IF NOT EXISTS openqa_jobs (
id SERIAL PRIMARY KEY,
update_settings INT REFERENCES update_openqa_settings(id),
incident_settings INT REFERENCES incident_openqa_settings(id),
update_settings INT REFERENCES update_openqa_settings(id) ON DELETE CASCADE,
incident_settings INT REFERENCES incident_openqa_settings(id) ON DELETE CASCADE,
name TEXT NOT NULL,
job_group TEXT NOT NULL,
status qa_status NOT NULL DEFAULT 'waiting',
Expand Down Expand Up @@ -94,3 +94,8 @@ ALTER TABLE incident_in_update ADD CONSTRAINT incident_in_update_settings_fkey
ALTER TABLE openqa_jobs DROP CONSTRAINT openqa_jobs_update_settings_fkey;
ALTER TABLE openqa_jobs ADD CONSTRAINT openqa_jobs_update_settings_fkey FOREIGN KEY (update_settings)
REFERENCES update_openqa_settings(id) ON DELETE CASCADE;

--4 up
ALTER TABLE openqa_jobs DROP CONSTRAINT openqa_jobs_incident_settings_fkey;
ALTER TABLE openqa_jobs ADD CONSTRAINT openqa_jobs_incident_settings_fkey FOREIGN KEY (incident_settings)
REFERENCES incident_openqa_settings(id) ON DELETE CASCADE;
4 changes: 2 additions & 2 deletions t/amqp.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ $dashboard_test->minimal_fixtures($t->app);
my $db = $t->app->pg->db;

sub _is_field ($field, $expected) {
is($db->query("select $field from openqa_jobs where id=7")->hash->{$field}, $expected);
is($db->query("SELECT $field FROM openqa_jobs WHERE id = 9")->hash->{$field}, $expected);
}

sub _set_default() {
$db->query("update openqa_jobs set status='waiting', job_id=4953203 where id=7");
$db->query("UPDATE openqa_jobs SET status = 'waiting', job_id = 4953203 WHERE id = 9");
}

subtest 'Handle done job' => sub {
Expand Down
5 changes: 5 additions & 0 deletions t/api.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ my $t = Test::Mojo->new(Dashboard => $config);
$dashboard_test->no_fixtures($t->app);
my $auth_headers = {Authorization => 'Token test_token', Accept => 'application/json'};

subtest 'Migrations' => sub {
is $t->app->pg->migrations->latest, 4, 'latest version';
is $t->app->pg->migrations->active, 4, 'active version';
};

subtest 'Unknown endpoint' => sub {
$t->get_ok('/api/unknown' => $auth_headers)->status_is(404)->json_is({error => 'Resource not found'});
};
Expand Down
80 changes: 73 additions & 7 deletions t/api_cleanup.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,47 @@ use Mojo::JSON qw(false true);

plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE};

my $dashboard_test = Dashboard::Test->new(online => $ENV{TEST_ONLINE}, schema => 'api_cleanup_test');
my $config = $dashboard_test->default_config;
my $t = Test::Mojo->new(Dashboard => $config);
$dashboard_test->cleanup_fixtures($t->app);
my $auth_headers = {Authorization => 'Token test_token', Accept => 'application/json'};

subtest 'Clean up old aggregate jobs (during sync)' => sub {
my $dashboard_test = Dashboard::Test->new(online => $ENV{TEST_ONLINE}, schema => 'api_cleanup_test');
my $config = $dashboard_test->default_config;
my $t = Test::Mojo->new(Dashboard => $config);
$dashboard_test->cleanup_fixtures($t->app);

$t->get_ok('/app/api/incident/16861')->status_is(200)->json_is('/details/incident/number', 16861)
->json_is('/details/incident/packages', ['perl-Minion', 'perl-Mojo-Pg'])->json_has('/details/jobs/20201107-1')
->json_has('/details/jobs/20201107-2')->json_has('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {passed => 1});

$t->patch_ok(
'/api/incidents' => $auth_headers => json => [
{
number => 16861,
project => 'SUSE:Maintenance:16861',
packages => ['perl-Minion', 'perl-Mojo-Pg'],
channels => ['Test'],
rr_number => 230067,
inReview => true,
inReviewQAM => true,
approved => false,
emu => true,
isActive => true
}
]
)->status_is(200)->json_is({message => 'Ok'});

$t->get_ok('/app/api/incident/16861')->status_is(200)->json_is('/details/incident/number', 16861)
->json_is('/details/incident/packages', ['perl-Minion', 'perl-Mojo-Pg'])->json_has('/details/jobs/20201107-1')
->json_has('/details/jobs/20201107-2')->json_has('/details/jobs/20201108-1');
->json_hasnt('/details/jobs/20201107-2')->json_hasnt('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {passed => 1});
};

subtest 'Clean up jobs after rr_number change (during sync)' => sub {
my $dashboard_test = Dashboard::Test->new(online => $ENV{TEST_ONLINE}, schema => 'api_cleanup_rr_test');
my $config = $dashboard_test->default_config;
my $t = Test::Mojo->new(Dashboard => $config);
$dashboard_test->minimal_fixtures($t->app);

$t->patch_ok(
'/api/incidents' => $auth_headers => json => [
Expand All @@ -31,7 +62,7 @@ subtest 'Clean up old aggregate jobs (during sync)' => sub {
project => 'SUSE:Maintenance:16861',
packages => ['perl-Minion', 'perl-Mojo-Pg'],
channels => ['Test'],
rr_number => undef,
rr_number => 230067,
inReview => true,
inReviewQAM => true,
approved => false,
Expand All @@ -41,9 +72,44 @@ subtest 'Clean up old aggregate jobs (during sync)' => sub {
]
)->status_is(200)->json_is({message => 'Ok'});

$t->get_ok('/app/api/incident/16860')->status_is(200)->json_is('/details/incident/number', 16860)
->json_is('/details/incident/packages', ['perl-Mojolicious'])->json_has('/details/jobs/20201107-1')
->json_has('/details/jobs/20201107-2')->json_has('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {passed => 1, failed => 1, waiting => 1});
$t->get_ok('/app/api/incident/16861')->status_is(200)->json_is('/details/incident/number', 16861)
->json_is('/details/incident/packages', ['perl-Minion', 'perl-Mojo-Pg'])->json_has('/details/jobs/20201107-1')
->json_hasnt('/details/jobs/20201107-2')->json_hasnt('/details/jobs/20201108-1');
->json_has('/details/jobs/20201107-2')->json_has('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {passed => 1});
$t->get_ok('/app/api/incident/16862')->status_is(200)->json_is('/details/incident/number', 16862)
->json_is('/details/incident/packages', ['curl'])->json_is('/details/incident_summary', {passed => 1});

$t->patch_ok(
'/api/incidents' => $auth_headers => json => [
{
number => 16861,
project => 'SUSE:Maintenance:16861',
packages => ['perl-Minion', 'perl-Mojo-Pg'],
channels => ['Test'],
rr_number => 230068,
inReview => true,
inReviewQAM => true,
approved => false,
emu => true,
isActive => true
}
]
)->status_is(200)->json_is({message => 'Ok'});

$t->get_ok('/app/api/incident/16860')->status_is(200)->json_is('/details/incident/number', 16860)
->json_is('/details/incident/packages', ['perl-Mojolicious'])->json_hasnt('/details/jobs/20201107-1')
->json_hasnt('/details/jobs/20201107-2')->json_hasnt('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {passed => 1, failed => 1, waiting => 1});
$t->get_ok('/app/api/incident/16861')->status_is(200)->json_is('/details/incident/number', 16861)
->json_is('/details/incident/packages', ['perl-Minion', 'perl-Mojo-Pg'])->json_hasnt('/details/jobs/20201107-1')
->json_hasnt('/details/jobs/20201107-2')->json_hasnt('/details/jobs/20201108-1')
->json_is('/details/incident_summary', {});
$t->get_ok('/app/api/incident/16862')->status_is(200)->json_is('/details/incident/number', 16862)
->json_is('/details/incident/packages', ['curl'])->json_is('/details/incident_summary', {passed => 1});
};

done_testing();
54 changes: 54 additions & 0 deletions t/lib/Dashboard/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ sub minimal_fixtures ($self, $app) {
settings => {DISTRI => 'sle', VERSION => '12-SP5', BUILD => ':17063:perl-Mojolicious'}
}
);
my $settings_four_id = $settings->add_incident_settings(
$incidents->id_for_number(16861),
{
incident => 16861,
version => '12-SP7',
flavor => 'Server-DVD-HA-Incidents-Install',
arch => 'aarch64',
withAggregate => true,
settings => {DISTRI => 'sle', VERSION => '12-SP7', BUILD => ':17063:perl-Mojolicious'}
}
);
my $settings_five_id = $settings->add_incident_settings(
$incidents->id_for_number(16862),
{
incident => 16862,
version => '13-SP7',
flavor => 'Server-DVD-HA-Incidents-Install',
arch => 'aarch64',
withAggregate => true,
settings => {DISTRI => 'sle', VERSION => '13-SP7', BUILD => ':17063:curl'}
}
);

my $jobs = $app->jobs;
$jobs->add(
Expand Down Expand Up @@ -181,6 +203,38 @@ sub minimal_fixtures ($self, $app) {
build => ':17063:perl-Mojolicious'
}
);
$jobs->add(
{
incident_settings => $settings_four_id,
update_settings => undef,
name => 'mau-webserver@64bit',
job_group => 'Maintenance: SLE 12 SP7 Kernel Incidents',
status => 'passed',
job_id => 4973199,
group_id => 285,
distri => 'sle',
flavor => 'Server-DVD-Incidents',
arch => 'aarch64',
version => '12-SP7',
build => ':17063:perl-Mojolicious'
}
);
$jobs->add(
{
incident_settings => $settings_five_id,
update_settings => undef,
name => 'mau-webserver@64bit',
job_group => 'Maintenance: SLE 12 SP7 Kernel Incidents',
status => 'passed',
job_id => 4983199,
group_id => 285,
distri => 'sle',
flavor => 'Server-DVD-Incidents',
arch => 'aarch64',
version => '12-SP7',
build => ':17063:curl'
}
);

# Successful build
my $settings_success_id = $settings->add_update_settings(
Expand Down