]>
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) = @_; | |
19d36a45 | 29 | die "Storage config not mocked! aborting\n" |
0fdbd016 | 30 | if defined($storecfg); |
19d36a45 | 31 | die "volid undefined\n" |
0fdbd016 | 32 | if !defined($volid); |
19d36a45 | 33 | die "snapname undefined\n" |
0fdbd016 FG |
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 { | |
19d36a45 | 43 | die "volume snapshot disabled\n"; |
0fdbd016 FG |
44 | } |
45 | } | |
46 | ||
47 | sub mocked_volume_snapshot_delete { | |
48 | my ($storecfg, $volid, $snapname) = @_; | |
19d36a45 | 49 | die "Storage config not mocked! aborting\n" |
0fdbd016 | 50 | if defined($storecfg); |
19d36a45 | 51 | die "volid undefined\n" |
0fdbd016 | 52 | if !defined($volid); |
19d36a45 | 53 | die "snapname undefined\n" |
0fdbd016 FG |
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 { | |
19d36a45 | 63 | die "volume snapshot delete disabled\n"; |
0fdbd016 FG |
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; | |
19d36a45 | 74 | die "lxc-[un]freeze disabled\n"; |
0fdbd016 FG |
75 | } |
76 | } | |
e2f2d0dc | 77 | die "unexpected run_command call: '$cmdstring', aborting\n"; |
0fdbd016 FG |
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 | ||
e2f2d0dc FG |
214 | printf("\n"); |
215 | printf("Running prepare tests\n"); | |
216 | printf("\n"); | |
0fdbd016 FG |
217 | $nodename = "prepare"; |
218 | ||
e2f2d0dc FG |
219 | printf("\n"); |
220 | printf("Setting has_feature to return true\n"); | |
221 | printf("\n"); | |
0fdbd016 FG |
222 | $snapshot_possible = 1; |
223 | ||
e2f2d0dc | 224 | printf("Successful snapshot_prepare with no existing snapshots\n"); |
0fdbd016 FG |
225 | testcase_prepare("101", "test", 0, "test comment", ''); |
226 | ||
e2f2d0dc | 227 | printf("Successful snapshot_prepare with one existing snapshot\n"); |
0fdbd016 FG |
228 | testcase_prepare("102", "test2", 0, "test comment", ""); |
229 | ||
e2f2d0dc | 230 | printf("Expected error for snapshot_prepare on locked container\n"); |
0fdbd016 FG |
231 | testcase_prepare("200", "test", 0, "test comment", "VM is locked (snapshot)\n"); |
232 | ||
e2f2d0dc | 233 | printf("Expected error for snapshot_prepare with duplicate snapshot name\n"); |
0fdbd016 FG |
234 | testcase_prepare("201", "test", 0, "test comment", "snapshot name 'test' already used\n"); |
235 | ||
e2f2d0dc | 236 | printf("Expected error for snapshot_prepare with save_vmstate\n"); |
0fdbd016 FG |
237 | testcase_prepare("202", "test", 1, "test comment", "implement me - snapshot_save_vmstate\n"); |
238 | ||
e2f2d0dc FG |
239 | printf("\n"); |
240 | printf("Setting has_feature to return false\n"); | |
241 | printf("\n"); | |
0fdbd016 FG |
242 | $snapshot_possible = 0; |
243 | ||
e2f2d0dc | 244 | printf("Expected error for snapshot_prepare if snapshots not possible\n"); |
0fdbd016 FG |
245 | testcase_prepare("300", "test", 0, "test comment", "snapshot feature is not available\n"); |
246 | ||
e2f2d0dc FG |
247 | printf("\n"); |
248 | printf("Running commit tests\n"); | |
249 | printf("\n"); | |
0fdbd016 FG |
250 | $nodename = "commit"; |
251 | ||
e2f2d0dc FG |
252 | printf("\n"); |
253 | printf("Setting has_feature to return true\n"); | |
254 | printf("\n"); | |
0fdbd016 FG |
255 | $snapshot_possible = 1; |
256 | ||
e2f2d0dc | 257 | printf("Successful snapshot_commit with one prepared snapshot\n"); |
0fdbd016 FG |
258 | testcase_commit("101", "test", ""); |
259 | ||
e2f2d0dc | 260 | printf("Successful snapshot_commit with one committed and one prepared snapshot\n"); |
0fdbd016 FG |
261 | testcase_commit("102", "test2", ""); |
262 | ||
e2f2d0dc | 263 | printf("Expected error for snapshot_commit with no snapshot lock\n"); |
0fdbd016 FG |
264 | testcase_commit("201", "test", "missing snapshot lock\n"); |
265 | ||
e2f2d0dc | 266 | printf("Expected error for snapshot_commit with invalid snapshot name\n"); |
0fdbd016 FG |
267 | testcase_commit("202", "test", "snapshot 'test' does not exist\n"); |
268 | ||
e2f2d0dc | 269 | printf("Expected error for snapshot_commit with invalid snapshot state\n"); |
0fdbd016 FG |
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; | |
e2f2d0dc FG |
274 | printf("\n"); |
275 | printf("Setting up Mocking for PVE::Storage\n"); | |
0fdbd016 FG |
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 | ||
e2f2d0dc FG |
282 | printf("\n"); |
283 | printf("Setting up Mocking for PVE::Tools\n"); | |
0fdbd016 FG |
284 | my $tools_module = new Test::MockModule('PVE::Tools'); |
285 | $tools_module->mock('run_command' => \&mocked_run_command); | |
e2f2d0dc | 286 | printf("\trun_command() mocked\n"); |
0fdbd016 FG |
287 | |
288 | $nodename = "create"; | |
e2f2d0dc FG |
289 | printf("\n"); |
290 | printf("Running create tests\n"); | |
291 | printf("\n"); | |
0fdbd016 | 292 | |
e2f2d0dc | 293 | printf("Successful snapshot_create with no existing snapshots\n"); |
0fdbd016 FG |
294 | testcase_create("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" }); |
295 | ||
e2f2d0dc | 296 | printf("Successful snapshot_create with one existing snapshots\n"); |
0fdbd016 FG |
297 | testcase_create("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" }); |
298 | ||
e2f2d0dc | 299 | printf("Expected error for snapshot_create when volume snapshot is not possible\n"); |
19d36a45 | 300 | testcase_create("201", "test", 0, "test comment", "volume snapshot disabled\n\n"); |
0fdbd016 | 301 | |
e2f2d0dc | 302 | printf("Expected error for snapshot_create with broken lxc-freeze\n"); |
0fdbd016 | 303 | $freeze_possible = 0; |
19d36a45 | 304 | testcase_create("202", "test", 0, "test comment", "lxc-[un]freeze disabled\n\n", undef, { "local:snapshotable-disk-1" => "test" }); |
0fdbd016 FG |
305 | $freeze_possible = 1; |
306 | ||
307 | $nodename = "delete"; | |
e2f2d0dc FG |
308 | printf("\n"); |
309 | printf("Running delete tests\n"); | |
310 | printf("\n"); | |
0fdbd016 | 311 | |
e2f2d0dc | 312 | printf("Successful snapshot_delete of only existing snapshot\n"); |
0fdbd016 FG |
313 | testcase_delete("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" }); |
314 | ||
e2f2d0dc | 315 | printf("Successful snapshot_delete of leaf snapshot\n"); |
0fdbd016 FG |
316 | testcase_delete("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" }); |
317 | ||
e2f2d0dc | 318 | printf("Successful snapshot_delete of root snapshot\n"); |
0fdbd016 FG |
319 | testcase_delete("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" }); |
320 | ||
e2f2d0dc | 321 | printf("Successful snapshot_delete of intermediate snapshot\n"); |
0fdbd016 FG |
322 | testcase_delete("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" }); |
323 | ||
e2f2d0dc | 324 | printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1\n"); |
0fdbd016 FG |
325 | testcase_delete("105", "test", 1, ""); |
326 | ||
e2f2d0dc | 327 | printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0\n"); |
19d36a45 | 328 | testcase_delete("201", "test", 0, "volume snapshot delete disabled\n"); |
0fdbd016 | 329 | |
e2f2d0dc | 330 | printf("Expected error for snapshot_delete with locked config\n"); |
0fdbd016 FG |
331 | testcase_delete("202", "test", 0, "VM is locked (backup)\n"); |
332 | ||
333 | ||
334 | done_testing(); |