]>
git.proxmox.com Git - pve-storage.git/blob - test/rbd_namespace.pl
0a1d02670b578c5653c7d094097e3887f84c9254
3 # This script is meant to be run manually on hyperconverged PVE server with a
4 # Ceph cluster. It tests how PVE handles RBD namespaces.
6 # The pool (default: rbd) must already exist. The namespace and VMs will be
9 # Parameters like names for the pool an namespace and the VMID can be
10 # configured. The VMIDs for the clones is $vmid -1 and $vmid -2.
12 # Cleanup is done after a successful run. Cleanup can also be called manually.
16 # * Snapshot rollback can sometimes be racy with stopping the VM and Ceph
17 # recognizing that the disk image is not in use anymore.
26 use PVE
::Tools
qw(run_command);
28 my $pool = "testpool";
29 my $use_existing= undef;
30 my $namespace = "testspace";
36 my $helpstring = "To override default values, set them as named parameters:
38 --pool pool name, default: ${pool}
39 --use-existing use existing pool, default: 0, needs --pool set
40 --namespace rbd namespace, default: ${namespace}
41 --vmid VMID of the test VM, default: ${vmid}
42 --cleanup Remove the storage definitions, namespaces and VMs
43 --debug Enable debug output\n";
47 "use-existing" => \
$use_existing,
48 "namespace=s" => \
$namespace,
50 "h|help" => \
$showhelp,
51 "cleanup" => \
$cleanup,
53 ) or die ($helpstring);
60 my $storage_name = "${pool}-${namespace}";
62 my $vmid_clone = int($vmid) - 1;
63 my $vmid_linked_clone = int($vmid) - 2;
66 print to_json
($_[0], { utf8
=> 8, pretty
=> 1, canonical
=> 1 }) . "\n";
73 my ($cmd, $json, $ignore_errors) = @_;
76 my $parser = sub {$raw .= shift;};
79 run_command
($cmd, outfunc
=> $parser);
82 die $err if !$ignore_errors;
89 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
90 $result = JSON
::decode_json
($1);
92 die "got unexpected data from command: '$cmd' -> '$raw'\n";
109 run_command
($cmd, outfunc
=> $out);
121 print "Preparing test environent\n";
123 my $pools = run_cmd
("ceph osd pool ls --format json", 1);
125 my %poolnames = map {$_ => 1} @$pools;
126 die "Pool '$pool' does not exist!\n"
127 if !exists($poolnames{$pool}) && $use_existing;
129 run_cmd
(['pveceph', 'pool', 'create', ${pool
}, '--add_storages', 1])
132 my $namespaces = run_cmd
(['rbd', '-p', ${pool
}, 'namespace', 'ls', '--format', 'json'], 1);
135 for my $i (@$namespaces) {
136 $ns_found = 1 if $i->{name
} eq $namespace;
140 print "Create namespace '${namespace}' in pool '${pool}'\n";
141 run_cmd
(['rbd', 'namespace', 'create', "${pool}/${namespace}"]);
144 my $storages = run_cmd
(['pvesh', 'get', 'storage', '--output-format', 'json'], 1);
149 print "Create storage definition\n";
150 for my $stor (@$storages) {
151 $pool_found = 1 if $stor->{storage
} eq $pool;
152 $rbd_found = 1 if $stor->{storage
} eq $storage_name;
155 run_cmd
(['pvesm', 'set', ${storage_name
}, '--krbd', '0']);
156 die "Enable the storage '$stor->{storage}'!" if $stor->{disable
};
160 die "No storage for pool '${pool}' found! Must have same name as pool!\n"
163 run_cmd
(['pvesm', 'add', 'rbd', $pool, '--pool', $pool, '--content', 'images,rootdir']);
165 # create PVE storages (librbd / krbd)
166 run_cmd
(['pvesm', 'add', 'rbd', ${storage_name
}, '--krbd', '0', '--pool', ${pool
}, '--namespace', ${namespace
}, '--content', 'images,rootdir'])
171 print "Create test VM ${vmid}\n";
172 my $vms = run_cmd
(['pvesh', 'get', 'cluster/resources', '--type', 'vm', '--output-format', 'json'], 1);
174 # TODO: introduce a force flag to make this behaviour configurable
176 if ($vm->{vmid
} eq $vmid) {
177 print "Test VM '${vmid}' already exists. It will be removed and recreated!\n";
178 run_cmd
(['qm', 'stop', ${vmid
}], 0, 1);
179 run_cmd
(['qm', 'destroy', ${vmid
}]);
182 run_cmd
(['qm', 'create', ${vmid
}, '--bios', 'ovmf', '--efidisk0', "${storage_name}:1", '--scsi0', "${storage_name}:2"]);
187 print "Cleaning up test environment!\n";
188 print "Removing VMs\n";
189 run_cmd
(['qm', 'stop', ${vmid
}], 0, 1);
190 run_cmd
(['qm', 'stop', ${vmid_linked_clone
}], 0, 1);
191 run_cmd
(['qm', 'stop', ${vmid_clone
}], 0, 1);
192 run_cmd
(['qm', 'destroy', ${vmid_linked_clone
}], 0, 1);
193 run_cmd
(['qm', 'destroy', ${vmid_clone
}], 0, 1);
194 run_cmd
(['for', 'i', 'in', "/dev/rbd/${pool}/${namespace}/*;", 'do', '/usr/bin/rbd', 'unmap', '\$i;', 'done'], 0, 1);
195 run_cmd
(['qm', 'unlock', ${vmid
}], 0, 1);
196 run_cmd
(['qm', 'destroy', ${vmid
}], 0, 1);
198 print "Removing Storage definition for ${storage_name}\n";
199 run_cmd
(['pvesm', 'remove', ${storage_name
}], 0, 1);
201 print "Removing RBD namespace '${pool}/${namespace}'\n";
202 run_cmd
(['rbd', 'namespace', 'remove', "${pool}/${namespace}"], 0, 1);
204 if (!$use_existing) {
205 print "Removing Storage definition for ${pool}\n";
206 run_cmd
(['pvesm', 'remove', ${pool
}], 0, 1);
207 print "Removing test pool\n";
208 run_cmd
(['pveceph', 'pool', 'destroy', $pool]);
213 # Example structure for tests
215 # name => "name of test section",
217 # ['some', 'prep', 'command'],
220 # ['test', 'cmd', $vmid],
221 # ['second', 'step', $vmid],
224 # ['cleanup', 'command'],
228 name
=> 'first VM start',
230 ['qm', 'start', $vmid],
234 name
=> 'snapshot/rollback',
236 ['qm', 'snapshot', $vmid, 'test'],
237 ['qm', 'rollback', $vmid, 'test'],
240 ['qm', 'unlock', $vmid],
244 name
=> 'remove snapshot',
246 ['qm', 'delsnapshot', $vmid, 'test'],
250 name
=> 'moving disk between namespaces',
252 ['qm', 'move_disk', $vmid, 'scsi0', $pool, '--delete', 1],
253 ['qm', 'move_disk', $vmid, 'scsi0', $storage_name, '--delete', 1],
257 name
=> 'switch to krbd',
259 ['qm', 'stop', $vmid],
260 ['pvesm', 'set', $storage_name, '--krbd', 1]
264 name
=> 'start VM with krbd',
266 ['qm', 'start', $vmid],
270 name
=> 'snapshot/rollback with krbd',
272 ['qm', 'snapshot', $vmid, 'test'],
273 ['qm', 'rollback', $vmid, 'test'],
276 ['qm', 'unlock', $vmid],
280 name
=> 'remove snapshot with krbd',
282 ['qm', 'delsnapshot', $vmid, 'test'],
286 name
=> 'moving disk between namespaces with krbd',
288 ['qm', 'move_disk', $vmid, 'scsi0', $pool, '--delete', 1],
289 ['qm', 'move_disk', $vmid, 'scsi0', $storage_name, '--delete', 1],
293 name
=> 'clone VM with krbd',
295 ['qm', 'clone', $vmid, $vmid_clone],
299 name
=> 'switch to non krbd',
301 ['qm', 'stop', $vmid],
302 ['qm', 'stop', $vmid_clone],
303 ['pvesm', 'set', $storage_name, '--krbd', 0]
307 name
=> 'templates and linked clone',
309 ['qm', 'template', $vmid],
310 ['qm', 'clone', $vmid, $vmid_linked_clone],
311 ['qm', 'start', $vmid_linked_clone],
312 ['qm', 'stop', $vmid_linked_clone],
316 name
=> 'start linked clone with krbd',
318 ['pvesm', 'set', $storage_name, '--krbd', 1]
321 ['qm', 'start', $vmid_linked_clone],
322 ['qm', 'stop', $vmid_linked_clone],
327 sub run_prep_cleanup
{
331 print join(' ', @$_). "\n";
340 ok
(run_test_cmd
($_), join(' ', @$_));
345 print "Running tests:\n";
349 $num_tests += scalar(@{$_->{steps
}}) if defined $_->{steps
};
352 print("Tests: $num_tests\n");
353 plan tests
=> $num_tests;
355 for my $test (@$tests) {
356 print "Section: $test->{name}\n";
357 run_prep_cleanup
($test->{preparations
}) if defined $test->{preparations
};
358 run_steps
($test->{steps
}) if defined $test->{steps
};
359 run_prep_cleanup
($test->{cleanup
}) if defined $test->{cleanup
};
364 if (Test
::More-
>builder->is_passing()) {