]> git.proxmox.com Git - pve-storage.git/blame - test/disklist_test.pm
bump version to 6.0-5
[pve-storage.git] / test / disklist_test.pm
CommitLineData
5774fe62
DC
1package PVE::Diskmanage::Test;
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use PVE::Diskmanage;
9use PVE::Tools;
10
11use Test::MockModule;
12use Test::More;
13use JSON;
14use Data::Dumper;
15
16my $testcasedir; # current case directory
17my $testcount = 0; # testcount for TAP::Harness
18my $diskmanage_module; # mockmodule for PVE::Diskmanage
19my $print = 0;
20
21sub mocked_run_command {
22 my ($cmd, %param) = @_;
23
24 my $outputlines = [];
25 if (my $ref = ref($cmd)) {
26 if ($cmd->[0] =~ m/udevadm/i) {
27 # simulate udevadm output
28 my $dev = $cmd->[3];
29 $dev =~ s|/sys/block/||;
30 @$outputlines = split(/\n/, read_test_file("${dev}_udevadm"));
31
32 } elsif ($cmd->[0] =~ m/smartctl/i) {
33 # simulate smartctl output
34 my $dev;
35 my $type;
36 if (@$cmd > 3) {
37 $dev = $cmd->[5];
38 $type = 'smart';
39 } else {
40 $dev = $cmd->[2];
41 $type = 'health';
42 }
43 $dev =~ s|/dev/||;
44 @$outputlines = split(/\n/, read_test_file("${dev}_${type}"));
45 } elsif ($cmd->[0] =~ m/sgdisk/i) {
46 # simulate sgdisk
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'));
51
52 } elsif ($cmd->[0] =~ m/pvs/i) {
53 # simulate lvs output
54 @$outputlines = split(/\n/, read_test_file('pvs'));
19dcd1ad
DC
55 } elsif ($cmd->[0] =~ m/lvs/i) {
56 @$outputlines = split(/\n/, read_test_file('lvs'));
5774fe62
DC
57 } else {
58 print "unexpected run_command call: '@$cmd', aborting\n";
59 die;
60 }
61 } else {
62 print "unexpected run_command call: '@$cmd', aborting\n";
63 die;
64 }
65
66 my $outfunc;
67 if ($param{outfunc}) {
68 $outfunc = $param{outfunc};
69 map { &$outfunc(($_)) } @$outputlines;
70
71 return 0;
72 }
73}
74
75sub mocked_get_sysdir_info {
76 my ($param) = @_;
77
78 my $originalsub = $diskmanage_module->original('get_sysdir_info');
79
80 $param =~ s|/sys/block|disk_tests/$testcasedir|;
81
82 return &$originalsub($param);
83}
84
eebcdb11
DC
85sub mocked_is_iscsi {
86 return 0;
87}
88
5774fe62
DC
89sub mocked_dir_glob_foreach {
90 my ($dir, $regex, $sub) = @_;
91
92 my $lines = [];
93
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'));
99 }
100
101 foreach my $line (@$lines) {
102 if ($line =~ m/$regex/) {
103 &$sub($line);
104 }
105 }
106}
107
108sub mocked_parse_proc_mounts {
109 my $text = read_test_file('mounts');
110
111 my $mounts = [];
112
113 foreach my $line(split(/\n/, $text)) {
114 push @$mounts, [split(/\s+/, $line)];
115 }
116
117 return $mounts;
118}
119
120sub read_test_file {
121 my ($filename) = @_;
122
123 if (!-f "disk_tests/$testcasedir/$filename") {
124 print "file '$testcasedir/$filename' not found\n";
125 return '';
126 }
127 open (my $fh, '<', "disk_tests/$testcasedir/$filename")
128 or die "Cannot open disk_tests/$testcasedir/$filename: $!";
129
130 my $output = <$fh> // '';
131 chomp $output if $output;
132 while (my $line = <$fh>) {
133 chomp $line;
134 $output .= "\n$line";
135 }
136
137 return $output;
138}
139
140
141sub test_disk_list {
142 my ($testdir) = @_;
143 subtest "Test '$testdir'" => sub {
144 my $testcount = 0;
145 $testcasedir = $testdir;
146
147 my $disks;
148 my $expected_disk_list;
149 eval {
150 $disks = PVE::Diskmanage::get_disks();
151 };
152 warn $@ if $@;
153 $expected_disk_list = decode_json(read_test_file('disklist_expected.json'));
154
155 print Dumper($disks) if $print;
156 $testcount++;
157 is_deeply($disks, $expected_disk_list, 'disk list should be the same');
158
159 foreach my $disk (sort keys %$disks) {
160 my $smart;
161 my $expected_smart;
162 eval {
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"));
166 };
167
168 if ($smart && $expected_smart) {
169 $testcount++;
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") {
172 $testcount++;
173 ok(0, "could not parse expected smart for '$disk'\n");
174 }
d0a217f6
DC
175 my $disk_tmp = {};
176
177 # test single disk parameter
178 $disk_tmp = PVE::Diskmanage::get_disks($disk);
179 warn $@ if $@;
180 $testcount++;
181 print Dumper $disk_tmp if $print;
182 is_deeply($disk_tmp->{$disk}, $expected_disk_list->{$disk}, "disk $disk should be the same");
183
184
185 # test wrong parameter
186 eval {
187 PVE::Diskmanage::get_disks( { test => 1 } );
188 };
189 my $err = $@;
190 $testcount++;
191 is_deeply($err, "disks is not a string or array reference\n", "error message should be the same");
192
5774fe62 193 }
d0a217f6
DC
194 # test multi disk parameter
195 $disks = PVE::Diskmanage::get_disks( [ keys %$disks ] );
196 $testcount++;
197 is_deeply($disks, $expected_disk_list, 'disk list should be the same');
5774fe62
DC
198
199 done_testing($testcount);
200 };
201}
202
203# start reading tests:
204
205if (@ARGV && $ARGV[0] eq 'print') {
206 $print = 1;
207}
208
209print("Setting up Mocking\n");
210$diskmanage_module = new Test::MockModule('PVE::Diskmanage', no_auto => 1);
211$diskmanage_module->mock('run_command' => \&mocked_run_command);
212print("\tMocked run_command\n");
213$diskmanage_module->mock('dir_glob_foreach' => \&mocked_dir_glob_foreach);
214print("\tMocked dir_glob_foreach\n");
215$diskmanage_module->mock('get_sysdir_info' => \&mocked_get_sysdir_info);
216print("\tMocked get_sysdir_info\n");
eebcdb11
DC
217$diskmanage_module->mock('is_iscsi' => \&mocked_is_iscsi);
218print("\tMocked is_iscsi\n");
5774fe62
DC
219$diskmanage_module->mock('assert_blockdev' => sub { return 1; });
220print("\tMocked assert_blockdev\n");
221$diskmanage_module->mock('dir_is_empty' => sub {
222 # all partitions have a holder dir
223 my $val = shift;
224 if ($val =~ m|^/sys/block/.+/.+/|) {
225 return 0;
226 }
227 return 1;
228 });
229print("\tMocked dir_is_empty\n");
230my $tools_module= new Test::MockModule('PVE::ProcFSTools', no_auto => 1);
231$tools_module->mock('parse_proc_mounts' => \&mocked_parse_proc_mounts);
232print("\tMocked parse_proc_mounts\n");
233print("Done Setting up Mocking\n\n");
234
235print("Beginning Tests:\n\n");
236opendir (my $dh, 'disk_tests')
237 or die "Cannot open disk_tests: $!";
238
239while (readdir $dh) {
240 my $dir = $_;
241 next if $dir eq '.' or $dir eq '..';
242 $testcount++;
243 test_disk_list($dir);
244}
245
246done_testing($testcount);