]> git.proxmox.com Git - qemu-server.git/blob - test/MigrationTest/Shared.pm
fix #4522: api: vncproxy: also set environment variable for ticket without websocket
[qemu-server.git] / test / MigrationTest / Shared.pm
1 package MigrationTest::Shared;
2
3 use strict;
4 use warnings;
5
6 use JSON;
7 use Test::MockModule;
8 use Socket qw(AF_INET);
9
10 use PVE::QemuConfig;
11 use PVE::Tools qw(file_set_contents file_get_contents lock_file_full);
12
13 my $RUN_DIR_PATH = $ENV{RUN_DIR_PATH} or die "no RUN_DIR_PATH set\n";
14
15 my $storage_config = decode_json(file_get_contents("${RUN_DIR_PATH}/storage_config"));
16 my $replication_config = decode_json(file_get_contents("${RUN_DIR_PATH}/replication_config"));
17 my $fail_config = decode_json(file_get_contents("${RUN_DIR_PATH}/fail_config"));
18 my $migrate_params = decode_json(file_get_contents("${RUN_DIR_PATH}/migrate_params"));
19 my $test_vmid = $migrate_params->{vmid};
20
21 # helpers
22
23 sub add_target_volid {
24 my ($volid) = @_;
25
26 PVE::Storage::parse_volume_id($volid);
27
28 lock_file_full("${RUN_DIR_PATH}/target_volids.lock", undef, 0, sub {
29 my $target_volids = decode_json(file_get_contents("${RUN_DIR_PATH}/target_volids"));
30 die "target volid already present " if defined($target_volids->{$volid});
31 $target_volids->{$volid} = 1;
32 file_set_contents("${RUN_DIR_PATH}/target_volids", to_json($target_volids));
33 });
34 die $@ if $@;
35 }
36
37 sub remove_target_volid {
38 my ($volid) = @_;
39
40 PVE::Storage::parse_volume_id($volid);
41
42 lock_file_full("${RUN_DIR_PATH}/target_volids.lock", undef, 0, sub {
43 my $target_volids = decode_json(file_get_contents("${RUN_DIR_PATH}/target_volids"));
44 die "target volid does not exist " if !defined($target_volids->{$volid});
45 delete $target_volids->{$volid};
46 file_set_contents("${RUN_DIR_PATH}/target_volids", to_json($target_volids));
47 });
48 die $@ if $@;
49 }
50
51 my $mocked_cfs_read_file = sub {
52 my ($file) = @_;
53
54 if ($file eq 'datacenter.cfg') {
55 return {};
56 } elsif ($file eq 'replication.cfg') {
57 return $replication_config;
58 }
59 die "cfs_read_file (mocked) - implement me: $file\n";
60 };
61
62 # mocked modules
63
64 our $cgroup_module = Test::MockModule->new("PVE::CGroup");
65 $cgroup_module->mock(
66 cgroup_mode => sub {
67 return 2;
68 },
69 );
70
71 our $cluster_module = Test::MockModule->new("PVE::Cluster");
72 $cluster_module->mock(
73 cfs_read_file => $mocked_cfs_read_file,
74 check_cfs_quorum => sub {
75 return 1;
76 },
77 );
78
79 our $mapping_usb_module = Test::MockModule->new("PVE::Mapping::USB");
80 $mapping_usb_module->mock(
81 config => sub {
82 return {};
83 },
84 );
85
86 our $mapping_pci_module = Test::MockModule->new("PVE::Mapping::PCI");
87 $mapping_pci_module->mock(
88 config => sub {
89 return {};
90 },
91 );
92
93 our $ha_config_module = Test::MockModule->new("PVE::HA::Config");
94 $ha_config_module->mock(
95 vm_is_ha_managed => sub {
96 return 0;
97 },
98 );
99
100 our $qemu_config_module = Test::MockModule->new("PVE::QemuConfig");
101 $qemu_config_module->mock(
102 assert_config_exists_on_node => sub {
103 return;
104 },
105 load_config => sub {
106 my ($class, $vmid, $node) = @_;
107 die "trying to load wrong config: '$vmid'\n" if $vmid ne $test_vmid;
108 return decode_json(file_get_contents("${RUN_DIR_PATH}/vm_config"));
109 },
110 lock_config => sub { # no use locking here because lock is local to node
111 my ($self, $vmid, $code, @param) = @_;
112 return $code->(@param);
113 },
114 write_config => sub {
115 my ($class, $vmid, $conf) = @_;
116 die "trying to write wrong config: '$vmid'\n" if $vmid ne $test_vmid;
117 file_set_contents("${RUN_DIR_PATH}/vm_config", to_json($conf));
118 },
119 );
120
121 our $qemu_server_cloudinit_module = Test::MockModule->new("PVE::QemuServer::Cloudinit");
122 $qemu_server_cloudinit_module->mock(
123 generate_cloudinitconfig => sub {
124 return;
125 },
126 );
127
128 our $qemu_server_module = Test::MockModule->new("PVE::QemuServer");
129 $qemu_server_module->mock(
130 clear_reboot_request => sub {
131 return 1;
132 },
133 get_efivars_size => sub {
134 return 128 * 1024;
135 },
136 );
137
138 our $replication_module = Test::MockModule->new("PVE::Replication");
139 $replication_module->mock(
140 run_replication => sub {
141 die "run_replication error" if $fail_config->{run_replication};
142
143 my $vm_config = PVE::QemuConfig->load_config($test_vmid);
144 return PVE::QemuConfig->get_replicatable_volumes(
145 $storage_config,
146 $test_vmid,
147 $vm_config,
148 );
149 },
150 );
151
152 our $replication_config_module = Test::MockModule->new("PVE::ReplicationConfig");
153 $replication_config_module->mock(
154 cfs_read_file => $mocked_cfs_read_file,
155 );
156
157 our $safe_syslog_module = Test::MockModule->new("PVE::SafeSyslog");
158 $safe_syslog_module->mock(
159 initlog => sub {},
160 syslog => sub {},
161 );
162
163 our $storage_module = Test::MockModule->new("PVE::Storage");
164 $storage_module->mock(
165 activate_volumes => sub {
166 return 1;
167 },
168 deactivate_volumes => sub {
169 return 1;
170 },
171 config => sub {
172 return $storage_config;
173 },
174 get_bandwidth_limit => sub {
175 return 123456;
176 },
177 cfs_read_file => $mocked_cfs_read_file,
178 );
179
180 our $storage_plugin_module = Test::MockModule->new("PVE::Storage::Plugin");
181 $storage_plugin_module->mock(
182 cluster_lock_storage => sub {
183 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
184
185 mkdir "${RUN_DIR_PATH}/lock";
186
187 my $path = "${RUN_DIR_PATH}/lock/pve-storage-${storeid}";
188 return PVE::Tools::lock_file($path, $timeout, $func, @param);
189 },
190 );
191
192 our $systemd_module = Test::MockModule->new("PVE::Systemd");
193 $systemd_module->mock(
194 wait_for_unit_removed => sub {
195 return;
196 },
197 enter_systemd_scope => sub {
198 return;
199 },
200 );
201
202 my $migrate_port_counter = 60000;
203
204 our $tools_module = Test::MockModule->new("PVE::Tools");
205 $tools_module->mock(
206 get_host_address_family => sub {
207 return AF_INET;
208 },
209 next_migrate_port => sub {
210 return $migrate_port_counter++;
211 },
212 );
213
214 1;