]> git.proxmox.com Git - pve-cluster.git/blob - data/test/corosync_parser_test.pl
6999d3abb64965a406d27c22b82ab62f5f4bd945
[pve-cluster.git] / data / test / corosync_parser_test.pl
1 #!/usr/bin/perl
2
3 use lib '..';
4
5 use strict;
6 use warnings;
7
8 use Test::MockModule;
9 use Test::More;
10
11 use PVE::Corosync;
12
13 my $known_hosts = {
14 "prox1" => "127.0.1.1",
15 "prox1-ring1" => "127.0.2.1",
16 "prox2" => "127.0.1.2",
17 "prox2-ring1" => "127.0.2.2",
18 "prox3" => "127.0.1.3",
19 "prox3-ring1" => "127.0.2.3",
20 "prox4" => "127.0.1.4",
21 "prox4-ring1" => "127.0.2.4",
22 };
23
24 sub mocked_resolve {
25 my ($hostname) = @_;
26
27 foreach my $host (keys %$known_hosts) {
28 return $known_hosts->{$host} if $hostname eq $host;
29 }
30
31 die "got unknown hostname '$hostname' during mocked resolve_hostname_like_corosync";
32 }
33
34 my $mocked_corosync_module = new Test::MockModule('PVE::Corosync');
35 $mocked_corosync_module->mock('resolve_hostname_like_corosync', \&mocked_resolve);
36
37 sub parser_self_check {
38 my $cfg_fn = shift;
39
40 my $outfile = "$cfg_fn.write";
41 my ($config1, $config2, $raw1, $raw2);
42
43 eval {
44 # read first time
45 $raw1 = PVE::Tools::file_get_contents($cfg_fn);
46 $config1 = PVE::Corosync::parse_conf($cfg_fn, $raw1);
47
48 # write config
49 $raw2 = PVE::Corosync::write_conf(undef, $config1);
50 # do not actually write cfg, but you can outcomment to do so, e.g. if
51 # you want to use diff for easy comparision
52 #PVE::Tools::file_set_contents($outfile, $raw2);
53
54 # reparse written config (must be the same as config1)
55 $config2 = PVE::Corosync::parse_conf(undef, $raw2);
56 }; warn $@ if $@;
57
58 # test verify_config
59 my ($err, $warn) = PVE::Corosync::verify_conf($config1);
60 die "verify_conf failed: " . join(" ++ ", @$err) if scalar(@$err);
61
62 # do not care for whitespace differences
63 delete $config1->{digest};
64 delete $config2->{digest};
65
66 is_deeply($config1, $config2, "self check hash: $cfg_fn");
67
68 # do not care about extra new lines
69 $raw1 =~ s/^\s*\n+//mg;
70 $raw2 =~ s/^\s*\n+//mg;
71
72 is($raw1, $raw2, "self check raw: $cfg_fn");
73 }
74
75 # exec tests
76 if (my $file = shift) {
77 parser_self_check($file);
78 } else {
79 foreach my $file (<corosync_configs/*.conf>) {
80 parser_self_check($file);
81 }
82 }
83
84 done_testing();