]> git.proxmox.com Git - qemu-server.git/blob - test/MigrationTest/Shared.pm
8ae6a6e1df3c8b5c4de56b33a7dd8266820c37c6
[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 $cluster_module = Test::MockModule->new("PVE::Cluster");
65 $cluster_module->mock(
66 cfs_read_file => $mocked_cfs_read_file,
67 check_cfs_quorum => sub {
68 return 1;
69 },
70 );
71
72 our $ha_config_module = Test::MockModule->new("PVE::HA::Config");
73 $ha_config_module->mock(
74 vm_is_ha_managed => sub {
75 return 0;
76 },
77 );
78
79 our $qemu_config_module = Test::MockModule->new("PVE::QemuConfig");
80 $qemu_config_module->mock(
81 assert_config_exists_on_node => sub {
82 return;
83 },
84 load_config => sub {
85 my ($class, $vmid, $node) = @_;
86 die "trying to load wrong config: '$vmid'\n" if $vmid ne $test_vmid;
87 return decode_json(file_get_contents("${RUN_DIR_PATH}/vm_config"));
88 },
89 lock_config => sub { # no use locking here because lock is local to node
90 my ($self, $vmid, $code, @param) = @_;
91 return $code->(@param);
92 },
93 write_config => sub {
94 my ($class, $vmid, $conf) = @_;
95 die "trying to write wrong config: '$vmid'\n" if $vmid ne $test_vmid;
96 file_set_contents("${RUN_DIR_PATH}/vm_config", to_json($conf));
97 },
98 );
99
100 our $qemu_server_cloudinit_module = Test::MockModule->new("PVE::QemuServer::Cloudinit");
101 $qemu_server_cloudinit_module->mock(
102 generate_cloudinitconfig => sub {
103 return;
104 },
105 );
106
107 our $qemu_server_module = Test::MockModule->new("PVE::QemuServer");
108 $qemu_server_module->mock(
109 clear_reboot_request => sub {
110 return 1;
111 },
112 get_efivars_size => sub {
113 return 128 * 1024;
114 },
115 );
116
117 our $replication_module = Test::MockModule->new("PVE::Replication");
118 $replication_module->mock(
119 run_replication => sub {
120 die "run_replication error" if $fail_config->{run_replication};
121
122 my $vm_config = PVE::QemuConfig->load_config($test_vmid);
123 return PVE::QemuConfig->get_replicatable_volumes(
124 $storage_config,
125 $test_vmid,
126 $vm_config,
127 );
128 },
129 );
130
131 our $replication_config_module = Test::MockModule->new("PVE::ReplicationConfig");
132 $replication_config_module->mock(
133 cfs_read_file => $mocked_cfs_read_file,
134 );
135
136 our $storage_module = Test::MockModule->new("PVE::Storage");
137 $storage_module->mock(
138 activate_volumes => sub {
139 return 1;
140 },
141 deactivate_volumes => sub {
142 return 1;
143 },
144 config => sub {
145 return $storage_config;
146 },
147 get_bandwidth_limit => sub {
148 return 123456;
149 },
150 cfs_read_file => $mocked_cfs_read_file,
151 );
152
153 our $storage_plugin_module = Test::MockModule->new("PVE::Storage::Plugin");
154 $storage_plugin_module->mock(
155 cluster_lock_storage => sub {
156 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
157
158 mkdir "${RUN_DIR_PATH}/lock";
159
160 my $path = "${RUN_DIR_PATH}/lock/pve-storage-${storeid}";
161 return PVE::Tools::lock_file($path, $timeout, $func, @param);
162 },
163 );
164
165 our $systemd_module = Test::MockModule->new("PVE::Systemd");
166 $systemd_module->mock(
167 wait_for_unit_removed => sub {
168 return;
169 },
170 enter_systemd_scope => sub {
171 return;
172 },
173 );
174
175 my $migrate_port_counter = 60000;
176
177 our $tools_module = Test::MockModule->new("PVE::Tools");
178 $tools_module->mock(
179 get_host_address_family => sub {
180 return AF_INET;
181 },
182 next_migrate_port => sub {
183 return $migrate_port_counter++;
184 },
185 );
186
187 1;