Skip to content

Commit a15746b

Browse files
committed
Log output of optipng when it fails
* Remove the `-quiet` parameter * Use `run_cmd_with_log_return_error` for logging the command output in the error case * Improve error messages in `run_cmd_with_log_return_error` to include the program name so the error message is also useful on its own (without the prior `Running cmd: …` message) * Make `run_cmd_with_log_return_error` return the error message so it can be passed as reason when using it to run `optipng` * See https://progress.opensuse.org/issues/190920
1 parent 29fc49d commit a15746b

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

lib/OpenQA/Utils.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
305305
my $stdout_level = $args{stdout} // 'debug';
306306
my $stderr_level = $args{stderr} // 'debug';
307307
my $output_file = $args{output_file};
308+
my $program = $cmd->[0] // 'cmd';
308309
log_info('Running cmd: ' . join(' ', @$cmd));
309310
try {
310311
my ($stdin, $stdout, $stderr) = ('') x 3;
@@ -314,8 +315,8 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
314315
my $return_code = ($error_code & 127) ? (undef) : ($error_code >> 8);
315316
my $message
316317
= defined $return_code
317-
? ("cmd returned $return_code")
318-
: sprintf('cmd died with signal %d', $error_code & 127);
318+
? ("$program returned $return_code")
319+
: sprintf('%s died with signal %d', $program, $error_code & 127);
319320
my $expected_return_codes = $args{expected_return_codes};
320321
chomp $stderr;
321322
if (
@@ -337,6 +338,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
337338
return_code => $return_code,
338339
stdout => $stdout,
339340
stderr => $stderr,
341+
message => $message,
340342
};
341343
}
342344
catch ($e) {
@@ -345,6 +347,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
345347
return_code => undef,
346348
stderr => 'an internal error occurred',
347349
stdout => '',
350+
message => "failed to execute $program",
348351
};
349352
}
350353
}

lib/OpenQA/Worker/Job.pm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use OpenQA::Jobs::Constants;
1111
use OpenQA::Worker::Engines::isotovideo;
1212
use OpenQA::Worker::Isotovideo::Client;
1313
use OpenQA::Log qw(log_error log_warning log_debug log_info redact_settings_in_file);
14-
use OpenQA::Utils qw(find_video_files usleep_backoff);
14+
use OpenQA::Utils qw(find_video_files usleep_backoff run_cmd_with_log_return_error);
1515

1616
use Digest::MD5;
1717
use Fcntl;
@@ -1243,14 +1243,14 @@ sub _optimize_image ($image, $job_settings, $optipng_bin = 'optipng', $pngquant_
12431243
my @command
12441244
= $job_settings->{USE_PNGQUANT}
12451245
? ($pngquant_bin, '--force', '--output', $image, $image)
1246-
: ($optipng_bin, '-quiet', '-o2', $image);
1246+
: ($optipng_bin, '-o2', $image);
1247+
my $res;
12471248
for (1 .. OPTIMIZE_ATTEMPTS) {
1248-
last if system(@command) == 0 || $? == -1;
1249+
$res = run_cmd_with_log_return_error \@command, stdout => 'trace', stderr => 'trace';
1250+
last if $res->{status};
12491251
sleep OPTIMIZE_RETRY_DELAY;
12501252
}
1251-
die "$OPTIMIZE_ERROR failed to execute $command[0]: $!\n" if $? == -1;
1252-
die sprintf("$OPTIMIZE_ERROR %s failed with signal %d\n", $command[0], $? & 127) if $? & 127;
1253-
die sprintf("$OPTIMIZE_ERROR %s exited with non-zero return code %d\n", $command[0], $? >> 8) if $?;
1253+
die "$OPTIMIZE_ERROR $res->{message}\n" unless $res->{status};
12541254
return 1;
12551255
}
12561256

t/16-utils-runcmd.t

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ subtest 'run (arbitrary) command' => sub {
5050
ok $res->{status}, 'status ok';
5151
is $res->{stdout}, "Hallo Welt\n", 'cmd output returned';
5252

53-
stdout_like { $res = run_cmd_with_log_return_error([qw(false)]) } qr/.*\[error\].*cmd returned [1-9][0-9]*/i;
53+
stdout_like { $res = run_cmd_with_log_return_error([qw(false)]) } qr/.*\[error\].*false returned [1-9][0-9]*/i;
5454
is $res->{return_code}, 1, 'correct non-zero exit code returned ($? >> 8)';
5555
ok !$res->{status}, 'status not ok (non-zero status returned)';
5656

57-
stdout_unlike { $res = run_cmd_with_log_return_error([qw(falße)]) } qr/.*cmd returned [1-9][0-9]*/i;
57+
stdout_unlike { $res = run_cmd_with_log_return_error([qw(falße)]) } qr/.*falße returned [1-9][0-9]*/i;
5858
is $res->{return_code}, undef, 'no exit code returned if command cannot be executed';
5959
is $res->{stderr}, 'an internal error occurred', 'error message returned as stderr';
6060
ok !$res->{status}, 'status not ok if command cannot be executed';
6161

6262
stdout_like { $res = run_cmd_with_log_return_error(['bash', '-c', 'kill -s KILL $$']) }
63-
qr/.*cmd died with signal 9\n.*/i;
63+
qr/.*bash died with signal 9\n.*/i;
6464
is $res->{return_code}, undef, 'no exit code returned if command dies with a signal';
6565
ok !$res->{status}, 'status not ok if command dies with a signal';
6666
};
@@ -81,29 +81,29 @@ subtest 'invoke Git commands for real testing error handling' => sub {
8181
combined_like {
8282
throws_ok { $git->check_sha('this-sha-does-not-exist') } qr/internal Git error/i,
8383
'check throws an exception'
84-
} qr/\[error\].*cmd returned [1-9][0-9]*/, 'Git error logged for check as well';
84+
} qr/\[error\].*git returned [1-9][0-9]*/, 'Git error logged for check as well';
8585
};
8686

8787
combined_like {
8888
$git->invoke_command($_) for ['init'], ['config', 'user.email', 'foo@bar'], ['config', 'user.name', 'Foo'];
8989
}
90-
qr/\[info\].*cmd returned 0\n/, 'initialized Git repo; successful command exit logged as info';
90+
qr/\[info\].*git returned 0\n/, 'initialized Git repo; successful command exit logged as info';
9191

9292
subtest 'error handling when checking sha' => sub {
9393
stdout_like { ok !$git->check_sha('this-sha-does-not-exist'), 'return code 1 interpreted correctly' }
94-
qr/\[info\].*cmd returned 1\n/i,
94+
qr/\[info\].*git returned 1\n/i,
9595
'no error logged if check returns false (despite Git returning 1)';
9696
};
9797

9898
subtest 'error handling when checking whether working directory is clean' => sub {
9999
my $test_file = $empty_tmp_dir->child('foo')->touch;
100-
combined_like { $git->commit({add => ['foo'], message => 'test'}) } qr/commit.*foo.*cmd returned 0/is,
100+
combined_like { $git->commit({add => ['foo'], message => 'test'}) } qr/commit.*foo.*git returned 0/is,
101101
'commit created';
102102
stdout_like { ok $git->is_workdir_clean, 'return code 0 interpreted correctly' }
103-
qr/\[info\].*cmd returned 0\n/i, 'no error (only info) logged if check returns true';
103+
qr/\[info\].*git returned 0\n/i, 'no error (only info) logged if check returns true';
104104
$test_file->spew('test');
105105
stdout_like { ok !$git->is_workdir_clean, 'return code 1 interpreted correctly' }
106-
qr/\[info\].*cmd returned 1\n/i,
106+
qr/\[info\].*git returned 1\n/i,
107107
'no error (only info) logged if check returns false (despite Git returning 1)';
108108
};
109109

t/24-worker-jobs.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,11 +1244,9 @@ subtest 'image optimization' => sub {
12441244
combined_like {
12451245
is $opt->('foo', {OPTIMIZE_IMAGES => 0}), 0, 'image optimization can be skipped';
12461246
throws_ok { $opt->('foo', {}) }
1247-
qr/(failed to execute optipng|optipng exited with non-zero return code)/,
1248-
'failing to run optipng is a hard error';
1247+
qr/(failed to execute optipng|optipng returned)/, 'failing to run optipng is a hard error';
12491248
throws_ok { $opt->('foo', {USE_PNGQUANT => 1}) }
1250-
qr/(failed to execute pngquant|pngquant exited with non-zero return code)/,
1251-
'failing to run pngquant is a hard error';
1249+
qr/(failed to execute pngquant|pngquant returned)/, 'failing to run pngquant is a hard error';
12521250
is $opt->('bar', {}, 'true'), 1, 'successful optimization';
12531251
}
12541252
qr/Optimizing foo.*Optimizing bar/s, 'optimizing logged';

0 commit comments

Comments
 (0)