]> git.proxmox.com Git - pve-manager.git/blob - bin/test/ReplicationTestEnv.pm
add regression test environment for replication
[pve-manager.git] / bin / test / ReplicationTestEnv.pm
1 package ReplicationTestEnv;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use lib ('.', '../..');
8
9 use Data::Dumper;
10
11 use PVE::INotify;
12 use PVE::Cluster;
13 use PVE::Storage;
14 use PVE::Replication;
15 use PVE::QemuConfig;
16 use PVE::LXC::Config;
17
18 use Test::MockModule;
19
20 our $mocked_nodename = 'node1';
21
22 our $mocked_replication_jobs = {};
23
24 my $pve_replicationconfig = Test::MockModule->new('PVE::ReplicationConfig');
25
26 our $mocked_vm_configs = {};
27
28 our $mocked_ct_configs = {};
29
30 my $mocked_vmlist = sub {
31 my $res = {};
32
33 foreach my $id (keys %$mocked_ct_configs) {
34 my $d = $mocked_ct_configs->{$id};
35 $res->{$id} = { 'type' => 'lxc', 'node' => $d->{node}, 'version' => 1 };
36 }
37 foreach my $id (keys %$mocked_vm_configs) {
38 my $d = $mocked_vm_configs->{$id};
39 $res->{$id} = { 'type' => 'qemu', 'node' => $d->{node}, 'version' => 1 };
40 }
41
42 return { 'ids' => $res };
43 };
44
45
46 my $statefile = ".mocked_repl_state";
47
48 unlink $statefile;
49 $PVE::Replication::state_path = $statefile;
50
51 my $mocked_write_state = sub {
52 my ($state) = @_;
53
54 PVE::Tools::file_set_contents($statefile, encode_json($state));
55 };
56
57 my $mocked_read_state = sub {
58
59 return {} if ! -e $statefile;
60
61 my $raw = PVE::Tools::file_get_contents($statefile);
62
63 return {} if $raw eq '';
64
65 return decode_json($raw);
66 };
67
68
69 my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
70
71 my $pve_inotify_module = Test::MockModule->new('PVE::INotify');
72
73 my $mocked_qemu_load_conf = sub {
74 my ($class, $vmid, $node) = @_;
75
76 $node = $mocked_nodename if !$node;
77
78 my $conf = $mocked_vm_configs->{$vmid};
79
80 die "no such vm '$vmid'" if !defined($conf);
81 die "vm '$vmid' on wrong node" if $conf->{node} ne $node;
82
83 return $conf;
84 };
85
86 my $pve_qemuserver_module = Test::MockModule->new('PVE::QemuServer');
87
88 my $pve_qemuconfig_module = Test::MockModule->new('PVE::QemuConfig');
89
90 my $mocked_lxc_load_conf = sub {
91 my ($class, $vmid, $node) = @_;
92
93 $node = $mocked_nodename if !$node;
94
95 my $conf = $mocked_ct_configs->{$vmid};
96
97 die "no such ct '$vmid'" if !defined($conf);
98 die "ct '$vmid' on wrong node" if $conf->{node} ne $node;
99
100 return $conf;
101 };
102
103 my $pve_lxc_config_module = Test::MockModule->new('PVE::LXC::Config');
104
105 my $mocked_replication_config = sub {
106
107 my $res = $mocked_replication_jobs;
108
109 return bless { ids => $res }, 'PVE::ReplicationConfig';
110 };
111
112 my $mocked_storage_config = {
113 ids => {
114 local => {
115 type => 'dir',
116 shared => 0,
117 content => {
118 'iso' => 1,
119 'backup' => 1,
120 },
121 path => "/var/lib/vz",
122 },
123 'local-zfs' => {
124 type => 'zfspool',
125 pool => 'nonexistent-testpool',
126 shared => 0,
127 content => {
128 'images' => 1,
129 'rootdir' => 1
130 },
131 },
132 },
133 };
134
135 my $pve_storage_module = Test::MockModule->new('PVE::Storage');
136
137 sub setup {
138 $pve_storage_module->mock(config => sub { return $mocked_storage_config; });
139
140 $pve_replicationconfig->mock(new => $mocked_replication_config);
141 $pve_qemuserver_module->mock(check_running => sub { return 0; });
142 $pve_qemuconfig_module->mock(load_config => $mocked_qemu_load_conf);
143
144 $pve_lxc_config_module->mock(load_config => $mocked_lxc_load_conf);
145
146
147 $pve_cluster_module->mock(get_vmlist => sub { return $mocked_vmlist->(); });
148 $pve_inotify_module->mock('nodename' => sub { return $mocked_nodename; });
149 };
150
151
152
153 1;