]>
Commit | Line | Data |
---|---|---|
0fdbd016 FG |
1 | package PVE::LXC; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
0fff7963 DM |
6 | use lib qw(..); |
7 | ||
0fdbd016 FG |
8 | use PVE::Storage; |
9 | use PVE::Storage::Plugin; | |
10 | use PVE::LXC; | |
11 | use PVE::Tools; | |
12 | ||
13 | use Test::MockModule; | |
14 | use Test::More; | |
15 | ||
16 | my $nodename; | |
17 | my $snapshot_possible; | |
18 | my $vol_snapshot_possible = {}; | |
19 | my $vol_snapshot_delete_possible = {}; | |
20 | my $vol_snapshot = {}; | |
21 | my $vol_snapshot_delete = {}; | |
22 | my $running; | |
23 | my $freeze_possible; | |
24 | ||
25 | # Mocked methods | |
26 | ||
27 | sub mocked_volume_snapshot { | |
28 | my ($storecfg, $volid, $snapname) = @_; | |
29 | die "Storage config not mocked! aborting" | |
30 | if defined($storecfg); | |
31 | die "volid undefined" | |
32 | if !defined($volid); | |
33 | die "snapname undefined" | |
34 | if !defined($snapname); | |
35 | if ($vol_snapshot_possible->{$volid}) { | |
36 | if (defined($vol_snapshot->{$volid})) { | |
37 | $vol_snapshot->{$volid} .= ",$snapname"; | |
38 | } else { | |
39 | $vol_snapshot->{$volid} = $snapname; | |
40 | } | |
41 | return 1; | |
42 | } else { | |
43 | die "volume snapshot disabled"; | |
44 | } | |
45 | } | |
46 | ||
47 | sub mocked_volume_snapshot_delete { | |
48 | my ($storecfg, $volid, $snapname) = @_; | |
49 | die "Storage config not mocked! aborting" | |
50 | if defined($storecfg); | |
51 | die "volid undefined" | |
52 | if !defined($volid); | |
53 | die "snapname undefined" | |
54 | if !defined($snapname); | |
55 | if ($vol_snapshot_delete_possible->{$volid}) { | |
56 | if (defined($vol_snapshot_delete->{$volid})) { | |
57 | $vol_snapshot_delete->{$volid} .= ",$snapname"; | |
58 | } else { | |
59 | $vol_snapshot_delete->{$volid} = $snapname; | |
60 | } | |
61 | return 1; | |
62 | } else { | |
63 | die "volume snapshot delete disabled"; | |
64 | } | |
65 | } | |
66 | ||
67 | sub mocked_run_command { | |
68 | my ($cmd, %param) = @_; | |
69 | my $cmdstring; | |
70 | if (my $ref = ref($cmd)) { | |
71 | $cmdstring = PVE::Tools::cmd2string($cmd); | |
72 | if ($cmdstring =~ m/.*\/lxc-(un)?freeze.*/) { | |
73 | return 1 if $freeze_possible; | |
74 | die "lxc-[un]freeze disabled"; | |
75 | } | |
76 | } | |
77 | die "unexpected run_command call, aborting"; | |
78 | } | |
79 | ||
80 | # Testing methods | |
81 | ||
82 | sub test_file { | |
83 | my ($exp_fn, $real_fn) = @_; | |
84 | my $ret; | |
85 | eval { | |
86 | $ret = system("diff -u '$exp_fn' '$real_fn'"); | |
87 | }; | |
88 | die if $@; | |
89 | return !$ret; | |
90 | } | |
91 | ||
92 | sub testcase_prepare { | |
93 | my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_; | |
94 | subtest "Preparing snapshot '$snapname' for vm '$vmid'" => sub { | |
95 | plan tests => 2; | |
96 | $@ = undef; | |
97 | eval { | |
98 | PVE::LXC::snapshot_prepare($vmid, $snapname, $save_vmstate, $comment); | |
99 | }; | |
100 | is($@, $exp_err, "\$@ correct"); | |
101 | ok(test_file("snapshot-expected/prepare/lxc/$vmid.conf", "snapshot-working/prepare/lxc/$vmid.conf"), "config file correct"); | |
102 | }; | |
103 | } | |
104 | ||
105 | sub testcase_commit { | |
106 | my ($vmid, $snapname, $exp_err) = @_; | |
107 | subtest "Committing snapshot '$snapname' for vm '$vmid'" => sub { | |
108 | plan tests => 2; | |
109 | $@ = undef; | |
110 | eval { | |
111 | PVE::LXC::snapshot_commit($vmid, $snapname); | |
112 | }; | |
113 | is($@, $exp_err, "\$@ correct"); | |
114 | ok(test_file("snapshot-expected/commit/lxc/$vmid.conf", "snapshot-working/commit/lxc/$vmid.conf"), "config file correct"); | |
115 | } | |
116 | } | |
117 | ||
118 | sub testcase_create { | |
119 | my ($vmid, $snapname, $save_vmstate, $comment, $exp_err, $exp_vol_snap, $exp_vol_snap_delete) = @_; | |
120 | subtest "Creating snapshot '$snapname' for vm '$vmid'" => sub { | |
121 | plan tests => 4; | |
122 | $vol_snapshot = {}; | |
123 | $vol_snapshot_delete = {}; | |
124 | $exp_vol_snap = {} if !defined($exp_vol_snap); | |
125 | $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete); | |
126 | $@ = undef; | |
127 | eval { | |
128 | PVE::LXC::snapshot_create($vmid, $snapname, $save_vmstate, $comment); | |
129 | }; | |
130 | is($@, $exp_err, "\$@ correct"); | |
131 | is_deeply($vol_snapshot, $exp_vol_snap, "created correct volume snapshots"); | |
132 | is_deeply($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots"); | |
133 | ok(test_file("snapshot-expected/create/lxc/$vmid.conf", "snapshot-working/create/lxc/$vmid.conf"), "config file correct"); | |
134 | }; | |
135 | } | |
136 | ||
137 | sub testcase_delete { | |
138 | my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_; | |
139 | subtest "Deleting snapshot '$snapname' of vm '$vmid'" => sub { | |
140 | plan tests => 3; | |
141 | $vol_snapshot_delete = {}; | |
142 | $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete); | |
143 | $@ = undef; | |
144 | eval { | |
145 | PVE::LXC::snapshot_delete($vmid, $snapname, $force); | |
146 | }; | |
147 | is($@, $exp_err, "\$@ correct"); | |
148 | is_deeply($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots"); | |
149 | ok(test_file("snapshot-expected/delete/lxc/$vmid.conf", "snapshot-working/delete/lxc/$vmid.conf"), "config file correct"); | |
150 | }; | |
151 | } | |
152 | ||
153 | # BEGIN redefine PVE::LXC methods | |
154 | sub config_file_lock { | |
155 | return "snapshot-working/pve-test.lock"; | |
156 | } | |
157 | ||
158 | sub cfs_config_path { | |
159 | my ($vmid, $node) = @_; | |
160 | ||
161 | $node = $nodename if !$node; | |
162 | return "snapshot-working/$node/lxc/$vmid.conf"; | |
163 | } | |
164 | ||
165 | sub load_config { | |
166 | my ($vmid, $node) = @_; | |
167 | ||
168 | my $filename = cfs_config_path($vmid, $node); | |
169 | ||
170 | my $raw = PVE::Tools::file_get_contents($filename); | |
171 | ||
172 | my $conf = PVE::LXC::parse_pct_config($filename, $raw); | |
173 | return $conf; | |
174 | } | |
175 | ||
176 | sub write_config { | |
177 | my ($vmid, $conf) = @_; | |
178 | ||
179 | my $filename = cfs_config_path($vmid); | |
180 | ||
181 | if ($conf->{snapshots}) { | |
182 | foreach my $snapname (keys %{$conf->{snapshots}}) { | |
183 | $conf->{snapshots}->{$snapname}->{snaptime} = "1234567890" | |
184 | if $conf->{snapshots}->{$snapname}->{snaptime}; | |
185 | } | |
186 | } | |
187 | ||
188 | my $raw = PVE::LXC::write_pct_config($filename, $conf); | |
189 | ||
190 | PVE::Tools::file_set_contents($filename, $raw); | |
191 | } | |
192 | ||
193 | sub has_feature { | |
194 | my ($feature, $conf, $storecfg, $snapname) = @_; | |
195 | return $snapshot_possible; | |
196 | } | |
197 | ||
198 | sub check_running { | |
199 | return $running; | |
200 | } | |
201 | ||
202 | sub sync_container_namespace { | |
203 | return; | |
204 | } | |
205 | ||
206 | # END redefine PVE::LXC methods | |
207 | ||
208 | PVE::Tools::run_command("rm -rf snapshot-working"); | |
209 | PVE::Tools::run_command("cp -a snapshot-input snapshot-working"); | |
210 | ||
211 | $running = 1; | |
212 | $freeze_possible = 1; | |
213 | ||
214 | printf(""); | |
215 | printf("Running prepare tests"); | |
216 | printf(""); | |
217 | $nodename = "prepare"; | |
218 | ||
219 | printf(""); | |
220 | printf("Setting has_feature to return true"); | |
221 | printf(""); | |
222 | $snapshot_possible = 1; | |
223 | ||
224 | printf("Successful snapshot_prepare with no existing snapshots"); | |
225 | testcase_prepare("101", "test", 0, "test comment", ''); | |
226 | ||
227 | printf("Successful snapshot_prepare with one existing snapshot"); | |
228 | testcase_prepare("102", "test2", 0, "test comment", ""); | |
229 | ||
230 | printf("Expected error for snapshot_prepare on locked container"); | |
231 | testcase_prepare("200", "test", 0, "test comment", "VM is locked (snapshot)\n"); | |
232 | ||
233 | printf("Expected error for snapshot_prepare with duplicate snapshot name"); | |
234 | testcase_prepare("201", "test", 0, "test comment", "snapshot name 'test' already used\n"); | |
235 | ||
236 | printf("Expected error for snapshot_prepare with save_vmstate"); | |
237 | testcase_prepare("202", "test", 1, "test comment", "implement me - snapshot_save_vmstate\n"); | |
238 | ||
239 | printf(""); | |
240 | printf("Setting has_feature to return false"); | |
241 | printf(""); | |
242 | $snapshot_possible = 0; | |
243 | ||
244 | printf("Expected error for snapshot_prepare if snapshots not possible"); | |
245 | testcase_prepare("300", "test", 0, "test comment", "snapshot feature is not available\n"); | |
246 | ||
247 | printf(""); | |
248 | printf("Running commit tests"); | |
249 | printf(""); | |
250 | $nodename = "commit"; | |
251 | ||
252 | printf(""); | |
253 | printf("Setting has_feature to return true"); | |
254 | printf(""); | |
255 | $snapshot_possible = 1; | |
256 | ||
257 | printf("Successful snapshot_commit with one prepared snapshot"); | |
258 | testcase_commit("101", "test", ""); | |
259 | ||
260 | printf("Successful snapshot_commit with one committed and one prepared snapshot"); | |
261 | testcase_commit("102", "test2", ""); | |
262 | ||
263 | printf("Expected error for snapshot_commit with no snapshot lock"); | |
264 | testcase_commit("201", "test", "missing snapshot lock\n"); | |
265 | ||
266 | printf("Expected error for snapshot_commit with invalid snapshot name"); | |
267 | testcase_commit("202", "test", "snapshot 'test' does not exist\n"); | |
268 | ||
269 | printf("Expected error for snapshot_commit with invalid snapshot state"); | |
270 | testcase_commit("203", "test", "wrong snapshot state\n"); | |
271 | ||
272 | $vol_snapshot_possible->{"local:snapshotable-disk-1"} = 1; | |
273 | $vol_snapshot_delete_possible->{"local:snapshotable-disk-1"} = 1; | |
274 | printf(""); | |
275 | printf("Setting up Mocking for PVE::Storage"); | |
276 | my $storage_module = new Test::MockModule('PVE::Storage'); | |
277 | $storage_module->mock('config', sub { return undef; }); | |
278 | $storage_module->mock('volume_snapshot', \&mocked_volume_snapshot); | |
279 | $storage_module->mock('volume_snapshot_delete', \&mocked_volume_snapshot_delete); | |
280 | printf("\tconfig(), volume_snapshot() and volume_snapshot_delete() mocked"); | |
281 | ||
282 | printf(""); | |
283 | printf("Setting up Mocking for PVE::Tools"); | |
284 | my $tools_module = new Test::MockModule('PVE::Tools'); | |
285 | $tools_module->mock('run_command' => \&mocked_run_command); | |
286 | printf("\trun_command() mocked"); | |
287 | ||
288 | $nodename = "create"; | |
289 | printf(""); | |
290 | printf("Running create tests"); | |
291 | printf(""); | |
292 | ||
293 | printf("Successful snapshot_create with no existing snapshots"); | |
294 | testcase_create("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" }); | |
295 | ||
296 | printf("Successful snapshot_create with one existing snapshots"); | |
297 | testcase_create("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" }); | |
298 | ||
299 | printf("Expected error for snapshot_create when volume snapshot is not possible"); | |
300 | testcase_create("201", "test", 0, "test comment", "volume snapshot disabled at snapshot-test.pm line 41.\n\n"); | |
301 | ||
302 | printf("Expected error for snapshot_create with broken lxc-freeze"); | |
303 | $freeze_possible = 0; | |
304 | testcase_create("202", "test", 0, "test comment", "lxc-[un]freeze disabled at snapshot-test.pm line 72.\n\n", undef, { "local:snapshotable-disk-1" => "test" }); | |
305 | $freeze_possible = 1; | |
306 | ||
307 | $nodename = "delete"; | |
308 | printf(""); | |
309 | printf("Running delete tests"); | |
310 | printf(""); | |
311 | ||
312 | printf("Successful snapshot_delete of only existing snapshot"); | |
313 | testcase_delete("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" }); | |
314 | ||
315 | printf("Successful snapshot_delete of leaf snapshot"); | |
316 | testcase_delete("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" }); | |
317 | ||
318 | printf("Successful snapshot_delete of root snapshot"); | |
319 | testcase_delete("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" }); | |
320 | ||
321 | printf("Successful snapshot_delete of intermediate snapshot"); | |
322 | testcase_delete("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" }); | |
323 | ||
324 | printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1"); | |
325 | testcase_delete("105", "test", 1, ""); | |
326 | ||
327 | printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0"); | |
328 | testcase_delete("201", "test", 0, "volume snapshot delete disabled at snapshot-test.pm line 61.\n"); | |
329 | ||
330 | printf("Expected error for snapshot_delete with locked config"); | |
331 | testcase_delete("202", "test", 0, "VM is locked (backup)\n"); | |
332 | ||
333 | ||
334 | done_testing(); |