]> git.proxmox.com Git - pve-container.git/blame - src/test/snapshot-test.pm
always use new/unistalled files for testing
[pve-container.git] / src / test / snapshot-test.pm
CommitLineData
0fdbd016
FG
1package PVE::LXC;
2
3use strict;
4use warnings;
5
0fff7963
DM
6use lib qw(..);
7
0fdbd016
FG
8use PVE::Storage;
9use PVE::Storage::Plugin;
10use PVE::LXC;
11use PVE::Tools;
12
13use Test::MockModule;
14use Test::More;
15
16my $nodename;
17my $snapshot_possible;
18my $vol_snapshot_possible = {};
19my $vol_snapshot_delete_possible = {};
20my $vol_snapshot = {};
21my $vol_snapshot_delete = {};
22my $running;
23my $freeze_possible;
24
25# Mocked methods
26
27sub 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
47sub 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
67sub 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
82sub 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
92sub 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
105sub 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
118sub 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
137sub 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
154sub config_file_lock {
155 return "snapshot-working/pve-test.lock";
156}
157
158sub cfs_config_path {
159 my ($vmid, $node) = @_;
160
161 $node = $nodename if !$node;
162 return "snapshot-working/$node/lxc/$vmid.conf";
163}
164
165sub 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
176sub 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
193sub has_feature {
194 my ($feature, $conf, $storecfg, $snapname) = @_;
195 return $snapshot_possible;
196}
197
198sub check_running {
199 return $running;
200}
201
202sub sync_container_namespace {
203 return;
204}
205
206# END redefine PVE::LXC methods
207
208PVE::Tools::run_command("rm -rf snapshot-working");
209PVE::Tools::run_command("cp -a snapshot-input snapshot-working");
210
211$running = 1;
212$freeze_possible = 1;
213
214printf("");
215printf("Running prepare tests");
216printf("");
217$nodename = "prepare";
218
219printf("");
220printf("Setting has_feature to return true");
221printf("");
222$snapshot_possible = 1;
223
224printf("Successful snapshot_prepare with no existing snapshots");
225testcase_prepare("101", "test", 0, "test comment", '');
226
227printf("Successful snapshot_prepare with one existing snapshot");
228testcase_prepare("102", "test2", 0, "test comment", "");
229
230printf("Expected error for snapshot_prepare on locked container");
231testcase_prepare("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
232
233printf("Expected error for snapshot_prepare with duplicate snapshot name");
234testcase_prepare("201", "test", 0, "test comment", "snapshot name 'test' already used\n");
235
236printf("Expected error for snapshot_prepare with save_vmstate");
237testcase_prepare("202", "test", 1, "test comment", "implement me - snapshot_save_vmstate\n");
238
239printf("");
240printf("Setting has_feature to return false");
241printf("");
242$snapshot_possible = 0;
243
244printf("Expected error for snapshot_prepare if snapshots not possible");
245testcase_prepare("300", "test", 0, "test comment", "snapshot feature is not available\n");
246
247printf("");
248printf("Running commit tests");
249printf("");
250$nodename = "commit";
251
252printf("");
253printf("Setting has_feature to return true");
254printf("");
255$snapshot_possible = 1;
256
257printf("Successful snapshot_commit with one prepared snapshot");
258testcase_commit("101", "test", "");
259
260printf("Successful snapshot_commit with one committed and one prepared snapshot");
261testcase_commit("102", "test2", "");
262
263printf("Expected error for snapshot_commit with no snapshot lock");
264testcase_commit("201", "test", "missing snapshot lock\n");
265
266printf("Expected error for snapshot_commit with invalid snapshot name");
267testcase_commit("202", "test", "snapshot 'test' does not exist\n");
268
269printf("Expected error for snapshot_commit with invalid snapshot state");
270testcase_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;
274printf("");
275printf("Setting up Mocking for PVE::Storage");
276my $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);
280printf("\tconfig(), volume_snapshot() and volume_snapshot_delete() mocked");
281
282printf("");
283printf("Setting up Mocking for PVE::Tools");
284my $tools_module = new Test::MockModule('PVE::Tools');
285$tools_module->mock('run_command' => \&mocked_run_command);
286printf("\trun_command() mocked");
287
288$nodename = "create";
289printf("");
290printf("Running create tests");
291printf("");
292
293printf("Successful snapshot_create with no existing snapshots");
294testcase_create("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
295
296printf("Successful snapshot_create with one existing snapshots");
297testcase_create("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
298
299printf("Expected error for snapshot_create when volume snapshot is not possible");
300testcase_create("201", "test", 0, "test comment", "volume snapshot disabled at snapshot-test.pm line 41.\n\n");
301
302printf("Expected error for snapshot_create with broken lxc-freeze");
303$freeze_possible = 0;
304testcase_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";
308printf("");
309printf("Running delete tests");
310printf("");
311
312printf("Successful snapshot_delete of only existing snapshot");
313testcase_delete("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
314
315printf("Successful snapshot_delete of leaf snapshot");
316testcase_delete("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
317
318printf("Successful snapshot_delete of root snapshot");
319testcase_delete("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
320
321printf("Successful snapshot_delete of intermediate snapshot");
322testcase_delete("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
323
324printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1");
325testcase_delete("105", "test", 1, "");
326
327printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0");
328testcase_delete("201", "test", 0, "volume snapshot delete disabled at snapshot-test.pm line 61.\n");
329
330printf("Expected error for snapshot_delete with locked config");
331testcase_delete("202", "test", 0, "VM is locked (backup)\n");
332
333
334done_testing();