2013-06-17 16:34:21 +00:00
use LWP::UserAgent ;
use JSON ;
2013-11-06 15:10:47 +00:00
use Test::Simple tests = > 17 ;
2013-06-17 16:34:21 +00:00
my $ ua = LWP::UserAgent - > new ;
$ ua - > cookie_jar ( { } ) ;
sub request_json {
my ( $ opts ) = @ _ ;
my $ req = HTTP::Request - > new ;
$ req - > method ( $ opts - > { method } or "GET" ) ;
$ req - > uri ( "http://localhost:3000$opts->{uri}" ) ;
$ req - > header ( Accept = > "application/json" ) ;
$ req - > content ( encode_json ( $ opts - > { data } ) ) if defined $ opts - > { data } ;
my $ res = $ ua - > request ( $ req ) ;
print $ res - > as_string ( ) ;
return $ res ;
}
2013-11-06 15:10:47 +00:00
my $ result = request_json ( { uri = > "/login" , method = > "POST" , data = > { username = > "root" , password = > "wrong" } } ) ;
ok ( $ result - > code ( ) == 403 , "Incorrect password rejected." ) ;
2013-06-17 16:34:21 +00:00
my $ result = request_json ( { uri = > "/login" , method = > "POST" , data = > { username = > "root" , password = > "foobar" } } ) ;
my $ user = decode_json ( $ result - > content ( ) ) ;
ok ( $ user - > { username } eq "root" , "The root user is named root" ) ;
2013-10-16 20:48:03 +00:00
ok ( $ user - > { userroles } - > [ 0 ] eq "admin" , "The root user is an admin" ) ;
2013-06-17 16:34:21 +00:00
$ user = decode_json ( request_json ( { uri = > "/current-user" } ) - > content ( ) ) ;
ok ( $ user - > { username } eq "root" , "The current user is named root" ) ;
2013-10-16 20:48:03 +00:00
ok ( $ user - > { userroles } - > [ 0 ] eq "admin" , "The current user is an admin" ) ;
2013-06-17 16:34:21 +00:00
ok ( request_json ( { uri = > '/project/sample' } ) - > code ( ) == 404 , "Non-existent projects don't exist" ) ;
$ result = request_json ( { uri = > '/project/sample' , method = > 'PUT' , data = > { displayname = > "Sample" , enabled = > "1" , } } ) ;
ok ( $ result - > code ( ) == 201 , "PUTting a new project creates it" ) ;
my $ project = decode_json ( request_json ( { uri = > '/project/sample' } ) - > content ( ) ) ;
ok ( ( not @ { $ project - > { jobsets } } ) , "A new project has no jobsets" ) ;
2014-09-26 11:47:36 +00:00
$ result = request_json ( { uri = > '/jobset/sample/default' , method = > 'PUT' , data = > { nixexprpath = > "default.nix" , nixexprinput = > "my-src" , inputs = > { "my-src" = > { type = > "path" , value = > "/run/jobset" } } , enabled = > "1" , checkinterval = > "3600" } } ) ;
2013-06-17 16:34:21 +00:00
ok ( $ result - > code ( ) == 201 , "PUTting a new jobset creates it" ) ;
my $ jobset = decode_json ( request_json ( { uri = > '/jobset/sample/default' } ) - > content ( ) ) ;
2013-10-24 17:18:36 +00:00
ok ( exists $ jobset - > { jobsetinputs } - > { "my-src" } , "The new jobset has a 'my-src' input" ) ;
2013-10-24 15:01:17 +00:00
2013-10-24 17:18:36 +00:00
ok ( $ jobset - > { jobsetinputs } - > { "my-src" } - > { jobsetinputalts } - > [ 0 ] eq "/run/jobset" , "The 'my-src' input is in /run/jobset" ) ;
2013-10-24 15:01:17 +00:00
2016-10-13 13:53:05 +00:00
system ( "hydra-eval-jobset sample default" ) ;
2013-06-17 16:34:21 +00:00
$ result = request_json ( { uri = > '/jobset/sample/default/evals' } ) ;
ok ( $ result - > code ( ) == 200 , "Can get evals of a jobset" ) ;
my $ evals = decode_json ( $ result - > content ( ) ) - > { evals } ;
my $ eval = $ evals - > [ 0 ] ;
2013-10-29 17:54:11 +00:00
ok ( $ eval - > { hasnewbuilds } == 1 , "The first eval of a jobset has new builds" ) ;
2013-06-17 16:34:21 +00:00
2016-10-13 13:53:05 +00:00
system ( "echo >> /run/jobset/default.nix; hydra-eval-jobset sample default" ) ;
2013-06-17 16:34:21 +00:00
my $ evals = decode_json ( request_json ( { uri = > '/jobset/sample/default/evals' } ) - > content ( ) ) - > { evals } ;
2013-10-29 17:54:11 +00:00
ok ( $ evals - > [ 0 ] - > { jobsetevalinputs } - > { "my-src" } - > { revision } != $ evals - > [ 1 ] - > { jobsetevalinputs } - > { "my-src" } - > { revision } , "Changing a jobset source changes its revision" ) ;
2013-10-24 19:38:20 +00:00
2013-10-29 17:54:11 +00:00
my $ build = decode_json ( request_json ( { uri = > "/build/" . $ evals - > [ 0 ] - > { builds } - > [ 0 ] } ) - > content ( ) ) ;
2013-06-17 16:34:21 +00:00
ok ( $ build - > { job } eq "job" , "The build's job name is job" ) ;
ok ( $ build - > { finished } == 0 , "The build isn't finished yet" ) ;
2015-08-12 13:40:47 +00:00
ok ( $ build - > { buildoutputs } - > { out } - > { path } =~ /^\/nix\/store\/[a-zA-Z0-9]{32}-job$/ , "The build's outpath is in the Nix store and named 'job'" ) ;