]> git.proxmox.com Git - qemu-server.git/blame - test/run_qemu_img_convert_tests.pl
schema: fix description of migrate_downtime parameter
[qemu-server.git] / test / run_qemu_img_convert_tests.pl
CommitLineData
458a2b26
DC
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use Test::More;
9use Test::MockModule;
10
11use PVE::QemuServer;
12
13my $vmid = 8006;
14my $storage_config = {
15 ids => {
16 local => {
17 content => {
18 images => 1,
19 },
20 path => "/var/lib/vz",
21 type => "dir",
22 shared => 0,
23 },
24 "rbd-store" => {
25 monhost => "127.0.0.42,127.0.0.21,::1",
188eb9c3 26 fsid => 'fc4181a6-56eb-4f68-b452-8ba1f381ca2a',
458a2b26
DC
27 content => {
28 images => 1
29 },
30 type => "rbd",
31 pool => "cpool",
32 username => "admin",
33 shared => 1
34 },
35 "local-lvm" => {
36 vgname => "pve",
37 bwlimit => "restore=1024",
38 type => "lvmthin",
39 thinpool => "data",
40 content => {
41 images => 1,
42 }
43 },
44 "zfs-over-iscsi" => {
45 type => "zfs",
46 iscsiprovider => "LIO",
47 lio_tpg => "tpg1",
48 portal => "127.0.0.1",
49 target => "iqn.2019-10.org.test:foobar",
50 pool => "tank",
51 }
52 }
53};
54
55my $tests = [
56 {
57 name => 'qcow2raw',
56d16f16 58 parameters => [ "local:$vmid/vm-$vmid-disk-0.qcow2", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
458a2b26
DC
59 expected => [
60 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "qcow2", "-O", "raw",
61 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.qcow2", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw"
62 ],
63 },
64 {
65 name => "raw2qcow2",
56d16f16 66 parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "local:$vmid/vm-$vmid-disk-0.qcow2", 1024*10, undef, 0, undef ],
458a2b26
DC
67 expected => [
68 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "qcow2",
69 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.qcow2"
70 ]
71 },
72 {
73 name => "local2rbd",
56d16f16 74 parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "rbd-store:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
458a2b26
DC
75 expected => [
76 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
77 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw", "rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none"
78 ]
79 },
80 {
81 name => "rbd2local",
56d16f16 82 parameters => [ "rbd-store:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
458a2b26
DC
83 expected => [
84 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
85 "rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw"
86 ]
87 },
88 {
89 name => "local2zos",
56d16f16 90 parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "zfs-over-iscsi:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
458a2b26
DC
91 expected => [
92 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "--target-image-opts",
93 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
94 "file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw"
95 ]
96 },
97 {
98 name => "zos2local",
56d16f16 99 parameters => [ "zfs-over-iscsi:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
458a2b26
DC
100 expected => [
101 "/usr/bin/qemu-img", "convert", "-p", "-n", "--image-opts", "-O", "raw",
102 "file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
103 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
104 ]
105 },
106 {
107 name => "zos2rbd",
56d16f16 108 parameters => [ "zfs-over-iscsi:vm-$vmid-disk-0", "rbd-store:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
458a2b26
DC
109 expected => [
110 "/usr/bin/qemu-img", "convert", "-p", "-n", "--image-opts", "-O", "raw",
111 "file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
112 "rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none"
113 ]
114 },
115 {
116 name => "rbd2zos",
56d16f16 117 parameters => [ "rbd-store:vm-$vmid-disk-0", "zfs-over-iscsi:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
458a2b26
DC
118 expected => [
119 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "--target-image-opts",
120 "rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none",
121 "file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
122 ]
123 },
124 {
125 name => "local2lvmthin",
56d16f16 126 parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "local-lvm:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
458a2b26
DC
127 expected => [
128 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
129 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
130 "/dev/pve/vm-$vmid-disk-0",
131 ]
132 },
133 {
134 name => "lvmthin2local",
56d16f16 135 parameters => [ "local-lvm:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
458a2b26
DC
136 expected => [
137 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
138 "/dev/pve/vm-$vmid-disk-0",
139 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
140 ]
141 },
142 {
143 name => "zeroinit",
56d16f16 144 parameters => [ "local-lvm:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 1, undef ],
458a2b26
DC
145 expected => [
146 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
147 "/dev/pve/vm-$vmid-disk-0",
148 "zeroinit:/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
149 ]
150 },
151 {
152 name => "notexistingstorage",
56d16f16 153 parameters => [ "local-lvm:vm-$vmid-disk-0", "not-existing:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 1, undef ],
ae200950 154 expected => "storage 'not-existing' does not exist\n",
458a2b26 155 },
af1f1ec0
DC
156 {
157 name => "vmdkfile",
56d16f16 158 parameters => [ "./test.vmdk", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
af1f1ec0
DC
159 expected => [
160 "/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "vmdk", "-O", "raw",
161 "./test.vmdk",
162 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
163 ]
164 },
165 {
166 name => "notexistingfile",
56d16f16 167 parameters => [ "/foo/bar", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
af1f1ec0
DC
168 expected => "source '/foo/bar' is not a valid volid nor path for qemu-img convert\n",
169 },
170 {
171 name => "efidisk",
56d16f16 172 parameters => [ "/usr/share/kvm/OVMF_VARS-pure-efi.fd", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, undef ],
af1f1ec0 173 expected => [
bdd1feef 174 "/usr/bin/qemu-img", "convert", "-p", "-n", "-O", "raw",
af1f1ec0
DC
175 "/usr/share/kvm/OVMF_VARS-pure-efi.fd",
176 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
177 ]
178 },
179 {
180 name => "efi2zos",
56d16f16 181 parameters => [ "/usr/share/kvm/OVMF_VARS-pure-efi.fd", "zfs-over-iscsi:vm-$vmid-disk-0", 1024*10, undef, 0, undef ],
af1f1ec0 182 expected => [
bdd1feef 183 "/usr/bin/qemu-img", "convert", "-p", "-n", "--target-image-opts",
af1f1ec0
DC
184 "/usr/share/kvm/OVMF_VARS-pure-efi.fd",
185 "file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
186 ]
56d16f16
LN
187 },
188 {
189 name => "bwlimit",
190 parameters => [ "local-lvm:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0, 1024 ],
191 expected => [
192 "/usr/bin/qemu-img", "convert", "-p", "-n", "-r", "1024K", "-f", "raw", "-O", "raw",
193 "/dev/pve/vm-$vmid-disk-0",
194 "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
195 ]
196 },
458a2b26
DC
197];
198
199my $command;
200
201my $storage_module = Test::MockModule->new("PVE::Storage");
202$storage_module->mock(
203 config => sub {
204 return $storage_config;
205 },
206 activate_volumes => sub {
207 return 1;
208 }
209);
210
211my $lio_module = Test::MockModule->new("PVE::Storage::LunCmd::LIO");
212$lio_module->mock(
213 run_lun_command => sub {
214 return 1;
215 }
216);
217
218# we use the exported run_command so we have to mock it there
219my $zfsplugin_module = Test::MockModule->new("PVE::Storage::ZFSPlugin");
220$zfsplugin_module->mock(
221 run_command => sub {
222 return 1;
223 }
224);
225
226# we use the exported run_command so we have to mock it there
227my $qemu_server_module = Test::MockModule->new("PVE::QemuServer");
228$qemu_server_module->mock(
229 run_command => sub {
230 $command = shift;
231 },
232 get_initiator_name => sub {
233 return "foobar";
234 }
235);
236
237foreach my $test (@$tests) {
238 my $name = $test->{name};
239 my $expected = $test->{expected};
240 eval { PVE::QemuServer::qemu_img_convert(@{$test->{parameters}}) };
241 if (my $err = $@) {
242 is ($err, $expected, $name);
243 } elsif (defined($command)) {
244 is_deeply($command, $expected, $name);
245 $command = undef;
246 } else {
247 fail($name);
248 note("no command")
249 }
250}
251
252done_testing();