| 1 | package Tapper::API::Plugin::API; | ||
| 2 | our $AUTHORITY = 'cpan:TAPPER'; | ||
| 3 | # ABSTRACT: API functions for Tapper access | ||
| 4 | $Tapper::API::Plugin::API::VERSION = '5.0.2'; | ||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use Mojolicious::Lite; | ||
| 10 | use JSON::XS; | ||
| 11 | use Tapper::Cmd::Testplan; | ||
| 12 | use Tapper::Cmd::Host; | ||
| 13 | use Tapper::Cmd::Testrun; | ||
| 14 | use Tapper::Model 'model'; | ||
| 15 | use File::ShareDir; | ||
| 16 | use File::Spec; | ||
| 17 | use Storable; | ||
| 18 | use File::Temp 'tempfile'; | ||
| 19 | |||
| 20 | plugin('RenderFile'); | ||
| 21 | |||
| 22 | my $cfg = Tapper::Config->subconfig; | ||
| 23 | my %tests = ( | ||
| 24 | "example1" => {groups => ["ALL"], timeout => 8000}, | ||
| 25 | "example2" => {groups => ["ALL"], timeout => 3600*24*2}, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub get_groups | ||
| 30 | { | ||
| 31 | my %groups; | ||
| 32 | foreach my $test (keys %tests) { | ||
| 33 | foreach my $group (@{$tests{$test}->{groups} || []}) { | ||
| 34 | $groups{$group} //= []; | ||
| 35 | push @{$groups{$group}}, {name => $test, %{$tests{$test}}}; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | return \%groups; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _get_api_base { | ||
| 42 | my $base_url = ''; | ||
| 43 | |||
| 44 | # package name full or just last part | ||
| 45 | my $plugin_name1 = __PACKAGE__; | ||
| 46 | my $plugin_name2 = $plugin_name1; | ||
| 47 | $plugin_name2 =~ s/^.*:://; | ||
| 48 | |||
| 49 | return unless $cfg->{api}{routes}; | ||
| 50 | |||
| 51 | foreach (@{$cfg->{api}{routes}}) { | ||
| 52 | if ($_->{module} eq $plugin_name1 or $_->{module} eq $plugin_name2) { | ||
| 53 | $base_url = $_->{url}; | ||
| 54 | last; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | return $base_url; | ||
| 58 | } | ||
| 59 | |||
| 60 | put 'host-new/room/:room/ip/:ip' => sub { | ||
| 61 | my $self = shift; | ||
| 62 | my $room = $self->param('room'); | ||
| 63 | my $ip = $self->param('ip'); | ||
| 64 | $self->render(json => {id => 12}, status => 201); | ||
| 65 | }; | ||
| 66 | |||
| 67 | put 'host-new/name/:name' => sub { | ||
| 68 | my $self = shift; | ||
| 69 | my $name = $self->param('name'); | ||
| 70 | my ($room, $ip) = split('-', $name, 2); # if multiple dashes are in $name, they end up in $ip | ||
| 71 | $self->render(json => {id => 12}, status => 201); | ||
| 72 | }; | ||
| 73 | |||
| 74 | put 'testrun-start/:testrun' => sub { | ||
| 75 | my $self = shift; | ||
| 76 | $self->app->renderer->default_format('json'); | ||
| 77 | |||
| 78 | |||
| 79 | my $data; | ||
| 80 | if ($self->tx->req->body) { | ||
| 81 | $data = eval{JSON::XS::decode_json($self->tx->req->body)}; | ||
| 82 | if ($@) { | ||
| 83 | $self->render(json => { | ||
| 84 | error => $@ | ||
| 85 | }, | ||
| 86 | status => 409, | ||
| 87 | ); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | if ($data->{group}) { | ||
| 93 | my $tests = get_groups()->{$data->{group}}; | ||
| 94 | push @{$data->{tests}}, @$tests; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $test = $self->param('testrun'); | ||
| 98 | my $test_filename = File::Spec->catfile($cfg->{paths}->{use_case_path},"$test.sc"); | ||
| 99 | if (not -r $test_filename) { | ||
| 100 | $test_filename = File::Spec->catfile($cfg->{paths}->{use_case_path},"$test.mpc"); | ||
| 101 | if (not -r $test_filename) { | ||
| 102 | my $error = "Tried to find testrun description in "; | ||
| 103 | $error .= File::Spec->catfile($cfg->{paths}->{use_case_path},"$test.sc"); | ||
| 104 | $error .= " and $test_filename. Neither was readable."; | ||
| 105 | $self->respond_to(json => {json => {error => $error}, status => 409,}, | ||
| 106 | any => {text => "Error - $error", status => 409,}, | ||
| 107 | ); | ||
| 108 | return; | ||
| 109 | |||
| 110 | } | ||
| 111 | } | ||
| 112 | my $cmd = Tapper::Cmd::Testrun->new(); | ||
| 113 | |||
| 114 | my $test_string = eval{$cmd->apply_macro($test_filename, $data)}; | ||
| 115 | if ($@) { | ||
| 116 | $self->respond_to(json => {json => {error => "$@"}, status => 409,}, | ||
| 117 | any => {text => "Error - $@", status => 409,}, | ||
| 118 | ); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | if ($data->{dryrun}) { | ||
| 122 | $self->render(text => $test_string, | ||
| 123 | status => 202, | ||
| 124 | ); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | my @ids = eval { | ||
| 129 | require YAML::Syck; | ||
| 130 | my @plan = YAML::Syck::Load($test_string); | ||
| 131 | if ($plan[0]->{scenario_type}) { | ||
| 132 | require Tapper::Cmd::Scenario; | ||
| 133 | return Tapper::Cmd::Scenario->new()->add(\@plan); # returns from eval block | ||
| 134 | } | ||
| 135 | # we have a plain precondition description without additional testrun description | ||
| 136 | # need to add options that $cmd->create needs | ||
| 137 | elsif (not $plan[0]->{preconditions}) { | ||
| 138 | # don't use \@plan, since we overwrite it in the next step | ||
| 139 | my $tmp_plan = { preconditions => [ @plan ], topic => uc($test) }; | ||
| 140 | @plan = ($tmp_plan); | ||
| 141 | } | ||
| 142 | $cmd->create($plan[0]); | ||
| 143 | }; | ||
| 144 | if ($@) { | ||
| 145 | $self->respond_to(json => {json => {error => "$@"}, status => 409,}, | ||
| 146 | any => {text => "Error - $@", status => 409,}, | ||
| 147 | ); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | my $text_return; | ||
| 151 | foreach my $id (@ids) { | ||
| 152 | $text_return .= "url: $cfg->{base_url}/testrun/id/$id\n"; | ||
| 153 | } | ||
| 154 | $self->respond_to(json => {json => {testrun => { | ||
| 155 | ids => [ @ids ], | ||
| 156 | links => [ map {"$cfg->{base_url}/testruns/id/$_"} @ids ], | ||
| 157 | }, | ||
| 158 | }, status => 202, | ||
| 159 | }, | ||
| 160 | any => {text => $text_return, status => 202,}, | ||
| 161 | ); | ||
| 162 | |||
| 163 | }; | ||
| 164 | |||
| 165 | |||
| 166 | put 'testplan-start/:testplan' => sub { | ||
| 167 | my $self = shift; | ||
| 168 | $self->app->renderer->default_format('json'); | ||
| 169 | |||
| 170 | |||
| 171 | my $data; | ||
| 172 | if ($self->tx->req->body) { | ||
| 173 | $data = eval{JSON::XS::decode_json($self->tx->req->body)}; | ||
| 174 | if ($@) { | ||
| 175 | $self->render(json => { | ||
| 176 | error => $@ | ||
| 177 | }, | ||
| 178 | status => 409, | ||
| 179 | ); | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | # if host does not yet exist, create it and bind it to integrationtest queue | ||
| 185 | if ($data->{hosts} and $data->{room}) { | ||
| 186 | HOST: | ||
| 187 | foreach my $host (@{$data->{hosts}}) { | ||
| 188 | my $room = $data->{room}; | ||
| 189 | if (not $host =~ /$room-/i) { | ||
| 190 | $host = join("-",lc($room),$host); | ||
| 191 | } | ||
| 192 | |||
| 193 | my $host_r = Tapper::Model::model()->resultset('Host')->search({name => $host}, {rows => 1})->first; | ||
| 194 | if ($host_r) { | ||
| 195 | # hosts already in DB, activate it if needed | ||
| 196 | if (not $host_r->active) { | ||
| 197 | $host_r->active(1); | ||
| 198 | $host_r->update; | ||
| 199 | } | ||
| 200 | next HOST; | ||
| 201 | } | ||
| 202 | # host not in DB, create it | ||
| 203 | my $cmd = Tapper::Cmd::Host->new(); | ||
| 204 | $cmd->add({name => $host, | ||
| 205 | active => 1, | ||
| 206 | free => 1, | ||
| 207 | comment => '(autoadded by Tapper::API)'}); | ||
| 208 | |||
| 209 | $host_r = Tapper::Model::model()->resultset('Host')->search({name => $host}, {rows => 1})->first; | ||
| 210 | my $queue_r = Tapper::Model::model('TestrunDB')->resultset('Queue')->search({name => 'integration'}, {rows => 1})->first; | ||
| 211 | |||
| 212 | # don't bind twice | ||
| 213 | next HOST if $host_r->queuehosts->search({queue_id => $queue_r->id}, {rows => 1})->first; | ||
| 214 | Tapper::Model::model('TestrunDB')->resultset('QueueHost')->new({queue_id => $queue_r->id, | ||
| 215 | host_id => $host_r->id, | ||
| 216 | })->insert; | ||
| 217 | |||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | my $cmd = Tapper::Cmd::Testplan->new(); | ||
| 222 | $data->{tests} //= []; | ||
| 223 | |||
| 224 | if ($data->{group}) { | ||
| 225 | my $tests = get_groups()->{$data->{group}}; | ||
| 226 | push @{$data->{tests}}, @$tests; | ||
| 227 | } | ||
| 228 | |||
| 229 | my $testplan = $self->param('testplan'); | ||
| 230 | my $plan_filename = File::Spec->catfile($cfg->{paths}->{testplan_path}, | ||
| 231 | lc((split(/::/,__PACKAGE__))[-1]), # last part of package name | ||
| 232 | "$testplan.tp"); | ||
| 233 | |||
| 234 | my $safe_data=Storable::dclone($data); # make deep copy of substitute because TT changes them | ||
| 235 | my $plan_evaluated = eval {$cmd->apply_macro($plan_filename, $data)}; | ||
| 236 | |||
| 237 | $data->{title} ||= $cmd->get_shortname($plan_evaluated); | ||
| 238 | $data->{title} ||= $testplan; | ||
| 239 | $data = $safe_data; | ||
| 240 | |||
| 241 | if ($data->{dryrun}) { | ||
| 242 | if ($@) { | ||
| 243 | $self->respond_to(json => {json => {error => "$@"}, status => 409,}, | ||
| 244 | any => {text => "Error - $@", status => 409,}, | ||
| 245 | ); | ||
| 246 | } else { | ||
| 247 | $self->render(text => $plan_evaluated, | ||
| 248 | status => 202, | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | return; | ||
| 252 | } | ||
| 253 | |||
| 254 | my $id = eval { | ||
| 255 | $cmd->testplannew( | ||
| 256 | { | ||
| 257 | file => $plan_filename, | ||
| 258 | substitutes => $data, | ||
| 259 | name => $data->{title}, | ||
| 260 | }); | ||
| 261 | }; | ||
| 262 | if ($@) { | ||
| 263 | $self->respond_to(json => {json => {error => "$@"}, status => 409,}, | ||
| 264 | any => {text => "Error - $@", status => 409,}, | ||
| 265 | ); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | my $tp = model('TestrunDB')->resultset('TestplanInstance')->find($id); | ||
| 269 | my @testplans = map { {id => $_->id, link => "$cfg->{base_url}/testruns/id/".$_->id }} $tp->testruns->all; | ||
| 270 | $self->respond_to(json => {json => {testplan => { | ||
| 271 | id => $id, | ||
| 272 | link => "$cfg->{base_url}/testplan/id/$id", | ||
| 273 | }, | ||
| 274 | testrun => \@testplans, | ||
| 275 | }, status => 202, | ||
| 276 | }, | ||
| 277 | any => {text => "Text - url: $cfg->{base_url}/testplan/id/$id\n", status => 202,}, | ||
| 278 | ); | ||
| 279 | |||
| 280 | }; | ||
| 281 | |||
| 282 | |||
| 283 | get 'query/testplan-status/id/:testplan' => sub { | ||
| 284 | my $self = shift; | ||
| 285 | my $testplan = $self->param('testplan'); | ||
| 286 | my $cmd = Tapper::Cmd::Testplan->new(); | ||
| 287 | my $result = $cmd->status($testplan); | ||
| 288 | $result->{link} = "$cfg->{base_url}/testplan/id/$testplan", | ||
| 289 | $self->render(json => $result, | ||
| 290 | status => 202, | ||
| 291 | ); | ||
| 292 | }; | ||
| 293 | |||
| 294 | get 'query/testrun-status/id/:testrun' => sub { | ||
| 295 | my $self = shift; | ||
| 296 | my $testrun = $self->param('testrun'); | ||
| 297 | my $cmd = Tapper::Cmd::Testrun->new(); | ||
| 298 | my $result = $cmd->status($testrun); | ||
| 299 | $result->{link} = "$cfg->{base_url}/testrun/id/$testrun", | ||
| 300 | $self->render(json => $result, | ||
| 301 | status => 202, | ||
| 302 | ); | ||
| 303 | }; | ||
| 304 | |||
| 305 | |||
| 306 | put 'testrun-cancel/id/:testrun' => sub { | ||
| 307 | my $self = shift; | ||
| 308 | my $testrun = $self->param('testrun'); | ||
| 309 | $self->render(json => { | ||
| 310 | $testrun => { | ||
| 311 | success => 'error', | ||
| 312 | 'error-msg' => 'You are not allowed to cancel this testrun' | ||
| 313 | }, | ||
| 314 | }, | ||
| 315 | status => 202, | ||
| 316 | ); | ||
| 317 | }; | ||
| 318 | |||
| 319 | put 'testplan-cancel/id/:testplan' => sub { | ||
| 320 | my $self = shift; | ||
| 321 | my $testplan = $self->param('testplan'); | ||
| 322 | $self->render(json => { | ||
| 323 | success => 'canceled', | ||
| 324 | 'error-msg' => undef, | ||
| 325 | }, | ||
| 326 | status => 202, | ||
| 327 | ); | ||
| 328 | }; | ||
| 329 | |||
| 330 | |||
| 331 | get 'query/testplan-list/' => sub { | ||
| 332 | my $self = shift; | ||
| 333 | $self->render(json => { | ||
| 334 | collection => { | ||
| 335 | options => ['room', 'group', 'tests', 'hosts'] | ||
| 336 | } | ||
| 337 | }, | ||
| 338 | status => 202, | ||
| 339 | ); | ||
| 340 | }; | ||
| 341 | |||
| 342 | |||
| 343 | get 'query/test-list/' => sub { | ||
| 344 | my $self = shift; | ||
| 345 | $self->render(json => \%tests, | ||
| 346 | status => 202, | ||
| 347 | ); | ||
| 348 | }; | ||
| 349 | |||
| 350 | any 'query/report-filelist/id/:report_id' => sub { | ||
| 351 | my $self = shift; | ||
| 352 | $self->app->renderer->default_format('json'); | ||
| 353 | |||
| 354 | my $report_id = $self->param('report_id'); | ||
| 355 | my $filter_filename = $self->param('filter_filename'); | ||
| 356 | my $file_result; | ||
| 357 | if ($filter_filename) { | ||
| 358 | $file_result = model->resultset('ReportFile')->search({report_id => $report_id, filename => {'like' => $filter_filename}}); | ||
| 359 | } else { | ||
| 360 | $file_result = model->resultset('ReportFile')->search({report_id => $report_id}); | ||
| 361 | } | ||
| 362 | |||
| 363 | $self->respond_to( | ||
| 364 | json => sub { $self->render(json => {map {$_->id => $_->filename} $file_result->all} )}, | ||
| 365 | html => sub { | ||
| 366 | my $file_ids = [map {$_->id} $file_result->all]; | ||
| 367 | $self->stash(file_ids => $file_ids, api_base => _get_api_base()); | ||
| 368 | $self->render(template => 'queryreportfilelistidreport_id') | ||
| 369 | } | ||
| 370 | ); | ||
| 371 | }; | ||
| 372 | |||
| 373 | any 'query/report-list/name/:report_name/limit/:limit' => sub { | ||
| 374 | my $self = shift; | ||
| 375 | $self->app->renderer->default_format('json'); | ||
| 376 | |||
| 377 | my $report_name = $self->param('report_name') || ''; | ||
| 378 | my $limit = $self->param('limit') || 1; | ||
| 379 | my $file_result; | ||
| 380 | if ($report_name) { | ||
| 381 | $file_result = model->resultset('Report')->search({filename => {'=' => $report_name}}, {limit => $limit}); | ||
| 382 | } | ||
| 383 | |||
| 384 | $self->respond_to( | ||
| 385 | json => sub { $self->render(json => {map {$_->id => $_->filename} $file_result->all} )}, | ||
| 386 | html => sub { | ||
| 387 | my $file_ids = [map {$_->id} $file_result->all]; | ||
| 388 | $self->stash(file_ids => $file_ids, api_base => _get_api_base()); | ||
| 389 | $self->render(template => 'queryreportfilelistidreport_id') | ||
| 390 | } | ||
| 391 | ); | ||
| 392 | }; | ||
| 393 | |||
| 394 | |||
| 395 | any 'query/testrun-filelist/id/:testrun_id' => sub { | ||
| 396 | my $self = shift; | ||
| 397 | $self->app->renderer->default_format('html'); | ||
| 398 | |||
| 399 | my $cmd_testrun = Tapper::Cmd::Testrun->new(); | ||
| 400 | my $testrun_id = $self->param('testrun_id'); | ||
| 401 | my $reports_rs = model->resultset('ReportgroupTestrun')->search({testrun_id => $testrun_id}); | ||
| 402 | my @report_ids = map {$_->report_id} $reports_rs->all; | ||
| 403 | $self->stash(report_ids => \@report_ids, api_base => _get_api_base()); | ||
| 404 | }; | ||
| 405 | |||
| 406 | any 'query/testplan-filelist/id/:testplan_id' => sub { | ||
| 407 | my $self = shift; | ||
| 408 | $self->app->renderer->default_format('html'); | ||
| 409 | |||
| 410 | my $testplan_id = $self->param('testplan_id'); | ||
| 411 | my $filter = $self->param('filter_filename'); | ||
| 412 | $filter ||= '%'; | ||
| 413 | |||
| 414 | my $cmd = Tapper::Cmd::Testplan->new(); | ||
| 415 | my $file_ids = $cmd->testplan_files($testplan_id, $filter); | ||
| 416 | $self->stash(file_ids => $file_ids, api_base => _get_api_base()); | ||
| 417 | }; | ||
| 418 | |||
| 419 | any 'query/reportfile/*filepath' => sub { | ||
| 420 | my $self = shift; | ||
| 421 | $self->app->renderer->default_format('html'); | ||
| 422 | |||
| 423 | my $filepath = $self->param('filepath'); | ||
| 424 | my $file_id = (split '/',$filepath)[-1]; | ||
| 425 | my $reportfile_result = model->resultset('ReportFile')->find($file_id); | ||
| 426 | if (not $reportfile_result) { | ||
| 427 | $self->render(text => "No file with '$filepath'", | ||
| 428 | status => 404, | ||
| 429 | ); | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | |||
| 433 | my $filename = $reportfile_result->filename; | ||
| 434 | my ($FH, $tmpname) = tempfile (TEMPLATE => "tapper-api-reportfile-${file_id}-XXXX", | ||
| 435 | UNLINK => 0, | ||
| 436 | TMPDIR => 1); | ||
| 437 | binmode $FH; | ||
| 438 | print $FH $reportfile_result->filecontent; | ||
| 439 | $self->render_file(filepath => $tmpname, filename => $filename); | ||
| 440 | unlink $FH; | ||
| 441 | |||
| 442 | # my $contenttype = $reportfile_result->contenttype eq 'plain' ? 'text/plain' : $reportfile_result->contenttype; | ||
| 443 | # my $disposition = $contenttype =~ /plain/ ? 'inline' : 'attachment'; | ||
| 444 | |||
| 445 | # $self->res->headers->content_type ($contenttype || 'application/octet-stream'); | ||
| 446 | # $self->res->headers->content_disposition("$disposition; filename=$filename;"); | ||
| 447 | # $self->render(text => $reportfile_result->filecontent, status => 202); | ||
| 448 | }; | ||
| 449 | |||
| 450 | |||
| 451 | put 'host-delete/id/:host' => sub { | ||
| 452 | my $self = shift; | ||
| 453 | my $host = $self->param('host'); | ||
| 454 | $self->render(json => {$host => {success => 'deleted'}}, status => 202,); | ||
| 455 | }; | ||
| 456 | |||
| 457 | 1; | ||
| 458 | |||
| 459 | =pod | ||
| 460 | |||
| 461 | =encoding UTF-8 | ||
| 462 | |||
| 463 | =head1 NAME | ||
| 464 | |||
| 465 | Tapper::API::Plugin::API - API functions for Tapper access | ||
| 466 | |||
| 467 | =head1 AUTHOR | ||
| 468 | |||
| 469 | Tapper Team <tapper-ops@amazon.com> | ||
| 470 | |||
| 471 | =head1 COPYRIGHT AND LICENSE | ||
| 472 | |||
| 473 | This software is Copyright (c) 2021 by Amazon. | ||
| 474 | |||
| 475 | This is free software, licensed under: | ||
| 476 | |||
| 477 | The (two-clause) FreeBSD License | ||
| 478 | |||
| 479 | =cut | ||
| 480 | |||
| 481 | __DATA__ |