| 1 |
package App::Yath::Plugin::YathUI; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use File::Spec; |
| 8 |
use Test2::Harness::Util qw/read_file mod2file looks_like_uuid/; |
| 9 |
use Test2::Harness::Util::JSON qw/decode_json/; |
| 10 |
|
| 11 |
use App::Yath::Options; |
| 12 |
use parent 'App::Yath::Plugin'; |
| 13 |
|
| 14 |
sub can_log { |
| 15 |
my ($option, $options) = @_; |
| 16 |
|
| 17 |
return 1 if $options->included->{'App::Yath::Options::Logging'}; |
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
sub can_finder { |
| 22 |
my ($option, $options) = @_; |
| 23 |
|
| 24 |
return 1 if $options->included->{'App::Yath::Options::Finder'}; |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
option_group {prefix => 'yathui', category => "YathUI Options"} => sub { |
| 29 |
option url => ( |
| 30 |
type => 's', |
| 31 |
alt => ['uri'], |
| 32 |
description => "Yath-UI url", |
| 33 |
long_examples => [" http://my-yath-ui.com/..."], |
| 34 |
); |
| 35 |
|
| 36 |
option api_key => ( |
| 37 |
type => 's', |
| 38 |
description => "Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user" |
| 39 |
); |
| 40 |
|
| 41 |
option project => ( |
| 42 |
type => 's', |
| 43 |
description => "The Yath-UI project for your test results", |
| 44 |
); |
| 45 |
|
| 46 |
option mode => ( |
| 47 |
type => 's', |
| 48 |
default => 'qvfd', |
| 49 |
description => "Set the upload mode (default 'qvfd')", |
| 50 |
long_examples => [ |
| 51 |
' summary', |
| 52 |
' qvf', |
| 53 |
' qvfd', |
| 54 |
' complete', |
| 55 |
], |
| 56 |
); |
| 57 |
|
| 58 |
option retry => ( |
| 59 |
type => 'c', |
| 60 |
description => "How many times to try an operation before giving up", |
| 61 |
default => 0, |
| 62 |
); |
| 63 |
|
| 64 |
option grace => ( |
| 65 |
description => "If yath cannot connect to yath-ui it normally throws an error, use this to make it fail gracefully. You get a warning, but things keep going.", |
| 66 |
default => 0, |
| 67 |
); |
| 68 |
|
| 69 |
option durations => ( |
| 70 |
description => "Poll duration data from Yath-UI to help order tests efficiently", |
| 71 |
default => 0, |
| 72 |
applicable => \&can_finder, |
| 73 |
); |
| 74 |
|
| 75 |
option coverage => ( |
| 76 |
description => "Poll coverage data from Yath-UI to determine what tests should be run for changed files", |
| 77 |
default => 0, |
| 78 |
applicable => \&can_finder, |
| 79 |
); |
| 80 |
|
| 81 |
# TODO |
| 82 |
# option median_durations => ( |
| 83 |
# type => 'b', |
| 84 |
# description => "Get median duration data", |
| 85 |
# default => 0, |
| 86 |
# ); |
| 87 |
|
| 88 |
option medium_duration => ( |
| 89 |
type => 's', |
| 90 |
description => "Minimum duration length (seconds) before a test goes from SHORT to MEDIUM", |
| 91 |
long_examples => [' 5'], |
| 92 |
default => 5, |
| 93 |
); |
| 94 |
|
| 95 |
option long_duration => ( |
| 96 |
type => 's', |
| 97 |
description => "Minimum duration length (seconds) before a test goes from MEDIUM to LONG", |
| 98 |
long_examples => [' 10'], |
| 99 |
default => 10, |
| 100 |
); |
| 101 |
|
| 102 |
option upload => ( |
| 103 |
description => "Upload the log to Yath-UI", |
| 104 |
default => 0, |
| 105 |
applicable => \&can_log, |
| 106 |
); |
| 107 |
|
| 108 |
post -1 => sub { |
| 109 |
my %params = @_; |
| 110 |
|
| 111 |
my $settings = $params{settings}; |
| 112 |
my $options = $params{options}; |
| 113 |
|
| 114 |
my $has_finder = $options->included->{'App::Yath::Options::Finder'}; |
| 115 |
my $has_logger = $options->included->{'App::Yath::Options::Logging'}; |
| 116 |
|
| 117 |
my $has_durations = $has_finder && $settings->yathui->durations; |
| 118 |
my $has_upload = $has_logger && $settings->yathui->upload; |
| 119 |
my $has_coverage = $has_finder && $settings->yathui->coverage; |
| 120 |
|
| 121 |
return unless $has_durations || $has_upload || $has_coverage; |
| 122 |
|
| 123 |
my $url = $settings->yathui->url or die "'--yathui-url URL' is required to use durations, coverage, or upload a log"; |
| 124 |
my $project = $settings->yathui->project or die "'--yathui-project NAME' is required to use durations, coverage, or upload a log"; |
| 125 |
my $grace = $settings->yathui->grace; |
| 126 |
|
| 127 |
$url =~ s{/+$}{}g; |
| 128 |
|
| 129 |
if ($has_upload) { |
| 130 |
$settings->logging->field(log => 1); |
| 131 |
$settings->logging->field(bzip2 => 1); |
| 132 |
} |
| 133 |
|
| 134 |
if ($has_coverage) { |
| 135 |
my $curl = join '/' => ($url, 'coverage', $project); |
| 136 |
$settings->cover->field(($grace ? 'maybe_from' : 'from'), $curl); |
| 137 |
} |
| 138 |
|
| 139 |
if ($has_durations) { |
| 140 |
my $med = $settings->yathui->medium_duration; |
| 141 |
my $long = $settings->yathui->long_duration; |
| 142 |
|
| 143 |
my $durl = join '/' => ($url, 'durations', $project, $med, $long); |
| 144 |
$settings->finder->field(($grace ? 'maybe_durations' : 'durations'), $durl); |
| 145 |
} |
| 146 |
|
| 147 |
return; |
| 148 |
}; |
| 149 |
}; |
| 150 |
|
| 151 |
sub grab_rerun { |
| 152 |
my $this = shift; |
| 153 |
my ($rerun, %params) = @_; |
| 154 |
|
| 155 |
return (0) if $rerun =~ m/\.jsonl(\.gz|\.bz2)?/; |
| 156 |
|
| 157 |
my $settings = $params{settings}; |
| 158 |
my $mode_hash = $params{mode_hash}; |
| 159 |
|
| 160 |
my $path; |
| 161 |
if ($rerun eq '1') { |
| 162 |
my $project = $settings->yathui->project or return (0); |
| 163 |
my $user = $settings->yathui->user // $ENV{USER}; |
| 164 |
|
| 165 |
$path = "$project/$user"; |
| 166 |
|
| 167 |
print "Re-run requested with no paremeters, ${ \__PACKAGE__ } querying YathUI (web request) for last run matching $path...\n"; |
| 168 |
|
| 169 |
# API Qwerk :-/ |
| 170 |
$path .= '/0'; |
| 171 |
} |
| 172 |
elsif (looks_like_uuid($rerun)) { |
| 173 |
$path = "$rerun"; |
| 174 |
print "Re-run requested with UUID, ${ \__PACKAGE__ } querying YathUI (web request) for matching run, or latest run from project or user matching the UUID\n"; |
| 175 |
} |
| 176 |
else { |
| 177 |
return (0); |
| 178 |
} |
| 179 |
|
| 180 |
$path = "rerun/$path"; |
| 181 |
|
| 182 |
my ($ok, $res, $data) = $this->_request($settings, $path, {json => 1}); |
| 183 |
|
| 184 |
if (!$ok) { |
| 185 |
print "Error getting a re-run data from yathui: $data...\n"; |
| 186 |
return (1); |
| 187 |
} |
| 188 |
|
| 189 |
return (1, $data); |
| 190 |
} |
| 191 |
|
| 192 |
sub _request { |
| 193 |
my $this = shift; |
| 194 |
my ($settings, $path, $payload) = @_; |
| 195 |
|
| 196 |
my $url = $settings->yathui->url; |
| 197 |
$url =~ s{/+$}{}g; |
| 198 |
$url = join "/" => ($url, $path); |
| 199 |
|
| 200 |
my %fields; |
| 201 |
|
| 202 |
for my $field (qw/project api_key mode/) { |
| 203 |
my $val = $settings->yathui->field($field) or next; |
| 204 |
$fields{$field} = $val; |
| 205 |
} |
| 206 |
|
| 207 |
require HTTP::Tiny; |
| 208 |
eval { require HTTP::Tiny::Multipart; 1 } or die "To use --yathui-* you must install HTTP::Tiny::Multipart.\n"; |
| 209 |
|
| 210 |
my $res; |
| 211 |
for (0 .. $settings->yathui->retry) { |
| 212 |
my $http = HTTP::Tiny->new; |
| 213 |
$res = $http->post_multipart( |
| 214 |
$url => { |
| 215 |
headers => {'Content-Type' => 'application/json'}, |
| 216 |
%fields, |
| 217 |
%$payload, |
| 218 |
}, |
| 219 |
); |
| 220 |
|
| 221 |
next unless $res; |
| 222 |
last if $res->{status} eq '200'; |
| 223 |
} |
| 224 |
|
| 225 |
my ($ok, $msg); |
| 226 |
if ($res && $res->{status} eq '200') { |
| 227 |
my $data; |
| 228 |
$ok = eval { $data = decode_json($res->{content}); 1 }; |
| 229 |
if ($ok) { |
| 230 |
return (1, $res, $data); |
| 231 |
} |
| 232 |
else { |
| 233 |
$msg = $@; |
| 234 |
} |
| 235 |
} |
| 236 |
else { |
| 237 |
if ($res) { |
| 238 |
$msg = "Server responded with " . $res->{status} . ":\n" . ($res->{content} // 'NO CONTENT'); |
| 239 |
} |
| 240 |
else { |
| 241 |
$msg = "Failed to upload yathui log, no response object"; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return (0, $res, $msg); |
| 246 |
} |
| 247 |
|
| 248 |
sub finish { |
| 249 |
my $this = shift; |
| 250 |
my %params = @_; |
| 251 |
|
| 252 |
my $settings = $params{settings}; |
| 253 |
|
| 254 |
return unless $settings->yathui->upload; |
| 255 |
|
| 256 |
my $log_file = $settings->logging->log_file; |
| 257 |
my ($filename) = reverse File::Spec->splitpath($log_file); |
| 258 |
|
| 259 |
my ($ok, $res, $data) = $this->_request( |
| 260 |
$settings, |
| 261 |
'upload', { |
| 262 |
log_file => { |
| 263 |
filename => $filename, |
| 264 |
content => read_file($log_file, no_decompress => 1), |
| 265 |
content_type => 'application/x-bzip2', |
| 266 |
}, |
| 267 |
|
| 268 |
action => 'Upload Log', |
| 269 |
json => 1, |
| 270 |
} |
| 271 |
); |
| 272 |
|
| 273 |
die "Error connecting to YathUI: $data\n" |
| 274 |
unless $ok; |
| 275 |
|
| 276 |
my $msg; |
| 277 |
if ($data->{errors} && @{$data->{errors}}) { |
| 278 |
$ok = 0; |
| 279 |
$msg = join "\n" => (@{$data->{errors}}); |
| 280 |
} |
| 281 |
elsif ($data->{messages}) { |
| 282 |
$ok = 1; |
| 283 |
|
| 284 |
my $url = $settings->yathui->url; |
| 285 |
$url =~ s{/+$}{}g; |
| 286 |
|
| 287 |
$msg = join "\n" => ( |
| 288 |
@{$data->{messages}}, |
| 289 |
$data->{run_id} ? ("YathUI run url: " . join '/' => ($url, 'run', $data->{run_id})) : (), |
| 290 |
); |
| 291 |
} |
| 292 |
else { |
| 293 |
$ok = 0; |
| 294 |
$msg = "No messages recieved"; |
| 295 |
} |
| 296 |
|
| 297 |
chomp($msg); |
| 298 |
$msg = "YathUI Upload: $msg"; |
| 299 |
if ($ok) { |
| 300 |
print "\n$msg\n"; |
| 301 |
} |
| 302 |
else { |
| 303 |
if ($settings->yathui->grace) { |
| 304 |
warn $msg; |
| 305 |
} |
| 306 |
else { |
| 307 |
die $msg; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
1; |
| 315 |
|
| 316 |
__END__ |
| 317 |
|
| 318 |
=pod |
| 319 |
|
| 320 |
=encoding UTF-8 |
| 321 |
|
| 322 |
=head1 NAME |
| 323 |
|
| 324 |
App::Yath::Plugin::YathUI - Plugin to interact with a YathUI server |
| 325 |
|
| 326 |
=head1 DESCRIPTION |
| 327 |
|
| 328 |
If you have a Yath-UI L<Test2::Harness::UI> server, you can use this module to |
| 329 |
have yath automatically upload logs or retrieve durations data |
| 330 |
|
| 331 |
=head1 PROVIDED OPTIONS |
| 332 |
|
| 333 |
=head2 COMMAND OPTIONS |
| 334 |
|
| 335 |
=head3 YathUI Options |
| 336 |
|
| 337 |
=over 4 |
| 338 |
|
| 339 |
=item --yathui-api-key ARG |
| 340 |
|
| 341 |
=item --yathui-api-key=ARG |
| 342 |
|
| 343 |
=item --no-yathui-api-key |
| 344 |
|
| 345 |
Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user |
| 346 |
|
| 347 |
|
| 348 |
=item --yathui-coverage |
| 349 |
|
| 350 |
=item --no-yathui-coverage |
| 351 |
|
| 352 |
Poll coverage data from Yath-UI to determine what tests should be run for changed files |
| 353 |
|
| 354 |
|
| 355 |
=item --yathui-durations |
| 356 |
|
| 357 |
=item --no-yathui-durations |
| 358 |
|
| 359 |
Poll duration data from Yath-UI to help order tests efficiently |
| 360 |
|
| 361 |
|
| 362 |
=item --yathui-grace |
| 363 |
|
| 364 |
=item --no-yathui-grace |
| 365 |
|
| 366 |
If yath cannot connect to yath-ui it normally throws an error, use this to make it fail gracefully. You get a warning, but things keep going. |
| 367 |
|
| 368 |
|
| 369 |
=item --yathui-long-duration 10 |
| 370 |
|
| 371 |
=item --no-yathui-long-duration |
| 372 |
|
| 373 |
Minimum duration length (seconds) before a test goes from MEDIUM to LONG |
| 374 |
|
| 375 |
|
| 376 |
=item --yathui-medium-duration 5 |
| 377 |
|
| 378 |
=item --no-yathui-medium-duration |
| 379 |
|
| 380 |
Minimum duration length (seconds) before a test goes from SHORT to MEDIUM |
| 381 |
|
| 382 |
|
| 383 |
=item --yathui-mode summary |
| 384 |
|
| 385 |
=item --yathui-mode qvf |
| 386 |
|
| 387 |
=item --yathui-mode qvfd |
| 388 |
|
| 389 |
=item --yathui-mode complete |
| 390 |
|
| 391 |
=item --no-yathui-mode |
| 392 |
|
| 393 |
Set the upload mode (default 'qvfd') |
| 394 |
|
| 395 |
|
| 396 |
=item --yathui-project ARG |
| 397 |
|
| 398 |
=item --yathui-project=ARG |
| 399 |
|
| 400 |
=item --no-yathui-project |
| 401 |
|
| 402 |
The Yath-UI project for your test results |
| 403 |
|
| 404 |
|
| 405 |
=item --yathui-retry |
| 406 |
|
| 407 |
=item --no-yathui-retry |
| 408 |
|
| 409 |
How many times to try an operation before giving up |
| 410 |
|
| 411 |
Can be specified multiple times |
| 412 |
|
| 413 |
|
| 414 |
=item --yathui-upload |
| 415 |
|
| 416 |
=item --no-yathui-upload |
| 417 |
|
| 418 |
Upload the log to Yath-UI |
| 419 |
|
| 420 |
|
| 421 |
=item --yathui-url http://my-yath-ui.com/... |
| 422 |
|
| 423 |
=item --uri http://my-yath-ui.com/... |
| 424 |
|
| 425 |
=item --no-yathui-url |
| 426 |
|
| 427 |
Yath-UI url |
| 428 |
|
| 429 |
|
| 430 |
=back |
| 431 |
|
| 432 |
=head1 SOURCE |
| 433 |
|
| 434 |
The source code repository for Test2-Harness can be found at |
| 435 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 436 |
|
| 437 |
=head1 MAINTAINERS |
| 438 |
|
| 439 |
=over 4 |
| 440 |
|
| 441 |
=item Chad Granum E<lt>[email protected]<gt> |
| 442 |
|
| 443 |
=back |
| 444 |
|
| 445 |
=head1 AUTHORS |
| 446 |
|
| 447 |
=over 4 |
| 448 |
|
| 449 |
=item Chad Granum E<lt>[email protected]<gt> |
| 450 |
|
| 451 |
=back |
| 452 |
|
| 453 |
=head1 COPYRIGHT |
| 454 |
|
| 455 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 456 |
|
| 457 |
This program is free software; you can redistribute it and/or |
| 458 |
modify it under the same terms as Perl itself. |
| 459 |
|
| 460 |
See F<http://dev.perl.org/licenses/> |
| 461 |
|
| 462 |
=cut |