From c7b63868c1d9c59bb77094093058de12695355f7 Mon Sep 17 00:00:00 2001 From: Sebastian Riedel Date: Thu, 16 May 2024 16:54:28 +0200 Subject: [PATCH] Add tests for obs command --- lib/Cavil/Command/obs.pm | 6 +- t/command_obs.t | 135 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 t/command_obs.t diff --git a/lib/Cavil/Command/obs.pm b/lib/Cavil/Command/obs.pm index 2f97b29169..755a86edbf 100644 --- a/lib/Cavil/Command/obs.pm +++ b/lib/Cavil/Command/obs.pm @@ -52,14 +52,14 @@ sub run ($self, @args) { my $app = $self->app; my $obs = $app->obs; my $info = $obs->package_info($api, $project, $pkg, {rev => $rev}); - return say dumper $info unless $download || $import; + return print STDOUT dumper $info unless $download || $import; # Download my ($srcpkg, $srcmd5, $verifymd5) = @{$info}{qw(package srcmd5 verifymd5)}; my $checkout_dir = $import ? $app->config->{checkout_dir} : $download; my $dir = path($checkout_dir, $srcpkg, $verifymd5)->make_path; $obs->download_source($api, $project, $pkg, $dir, {rev => $srcmd5}); - return say qq{Downloaded $pkg to "$dir".} if $download; + return print STDOUT qq{Downloaded $pkg to "$dir".\n} if $download; # Index my $user = $app->users->licensedigger; @@ -96,7 +96,7 @@ sub run ($self, @args) { 9 ); - say "Triggered obs_import job $job"; + print STDOUT "Triggered obs_import job $job\n"; } 1; diff --git a/t/command_obs.t b/t/command_obs.t new file mode 100644 index 0000000000..12c130ffe6 --- /dev/null +++ b/t/command_obs.t @@ -0,0 +1,135 @@ +# Copyright (C) 2024 SUSE LLC +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, see . + +use Mojo::Base -strict; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More; +use Test::Mojo; +use Cavil::Test; +use Mojo::File qw(tempdir); +use Mojolicious::Lite; + +plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE}; + +my $cavil_test = Cavil::Test->new(online => $ENV{TEST_ONLINE}, schema => 'command_obs_test'); +my $config = $cavil_test->default_config; +my $t = Test::Mojo->new(Cavil => $config); +my $app = $t->app; +$cavil_test->no_fixtures($app); + +app->log->level('error'); + +app->routes->add_condition( + query => sub { + my ($route, $c, $captures, $hash) = @_; + + for my $key (keys %$hash) { + my $values = ref $hash->{$key} ? $hash->{$key} : [$hash->{$key}]; + my $param = $c->req->url->query->param($key); + return undef unless defined $param && grep { $param eq $_ } @$values; + } + + return 1; + } +); + +get '/public/source/:project/perl-Mojolicious' => [project => ['home:kraih']] => (query => {view => 'info'}) => + {text => <<'EOF'}; + + perl-Mojolicious.spec + +EOF + +get '/public/source/:project/perl-Mojolicious/_meta' => [project => ['home:kraih']] => {text => <<'EOF'}; + + Mojolicious + + Real-time web framework + + +EOF + +get '/public/source/:project/perl-Mojolicious' => [project => ['home:kraih']] => + (query => {expand => 1, rev => [1, '0e5c2d1c0c4178869cf7fb82482b9c52']}) => {text => <<'EOF'}; + + + + + + +EOF + +get '/public/source/:project/perl-Mojolicious/perl-Mojolicious.spec' => [project => ['home:kraih']] => + (query => {rev => '9199eca9ec0fa5cffe4c3a6cb99a8093'}) => {text => 'Mojolicious spec!'}; + +get '/public/source/:project/perl-Mojolicious/:special' => [project => ['home:kraih']] => + (query => {rev => '9199eca9ec0fa5cffe4c3a6cb99a8093'}) => [special => ['perl-Mojo#licious.changes']] => + {text => 'Mojolicious changes!'}; + +my $api = 'http://127.0.0.1:' . $app->obs->ua->server->app(app)->url->port; + +subtest 'OBS' => sub { + subtest 'Info' => sub { + my $buffer = ''; + { + open my $handle, '>', \$buffer; + local *STDOUT = $handle; + $app->start('obs', $api, 'home:kraih', 'perl-Mojolicious'); + } + like $buffer, qr/package.+perl-Mojolicious/, 'package info'; + like $buffer, qr/srcmd5.+0e5c2d1c0c4178869cf7fb82482b9c52/, 'srcmd5 info'; + }; + + subtest 'Download' => sub { + my $tempdir = tempdir; + my $buffer = ''; + { + open my $handle, '>', \$buffer; + local *STDOUT = $handle; + $app->start('obs', $api, 'home:kraih', 'perl-Mojolicious', '-d', $tempdir); + } + like $buffer, qr/Downloaded/i, 'right output'; + ok -e $tempdir->child('perl-Mojolicious', 'bb19066400b2b60e2310b45f10d12f56', 'perl-Mojo#licious.changes'), + 'spec file exists'; + ok -e $tempdir->child('perl-Mojolicious', 'bb19066400b2b60e2310b45f10d12f56', 'perl-Mojolicious.spec'), + 'spec file exists'; + }; + + subtest 'Import' => sub { + is $app->minion->jobs({tasks => ['obs_import']})->total, 0, 'no jobs queued'; + my $buffer = ''; + { + open my $handle, '>', \$buffer; + local *STDOUT = $handle; + $app->start('obs', $api, 'home:kraih', 'perl-Mojolicious', '--import'); + } + like $buffer, qr/Triggered obs_import job 1/i, 'right output'; + is $app->minion->jobs({tasks => ['obs_import']})->total, 1, 'job queued'; + }; +}; + +done_testing();