]> git.proxmox.com Git - pve-manager.git/commitdiff
add regression test environment for replication
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 15 May 2017 07:07:22 +0000 (09:07 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 31 May 2017 06:23:46 +0000 (08:23 +0200)
bin/test/Makefile
bin/test/ReplicationTestEnv.pm [new file with mode: 0755]
bin/test/replication_test1.pl [new file with mode: 0755]

index 6833adffe105be5de7e2e1d133caf2fcca631067..b379dfa987f39498c1387ea928422dff23e9093c 100644 (file)
@@ -4,6 +4,8 @@ all:
 
 check:
        ./balloontest.pl
+       ./replication_test1.pl
+
 
 .PHONY: install
 install:
diff --git a/bin/test/ReplicationTestEnv.pm b/bin/test/ReplicationTestEnv.pm
new file mode 100755 (executable)
index 0000000..33b750c
--- /dev/null
@@ -0,0 +1,153 @@
+package ReplicationTestEnv;
+
+use strict;
+use warnings;
+use JSON;
+
+use lib ('.', '../..');
+
+use Data::Dumper;
+
+use PVE::INotify;
+use PVE::Cluster;
+use PVE::Storage;
+use PVE::Replication;
+use PVE::QemuConfig;
+use PVE::LXC::Config;
+
+use Test::MockModule;
+
+our $mocked_nodename = 'node1';
+
+our $mocked_replication_jobs = {};
+
+my $pve_replicationconfig = Test::MockModule->new('PVE::ReplicationConfig');
+
+our $mocked_vm_configs = {};
+
+our $mocked_ct_configs = {};
+
+my $mocked_vmlist = sub {
+    my $res = {};
+
+    foreach my $id (keys %$mocked_ct_configs) {
+       my $d = $mocked_ct_configs->{$id};
+       $res->{$id} = { 'type' => 'lxc', 'node' => $d->{node}, 'version' => 1 };
+    }
+    foreach my $id (keys %$mocked_vm_configs) {
+       my $d = $mocked_vm_configs->{$id};
+       $res->{$id} = { 'type' => 'qemu', 'node' => $d->{node}, 'version' => 1 };
+    }
+
+    return { 'ids' => $res };
+};
+
+
+my $statefile = ".mocked_repl_state";
+
+unlink $statefile;
+$PVE::Replication::state_path = $statefile;
+
+my $mocked_write_state = sub {
+    my ($state) = @_;
+
+    PVE::Tools::file_set_contents($statefile, encode_json($state));
+};
+
+my $mocked_read_state = sub {
+
+    return {} if ! -e $statefile;
+
+    my $raw = PVE::Tools::file_get_contents($statefile);
+
+    return {} if $raw eq '';
+
+    return decode_json($raw);
+};
+
+
+my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
+
+my $pve_inotify_module = Test::MockModule->new('PVE::INotify');
+
+my $mocked_qemu_load_conf = sub {
+    my ($class, $vmid, $node) = @_;
+
+    $node = $mocked_nodename if !$node;
+
+    my $conf = $mocked_vm_configs->{$vmid};
+
+    die "no such vm '$vmid'" if !defined($conf);
+    die "vm '$vmid' on wrong node" if $conf->{node} ne $node;
+
+    return $conf;
+};
+
+my $pve_qemuserver_module = Test::MockModule->new('PVE::QemuServer');
+
+my $pve_qemuconfig_module = Test::MockModule->new('PVE::QemuConfig');
+
+my $mocked_lxc_load_conf = sub {
+    my ($class, $vmid, $node) = @_;
+
+    $node = $mocked_nodename if !$node;
+
+    my $conf = $mocked_ct_configs->{$vmid};
+
+    die "no such ct '$vmid'" if !defined($conf);
+    die "ct '$vmid' on wrong node" if $conf->{node} ne $node;
+
+    return $conf;
+};
+
+my $pve_lxc_config_module = Test::MockModule->new('PVE::LXC::Config');
+
+my $mocked_replication_config = sub {
+
+    my $res = $mocked_replication_jobs;
+    
+    return bless { ids => $res }, 'PVE::ReplicationConfig';
+};
+
+my $mocked_storage_config = {
+    ids => {
+       local => {
+           type => 'dir',
+           shared => 0,
+           content => {
+               'iso' => 1,
+               'backup' => 1,
+           },
+           path => "/var/lib/vz",
+       },
+       'local-zfs' => {
+           type => 'zfspool',
+           pool => 'nonexistent-testpool',
+           shared => 0,
+           content => {
+               'images' => 1,
+               'rootdir' => 1
+           },
+       },
+    },
+};
+
+my $pve_storage_module = Test::MockModule->new('PVE::Storage');
+sub setup {
+    $pve_storage_module->mock(config => sub { return $mocked_storage_config; });
+
+    $pve_replicationconfig->mock(new => $mocked_replication_config);    
+    $pve_qemuserver_module->mock(check_running => sub { return 0; });
+    $pve_qemuconfig_module->mock(load_config => $mocked_qemu_load_conf);
+
+    $pve_lxc_config_module->mock(load_config => $mocked_lxc_load_conf);
+
+
+    $pve_cluster_module->mock(get_vmlist => sub { return $mocked_vmlist->(); });
+    $pve_inotify_module->mock('nodename' => sub { return $mocked_nodename; });
+};
+
+
+
+1;
diff --git a/bin/test/replication_test1.pl b/bin/test/replication_test1.pl
new file mode 100755 (executable)
index 0000000..0dfcf05
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+# Note: Test if mockup from ReplicationTestEnv works
+
+use strict;
+use warnings;
+use JSON;
+
+use lib ('.', '../..');
+
+use Data::Dumper;
+
+use ReplicationTestEnv;
+use Test::More tests => 3;
+
+$ReplicationTestEnv::mocked_nodename = 'node1';
+
+my $testjob = {
+    'type'  => 'local',
+    'target' => 'node1',
+    'guest' => 900,
+};
+
+$ReplicationTestEnv::mocked_replication_jobs = {
+    job_900_to_node1 => $testjob,
+};
+
+$ReplicationTestEnv::mocked_vm_configs = {
+    900 => {
+       node => 'node1',
+       snapshots => {},
+       ide0 => 'local-lvm:vm-900-disk-1,size=4G',
+       memory => 512,
+       ide2 => 'none,media=cdrom',
+    },
+};
+
+ReplicationTestEnv::setup();
+
+ok(PVE::INotify::nodename() eq 'node1');
+
+my $list = PVE::Cluster::get_vmlist();
+is_deeply($list, { ids => {900 => { node => 'node1', type => 'qemu', version => 1}}});
+my $cfg = PVE::ReplicationConfig->new();
+is_deeply($cfg, { ids => { job_900_to_node1 => $testjob }});
+
+exit(0);