| 1 |
package Test2::Harness::Runner::Job; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/confess croak/; |
| 8 |
use Config qw/%Config/; |
| 9 |
use List::Util qw/min/; |
| 10 |
use Scalar::Util qw/weaken blessed/; |
| 11 |
use Test2::Util qw/CAN_REALLY_FORK/; |
| 12 |
use Time::HiRes qw/time/; |
| 13 |
|
| 14 |
use File::Spec(); |
| 15 |
use File::Temp(); |
| 16 |
|
| 17 |
use Test2::Harness::Util qw/fqmod clean_path write_file_atomic write_file mod2file open_file parse_exit process_includes chmod_tmp/; |
| 18 |
use Test2::Harness::IPC; |
| 19 |
|
| 20 |
use parent 'Test2::Harness::IPC::Process'; |
| 21 |
use Test2::Harness::Util::HashBase( |
| 22 |
qw{ <task <runner <run <settings }, # required |
| 23 |
qw{ |
| 24 |
<fork_callback |
| 25 |
<last_output_size |
| 26 |
+output_changed |
| 27 |
|
| 28 |
+verbose |
| 29 |
|
| 30 |
+via |
| 31 |
|
| 32 |
+run_dir +job_dir +tmp_dir +event_dir |
| 33 |
|
| 34 |
+ch_dir +unsafe_inc |
| 35 |
|
| 36 |
+use_fork +use_w_switch |
| 37 |
|
| 38 |
+includes +runner_includes |
| 39 |
+switches |
| 40 |
+use_stream |
| 41 |
+cli_includes |
| 42 |
+cli_options |
| 43 |
|
| 44 |
+smoke |
| 45 |
+retry +retry_isolated +is_try |
| 46 |
|
| 47 |
+args +file +run_file |
| 48 |
|
| 49 |
+out_file +err_file +in_file +bail_file |
| 50 |
|
| 51 |
+load +load_import |
| 52 |
|
| 53 |
+event_uuids +mem_usage +io_events |
| 54 |
|
| 55 |
+env_vars |
| 56 |
|
| 57 |
+event_timeout +post_exit_timeout +use_timeout |
| 58 |
|
| 59 |
+switches_from_env |
| 60 |
|
| 61 |
+et_file +pet_file |
| 62 |
|
| 63 |
+min_slots |
| 64 |
+max_slots |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
sub category { 'job' } |
| 69 |
|
| 70 |
sub init { |
| 71 |
my $self = shift; |
| 72 |
|
| 73 |
croak "'runner' is a required attribute" unless $self->{+RUNNER}; |
| 74 |
croak "'run' is a required attribute" unless $self->{+RUN}; |
| 75 |
croak "'settings' is a required attribute" unless $self->{+SETTINGS}; |
| 76 |
|
| 77 |
delete $self->{+JOB_DIR}; |
| 78 |
|
| 79 |
# Avoid a ref cycle |
| 80 |
#weaken($self->{+RUNNER}); |
| 81 |
|
| 82 |
my $task = $self->{+TASK} or croak "'task' is a required attribute"; |
| 83 |
|
| 84 |
delete $self->{+LAST_OUTPUT_SIZE}; |
| 85 |
|
| 86 |
confess "Task does not have a job ID" unless $task->{job_id}; |
| 87 |
confess "Task does not have a file" unless $task->{file}; |
| 88 |
} |
| 89 |
|
| 90 |
sub job_id { $_[0]->{+TASK}->{job_id} } |
| 91 |
|
| 92 |
sub prepare_dir { |
| 93 |
my $self = shift; |
| 94 |
|
| 95 |
$self->job_dir(); |
| 96 |
$self->tmp_dir(); |
| 97 |
$self->event_dir(); |
| 98 |
} |
| 99 |
|
| 100 |
sub via { |
| 101 |
my $self = shift; |
| 102 |
|
| 103 |
return undef if $self->{+SETTINGS}->debug->dummy; |
| 104 |
return undef if $self->{+TASK}->{resource_skip}; |
| 105 |
|
| 106 |
return $self->{+VIA} if exists $self->{+VIA}; |
| 107 |
|
| 108 |
my $task = $self->{+TASK}; |
| 109 |
return $self->{+VIA} = $task->{via} if $task->{via}; |
| 110 |
|
| 111 |
return $self->{+VIA} = $self->{+FORK_CALLBACK} if $self->{+FORK_CALLBACK} && $self->use_fork; |
| 112 |
|
| 113 |
return $self->{+VIA} = undef; |
| 114 |
} |
| 115 |
|
| 116 |
sub spawn_params { |
| 117 |
my $self = shift; |
| 118 |
|
| 119 |
my $task = $self->{+TASK}; |
| 120 |
|
| 121 |
my $skip; |
| 122 |
my $resource_skip_is_fail; |
| 123 |
$skip = 'dummy mode' if $self->{+SETTINGS}->debug->dummy; |
| 124 |
if ($self->{+TASK}->{resource_skip}) { |
| 125 |
my $msg = "Some resources are not available: " . join(', ' => @{$self->{+TASK}->{resource_skip}}); |
| 126 |
$skip = $msg; |
| 127 |
$resource_skip_is_fail = 1 if $self->{+SETTINGS}->runner->fail_on_resource_skip; |
| 128 |
} |
| 129 |
|
| 130 |
my $command; |
| 131 |
if (!$skip && $task->{binary} || $task->{non_perl}) { |
| 132 |
my $file = $self->ch_dir ? $self->file : $self->rel_file; |
| 133 |
$file = clean_path($file); |
| 134 |
$command = [$file, $self->args]; |
| 135 |
unshift @$command => $^X if $task->{non_perl} && !(-x $file) && !$task->{binary}; |
| 136 |
} |
| 137 |
else { |
| 138 |
my @skip_or_fail; |
| 139 |
if ($skip && $resource_skip_is_fail) { |
| 140 |
@skip_or_fail = ('-e', "print \"1..1\\nnot ok 1 - $skip\\n\""); |
| 141 |
} |
| 142 |
elsif ($skip) { |
| 143 |
@skip_or_fail = ('-e', "print \"1..0 # SKIP $skip\""); |
| 144 |
} |
| 145 |
|
| 146 |
$command = [ |
| 147 |
$^X, |
| 148 |
$self->cli_includes, |
| 149 |
$self->{+SETTINGS}->runner->nytprof ? ('-d:NYTProf') : (), |
| 150 |
$self->switches, |
| 151 |
$self->cli_options, |
| 152 |
|
| 153 |
@skip_or_fail ? @skip_or_fail : (sub { $self->run_file }), |
| 154 |
|
| 155 |
$self->args, |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
my $out_fh = open_file($self->out_file, '>'); |
| 160 |
my $err_fh = open_file($self->err_file, '>'); |
| 161 |
my $in_fh = open_file($self->in_file, '<'); |
| 162 |
|
| 163 |
return { |
| 164 |
command => $command, |
| 165 |
stdin => $in_fh, |
| 166 |
stdout => $out_fh, |
| 167 |
stderr => $err_fh, |
| 168 |
chdir => $self->ch_dir(), |
| 169 |
env => $self->env_vars(), |
| 170 |
}; |
| 171 |
} |
| 172 |
|
| 173 |
sub switches_from_env { |
| 174 |
my $self = shift; |
| 175 |
|
| 176 |
return @{$self->{+SWITCHES_FROM_ENV}} if $self->{+SWITCHES_FROM_ENV}; |
| 177 |
|
| 178 |
return @{$self->{+SWITCHES_FROM_ENV} = []} unless $ENV{HARNESS_PERL_SWITCHES}; |
| 179 |
|
| 180 |
return @{$self->{+SWITCHES_FROM_ENV} = [split /\s+/, $ENV{HARNESS_PERL_SWITCHES}]}; |
| 181 |
} |
| 182 |
|
| 183 |
my %JSON_SKIP = ( |
| 184 |
SETTINGS() => 1, |
| 185 |
TASK() => 1, |
| 186 |
RUNNER() => 1, |
| 187 |
RUN() => 1, |
| 188 |
CLI_INCLUDES() => 1, |
| 189 |
CLI_OPTIONS() => 1, |
| 190 |
ERR_FILE() => 1, |
| 191 |
ET_FILE() => 1, |
| 192 |
EVENT_DIR() => 1, |
| 193 |
EXIT() => 1, |
| 194 |
EXIT_TIME() => 1, |
| 195 |
IN_FILE() => 1, |
| 196 |
JOB_DIR() => 1, |
| 197 |
LAST_OUTPUT_SIZE() => 1, |
| 198 |
OUT_FILE() => 1, |
| 199 |
BAIL_FILE() => 1, |
| 200 |
OUTPUT_CHANGED() => 1, |
| 201 |
PET_FILE() => 1, |
| 202 |
RUN_DIR() => 1, |
| 203 |
TMP_DIR() => 1, |
| 204 |
); |
| 205 |
|
| 206 |
sub TO_JSON { |
| 207 |
my $self = shift; |
| 208 |
|
| 209 |
my $out = { %{$self->{+TASK}} }; |
| 210 |
|
| 211 |
for my $attr (Test2::Harness::Util::HashBase::attr_list(blessed($self))) { |
| 212 |
next if $JSON_SKIP{$attr}; |
| 213 |
$self->$attr unless defined $self->{$attr}; |
| 214 |
$out->{$attr} = $self->{$attr}; |
| 215 |
} |
| 216 |
|
| 217 |
delete $out->{+FORK_CALLBACK}; |
| 218 |
delete $out->{+VIA} if ref($out->{+VIA}) eq 'CODE'; |
| 219 |
|
| 220 |
$out->{job_name} //= $out->{job_id}; |
| 221 |
$out->{abs_file} = clean_path($self->file); |
| 222 |
|
| 223 |
return $out; |
| 224 |
} |
| 225 |
|
| 226 |
sub run_file { |
| 227 |
my $self = shift; |
| 228 |
return $self->{+RUN_FILE} //= $self->rel_file; |
| 229 |
} |
| 230 |
|
| 231 |
sub rel_file { File::Spec->abs2rel($_[0]->file) } |
| 232 |
sub file { $_[0]->{+FILE} //= clean_path($_[0]->{+TASK}->{file}, 0) } |
| 233 |
sub err_file { $_[0]->{+ERR_FILE} //= clean_path(File::Spec->catfile($_[0]->job_dir, 'stderr')) } |
| 234 |
sub out_file { $_[0]->{+OUT_FILE} //= clean_path(File::Spec->catfile($_[0]->job_dir, 'stdout')) } |
| 235 |
sub bail_file { $_[0]->{+BAIL_FILE} //= clean_path(File::Spec->catfile($_[0]->event_dir, 'bail')) } |
| 236 |
sub et_file { $_[0]->{+ET_FILE} //= clean_path(File::Spec->catfile($_[0]->job_dir, 'event_timeout')) } |
| 237 |
sub pet_file { $_[0]->{+PET_FILE} //= clean_path(File::Spec->catfile($_[0]->job_dir, 'post_exit_timeout')) } |
| 238 |
sub run_dir { $_[0]->{+RUN_DIR} //= clean_path(File::Spec->catdir($_[0]->{+RUNNER}->dir, $_[0]->{+RUN}->run_id)) } |
| 239 |
|
| 240 |
sub bailed_out { |
| 241 |
my $self = shift; |
| 242 |
|
| 243 |
if(-f $self->bail_file) { |
| 244 |
my $fh = open_file($self->bail_file, '<'); |
| 245 |
my $reason = <$fh> || 1; |
| 246 |
return $reason; |
| 247 |
} |
| 248 |
|
| 249 |
my $fh = open_file($self->out_file, '<'); |
| 250 |
while (my $line = <$fh>) { |
| 251 |
next unless $line =~ m/^Bail out!\s*(.*)$/; |
| 252 |
return $1 || 1; |
| 253 |
} |
| 254 |
|
| 255 |
return ""; |
| 256 |
} |
| 257 |
|
| 258 |
sub output_size { |
| 259 |
my $self = shift; |
| 260 |
|
| 261 |
my $size = 0; |
| 262 |
|
| 263 |
$size += -s $self->err_file || 0; |
| 264 |
$size += -s $self->out_file || 0; |
| 265 |
|
| 266 |
return $self->{+LAST_OUTPUT_SIZE} = $size; |
| 267 |
} |
| 268 |
|
| 269 |
sub output_changed { |
| 270 |
my $self = shift; |
| 271 |
|
| 272 |
my $last = $self->{+LAST_OUTPUT_SIZE}; |
| 273 |
my $size = $self->output_size(); |
| 274 |
|
| 275 |
# Output changed, update time |
| 276 |
return $self->{+OUTPUT_CHANGED} = time() if $last && $size != $last; |
| 277 |
|
| 278 |
# Return the last recorded time, if there is no previously recorded time then the record starts now |
| 279 |
return $self->{+OUTPUT_CHANGED} //= time(); |
| 280 |
} |
| 281 |
|
| 282 |
sub verbose { $_[0]->{+VERBOSE} //= $_[0]->{+TASK}->{verbose} // 0 } |
| 283 |
sub is_try { $_[0]->{+IS_TRY} //= $_[0]->{+TASK}->{is_try} // 0 } |
| 284 |
sub ch_dir { $_[0]->{+CH_DIR} //= $_[0]->{+TASK}->{ch_dir} // '' } |
| 285 |
sub unsafe_inc { $_[0]->{+UNSAFE_INC} //= $_[0]->{+RUNNER}->unsafe_inc } |
| 286 |
sub event_uuids { $_[0]->{+EVENT_UUIDS} //= $_[0]->run->event_uuids } |
| 287 |
sub mem_usage { $_[0]->{+MEM_USAGE} //= $_[0]->run->mem_usage } |
| 288 |
|
| 289 |
sub io_events { $_[0]->{+IO_EVENTS} //= $_[0]->_fallback(io_events => 1, qw/task run/) } |
| 290 |
|
| 291 |
sub smoke { $_[0]->{+SMOKE} //= $_[0]->_fallback(smoke => 0, qw/task/) } |
| 292 |
sub retry_isolated { $_[0]->{+RETRY_ISOLATED} //= $_[0]->_fallback(retry_isolated => 0, qw/task run/) } |
| 293 |
sub use_stream { $_[0]->{+USE_STREAM} //= $_[0]->_fallback(use_stream => 1, qw/task run/) } |
| 294 |
sub use_timeout { $_[0]->{+USE_TIMEOUT} //= $_[0]->_fallback(use_timeout => 1, qw/task/) } |
| 295 |
sub retry { $_[0]->{+RETRY} //= $_[0]->_fallback(retry => undef, qw/task run/) } |
| 296 |
sub event_timeout { $_[0]->{+EVENT_TIMEOUT} //= $_[0]->_fallback(event_timeout => undef, qw/task runner/) } |
| 297 |
sub post_exit_timeout { $_[0]->{+POST_EXIT_TIMEOUT} //= $_[0]->_fallback(post_exit_timeout => undef, qw/task runner/) } |
| 298 |
|
| 299 |
sub min_slots { $_[0]->{+MIN_SLOTS} //= $_[0]->_fallback_non_bool(min_slots => 1, qw/task/) } |
| 300 |
sub max_slots { $_[0]->{+MAX_SLOTS} //= $_[0]->_fallback_non_bool(max_slots => 1, qw/task/) } |
| 301 |
|
| 302 |
sub args { @{$_[0]->{+ARGS} //= $_[0]->_merge_sources(test_args => qw/task run/)} } |
| 303 |
sub load { @{$_[0]->{+LOAD} //= [@{$_[0]->run->load // []}]} } |
| 304 |
|
| 305 |
sub cli_includes { |
| 306 |
my $self = shift; |
| 307 |
|
| 308 |
# '.' is handled via the PERL_USE_UNSAFE_INC env var set later |
| 309 |
$self->{+CLI_INCLUDES} //= [map { "-I$_" } grep { $_ ne '.' } $self->includes]; |
| 310 |
|
| 311 |
return @{$self->{+CLI_INCLUDES}}; |
| 312 |
} |
| 313 |
|
| 314 |
sub runner_includes { @{$_[0]->{+RUNNER_INCLUDES} //= [$_[0]->{+RUNNER}->all_libs]} } |
| 315 |
|
| 316 |
sub _merge_sources { |
| 317 |
my $self = shift; |
| 318 |
my ($name, @from) = @_; |
| 319 |
|
| 320 |
my @vals; |
| 321 |
for my $from (@from) { |
| 322 |
my $source = $self->$from; |
| 323 |
my $val = blessed($source) ? $source->$name : $source->{$name}; |
| 324 |
next unless defined $val; |
| 325 |
next unless @$val; |
| 326 |
push @vals => @$val; |
| 327 |
} |
| 328 |
|
| 329 |
return \@vals; |
| 330 |
} |
| 331 |
|
| 332 |
sub _fallback_non_bool { |
| 333 |
my $self = shift; |
| 334 |
my ($name, $default, @from) = @_; |
| 335 |
|
| 336 |
for my $from (@from) { |
| 337 |
my $source = $self->$from; |
| 338 |
my $val = blessed($source) ? $source->$name : $source->{$name}; |
| 339 |
return $val if defined $val; |
| 340 |
} |
| 341 |
|
| 342 |
return $default; |
| 343 |
} |
| 344 |
|
| 345 |
sub _fallback { |
| 346 |
my $self = shift; |
| 347 |
my ($name, $default, @from) = @_; |
| 348 |
|
| 349 |
my @vals; |
| 350 |
for my $from (@from) { |
| 351 |
my $source = $self->$from; |
| 352 |
my $val = blessed($source) ? $source->$name : $source->{$name}; |
| 353 |
push @vals => $val if defined $val; |
| 354 |
} |
| 355 |
|
| 356 |
return $default unless @vals; |
| 357 |
|
| 358 |
# If the default is a ref we will just return the first value we found, truthiness check is useless |
| 359 |
return shift @vals if ref $default || !defined($default) || $default !~ m/^(0|1)$/; |
| 360 |
|
| 361 |
# If the default is true, then we only return true if none of the vals are false |
| 362 |
return !grep { !$_ } @vals if $default; |
| 363 |
|
| 364 |
# If the default is false, then we return true if any of the valse are true |
| 365 |
return grep { $_ } @vals; |
| 366 |
} |
| 367 |
|
| 368 |
sub job_dir { |
| 369 |
my $self = shift; |
| 370 |
return $self->{+JOB_DIR} if $self->{+JOB_DIR}; |
| 371 |
|
| 372 |
my $job_dir = File::Spec->catdir($self->run_dir, $self->{+TASK}->{job_id} . '+' . $self->is_try); |
| 373 |
mkdir($job_dir) or die "$$ $0 Could not create job directory '$job_dir': $!"; |
| 374 |
chmod_tmp($job_dir); |
| 375 |
$self->{+JOB_DIR} = $job_dir; |
| 376 |
} |
| 377 |
|
| 378 |
sub tmp_dir { |
| 379 |
my $self = shift; |
| 380 |
|
| 381 |
return $self->{+TMP_DIR} if $self->{+TMP_DIR}; |
| 382 |
|
| 383 |
my $tmp_dir = File::Temp::tempdir("XXXXXX", DIR => $self->runner->tmp_dir); |
| 384 |
chmod_tmp($tmp_dir); |
| 385 |
|
| 386 |
$self->{+TMP_DIR} = clean_path($tmp_dir); |
| 387 |
} |
| 388 |
|
| 389 |
sub make_event_dir { $_[0]->event_dir } |
| 390 |
sub event_dir { |
| 391 |
my $self = shift; |
| 392 |
return $self->{+EVENT_DIR} if $self->{+EVENT_DIR}; |
| 393 |
|
| 394 |
my $events_dir = File::Spec->catdir($self->job_dir, 'events'); |
| 395 |
unless (-d $events_dir) { |
| 396 |
mkdir($events_dir) or die "$$ $0 Could not create events directory '$events_dir': $!"; |
| 397 |
} |
| 398 |
$self->{+EVENT_DIR} = $events_dir; |
| 399 |
} |
| 400 |
|
| 401 |
sub in_file { |
| 402 |
my $self = shift; |
| 403 |
return $self->{+IN_FILE} if $self->{+IN_FILE}; |
| 404 |
|
| 405 |
my $task = $self->{+TASK}; |
| 406 |
|
| 407 |
unless ($task->{input}) { |
| 408 |
my $from_run = $self->run->input_file; |
| 409 |
return $self->{+IN_FILE} = $from_run if $from_run; |
| 410 |
} |
| 411 |
|
| 412 |
my $stdin = File::Spec->catfile($self->job_dir, 'stdin'); |
| 413 |
|
| 414 |
my $content = $task->{input} // $self->run->input // ''; |
| 415 |
write_file($stdin, $content); |
| 416 |
|
| 417 |
return $self->{+IN_FILE} = $stdin; |
| 418 |
} |
| 419 |
|
| 420 |
sub use_fork { |
| 421 |
my $self = shift; |
| 422 |
|
| 423 |
return $self->{+USE_FORK} if defined $self->{+USE_FORK}; |
| 424 |
|
| 425 |
my $task = $self->{+TASK}; |
| 426 |
|
| 427 |
return $self->{+USE_FORK} = 0 unless CAN_REALLY_FORK; |
| 428 |
return $self->{+USE_FORK} = 0 if $task->{binary}; |
| 429 |
return $self->{+USE_FORK} = 0 if $task->{non_perl}; |
| 430 |
return $self->{+USE_FORK} = 0 if defined($task->{use_fork}) && !$task->{use_fork}; |
| 431 |
return $self->{+USE_FORK} = 0 if defined($task->{use_preload}) && !$task->{use_preload}; |
| 432 |
|
| 433 |
# -w switch is ok, otherwise it is a no-go |
| 434 |
return $self->{+USE_FORK} = 0 if grep { !m/\s*-w\s*/ } $self->switches; |
| 435 |
|
| 436 |
my $runner = $self->{+RUNNER}; |
| 437 |
return $self->{+USE_FORK} = 0 unless $runner->use_fork; |
| 438 |
|
| 439 |
return $self->{+USE_FORK} = 1; |
| 440 |
} |
| 441 |
|
| 442 |
sub includes { |
| 443 |
my $self = shift; |
| 444 |
|
| 445 |
return @{$self->{+INCLUDES}} if $self->{+INCLUDES}; |
| 446 |
|
| 447 |
$self->{+INCLUDES} = [ |
| 448 |
process_includes( |
| 449 |
list => [$self->runner_includes, @{$self->{+SETTINGS}->harness->orig_inc}], |
| 450 |
include_dot => $self->unsafe_inc, |
| 451 |
include_current => 1, |
| 452 |
clean => 1, |
| 453 |
$self->ch_dir ? (ch_dir => $self->ch_dir) : (), |
| 454 |
) |
| 455 |
]; |
| 456 |
|
| 457 |
return @{$self->{+INCLUDES}}; |
| 458 |
} |
| 459 |
|
| 460 |
sub cli_options { |
| 461 |
my $self = shift; |
| 462 |
|
| 463 |
my $event_dir = $self->event_dir; |
| 464 |
my $job_id = $self->job_id; |
| 465 |
|
| 466 |
return ( |
| 467 |
$self->use_stream ? ("-MTest2::Formatter::Stream=dir,$event_dir,job_id,$job_id") : (), |
| 468 |
$self->event_uuids ? ('-MTest2::Plugin::UUID') : (), |
| 469 |
$self->mem_usage ? ('-MTest2::Plugin::MemUsage') : (), |
| 470 |
$self->io_events ? ('-MTest2::Plugin::IOEvents') : (), |
| 471 |
(map { @{$_->[1]} ? "-M$_->[0]=" . join(',' => @{$_->[1]}) : "-M$_->[0]" } $self->load_import), |
| 472 |
(map { "-m$_" } $self->load), |
| 473 |
); |
| 474 |
} |
| 475 |
|
| 476 |
sub switches { |
| 477 |
my $self = shift; |
| 478 |
|
| 479 |
return @{$self->{+SWITCHES}} if $self->{+SWITCHES}; |
| 480 |
|
| 481 |
my @switches; |
| 482 |
|
| 483 |
my %seen; |
| 484 |
for my $s (@{$self->{+TASK}->{switches} // []}) { |
| 485 |
$seen{$s}++; |
| 486 |
$self->{+USE_W_SWITCH} = 1 if $s =~ m/\s*-w\s*/; |
| 487 |
push @switches => $s; |
| 488 |
} |
| 489 |
|
| 490 |
my %seen2; |
| 491 |
for my $s (@{$self->{+RUNNER}->switches // []}) { |
| 492 |
next if $seen{$s}; |
| 493 |
$seen2{$s}++; |
| 494 |
$self->{+USE_W_SWITCH} = 1 if $s =~ m/\s*-w\s*/; |
| 495 |
push @switches => $s; |
| 496 |
} |
| 497 |
|
| 498 |
for my $s ($self->switches_from_env) { |
| 499 |
next if $seen{$s}; |
| 500 |
next if $seen2{$s}; |
| 501 |
$self->{+USE_W_SWITCH} = 1 if $s =~ m/\s*-w\s*/; |
| 502 |
push @switches => $s; |
| 503 |
} |
| 504 |
|
| 505 |
return @{$self->{+SWITCHES} = \@switches}; |
| 506 |
} |
| 507 |
|
| 508 |
sub prof_file { |
| 509 |
my $self = shift; |
| 510 |
my $file =$self->rel_file; |
| 511 |
|
| 512 |
$file =~ s{/}{-}g; |
| 513 |
$file =~ s{\.[^\.]+$}{.nytprof}g; |
| 514 |
|
| 515 |
return $file; |
| 516 |
} |
| 517 |
|
| 518 |
sub env_vars { |
| 519 |
my $self = shift; |
| 520 |
|
| 521 |
return $self->{+ENV_VARS} if $self->{+ENV_VARS}; |
| 522 |
|
| 523 |
my $from_run = $self->run->env_vars; |
| 524 |
my $from_task = $self->{+TASK}->{env_vars}; |
| 525 |
|
| 526 |
my @p5l = ($from_task->{PERL5LIB}, $from_run->{PERL5LIB}); |
| 527 |
push @p5l => $self->includes if $self->{+TASK}->{binary} || $self->{+TASK}->{non_perl}; |
| 528 |
push @p5l => $ENV{PERL5LIB} if $ENV{PERL5LIB}; |
| 529 |
my $p5l = join $Config{path_sep} => grep { defined $_ && $_ ne '.' } @p5l; |
| 530 |
|
| 531 |
my $verbose = $self->verbose; |
| 532 |
|
| 533 |
return $self->{+ENV_VARS} = { |
| 534 |
$from_run ? (%$from_run) : (), |
| 535 |
$from_task ? (%$from_task) : (), |
| 536 |
|
| 537 |
$self->use_stream ? (T2_FORMATTER => 'Stream', T2_STREAM_DIR => $self->event_dir, T2_STREAM_JOB_ID => $self->job_id) : (), |
| 538 |
|
| 539 |
$self->{+SETTINGS}->runner->nytprof ? (NYTPROF => "addpid=1:start=begin") : (), |
| 540 |
|
| 541 |
PERL5LIB => $p5l, |
| 542 |
PERL_USE_UNSAFE_INC => $self->unsafe_inc, |
| 543 |
TEST2_JOB_DIR => $self->job_dir, |
| 544 |
TEST2_RUN_DIR => $self->run_dir, |
| 545 |
TMPDIR => $self->tmp_dir, |
| 546 |
TEMPDIR => $self->tmp_dir, |
| 547 |
SYSTEM_TMPDIR => $self->{+SETTINGS}->harness->orig_tmp, |
| 548 |
SYSTEM_TMPDIR_PERMS => $self->{+SETTINGS}->harness->orig_tmp_perms, |
| 549 |
|
| 550 |
HARNESS_IS_VERBOSE => $verbose, |
| 551 |
T2_HARNESS_IS_VERBOSE => $verbose, |
| 552 |
|
| 553 |
HARNESS_ACTIVE => 1, |
| 554 |
TEST2_HARNESS_ACTIVE => 1, |
| 555 |
|
| 556 |
T2_HARNESS_JOB_FILE => $self->rel_file, |
| 557 |
T2_HARNESS_JOB_NAME => $self->{+TASK}->{job_name}, |
| 558 |
T2_HARNESS_JOB_IS_TRY => $self->{+IS_TRY} // 0, |
| 559 |
T2_HARNESS_JOB_DURATION => $self->{+TASK}->{duration} // '', |
| 560 |
}; |
| 561 |
} |
| 562 |
|
| 563 |
sub load_import { |
| 564 |
my $self = shift; |
| 565 |
|
| 566 |
return @{$self->{+LOAD_IMPORT}} if $self->{+LOAD_IMPORT}; |
| 567 |
|
| 568 |
my $from_run = $self->run->load_import; |
| 569 |
|
| 570 |
my @out; |
| 571 |
for my $mod (@{$from_run->{'@'} // []}) { |
| 572 |
push @out => [$mod, $from_run->{$mod} // []]; |
| 573 |
} |
| 574 |
|
| 575 |
return @{$self->{+LOAD_IMPORT} = \@out}; |
| 576 |
} |
| 577 |
|
| 578 |
sub use_w_switch { |
| 579 |
my $self = shift; |
| 580 |
return $self->{+USE_W_SWITCH} if defined $self->{+USE_W_SWITCH}; |
| 581 |
$self->switches; |
| 582 |
return $self->{+USE_W_SWITCH}; |
| 583 |
} |
| 584 |
|
| 585 |
sub set_exit { |
| 586 |
my $self = shift; |
| 587 |
my ($runner, $exit, $time, @args) = @_; |
| 588 |
|
| 589 |
$self->SUPER::set_exit(@_); |
| 590 |
|
| 591 |
my $file = File::Spec->catfile($self->job_dir, 'exit'); |
| 592 |
|
| 593 |
my $e = parse_exit($exit); |
| 594 |
|
| 595 |
write_file_atomic($file, join(" " => $exit, $e->{err}, $e->{sig}, $e->{dmp}, $time, @args)); |
| 596 |
} |
| 597 |
|
| 598 |
1; |
| 599 |
|
| 600 |
__END__ |
| 601 |
|
| 602 |
=pod |
| 603 |
|
| 604 |
=encoding UTF-8 |
| 605 |
|
| 606 |
=head1 NAME |
| 607 |
|
| 608 |
Test2::Harness::Runner::Job - Representation of a test job. |
| 609 |
|
| 610 |
=head1 DESCRIPTION |
| 611 |
|
| 612 |
This module takes all the data from a test file queue item, a run, and runner |
| 613 |
settings, and mashes them together to figure out what is actually needed to run |
| 614 |
a job. |
| 615 |
|
| 616 |
=head1 METHODS |
| 617 |
|
| 618 |
Note, this object subclasses L<Test2::Harness::IPC::Process>. |
| 619 |
|
| 620 |
=over 4 |
| 621 |
|
| 622 |
=item $arrayref = $job->args |
| 623 |
|
| 624 |
Get the arguments for the test either from the queue item, or from the run. |
| 625 |
|
| 626 |
=item $path = $job->bail_file |
| 627 |
|
| 628 |
Path to the events-file used in case of a bail-out |
| 629 |
|
| 630 |
=item $bool = $job->bailed_out |
| 631 |
|
| 632 |
True if the test job bailed out. |
| 633 |
|
| 634 |
=item $cat $job->category |
| 635 |
|
| 636 |
Process category, always 'job' unless overridden in a subclass. |
| 637 |
|
| 638 |
=item $path = $job->ch_dir |
| 639 |
|
| 640 |
If this job first requires a change in directory before running, this will |
| 641 |
return the path. |
| 642 |
|
| 643 |
=item @list = $job->cli_includes |
| 644 |
|
| 645 |
List of includes for a command line launch of this job. |
| 646 |
|
| 647 |
=item @list = $job->cli_options |
| 648 |
|
| 649 |
List of options for a command line launch of this job. |
| 650 |
|
| 651 |
=item $hashref = $job->env_vars |
| 652 |
|
| 653 |
Get environment variables to set when launching this job. |
| 654 |
|
| 655 |
=item $path = $job->out_file |
| 656 |
|
| 657 |
File to which all STDOUT for the job will be written. |
| 658 |
|
| 659 |
=item $path = $job->err_file |
| 660 |
|
| 661 |
File to which all STDERR for the job will be written. |
| 662 |
|
| 663 |
=item $path = $job->et_file |
| 664 |
|
| 665 |
File to which event timeout notifications will be written. |
| 666 |
|
| 667 |
=item $path = $job->pet_file |
| 668 |
|
| 669 |
File to which post exit timeout events will be written. |
| 670 |
|
| 671 |
=item $path = $job->event_dir |
| 672 |
|
| 673 |
Directory to which L<Test2::Formatter::Stream> events will be written. |
| 674 |
|
| 675 |
=item $time = $job->event_timeout |
| 676 |
|
| 677 |
Event timeout specification, if any, first from test queue item, then from |
| 678 |
runner. |
| 679 |
|
| 680 |
=item $time = $job->post_exit_timeout |
| 681 |
|
| 682 |
Post exit timeout specification, if any, first from test queue item, then from |
| 683 |
runner. |
| 684 |
|
| 685 |
=item $bool = $job->event_uuids |
| 686 |
|
| 687 |
Use L<Test2::Plugin::UUID> inside the test. |
| 688 |
|
| 689 |
=item $path = $job->file |
| 690 |
|
| 691 |
Test file the job will be running. |
| 692 |
|
| 693 |
=item $coderef = $job->fork_callback |
| 694 |
|
| 695 |
If the job is to be launched via fork, use this callback. |
| 696 |
|
| 697 |
=item $path = $job->in_file |
| 698 |
|
| 699 |
File containing STDIN to be provided to the test. |
| 700 |
|
| 701 |
=item @list = $job->includes |
| 702 |
|
| 703 |
Paths to add to @INC for the test. |
| 704 |
|
| 705 |
=item $bool = $job->io_events |
| 706 |
|
| 707 |
True if L<Test2::Plugin::IOEvents> should be used. |
| 708 |
|
| 709 |
=item $int = $job->is_try |
| 710 |
|
| 711 |
This starts at 0 and will be incremented for every retry of the job. |
| 712 |
|
| 713 |
=item $path = $job->job_dir |
| 714 |
|
| 715 |
Temporary directory housing all files related to this job when it runs. |
| 716 |
|
| 717 |
=item $uuid = $job->job_id |
| 718 |
|
| 719 |
UUID for this job. |
| 720 |
|
| 721 |
=item @list = $job->load |
| 722 |
|
| 723 |
Modules to load when starting this job. |
| 724 |
|
| 725 |
=item @list = $job->load_import |
| 726 |
|
| 727 |
Modules to load and import when starting this job. |
| 728 |
|
| 729 |
=item $bool = $job->mem_usage |
| 730 |
|
| 731 |
True if the L<Test2::Plugin::MemUsage> plugin should be used. |
| 732 |
|
| 733 |
=item $path = $job->run_file |
| 734 |
|
| 735 |
Usually the same as rel_file, but you can specify an alternative file to |
| 736 |
actually run. |
| 737 |
|
| 738 |
=item $path = $job->rel_file |
| 739 |
|
| 740 |
Relative path to the file. |
| 741 |
|
| 742 |
=item $int = $job->retry |
| 743 |
|
| 744 |
How many times the test should be retried if it fails. |
| 745 |
|
| 746 |
=item $bool = $job->retry_isolated |
| 747 |
|
| 748 |
True if the test should be retried in isolation if it fails. |
| 749 |
|
| 750 |
=item $run = $job->run |
| 751 |
|
| 752 |
The L<Test2::Harness::Runner::Run> instance. |
| 753 |
|
| 754 |
=item $path = $job->run_dir |
| 755 |
|
| 756 |
Path to the temporary directory housing all the data about the run. |
| 757 |
|
| 758 |
=item $runner = $job->runner |
| 759 |
|
| 760 |
The L<Test2::Harness::Runner> instance. |
| 761 |
|
| 762 |
=item @list = $job->runner_includes |
| 763 |
|
| 764 |
Search path includes provided directly by the runner. |
| 765 |
|
| 766 |
=item $settings = $job->settings |
| 767 |
|
| 768 |
The L<Test2::Harness::Settings> instance. |
| 769 |
|
| 770 |
=item $bool = $job->smoke |
| 771 |
|
| 772 |
True if the test is a priority smoke test. |
| 773 |
|
| 774 |
=item $hashref = $job->spawn_params |
| 775 |
|
| 776 |
Parameters for C<run_cmd()> in L<Test2::Harness::Util::IPC> when launching this |
| 777 |
job. If the task has a C<resource_skip> set (indicating permanently unavailable |
| 778 |
resources), the command will emit TAP to skip the test. When the |
| 779 |
C<--fail-on-resource-skip> runner option is enabled, the command will instead |
| 780 |
emit a TAP test failure. |
| 781 |
|
| 782 |
=item @list = $job->switches |
| 783 |
|
| 784 |
Command line switches for perl when running this test. |
| 785 |
|
| 786 |
=item $hashref = $job->task |
| 787 |
|
| 788 |
Task data from the queue. |
| 789 |
|
| 790 |
=item $path = $job->tmp_dir |
| 791 |
|
| 792 |
Temp dir created specifically for this job. |
| 793 |
|
| 794 |
=item $bool = $job->unsafe_inc |
| 795 |
|
| 796 |
True if '.' should be added to C<@INC>. |
| 797 |
|
| 798 |
=item $bool = $job->use_fork |
| 799 |
|
| 800 |
True if this job should be launched via fork. |
| 801 |
|
| 802 |
=item $bool = $job->use_stream |
| 803 |
|
| 804 |
True if this job should use L<Test2::Formatter::Stream>. |
| 805 |
|
| 806 |
=item $bool = $job->use_timeout |
| 807 |
|
| 808 |
True if this job should timeout due to lack of activity. |
| 809 |
|
| 810 |
=item $bool = $job->use_w_switch |
| 811 |
|
| 812 |
True if the C<-w> switch should be used for this test. |
| 813 |
|
| 814 |
=back |
| 815 |
|
| 816 |
=head1 SOURCE |
| 817 |
|
| 818 |
The source code repository for Test2-Harness can be found at |
| 819 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 820 |
|
| 821 |
=head1 MAINTAINERS |
| 822 |
|
| 823 |
=over 4 |
| 824 |
|
| 825 |
=item Chad Granum E<lt>[email protected]<gt> |
| 826 |
|
| 827 |
=back |
| 828 |
|
| 829 |
=head1 AUTHORS |
| 830 |
|
| 831 |
=over 4 |
| 832 |
|
| 833 |
=item Chad Granum E<lt>[email protected]<gt> |
| 834 |
|
| 835 |
=back |
| 836 |
|
| 837 |
=head1 COPYRIGHT |
| 838 |
|
| 839 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 840 |
|
| 841 |
This program is free software; you can redistribute it and/or |
| 842 |
modify it under the same terms as Perl itself. |
| 843 |
|
| 844 |
See F<http://dev.perl.org/licenses/> |
| 845 |
|
| 846 |
=cut |