]>
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);
87 sub mocked_dir_glob_foreach
{
88 my ($dir, $regex, $sub) = @_;
92 # read lines in from file
93 if ($dir =~ m{^/sys/block$} ) {
94 @$lines = split(/\n/, read_test_file
('disklist'));
95 } elsif ($dir =~ m{^/sys/block/([^/]+)}) {
96 @$lines = split(/\n/, read_test_file
('partlist'));
99 foreach my $line (@$lines) {
100 if ($line =~ m/$regex/) {
106 sub mocked_parse_proc_mounts
{
107 my $text = read_test_file
('mounts');
111 foreach my $line(split(/\n/, $text)) {
112 push @$mounts, [split(/\s+/, $line)];
121 if (!-f
"disk_tests/$testcasedir/$filename") {
122 print "file '$testcasedir/$filename' not found\n";
125 open (my $fh, '<', "disk_tests/$testcasedir/$filename")
126 or die "Cannot open disk_tests/$testcasedir/$filename: $!";
128 my $output = <$fh> // '';
129 chomp $output if $output;
130 while (my $line = <$fh>) {
132 $output .= "\n$line";
141 subtest
"Test '$testdir'" => sub {
143 $testcasedir = $testdir;
146 my $expected_disk_list;
148 $disks = PVE
::Diskmanage
::get_disks
();
151 $expected_disk_list = decode_json
(read_test_file
('disklist_expected.json'));
153 print Dumper
($disks) if $print;
155 is_deeply
($disks, $expected_disk_list, 'disk list should be the same');
157 foreach my $disk (sort keys %$disks) {
161 $smart = PVE
::Diskmanage
::get_smart_data
("/dev/$disk");
162 print Dumper
($smart) if $print;
163 $expected_smart = decode_json
(read_test_file
("${disk}_smart_expected.json"));
166 if ($smart && $expected_smart) {
168 is_deeply
($smart, $expected_smart, "smart data for '$disk' should be the same");
169 } elsif ($smart && -f
"disk_tests/$testcasedir/${disk}_smart_expected.json") {
171 ok
(0, "could not parse expected smart for '$disk'\n");
175 done_testing
($testcount);
179 # start reading tests:
181 if (@ARGV && $ARGV[0] eq 'print') {
185 print("Setting up Mocking\n");
186 $diskmanage_module = new Test
::MockModule
('PVE::Diskmanage', no_auto
=> 1);
187 $diskmanage_module->mock('run_command' => \
&mocked_run_command
);
188 print("\tMocked run_command\n");
189 $diskmanage_module->mock('dir_glob_foreach' => \
&mocked_dir_glob_foreach
);
190 print("\tMocked dir_glob_foreach\n");
191 $diskmanage_module->mock('get_sysdir_info' => \
&mocked_get_sysdir_info
);
192 print("\tMocked get_sysdir_info\n");
193 $diskmanage_module->mock('is_iscsi' => \
&mocked_is_iscsi
);
194 print("\tMocked is_iscsi\n");
195 $diskmanage_module->mock('assert_blockdev' => sub { return 1; });
196 print("\tMocked assert_blockdev\n");
197 $diskmanage_module->mock('dir_is_empty' => sub {
198 # all partitions have a holder dir
200 if ($val =~ m
|^/sys/block
/.+/.+/|) {
205 print("\tMocked dir_is_empty\n");
206 my $tools_module= new Test
::MockModule
('PVE::ProcFSTools', no_auto
=> 1);
207 $tools_module->mock('parse_proc_mounts' => \
&mocked_parse_proc_mounts
);
208 print("\tMocked parse_proc_mounts\n");
209 print("Done Setting up Mocking\n\n");
211 print("Beginning Tests:\n\n");
212 opendir (my $dh, 'disk_tests')
213 or die "Cannot open disk_tests: $!";
215 while (readdir $dh) {
217 next if $dir eq '.' or $dir eq '..';
219 test_disk_list
($dir);
222 done_testing
($testcount);