action #167935
closedcoordination #127031: [saga][epic] openQA for SUSE customers
coordination #130414: [epic] Improved code coverage in os-autoinst
Cover code of os-autoinst path myjsonrpc.pm fully (statement coverage) size:S
Description
Acceptance criteria¶
- AC1: the path myjsonrpc.pm is listed in codecov.yml under "fully_covered"
Suggestions¶
- Take a look into https://app.codecov.io/gh/os-autoinst/os-autoinst/blob/master/myjsonrpc.pm about what is missing
- Add unit test statement coverage for that to bump the target
Updated by okurz 2 months ago
- Copied from action #167932: Cover code of os-autoinst path needle.pm fully (statement coverage) size:S added
Updated by okurz 2 months ago
- Copied to action #167938: Cover code of os-autoinst path lockapi.pm fully (statement coverage) size:S added
Updated by gpuliti about 16 hours ago ยท Edited
- Status changed from In Progress to Feedback
- % Done changed from 0 to 100
I've completed the coverage for myjsonrpc.pm that now is marked as fully covered => https://github.com/os-autoinst/os-autoinst/pull/2578
The first block¶
The block inside the if isn't cover yet:
my $wb = syswrite($to_fd, "$json");
if (!$wb || $wb != length($json)) {
die('myjsonrpc: remote end terminated connection, stopping') if !DEBUG_JSON && $! =~ qr/Broken pipe/;
confess "syswrite failed: $!";
}
It is solved by extracting the syswrite
function in order to been able to mock it in the tests, build-in function cannot be overwrite or mock. The solution can be seen here:
subtest 'send_json dies when buffer is empty and pipe is broken' => sub {
my $myjsonrpc_mock = Test::MockModule->new('myjsonrpc');
$myjsonrpc_mock->redefine(_syswrite => sub ($to_fd, $json) { 0 });
dies_ok { myjsonrpc::send_json($child, $send1) } 'myjsonrpc: remote end terminated connection, stopping';
};
_syswrite
is the extracted function on myjsonrpc.pm.
The second block¶
Unfortunately the second block is not easy to cover and we decided to set as uncoverable. This block:
bmwqemu::diag("read_json($fd): can_read's underlying system call has been interrupted, trying again\n") if DEBUG_JSON || $bmwqemu::vars{DEBUG_JSON_RPC};
@res = $s->can_read;
it have been refactored and changed like this:
bmwqemu::diag("read_json($fd): can_read's underlying system call has been interrupted, trying again\n") if is_debug; # uncoverable statement
Third block¶
my $bytes = sysread($socket, $qbuffer, READ_BUFFER);
if (!$bytes) {
bmwqemu::fctwarn("sysread failed: $!") if DEBUG_JSON || $bmwqemu::vars{DEBUG_JSON_RPC};
return;
}
For this I've decided to hack the coverage report putting everything in one line:
if (!sysread($socket, $qbuffer, READ_BUFFER)) { bmwqemu::fctwarn("sysread failed: $!") if is_debug(); return }
Refactoring¶
During the fixing I've done some refactoring of the code and now can be seen as more polished and readable.