]>
git.proxmox.com Git - pve-storage.git/blob - test/disklist_test.pm
527e882afadee340df227d7652d6e91992e7bef5
1 package PVE
::Diskmanage
::Test
;
16 my $testcasedir; # current case directory
17 my $testcount = 0; # testcount for TAP::Harness
18 my $diskmanage_module; # mockmodule for PVE::Diskmanage
21 sub mocked_run_command
{
22 my ($cmd, %param) = @_;
25 if (my $ref = ref($cmd)) {
26 if ($cmd->[0] =~ m/udevadm/i) {
27 # simulate udevadm output
29 $dev =~ s
|/sys/block
/||;
30 @$outputlines = split(/\n/, read_test_file
("${dev}_udevadm"));
32 } elsif ($cmd->[0] =~ m/smartctl/i) {
33 # simulate smartctl output
44 @$outputlines = split(/\n/, read_test_file
("${dev}_${type}"));
45 } elsif ($cmd->[0] =~ m/sgdisk/i) {
47 die "implement me: @$cmd\n";
48 } elsif ($cmd->[0] =~ m/zpool/i) {
49 # simulate zpool output
50 @$outputlines = split(/\n/, read_test_file
('zpool'));
52 } elsif ($cmd->[0] =~ m/pvs/i) {
54 @$outputlines = split(/\n/, read_test_file
('pvs'));
55 } elsif ($cmd->[0] =~ m/lvs/i) {
56 @$outputlines = split(/\n/, read_test_file
('lvs'));
58 print "unexpected run_command call: '@$cmd', aborting\n";
62 print "unexpected run_command call: '@$cmd', aborting\n";
67 if ($param{outfunc
}) {
68 $outfunc = $param{outfunc
};
69 map { &$outfunc(($_)) } @$outputlines;
75 sub mocked_get_sysdir_info
{
78 my $originalsub = $diskmanage_module->original('get_sysdir_info');
80 $param =~ s
|/sys/block
|disk_tests
/$testcasedir|;
82 return &$originalsub($param);
89 sub mocked_dir_glob_foreach
{
90 my ($dir, $regex, $sub) = @_;
94 # read lines in from file
95 if ($dir =~ m{^/sys/block$} ) {
96 @$lines = split(/\n/, read_test_file
('disklist'));
97 } elsif ($dir =~ m{^/sys/block/([^/]+)}) {
98 @$lines = split(/\n/, read_test_file
('partlist'));
101 foreach my $line (@$lines) {
102 if ($line =~ m/$regex/) {
108 sub mocked_parse_proc_mounts
{
109 my $text = read_test_file
('mounts');
113 foreach my $line(split(/\n/, $text)) {
114 push @$mounts, [split(/\s+/, $line)];
123 if (!-f
"disk_tests/$testcasedir/$filename") {
124 print "file '$testcasedir/$filename' not found\n";
127 open (my $fh, '<', "disk_tests/$testcasedir/$filename")
128 or die "Cannot open disk_tests/$testcasedir/$filename: $!";
130 my $output = <$fh> // '';
131 chomp $output if $output;
132 while (my $line = <$fh>) {
134 $output .= "\n$line";
143 subtest
"Test '$testdir'" => sub {
145 $testcasedir = $testdir;
148 my $expected_disk_list;
150 $disks = PVE
::Diskmanage
::get_disks
();
153 $expected_disk_list = decode_json
(read_test_file
('disklist_expected.json'));
155 print Dumper
($disks) if $print;
157 is_deeply
($disks, $expected_disk_list, 'disk list should be the same');
159 foreach my $disk (sort keys %$disks) {
163 $smart = PVE
::Diskmanage
::get_smart_data
("/dev/$disk");
164 print Dumper
($smart) if $print;
165 $expected_smart = decode_json
(read_test_file
("${disk}_smart_expected.json"));
168 if ($smart && $expected_smart) {
170 is_deeply
($smart, $expected_smart, "smart data for '$disk' should be the same");
171 } elsif ($smart && -f
"disk_tests/$testcasedir/${disk}_smart_expected.json") {
173 ok
(0, "could not parse expected smart for '$disk'\n");
177 # test single disk parameter
178 $disk_tmp = PVE
::Diskmanage
::get_disks
($disk);
181 print Dumper
$disk_tmp if $print;
182 is_deeply
($disk_tmp->{$disk}, $expected_disk_list->{$disk}, "disk $disk should be the same");
185 # test wrong parameter
187 PVE
::Diskmanage
::get_disks
( { test
=> 1 } );
191 is_deeply
($err, "disks is not a string or array reference\n", "error message should be the same");
194 # test multi disk parameter
195 $disks = PVE
::Diskmanage
::get_disks
( [ keys %$disks ] );
197 is_deeply
($disks, $expected_disk_list, 'disk list should be the same');
199 done_testing
($testcount);
203 # start reading tests:
205 if (@ARGV && $ARGV[0] eq 'print') {
209 print("Setting up Mocking\n");
210 $diskmanage_module = new Test
::MockModule
('PVE::Diskmanage', no_auto
=> 1);
211 $diskmanage_module->mock('run_command' => \
&mocked_run_command
);
212 print("\tMocked run_command\n");
213 $diskmanage_module->mock('dir_glob_foreach' => \
&mocked_dir_glob_foreach
);
214 print("\tMocked dir_glob_foreach\n");
215 $diskmanage_module->mock('get_sysdir_info' => \
&mocked_get_sysdir_info
);
216 print("\tMocked get_sysdir_info\n");
217 $diskmanage_module->mock('is_iscsi' => \
&mocked_is_iscsi
);
218 print("\tMocked is_iscsi\n");
219 $diskmanage_module->mock('assert_blockdev' => sub { return 1; });
220 print("\tMocked assert_blockdev\n");
221 $diskmanage_module->mock('dir_is_empty' => sub {
222 # all partitions have a holder dir
224 if ($val =~ m
|^/sys/block
/.+/.+/|) {
229 print("\tMocked dir_is_empty\n");
230 my $tools_module= new Test
::MockModule
('PVE::ProcFSTools', no_auto
=> 1);
231 $tools_module->mock('parse_proc_mounts' => \
&mocked_parse_proc_mounts
);
232 print("\tMocked parse_proc_mounts\n");
233 print("Done Setting up Mocking\n\n");
235 print("Beginning Tests:\n\n");
236 opendir (my $dh, 'disk_tests')
237 or die "Cannot open disk_tests: $!";
239 while (readdir $dh) {
241 next if $dir eq '.' or $dir eq '..';
243 test_disk_list
($dir);
246 done_testing
($testcount);