]>
git.proxmox.com Git - pve-storage.git/blob - test/disklist_test.pm
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'));
56 print "unexpected run_command call: '@$cmd', aborting\n";
60 print "unexpected run_command call: '@$cmd', aborting\n";
65 if ($param{outfunc
}) {
66 $outfunc = $param{outfunc
};
67 map { &$outfunc(($_)) } @$outputlines;
73 sub mocked_get_sysdir_info
{
76 my $originalsub = $diskmanage_module->original('get_sysdir_info');
78 $param =~ s
|/sys/block
|disk_tests
/$testcasedir|;
80 return &$originalsub($param);
83 sub mocked_dir_glob_foreach
{
84 my ($dir, $regex, $sub) = @_;
88 # read lines in from file
89 if ($dir =~ m{^/sys/block$} ) {
90 @$lines = split(/\n/, read_test_file
('disklist'));
91 } elsif ($dir =~ m{^/sys/block/([^/]+)}) {
92 @$lines = split(/\n/, read_test_file
('partlist'));
95 foreach my $line (@$lines) {
96 if ($line =~ m/$regex/) {
102 sub mocked_parse_proc_mounts
{
103 my $text = read_test_file
('mounts');
107 foreach my $line(split(/\n/, $text)) {
108 push @$mounts, [split(/\s+/, $line)];
117 if (!-f
"disk_tests/$testcasedir/$filename") {
118 print "file '$testcasedir/$filename' not found\n";
121 open (my $fh, '<', "disk_tests/$testcasedir/$filename")
122 or die "Cannot open disk_tests/$testcasedir/$filename: $!";
124 my $output = <$fh> // '';
125 chomp $output if $output;
126 while (my $line = <$fh>) {
128 $output .= "\n$line";
137 subtest
"Test '$testdir'" => sub {
139 $testcasedir = $testdir;
142 my $expected_disk_list;
144 $disks = PVE
::Diskmanage
::get_disks
();
147 $expected_disk_list = decode_json
(read_test_file
('disklist_expected.json'));
149 print Dumper
($disks) if $print;
151 is_deeply
($disks, $expected_disk_list, 'disk list should be the same');
153 foreach my $disk (sort keys %$disks) {
157 $smart = PVE
::Diskmanage
::get_smart_data
("/dev/$disk");
158 print Dumper
($smart) if $print;
159 $expected_smart = decode_json
(read_test_file
("${disk}_smart_expected.json"));
162 if ($smart && $expected_smart) {
164 is_deeply
($smart, $expected_smart, "smart data for '$disk' should be the same");
165 } elsif ($smart && -f
"disk_tests/$testcasedir/${disk}_smart_expected.json") {
167 ok
(0, "could not parse expected smart for '$disk'\n");
171 done_testing
($testcount);
175 # start reading tests:
177 if (@ARGV && $ARGV[0] eq 'print') {
181 print("Setting up Mocking\n");
182 $diskmanage_module = new Test
::MockModule
('PVE::Diskmanage', no_auto
=> 1);
183 $diskmanage_module->mock('run_command' => \
&mocked_run_command
);
184 print("\tMocked run_command\n");
185 $diskmanage_module->mock('dir_glob_foreach' => \
&mocked_dir_glob_foreach
);
186 print("\tMocked dir_glob_foreach\n");
187 $diskmanage_module->mock('get_sysdir_info' => \
&mocked_get_sysdir_info
);
188 print("\tMocked get_sysdir_info\n");
189 $diskmanage_module->mock('assert_blockdev' => sub { return 1; });
190 print("\tMocked assert_blockdev\n");
191 $diskmanage_module->mock('dir_is_empty' => sub {
192 # all partitions have a holder dir
194 if ($val =~ m
|^/sys/block
/.+/.+/|) {
199 print("\tMocked dir_is_empty\n");
200 my $tools_module= new Test
::MockModule
('PVE::ProcFSTools', no_auto
=> 1);
201 $tools_module->mock('parse_proc_mounts' => \
&mocked_parse_proc_mounts
);
202 print("\tMocked parse_proc_mounts\n");
203 print("Done Setting up Mocking\n\n");
205 print("Beginning Tests:\n\n");
206 opendir (my $dh, 'disk_tests')
207 or die "Cannot open disk_tests: $!";
209 while (readdir $dh) {
211 next if $dir eq '.' or $dir eq '..';
213 test_disk_list
($dir);
216 done_testing
($testcount);