Project

General

Profile

Actions

action #167935

closed

coordination #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

Added by okurz 2 months ago. Updated about 12 hours ago.

Status:
Resolved
Priority:
Low
Assignee:
Category:
Feature requests
Target version:
Start date:
2024-10-08
Due date:
% Done:

100%

Estimated time:

Description

Acceptance criteria

  • AC1: the path myjsonrpc.pm is listed in codecov.yml under "fully_covered"

Suggestions


Related issues 2 (2 open0 closed)

Copied from openQA Project (public) - action #167932: Cover code of os-autoinst path needle.pm fully (statement coverage) size:SWorkable2024-10-08

Actions
Copied to openQA Project (public) - action #167938: Cover code of os-autoinst path lockapi.pm fully (statement coverage) size:SIn Progressgpuliti2024-10-08

Actions
Actions #1

Updated by okurz 2 months ago

  • Copied from action #167932: Cover code of os-autoinst path needle.pm fully (statement coverage) size:S added
Actions #2

Updated by okurz 2 months ago

  • Copied to action #167938: Cover code of os-autoinst path lockapi.pm fully (statement coverage) size:S added
Actions #3

Updated by okurz about 2 months ago

  • Parent task changed from #167917 to #130414
Actions #4

Updated by okurz 14 days ago

  • Target version changed from future to Ready
Actions #5

Updated by okurz 14 days ago

  • Subject changed from Cover code of os-autoinst path myjsonrpc.pm fully (statement coverage) size:M to Cover code of os-autoinst path myjsonrpc.pm fully (statement coverage)
Actions #6

Updated by gpuliti 12 days ago

  • Assignee set to gpuliti
Actions #8

Updated by okurz 10 days ago

  • Subject changed from Cover code of os-autoinst path myjsonrpc.pm fully (statement coverage) to Cover code of os-autoinst path myjsonrpc.pm fully (statement coverage) size:S
  • Status changed from New to Workable
Actions #9

Updated by gpuliti 10 days ago

  • Status changed from Workable to In Progress
Actions #10

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.

Actions #11

Updated by gpuliti about 12 hours ago

  • Status changed from Feedback to Resolved
Actions

Also available in: Atom PDF