|
| 1 | +# -*- mode: cperl -*- |
| 2 | +# ${license-info} |
| 3 | +# ${author-info} |
| 4 | +# ${build-info} |
| 5 | + |
| 6 | +=pod |
| 7 | +
|
| 8 | +=head1 DESCRIPTION |
| 9 | +
|
| 10 | +Tests for the C<execute_command> method in NCM::Component::spma::dnf. |
| 11 | +This method executes shell commands and returns (exit code, stdout, stderr). |
| 12 | +
|
| 13 | +=head1 TESTS |
| 14 | +
|
| 15 | +The tests verify that commands are executed correctly and that |
| 16 | +successes and errors are properly propagated to the caller. |
| 17 | +
|
| 18 | +=cut |
| 19 | + |
| 20 | +use strict; |
| 21 | +use warnings; |
| 22 | +use Readonly; |
| 23 | +use Test::More; |
| 24 | +use Test::Quattor; |
| 25 | +use NCM::Component::spma::dnf; |
| 26 | +use CAF::Object; |
| 27 | + |
| 28 | +$CAF::Object::NoAction = 1; |
| 29 | + |
| 30 | +my $cmp = NCM::Component::spma::dnf->new("spma"); |
| 31 | + |
| 32 | +Readonly::Array my @TEST_CMD => ("echo", "test"); |
| 33 | +Readonly my $CMD_LINE => join(" ", @TEST_CMD); |
| 34 | +Readonly my $WHY => "testing command execution"; |
| 35 | + |
| 36 | +=pod |
| 37 | +
|
| 38 | +=head2 Test successful command execution |
| 39 | +
|
| 40 | +=cut |
| 41 | + |
| 42 | +set_desired_output($CMD_LINE, "test output"); |
| 43 | +set_desired_err($CMD_LINE, ""); |
| 44 | +set_command_status($CMD_LINE, 0); |
| 45 | + |
| 46 | +my ($exit, $out, $err) = $cmp->execute_command(\@TEST_CMD, $WHY, 1); |
| 47 | +is($exit, 0, "execute_command returns exit code 0 on success"); |
| 48 | +is($out, "test output", "execute_command captures stdout"); |
| 49 | +is($err, "", "execute_command captures empty stderr"); |
| 50 | + |
| 51 | +my $cmd = get_command($CMD_LINE); |
| 52 | +ok($cmd, "Correct command called"); |
| 53 | +is($cmd->{method}, "execute", "Correct method called"); |
| 54 | + |
| 55 | +=pod |
| 56 | +
|
| 57 | +=head2 Test command with stderr output |
| 58 | +
|
| 59 | +=cut |
| 60 | + |
| 61 | +set_desired_output($CMD_LINE, "stdout content"); |
| 62 | +set_desired_err($CMD_LINE, "stderr content"); |
| 63 | +set_command_status($CMD_LINE, 0); |
| 64 | + |
| 65 | +($exit, $out, $err) = $cmp->execute_command(\@TEST_CMD, $WHY, 1); |
| 66 | +is($exit, 0, "exit code 0 with stderr output"); |
| 67 | +is($out, "stdout content", "stdout captured correctly"); |
| 68 | +is($err, "stderr content", "stderr captured correctly"); |
| 69 | + |
| 70 | +=pod |
| 71 | +
|
| 72 | +=head2 Test command failure (non-zero exit) |
| 73 | +
|
| 74 | +=cut |
| 75 | + |
| 76 | +set_desired_output($CMD_LINE, ""); |
| 77 | +set_desired_err($CMD_LINE, "error message"); |
| 78 | +set_command_status($CMD_LINE, 256); |
| 79 | + |
| 80 | +($exit, $out, $err) = $cmp->execute_command(\@TEST_CMD, $WHY, 1); |
| 81 | +isnt($exit, 0, "non-zero exit code returned on failure"); |
| 82 | +is($err, "error message", "error message captured"); |
| 83 | + |
| 84 | +=pod |
| 85 | +
|
| 86 | +=head2 Test keeps_state parameter |
| 87 | +
|
| 88 | +=cut |
| 89 | + |
| 90 | +set_desired_output($CMD_LINE, "output"); |
| 91 | +set_desired_err($CMD_LINE, ""); |
| 92 | +set_command_status($CMD_LINE, 0); |
| 93 | + |
| 94 | +($exit, $out, $err) = $cmp->execute_command(\@TEST_CMD, $WHY, 1); |
| 95 | +$cmd = get_command($CMD_LINE); |
| 96 | +ok(!$cmd->{object}->{NoAction}, "keeps_state=1 disables NoAction for command"); |
| 97 | + |
| 98 | +($exit, $out, $err) = $cmp->execute_command(\@TEST_CMD, $WHY, 0); |
| 99 | +$cmd = get_command($CMD_LINE); |
| 100 | +ok($cmd->{object}->{NoAction}, "keeps_state=0 keeps NoAction for command"); |
| 101 | + |
| 102 | +done_testing(); |
0 commit comments