Skip to content

Commit

Permalink
openqa-clone-job: Allow printing result as JSON
Browse files Browse the repository at this point in the history
Printing the result of `openqa-clone-job` as JSON would likely be useful
for `openqa-investigate`'s dependency handling (see
https://progress.opensuse.org/issues/95783) but I suppose it is a good idea
in general to provide a machine-readable output format.
  • Loading branch information
Martchus committed Mar 17, 2022
1 parent 9c1b2bd commit 5ac2693
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
11 changes: 6 additions & 5 deletions lib/OpenQA/Script/CloneJob.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ sub clone_job_get_job ($jobid, $url_handler, $options) {
exit 1;
}
my $job = $tx->res->json->{job};
print Cpanel::JSON::XS->new->pretty->encode($job) if $options->{verbose};
print STDERR Cpanel::JSON::XS->new->pretty->encode($job) if $options->{verbose};
return $job;
}

Expand Down Expand Up @@ -131,10 +131,10 @@ sub clone_job_download_assets ($jobid, $job, $url_handler, $options) {

die "can't write $options->{dir}/$type\n" unless -w "$options->{dir}/$type";

print "downloading\n$from\nto\n$dst\n";
print STDERR "downloading\n$from\nto\n$dst\n";
my $r = $ua->mirror($from, $dst);
unless ($r->is_success || $r->code == 304) {
print "$jobid failed: $file, ", $r->status_line, "\n";
print STDERR "$jobid failed: $file, ", $r->status_line, "\n";
die "Can't clone due to missing assets: ", $r->status_line, " \n"
unless $options->{'ignore-missing-assets'};
}
Expand Down Expand Up @@ -210,6 +210,7 @@ sub handle_tx ($tx, $url_handler, $options, $jobs) {
my $json = $res->json;
if (!$tx->error && ref $json eq 'HASH' && ref $json->{ids} eq 'HASH') {
my $cloned_jobs = $json->{ids};
print Cpanel::JSON::XS->new->pretty->encode($cloned_jobs) and return $cloned_jobs if $options->{'json-output'};
my $base_url = openqa_baseurl($url_handler->{local_url});
for my $orig_job_id (keys %$cloned_jobs) {
my $orig_job = $jobs->{$orig_job_id};
Expand Down Expand Up @@ -245,7 +246,7 @@ sub clone_job ($jobid, $url_handler, $options, $post_params = {}, $jobs = {}, $d
next unless $job->{$job_type};

my ($chained, $directly_chained, $parallel) = get_deps($job, $options, $job_type);
print "Cloning $job_type of $job->{name}\n" if @$chained || @$directly_chained || @$parallel;
print STDERR "Cloning $job_type of $job->{name}\n" if @$chained || @$directly_chained || @$parallel;

for my $dependencies ($chained, $directly_chained, $parallel) {
if ($job_type eq 'children') {
Expand Down Expand Up @@ -276,7 +277,7 @@ sub post_jobs ($post_params, $url_handler, $options) {
map { my $key = "$_:$job_id"; $key => $params_for_job->{$_} } keys %$params_for_job
} keys %$post_params;
$composed_params{is_clone_job} = 1; # used to figure out if this is a clone operation
print Cpanel::JSON::XS->new->pretty->encode(\%composed_params) if $options->{verbose};
print STDERR Cpanel::JSON::XS->new->pretty->encode(\%composed_params) if $options->{verbose};
my ($local, $local_url) = ($url_handler->{local}, $url_handler->{local_url}->clone);
return $local->max_redirects(3)->post($local_url, form => \%composed_params);
}
Expand Down
8 changes: 4 additions & 4 deletions script/openqa-clone-job
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ sub usage ($r) {
sub parse_options() {
GetOptions(
\%options, "from=s", "host=s", "dir=s",
"apikey:s", "apisecret:s", "verbose|v", "skip-deps",
"skip-chained-deps", "skip-download", "parental-inheritance", "help|h",
"show-progress", "within-instance|w=s", "clone-children", "max-depth:i",
"ignore-missing-assets",
"apikey:s", "apisecret:s", "verbose|v", "json-output|j",
"skip-deps", "skip-chained-deps", "skip-download", "parental-inheritance",
"help|h", "show-progress", "within-instance|w=s", "clone-children",
"max-depth:i", "ignore-missing-assets",
) or usage(1);
usage(0) if $options{help};
usage(1) if $options{help} || ($options{'within-instance'} && $options{from});
Expand Down
20 changes: 17 additions & 3 deletions t/35-script_clone_job.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
use OpenQA::Test::TimeLimit '6';
use Mojo::Base -signatures;
use Test::Exception;
use Test::Output 'combined_like';
use Test::Output qw(combined_like output_from);
use Test::MockObject;
use Test::MockModule;
use OpenQA::Script::CloneJob;
use Mojo::JSON qw(decode_json);
use Mojo::URL;
use Mojo::File 'tempdir';
use Mojo::Transaction;
Expand Down Expand Up @@ -297,11 +299,23 @@ subtest 'overall cloning with parallel and chained dependencies' => sub {
} or diag explain \@post_args;
};

subtest 'clone only parallel children' => sub {
subtest 'clone only parallel children, enable json output' => sub {
# invoke handle_tx with fake data
$clone_mock->redefine(
handle_tx => sub (@) {
my $res = Test::MockObject->new->set_always(json => {ids => {1 => 2}});
my $tx = Test::MockObject->new->set_false('error')->set_always(res => $res);
$clone_mock->original('handle_tx')->($tx, undef, \%options, undef);
});

@post_args = ();
$fake_jobs{41}->{children}->{Chained} = [7];
$options{'parental-inheritance'} = undef;
combined_like { OpenQA::Script::CloneJob::clone_jobs(41, \%options) } qr/cloning/i, 'output logged';
$options{'json-output'} = 1;
my ($stdout, $stderr) = output_from { OpenQA::Script::CloneJob::clone_jobs(41, \%options) };
my $json_output = decode_json $stdout;
like $stderr, qr/cloning/i, 'logs end up in stderr';
is_deeply $json_output, {1 => 2}, 'fake response printed as JSON' or diag explain $json_output;
subtest 'post args' => sub {
my $params = $check_common_post_args->() or return;
is delete $params->{'FOO:41'}, 'bar', 'setting passed to main job';
Expand Down

0 comments on commit 5ac2693

Please sign in to comment.