diff --git a/t/14-grutasks-git.t b/t/14-grutasks-git.t index 05f9301ef4a..20f32b05758 100644 --- a/t/14-grutasks-git.t +++ b/t/14-grutasks-git.t @@ -11,6 +11,7 @@ use OpenQA::Task::Git::Clone; require OpenQA::Test::Database; use OpenQA::Test::Utils qw(run_gru_job perform_minion_jobs); use OpenQA::Test::TimeLimit '20'; +use Test::Output qw(stderr_like); use Test::MockModule; use Test::Mojo; use Test::Warnings qw(:report_warnings); @@ -25,6 +26,7 @@ chdir $workdir; path('t/data/openqa/db')->make_path; my $git_clones = "$workdir/git-clones"; mkdir $git_clones; +mkdir "$git_clones/$_" for qw(default branch dirty-error dirty-status nodefault wrong-url); my $schema = OpenQA::Test::Database->new->create(); my $t = Test::Mojo->new('OpenQA::WebAPI'); @@ -40,9 +42,9 @@ subtest 'git clone' => sub { my $openqa_git = Test::MockModule->new('OpenQA::Git'); my @mocked_git_calls; my $clone_dirs = { - '/etc/' => 'http://localhost/foo.git', - '/root/' => 'http://localhost/foo.git#foobranch', - '/this_directory_does_not_exist/' => 'http://localhost/bar.git', + "$git_clones/default/" => 'http://localhost/foo.git', + "$git_clones/branch/" => 'http://localhost/foo.git#foobranch', + "$git_clones/this_directory_does_not_exist/" => 'http://localhost/bar.git', }; $openqa_git->redefine( run_cmd_with_log_return_error => sub ($cmd) { @@ -60,6 +62,9 @@ subtest 'git clone' => sub { elsif ($path =~ m/opensuse/) { $stdout = 'http://osado'; } + elsif ($path =~ m/wrong-url/) { + $stdout = 'http://other'; + } } elsif ($action eq 'ls-remote') { $stdout = 'ref: refs/heads/master HEAD'; @@ -69,9 +74,9 @@ subtest 'git clone' => sub { $stdout = 'master'; } elsif ($action eq 'diff-index') { - $return_code = 1 if $path eq '/opt/'; # /opt/ simulates dirty checkout + $return_code = 1 if $path =~ m/dirty-status/; $return_code = 2 - if $path eq '/lib/'; # /lib/ simulates error when checking checkout dirty status + if $path =~ m/dirty-error/; } return { status => $return_code == 0, @@ -84,19 +89,19 @@ subtest 'git clone' => sub { is $res->{result}, 'Job successfully executed', 'minion job result indicates success'; #<<< no perltidy my $expected_calls = [ - # /etc/ - ['get-url' => 'git -C /etc/ remote get-url origin'], - ['check dirty' => 'git -C /etc/ diff-index HEAD --exit-code'], + # /branch + ['get-url' => 'git -C /branch/ remote get-url origin'], + ['check dirty' => 'git -C /branch/ diff-index HEAD --exit-code'], + ['current branch' => 'git -C /branch/ branch --show-current'], + ['fetch branch' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git -C /branch/ fetch origin foobranch'], + + # /default/ + ['get-url' => 'git -C /default/ remote get-url origin'], + ['check dirty' => 'git -C /default/ diff-index HEAD --exit-code'], ['default remote' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git ls-remote --symref http://localhost/foo.git HEAD'], - ['current branch' => 'git -C /etc/ branch --show-current'], - ['fetch default' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git -C /etc/ fetch origin master'], - ['reset' => 'git -C /etc/ reset --hard origin/master'], - - # /root - ['get-url' => 'git -C /root/ remote get-url origin'], - ['check dirty' => 'git -C /root/ diff-index HEAD --exit-code'], - ['current branch' => 'git -C /root/ branch --show-current'], - ['fetch branch' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git -C /root/ fetch origin foobranch'], + ['current branch' => 'git -C /default/ branch --show-current'], + ['fetch default' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git -C /default/ fetch origin master'], + ['reset' => 'git -C /default/ reset --hard origin/master'], # /this_directory_does_not_exist/ ['clone' => 'env GIT_SSH_COMMAND="ssh -oBatchMode=yes" git clone http://localhost/bar.git /this_directory_does_not_exist/'], @@ -109,8 +114,11 @@ subtest 'git clone' => sub { subtest 'no default remote branch' => sub { $ENV{OPENQA_GIT_CLONE_RETRIES} = 0; - $clone_dirs = {'/tmp/' => 'http://localhost/nodefault.git'}; - my $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}); + $clone_dirs = {"$git_clones/nodefault" => 'http://localhost/nodefault.git'}; + stderr_like { + $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}) + } + qr(Error detecting remote default), 'error on stderr'; is $res->{state}, 'failed', 'minion job failed'; like $res->{result}, qr/Error detecting remote default.*ref: something/, 'error message'; }; @@ -127,27 +135,45 @@ subtest 'git clone' => sub { $ENV{OPENQA_GIT_CLONE_RETRIES} = 0; my $openqa_clone = Test::MockModule->new('OpenQA::Task::Git::Clone'); $openqa_clone->redefine(_git_clone => sub (@) { die "fake error\n" }); - $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}); + stderr_like { + $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}) + } + qr(fake error), 'error message on stderr'; is $res->{retries}, 0, 'job retries not incremented'; is $res->{state}, 'failed', 'job considered failed'; }; subtest 'dirty git checkout' => sub { - # /opt/ is mocked to be always reported as dirty - $clone_dirs = {'/opt/' => 'http://localhost/foo.git'}; - my $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}); + $clone_dirs = {"$git_clones/dirty-status" => 'http://localhost/foo.git'}; + stderr_like { + $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}) + } + qr(git diff-index HEAD), 'error about diff on stderr'; is $res->{state}, 'failed', 'minion job failed'; like $res->{result}, qr/NOT updating dirty git checkout/, 'error message'; }; subtest 'error testing dirty git checkout' => sub { - # /lib/ is mocked to be always reported as dirty - $clone_dirs = {'/lib/' => 'http://localhost/foo.git'}; - my $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}); + $clone_dirs = {"$git_clones/dirty-error/" => 'http://localhost/foo.git'}; + stderr_like { + $res = run_gru_job($t->app, 'git_clone', $clone_dirs, {priority => 10}) + } + qr(Unexpected exit code 2), 'error message on stderr'; is $res->{state}, 'failed', 'minion job failed'; like $res->{result}, qr/Internal Git error: Unexpected exit code 2/, 'error message'; }; + subtest 'error because of different url' => sub { + $clone_dirs = {}; + my $clone_dirs2 = {"$git_clones/wrong-url/" => 'http://localhost/different.git'}; + stderr_like { + $res = run_gru_job($t->app, 'git_clone', $clone_dirs2, {priority => 10}) + } + qr(Local checkout.*has origin.*but requesting to clone from), 'Warning about different url'; + is $res->{state}, 'finished', 'minion job finished'; + is $res->{result}, 'Job successfully executed', 'minion job result indicates success'; + }; + subtest 'update clones without CASEDIR' => sub { @mocked_git_calls = (); #<<< no perltidy