1 package MigrationTest
::Shared
;
8 use Socket
qw(AF_INET);
11 use PVE
::Tools
qw(file_set_contents file_get_contents lock_file_full);
13 my $RUN_DIR_PATH = $ENV{RUN_DIR_PATH
} or die "no RUN_DIR_PATH set\n";
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
};
23 sub add_target_volid
{
26 PVE
::Storage
::parse_volume_id
($volid);
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));
37 sub remove_target_volid
{
40 PVE
::Storage
::parse_volume_id
($volid);
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));
51 my $mocked_cfs_read_file = sub {
54 if ($file eq 'datacenter.cfg') {
56 } elsif ($file eq 'replication.cfg') {
57 return $replication_config;
59 die "cfs_read_file (mocked) - implement me: $file\n";
64 our $cgroup_module = Test
::MockModule-
>new("PVE::CGroup");
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 {
79 our $ha_config_module = Test
::MockModule-
>new("PVE::HA::Config");
80 $ha_config_module->mock(
81 vm_is_ha_managed
=> sub {
86 our $qemu_config_module = Test
::MockModule-
>new("PVE::QemuConfig");
87 $qemu_config_module->mock(
88 assert_config_exists_on_node
=> sub {
92 my ($class, $vmid, $node) = @_;
93 die "trying to load wrong config: '$vmid'\n" if $vmid ne $test_vmid;
94 return decode_json
(file_get_contents
("${RUN_DIR_PATH}/vm_config"));
96 lock_config
=> sub { # no use locking here because lock is local to node
97 my ($self, $vmid, $code, @param) = @_;
98 return $code->(@param);
100 write_config
=> sub {
101 my ($class, $vmid, $conf) = @_;
102 die "trying to write wrong config: '$vmid'\n" if $vmid ne $test_vmid;
103 file_set_contents
("${RUN_DIR_PATH}/vm_config", to_json
($conf));
107 our $qemu_server_cloudinit_module = Test
::MockModule-
>new("PVE::QemuServer::Cloudinit");
108 $qemu_server_cloudinit_module->mock(
109 generate_cloudinitconfig
=> sub {
114 our $qemu_server_module = Test
::MockModule-
>new("PVE::QemuServer");
115 $qemu_server_module->mock(
116 clear_reboot_request
=> sub {
119 get_efivars_size
=> sub {
124 our $replication_module = Test
::MockModule-
>new("PVE::Replication");
125 $replication_module->mock(
126 run_replication
=> sub {
127 die "run_replication error" if $fail_config->{run_replication
};
129 my $vm_config = PVE
::QemuConfig-
>load_config($test_vmid);
130 return PVE
::QemuConfig-
>get_replicatable_volumes(
138 our $replication_config_module = Test
::MockModule-
>new("PVE::ReplicationConfig");
139 $replication_config_module->mock(
140 cfs_read_file
=> $mocked_cfs_read_file,
143 our $safe_syslog_module = Test
::MockModule-
>new("PVE::SafeSyslog");
144 $safe_syslog_module->mock(
149 our $storage_module = Test
::MockModule-
>new("PVE::Storage");
150 $storage_module->mock(
151 activate_volumes
=> sub {
154 deactivate_volumes
=> sub {
158 return $storage_config;
160 get_bandwidth_limit
=> sub {
163 cfs_read_file
=> $mocked_cfs_read_file,
166 our $storage_plugin_module = Test
::MockModule-
>new("PVE::Storage::Plugin");
167 $storage_plugin_module->mock(
168 cluster_lock_storage
=> sub {
169 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
171 mkdir "${RUN_DIR_PATH}/lock";
173 my $path = "${RUN_DIR_PATH}/lock/pve-storage-${storeid}";
174 return PVE
::Tools
::lock_file
($path, $timeout, $func, @param);
178 our $systemd_module = Test
::MockModule-
>new("PVE::Systemd");
179 $systemd_module->mock(
180 wait_for_unit_removed
=> sub {
183 enter_systemd_scope
=> sub {
188 my $migrate_port_counter = 60000;
190 our $tools_module = Test
::MockModule-
>new("PVE::Tools");
192 get_host_address_family
=> sub {
195 next_migrate_port
=> sub {
196 return $migrate_port_counter++;