]> git.proxmox.com Git - qemu-server.git/blame - test/run_config2command_tests.pl
t/cfg2cmd: mock iscsi initiator name
[qemu-server.git] / test / run_config2command_tests.pl
CommitLineData
7b963e57
TL
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use Test::More;
9use Test::MockModule;
10
11use PVE::Tools qw(file_get_contents file_set_contents run_command);
12use PVE::QemuConfig;
13use PVE::QemuServer;
14
15my $base_env = {
16 storage_config => {
17 ids => {
18 local => {
19 content => {
20 images => 1,
21 },
22 path => '/var/lib/vz',
23 type => 'dir',
24 shared => 0,
25 },
26 'cifs-store' => {
27 shared => 1,
28 path => '/mnt/pve/cifs-store',
29 username => 'guest',
30 server => '127.0.0.42',
31 type => 'cifs',
32 share => 'CIFShare',
33 content => {
34 images => 1
35 },
36 },
37 'rbd-store' => {
38 monhost => '127.0.0.42,127.0.0.21,::1',
39 content => {
40 images => 1
41 },
42 type => 'rbd',
43 pool => 'cpool',
44 username => 'admin',
45 shared => 1
46 },
47 'local-lvm' => {
48 vgname => 'pve',
49 bwlimit => 'restore=1024',
50 type => 'lvmthin',
51 thinpool => 'data',
52 content => {
53 images => 1,
54 }
55 }
56 }
57 },
58 vmid => 8006,
59 real_qemu_version => PVE::QemuServer::kvm_user_version(), # not yet mocked
60};
61
62my $current_test; # = {
63# description => 'Test description', # if available
64# qemu_version => '2.12',
65# host_arch => 'HOST_ARCH',
66# config => { config hash },
67# expected => [ expected outcome cmd line array ],
68# };
69
70# use the config description to allow changing environment, fields are:
71# TEST: A single line describing the test, gets outputted
72# QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
73# HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
74# all fields are optional
75sub parse_test($) {
76 my ($config_fn) = @_;
77
78 $current_test = {}; # reset
79
80 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
81 my $config_raw = file_get_contents($config_fn);
82 my $config = PVE::QemuServer::parse_vm_config($fake_config_fn, $config_raw);
83
84 $current_test->{config} = $config;
85
86 my $description = $config->{description} // '';
87
88 while ($description =~ /^\h*(.*?)\h*$/gm) {
89 my $line = $1;
90 next if !$line || $line =~ /^#/;
91 $line =~ s/^\s+//;
92 $line =~ s/\s+$//;
93
94 if ($line =~ /^TEST:\s*(.*)\s*$/) {
95 $current_test->{description} = "$1";
96 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
97 $current_test->{qemu_version} = "$1";
98 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
99 $current_test->{host_arch} = "$1";
100 }
101 }
102}
103
104my $qemu_server_module;
105$qemu_server_module = Test::MockModule->new('PVE::QemuServer');
106$qemu_server_module->mock(
107 kvm_user_version => sub {
108 return $current_test->{qemu_version} // $base_env->{real_qemu_version};
109 },
110 get_host_arch => sub() {
111 return $current_test->{host_arch} // 'x86_64';
112 },
a7740135
WB
113 get_initiator_name => sub {
114 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
115 }
7b963e57
TL
116);
117
118my $qemu_server_config;
119$qemu_server_config = Test::MockModule->new('PVE::QemuConfig');
120$qemu_server_config->mock(
121 load_config => sub {
122 my ($class, $vmid, $node) = @_;
123
124 return $current_test->{config};
125 },
126);
127
128sub do_test($) {
129 my ($config_fn) = @_;
130
131 die "no such input test config: $config_fn\n" if ! -f $config_fn;
132
133 parse_test $config_fn;
134
135 $config_fn =~ /([^\/]+)$/;
136 my $testname = "$1";
137 if (my $desc = $current_test->{description}) {
138 $testname = "'$testname' - $desc";
139 }
140
141 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
142
143 my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $vmid);
144
145 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
146 $cmdline .= "\n";
147
148 my $cmd_fn = "$config_fn.cmd";
149
150 if (-f $cmd_fn) {
151 my $cmdline_expected = file_get_contents($cmd_fn);
152
153 my $cmd_expected = [ sort split /\s*\\?\n\s*/, $cmdline_expected ];
154 my $cmd = [ sort split /\s*\\?\n\s*/, $cmdline ];
155
156 # comment out for easier debugging
157 #file_set_contents("$cmd_fn.tmp", $cmdline);
158
159 is_deeply($cmd, $cmd_expected, "$testname")
160 } else {
161 file_set_contents($cmd_fn, $cmdline);
162 }
163}
164
165print "testing config to command stabillity\n";
166
167# exec tests
168if (my $file = shift) {
169 do_test $file;
170} else {
171 foreach my $file (<cfg2cmd/*.conf>) {
172 do_test $file;
173 }
174}
175
176done_testing();