Skip to content

Commit

Permalink
Extend worker tests to reach 100 % statement coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Oct 16, 2024
1 parent 2e5364d commit 4d22126
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ coverage:
target: 100.0
paths:
- lib/openQA/Worker
- lib/openQA/Worker.pm
tests:
target: 100.0
paths:
Expand Down
4 changes: 1 addition & 3 deletions lib/OpenQA/Worker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,7 @@ sub _lock_pool_directory ($self) {

chdir $pool_directory || die "cannot change directory to $pool_directory: $!\n";
open(my $lockfd, '>>', '.locked') or die "cannot open lock file in $pool_directory: $!\n";
unless (fcntl($lockfd, F_SETLK, pack('ssqql', F_WRLCK, 0, 0, 0, $$))) {
die "$pool_directory already locked\n";
}
die "$pool_directory already locked\n" unless fcntl $lockfd, F_SETLK, pack('ssqql', F_WRLCK, 0, 0, 0, $$);
$lockfd->autoflush(1);
truncate($lockfd, 0);
print $lockfd "$$\n";
Expand Down
28 changes: 27 additions & 1 deletion t/24-worker-overall.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use OpenQA::Constants qw(WORKER_COMMAND_QUIT WORKER_SR_API_FAILURE WORKER_SR_DIE
use OpenQA::Worker;
use OpenQA::Worker::Job;
use OpenQA::Worker::WebUIConnection;
use Socket qw(getaddrinfo);

$ENV{OPENQA_CONFIG} = "$FindBin::Bin/data/24-worker-overall";

Expand Down Expand Up @@ -415,7 +416,8 @@ subtest 'stopping' => sub {
my $fake_job = OpenQA::Worker::Job->new($worker, undef, {some => 'info'});
$worker->current_job($fake_job);
$worker->stop_current_job;
is($fake_job->status, 'stopped', 'job stopped');
is $fake_job->status, 'stopped', 'job stopped';
ok !$worker->is_stopping, 'worker is not considered stopping as job has already stopped';
};

subtest 'stop worker gracefully' => sub {
Expand Down Expand Up @@ -726,6 +728,7 @@ subtest 'handle job status changes' => sub {
$worker->current_webui_host('some-host');
$worker->settings->global_settings->{TERMINATE_AFTER_JOBS_DONE} = 1;
$worker->_init_queue([$fake_job]); # assume there's another job in the queue
is $worker->find_current_or_pending_job(42), $fake_job, 'queued job can be found';
combined_like {
$worker->_handle_job_status_changed($fake_job, {status => 'stopped', reason => 'another test'});
}
Expand All @@ -738,6 +741,7 @@ subtest 'handle job status changes' => sub {
}
qr/Job 42 from some-host finished - reason: yet another/, 'status of 2nd job logged';
is($stop_called, WORKER_COMMAND_QUIT, 'worker stopped after no more jobs left in the queue');
is $worker->find_current_or_pending_job(42), undef, 'queued job no longer pending';

$worker->settings->global_settings->{TERMINATE_AFTER_JOBS_DONE} = 0;

Expand Down Expand Up @@ -899,4 +903,26 @@ subtest 'handle critical error' => sub {
is $kill_called, 1, 'worker tried to kill itself in the end';
};

subtest 'resolving 127.0.0.1 without relying on getaddrinfo()' => sub {
combined_like {
is_deeply [sort keys %{getaddrinfo('127.0.0.1')}],
[qw(addr canonname family protocol socktype)],
'got expected fields'
} qr/Running patched getaddrinfo/s,
'using patched getaddrinfo()';
};

subtest 'storing package list' => sub {
$worker->pool_directory(tempdir('pool-dir-XXXXX'));
combined_like { $worker->_store_package_list('echo foo') }
qr/Gathering package information/, 'log message about command invocation';
is $worker->pool_directory->child('worker_packages.txt')->slurp('UTF-8'), "foo\n", 'package list written';

combined_like { $worker->_store_package_list('false') }
qr/could not be executed/, 'log message about error';

combined_like { $worker->_store_package_list('true') }
qr/doesn't return any data/, 'log message about no data';
};

done_testing();

0 comments on commit 4d22126

Please sign in to comment.