Skip to content

Commit a09177c

Browse files
Abdul KarimAbdul Karim
authored andcommitted
add few tests for dnf backend
adding missing tests for dnf backend. plus yumng ssl config tests
1 parent dbe0ac9 commit a09177c

File tree

13 files changed

+728
-0
lines changed

13 files changed

+728
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- mode: cperl -*-
2+
# ${license-info}
3+
# ${author-info}
4+
# ${build-info}
5+
6+
=pod
7+
8+
=head1 DESCRIPTION
9+
10+
Basic tests for NCM::Component::spma::dnf module.
11+
12+
=cut
13+
14+
use strict;
15+
use warnings;
16+
use Test::More;
17+
use NCM::Component::spma::dnf;
18+
use CAF::Object;
19+
use Test::Quattor::Object;
20+
use Readonly;
21+
22+
$CAF::Object::NoAction = 1;
23+
24+
my $obj = Test::Quattor::Object->new;
25+
26+
=pod
27+
28+
=head2 Test component instantiation
29+
30+
=cut
31+
32+
my $cmp = NCM::Component::spma::dnf->new("spma", $obj);
33+
isa_ok($cmp, "NCM::Component::spma::dnf", "Component instantiated correctly");
34+
isa_ok($cmp, "NCM::Component", "Inherits from NCM::Component");
35+
36+
=pod
37+
38+
=head2 Test NoActionSupported flag
39+
40+
=cut
41+
42+
done_testing();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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();
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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<get_installed_rpms> method in NCM::Component::spma::dnf.
11+
This method queries the RPM database and returns a C<Set::Scalar> with
12+
all installed packages.
13+
14+
=head1 TESTS
15+
16+
=cut
17+
18+
use strict;
19+
use warnings;
20+
use Readonly;
21+
use Test::More;
22+
use Test::Quattor;
23+
use Test::MockModule;
24+
use NCM::Component::spma::dnf;
25+
use CAF::Object;
26+
27+
$CAF::Object::NoAction = 1;
28+
29+
# Suppress error logging during failure tests
30+
my $mock = Test::MockModule->new('NCM::Component::spma::dnf');
31+
my $error_count = 0;
32+
$mock->mock('error', sub { $error_count++; });
33+
34+
Readonly my $CMD => join(" ", NCM::Component::spma::dnf::RPM_QUERY_INSTALLED());
35+
36+
my $cmp = NCM::Component::spma::dnf->new("spma");
37+
38+
=pod
39+
40+
=head2 Test empty package list
41+
42+
=cut
43+
44+
set_desired_output($CMD, "");
45+
set_desired_err($CMD, "");
46+
set_command_status($CMD, 0);
47+
48+
my $pkgs = $cmp->get_installed_rpms();
49+
isa_ok($pkgs, "Set::Scalar", "Received a Set::Scalar with empty input");
50+
is($pkgs->size, 0, "Empty set returned for empty output");
51+
52+
=pod
53+
54+
=head2 Test with package list
55+
56+
=cut
57+
58+
set_desired_output($CMD, "glibc-0:2.28-151.el8.x86_64\nkernel-0:4.18.0-305.el8.x86_64\n");
59+
set_desired_err($CMD, "");
60+
set_command_status($CMD, 0);
61+
62+
$pkgs = $cmp->get_installed_rpms();
63+
isa_ok($pkgs, "Set::Scalar", "Received a Set::Scalar");
64+
is($pkgs->size, 2, "Set contains two packages");
65+
ok($pkgs->has("glibc-0:2.28-151.el8.x86_64"), "Set contains glibc package");
66+
ok($pkgs->has("kernel-0:4.18.0-305.el8.x86_64"), "Set contains kernel package");
67+
68+
=pod
69+
70+
=head2 Test RPM command failure
71+
72+
=cut
73+
74+
set_desired_output($CMD, "");
75+
set_desired_err($CMD, "rpm: error");
76+
set_command_status($CMD, 1);
77+
78+
$pkgs = $cmp->get_installed_rpms();
79+
is($pkgs, undef, "Returns undef on RPM command failure");
80+
is($error_count, 1, "Error reported on failure");
81+
82+
done_testing();
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- mode: cperl -*-
2+
# ${license-info}
3+
# ${author-info}
4+
# ${build-info}
5+
6+
=pod
7+
8+
=head1 DESCRIPTION
9+
10+
Tests for the DNF module management methods in NCM::Component::spma::dnf:
11+
C<generate_dnf_modules> and C<set_default_modules>.
12+
13+
=head1 TESTS
14+
15+
=cut
16+
17+
use strict;
18+
use warnings;
19+
use Test::More;
20+
use Test::Quattor qw(dnf_modules);
21+
use NCM::Component::spma::dnf;
22+
use CAF::Object;
23+
use Test::Quattor::Object;
24+
use Readonly;
25+
use File::Path qw(mkpath rmtree);
26+
27+
use Test::Quattor::TextRender::Base;
28+
29+
my $caf_trd = mock_textrender();
30+
31+
$CAF::Object::NoAction = 1;
32+
33+
my $obj = Test::Quattor::Object->new;
34+
35+
my $cmp = NCM::Component::spma::dnf->new("spma", $obj);
36+
37+
Readonly my $MODULES_DIR => "target/test/dnf.modules.defaults.d";
38+
39+
sub initialize_modules_dir {
40+
rmtree($MODULES_DIR) if -d $MODULES_DIR;
41+
mkpath($MODULES_DIR);
42+
}
43+
44+
sub create_module_file {
45+
my ($name) = @_;
46+
open(my $fh, ">", "$MODULES_DIR/$name.yaml");
47+
close($fh) or die("Could not write $name.yaml: $!");
48+
}
49+
50+
=pod
51+
52+
=head2 Test generate_dnf_modules
53+
54+
=cut
55+
56+
initialize_modules_dir();
57+
58+
my $modules = {
59+
'postgresql' => { stream => '13' },
60+
'nodejs' => { stream => '16' },
61+
};
62+
63+
my $changes = $cmp->generate_dnf_modules($MODULES_DIR, $modules);
64+
ok(defined($changes), "generate_dnf_modules returns defined value");
65+
66+
my $head = "# File generated by NCM::Component::spma::dnf. Do not edit\n";
67+
68+
my $fh = get_file("$MODULES_DIR/postgresql.yaml");
69+
ok(defined($fh), "postgresql module file created");
70+
like("$fh", qr/postgresql/, "postgresql module file has correct content");
71+
72+
$fh = get_file("$MODULES_DIR/nodejs.yaml");
73+
ok(defined($fh), "nodejs module file created");
74+
like("$fh", qr/nodejs/, "nodejs module file has correct content");
75+
76+
=pod
77+
78+
=head2 Test set_default_modules with config
79+
80+
=cut
81+
82+
initialize_modules_dir();
83+
create_module_file("wanted_mod");
84+
create_module_file("unwanted_mod");
85+
86+
my $cfg = get_config_for_profile("dnf_modules");
87+
88+
# Track cleanup calls
89+
my @cleaned_files;
90+
my $mock_cmp = Test::MockModule->new('NCM::Component::spma::dnf');
91+
$mock_cmp->mock('cleanup', sub {
92+
my ($self, $file) = @_;
93+
push @cleaned_files, $file;
94+
unlink $file or warn("Failed to remove $file: $!");
95+
return 1;
96+
});
97+
98+
my $result = $cmp->set_default_modules($cfg, $MODULES_DIR);
99+
ok($result, "set_default_modules returns success");
100+
101+
ok(!-e "$MODULES_DIR/unwanted_mod.yaml", "Unwanted module file removed");
102+
103+
=pod
104+
105+
=head2 Test set_default_modules with missing directory
106+
107+
=cut
108+
109+
rmtree($MODULES_DIR) || warn("Failed to cleanup $MODULES_DIR: $!");
110+
111+
$result = $cmp->set_default_modules($cfg, $MODULES_DIR);
112+
ok($result, "set_default_modules handles missing directory");
113+
114+
=pod
115+
116+
=head2 Cleanup
117+
118+
=cut
119+
120+
rmtree($MODULES_DIR) if -d $MODULES_DIR;
121+
122+
done_testing();

0 commit comments

Comments
 (0)