]> git.proxmox.com Git - qemu-server.git/blob - test/MigrationTest/QmMock.pm
fix #4522: api: vncproxy: also set environment variable for ticket without websocket
[qemu-server.git] / test / MigrationTest / QmMock.pm
1 package MigrationTest::QmMock;
2
3 use strict;
4 use warnings;
5
6 use JSON;
7 use Test::MockModule;
8
9 use MigrationTest::Shared;
10
11 use PVE::API2::Qemu;
12 use PVE::Storage;
13 use PVE::Tools qw(file_set_contents file_get_contents);
14
15 use PVE::CLIHandler;
16 use base qw(PVE::CLIHandler);
17
18 my $RUN_DIR_PATH = $ENV{RUN_DIR_PATH} or die "no RUN_DIR_PATH set\n";
19
20 my $target_volids = decode_json(file_get_contents("${RUN_DIR_PATH}/target_volids"));
21 my $fail_config = decode_json(file_get_contents("${RUN_DIR_PATH}/fail_config"));
22 my $migrate_params = decode_json(file_get_contents("${RUN_DIR_PATH}/migrate_params"));
23 my $nodename = $migrate_params->{target};
24
25 my $kvm_exectued = 0;
26
27 sub setup_environment {
28 my $rpcenv = PVE::RPCEnvironment::init('MigrationTest::QmMock', 'cli');
29 }
30
31 # mock RPCEnvironment directly
32
33 sub get_user {
34 return 'root@pam';
35 }
36
37 sub fork_worker {
38 my ($self, $dtype, $id, $user, $function, $background) = @_;
39 $function->(123456);
40 return '123456';
41 }
42
43 # mocked modules
44
45 my $inotify_module = Test::MockModule->new("PVE::INotify");
46 $inotify_module->mock(
47 nodename => sub {
48 return $nodename;
49 },
50 );
51
52 $MigrationTest::Shared::qemu_server_module->mock(
53 nodename => sub {
54 return $nodename;
55 },
56 config_to_command => sub {
57 return [ 'mocked_kvm_command' ];
58 },
59 );
60
61 my $qemu_server_helpers_module = Test::MockModule->new("PVE::QemuServer::Helpers");
62 $qemu_server_helpers_module->mock(
63 vm_running_locally => sub {
64 return $kvm_exectued;
65 },
66 );
67
68 # to make sure we get valid and predictable names
69 my $disk_counter = 10;
70
71 $MigrationTest::Shared::storage_module->mock(
72 vdisk_alloc => sub {
73 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
74
75 die "vdisk_alloc (mocked) - name is not expected to be set - implement me\n"
76 if defined($name);
77
78 my $name_without_extension = "vm-${vmid}-disk-${disk_counter}";
79 $disk_counter++;
80
81 my $volid;
82 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
83 if ($scfg->{path}) {
84 $volid = "${storeid}:${vmid}/${name_without_extension}.${fmt}";
85 } else {
86 $volid = "${storeid}:${name_without_extension}";
87 }
88
89 PVE::Storage::parse_volume_id($volid);
90
91 die "vdisk_alloc '$volid' error\n" if $fail_config->{vdisk_alloc}
92 && $fail_config->{vdisk_alloc} eq $volid;
93
94 MigrationTest::Shared::add_target_volid($volid);
95
96 return $volid;
97 },
98 );
99
100 $MigrationTest::Shared::qemu_server_module->mock(
101 mon_cmd => sub {
102 my ($vmid, $command, %params) = @_;
103
104 if ($command eq 'nbd-server-start') {
105 return;
106 } elsif ($command eq 'block-export-add') {
107 return;
108 } elsif ($command eq 'query-block') {
109 return [];
110 } elsif ($command eq 'qom-set') {
111 return;
112 }
113 die "mon_cmd (mocked) - implement me: $command";
114 },
115 run_command => sub {
116 my ($cmd_full, %param) = @_;
117
118 my $cmd_msg = to_json($cmd_full);
119
120 my $cmd = shift @{$cmd_full};
121
122 if ($cmd eq '/bin/systemctl') {
123 return;
124 } elsif ($cmd eq 'mocked_kvm_command') {
125 $kvm_exectued = 1;
126 return 0;
127 }
128 die "run_command (mocked) - implement me: ${cmd_msg}";
129 },
130 set_migration_caps => sub {
131 return;
132 },
133 vm_migrate_alloc_nbd_disks => sub{
134 my $nbd = $MigrationTest::Shared::qemu_server_module->original('vm_migrate_alloc_nbd_disks')->(@_);
135 file_set_contents("${RUN_DIR_PATH}/nbd_info", to_json($nbd));
136 return $nbd;
137 },
138 );
139
140 our $cmddef = {
141 start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } ],
142 };
143
144 MigrationTest::QmMock->run_cli_handler();
145
146 1;