Jobset: add HTTP API test

This commit is contained in:
Cole Helbling 2021-04-28 09:25:26 -07:00
parent 725c9c2f81
commit 948b3cf073
No known key found for this signature in database
GPG key ID: B37E0F2371016A4C

176
t/Controller/Jobset/http.t Normal file
View file

@ -0,0 +1,176 @@
use feature 'unicode_strings';
use strict;
use Setup;
use JSON qw(decode_json encode_json);
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0;
require Catalyst::Test;
Catalyst::Test->import('Hydra');
use HTTP::Request::Common qw(POST PUT GET DELETE);
# This test verifies that creating, reading, updating, and deleting a jobset via
# the HTTP API works as expected.
my $db = Hydra::Model::DB->new;
hydra_setup($db);
# Create a user to log in to
my $user = $db->resultset('Users')->create({ username => 'alice', emailaddress => 'root@invalid.org', password => '!' });
$user->setPassword('foobar');
$user->userroles->update_or_create({ role => 'admin' });
my $project = $db->resultset('Projects')->create({name => 'tests', displayname => 'Tests', owner => 'alice'});
# Login and save cookie for future requests
my $req = request(POST '/login',
Referer => 'http://localhost/',
Content => {
username => 'alice',
password => 'foobar'
}
);
is($req->code, 302);
my $cookie = $req->header("set-cookie");
subtest 'Create new jobset "job" as flake type' => sub {
my $jobsetcreate = request(PUT '/jobset/tests/job',
Accept => 'application/json',
Content_Type => 'application/json',
Cookie => $cookie,
Content => encode_json({
enabled => 2,
visible => 1,
name => "job",
type => 1,
description => "test jobset",
flake => "github:nixos/nix",
checkinterval => 0,
schedulingshares => 100,
keepnr => 3
})
);
ok($jobsetcreate->is_success);
is($jobsetcreate->header("location"), "http://localhost/jobset/tests/job");
};
subtest 'Read newly-created jobset "job"' => sub {
my $jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok($jobsetinfo->is_success);
is(decode_json($jobsetinfo->content), {
checkinterval => 0,
description => "test jobset",
emailoverride => "",
enabled => 2,
enableemail => JSON::false,
errortime => undef,
errormsg => "",
fetcherrormsg => "",
flake => "github:nixos/nix",
hidden => JSON::false,
jobsetinputs => {},
keepnr => 3,
lastcheckedtime => undef,
name => "job",
nixexprinput => "",
nixexprpath => "",
project => "tests",
schedulingshares => 100,
starttime => undef,
triggertime => undef,
type => 1
});
};
subtest 'Update jobset "job" to legacy type' => sub {
my $jobsetupdate = request(PUT '/jobset/tests/job',
Accept => 'application/json',
Content_Type => 'application/json',
Cookie => $cookie,
Content => encode_json({
enabled => 3,
visible => 1,
name => "job",
type => 0,
nixexprinput => "ofborg",
nixexprpath => "release.nix",
jobsetinputs => {
ofborg => {
name => "ofborg",
type => "git",
value => "https://github.com/NixOS/ofborg.git released"
}
},
description => "test jobset",
checkinterval => 0,
schedulingshares => 50,
keepnr => 1
})
);
ok($jobsetupdate->is_success);
# Read newly-updated jobset "job"
my $jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok($jobsetinfo->is_success);
is(decode_json($jobsetinfo->content), {
checkinterval => 0,
description => "test jobset",
emailoverride => "",
enabled => 3,
enableemail => JSON::false,
errortime => undef,
errormsg => "",
fetcherrormsg => "",
flake => "",
hidden => JSON::false,
jobsetinputs => {
ofborg => {
name => "ofborg",
type => "git",
emailresponsible => JSON::false,
jobsetinputalts => [
"https://github.com/NixOS/ofborg.git released"
]
}
},
keepnr => 1,
lastcheckedtime => undef,
name => "job",
nixexprinput => "ofborg",
nixexprpath => "release.nix",
project => "tests",
schedulingshares => 50,
starttime => undef,
triggertime => undef,
type => 0
});
};
subtest 'Delete jobset "job"' => sub {
my $jobsetinfo = request(DELETE '/jobset/tests/job',
Accept => 'application/json',
Cookie => $cookie
);
ok($jobsetinfo->is_success);
# Jobset "job" should no longer exist.
$jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok(!$jobsetinfo->is_success);
};
done_testing;