9 use PVE
::Storage
::Plugin
;
17 my $snapshot_possible;
18 my $vol_snapshot_possible = {};
19 my $vol_snapshot_delete_possible = {};
20 my $vol_snapshot = {};
21 my $vol_snapshot_delete = {};
27 sub mocked_volume_snapshot
{
28 my ($storecfg, $volid, $snapname) = @_;
29 die "Storage config not mocked! aborting"
30 if defined($storecfg);
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";
39 $vol_snapshot->{$volid} = $snapname;
43 die "volume snapshot disabled";
47 sub mocked_volume_snapshot_delete
{
48 my ($storecfg, $volid, $snapname) = @_;
49 die "Storage config not mocked! aborting"
50 if defined($storecfg);
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";
59 $vol_snapshot_delete->{$volid} = $snapname;
63 die "volume snapshot delete disabled";
67 sub mocked_run_command
{
68 my ($cmd, %param) = @_;
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";
77 die "unexpected run_command call, aborting";
83 my ($exp_fn, $real_fn) = @_;
86 $ret = system("diff -u '$exp_fn' '$real_fn'");
92 sub testcase_prepare
{
93 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_;
94 subtest
"Preparing snapshot '$snapname' for vm '$vmid'" => sub {
98 PVE
::LXC
::snapshot_prepare
($vmid, $snapname, $save_vmstate, $comment);
100 is($@, $exp_err, "\$@ correct");
101 ok
(test_file
("snapshot-expected/prepare/lxc/$vmid.conf", "snapshot-working/prepare/lxc/$vmid.conf"), "config file correct");
105 sub testcase_commit
{
106 my ($vmid, $snapname, $exp_err) = @_;
107 subtest
"Committing snapshot '$snapname' for vm '$vmid'" => sub {
111 PVE
::LXC
::snapshot_commit
($vmid, $snapname);
113 is($@, $exp_err, "\$@ correct");
114 ok
(test_file
("snapshot-expected/commit/lxc/$vmid.conf", "snapshot-working/commit/lxc/$vmid.conf"), "config file correct");
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 {
123 $vol_snapshot_delete = {};
124 $exp_vol_snap = {} if !defined($exp_vol_snap);
125 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
128 PVE
::LXC
::snapshot_create
($vmid, $snapname, $save_vmstate, $comment);
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");
137 sub testcase_delete
{
138 my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_;
139 subtest
"Deleting snapshot '$snapname' of vm '$vmid'" => sub {
141 $vol_snapshot_delete = {};
142 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
145 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $force);
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");
153 # BEGIN redefine PVE::LXC methods
154 sub config_file_lock
{
155 return "snapshot-working/pve-test.lock";
158 sub cfs_config_path
{
159 my ($vmid, $node) = @_;
161 $node = $nodename if !$node;
162 return "snapshot-working/$node/lxc/$vmid.conf";
166 my ($vmid, $node) = @_;
168 my $filename = cfs_config_path
($vmid, $node);
170 my $raw = PVE
::Tools
::file_get_contents
($filename);
172 my $conf = PVE
::LXC
::parse_pct_config
($filename, $raw);
177 my ($vmid, $conf) = @_;
179 my $filename = cfs_config_path
($vmid);
181 if ($conf->{snapshots
}) {
182 foreach my $snapname (keys %{$conf->{snapshots
}}) {
183 $conf->{snapshots
}->{$snapname}->{snaptime
} = "1234567890"
184 if $conf->{snapshots
}->{$snapname}->{snaptime
};
188 my $raw = PVE
::LXC
::write_pct_config
($filename, $conf);
190 PVE
::Tools
::file_set_contents
($filename, $raw);
194 my ($feature, $conf, $storecfg, $snapname) = @_;
195 return $snapshot_possible;
202 sub sync_container_namespace
{
206 # END redefine PVE::LXC methods
208 PVE
::Tools
::run_command
("rm -rf snapshot-working");
209 PVE
::Tools
::run_command
("cp -a snapshot-input snapshot-working");
212 $freeze_possible = 1;
215 printf("Running prepare tests");
217 $nodename = "prepare";
220 printf("Setting has_feature to return true");
222 $snapshot_possible = 1;
224 printf("Successful snapshot_prepare with no existing snapshots");
225 testcase_prepare
("101", "test", 0, "test comment", '');
227 printf("Successful snapshot_prepare with one existing snapshot");
228 testcase_prepare
("102", "test2", 0, "test comment", "");
230 printf("Expected error for snapshot_prepare on locked container");
231 testcase_prepare
("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
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");
236 printf("Expected error for snapshot_prepare with save_vmstate");
237 testcase_prepare
("202", "test", 1, "test comment", "implement me - snapshot_save_vmstate\n");
240 printf("Setting has_feature to return false");
242 $snapshot_possible = 0;
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");
248 printf("Running commit tests");
250 $nodename = "commit";
253 printf("Setting has_feature to return true");
255 $snapshot_possible = 1;
257 printf("Successful snapshot_commit with one prepared snapshot");
258 testcase_commit
("101", "test", "");
260 printf("Successful snapshot_commit with one committed and one prepared snapshot");
261 testcase_commit
("102", "test2", "");
263 printf("Expected error for snapshot_commit with no snapshot lock");
264 testcase_commit
("201", "test", "missing snapshot lock\n");
266 printf("Expected error for snapshot_commit with invalid snapshot name");
267 testcase_commit
("202", "test", "snapshot 'test' does not exist\n");
269 printf("Expected error for snapshot_commit with invalid snapshot state");
270 testcase_commit
("203", "test", "wrong snapshot state\n");
272 $vol_snapshot_possible->{"local:snapshotable-disk-1"} = 1;
273 $vol_snapshot_delete_possible->{"local:snapshotable-disk-1"} = 1;
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");
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");
288 $nodename = "create";
290 printf("Running create tests");
293 printf("Successful snapshot_create with no existing snapshots");
294 testcase_create
("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
296 printf("Successful snapshot_create with one existing snapshots");
297 testcase_create
("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
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");
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;
307 $nodename = "delete";
309 printf("Running delete tests");
312 printf("Successful snapshot_delete of only existing snapshot");
313 testcase_delete
("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
315 printf("Successful snapshot_delete of leaf snapshot");
316 testcase_delete
("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
318 printf("Successful snapshot_delete of root snapshot");
319 testcase_delete
("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
321 printf("Successful snapshot_delete of intermediate snapshot");
322 testcase_delete
("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
324 printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1");
325 testcase_delete
("105", "test", 1, "");
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");
330 printf("Expected error for snapshot_delete with locked config");
331 testcase_delete
("202", "test", 0, "VM is locked (backup)\n");