[pve-devel] [RFC cluster 1/2] add simple corosync config parser self check

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Jun 26 14:10:57 CEST 2017


Each test reads and parses a config "writes" it again and then
re-parses it.
Then both the parsed hash structures and the raw config get compared
This is cheap and should catch simple regressions in either the
parser or writer, as currently we have no safety net that
modifications on either one didn't cause regressions.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 data/Makefile.am                               |  2 +-
 data/test/Makefile                             |  9 +++++
 data/test/corosync_configs/multiple-nodes.conf | 56 ++++++++++++++++++++++++++
 data/test/corosync_configs/single-node.conf    | 30 ++++++++++++++
 data/test/corosync_parser_test.pl              | 55 +++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 data/test/Makefile
 create mode 100644 data/test/corosync_configs/multiple-nodes.conf
 create mode 100644 data/test/corosync_configs/single-node.conf
 create mode 100755 data/test/corosync_parser_test.pl

diff --git a/data/Makefile.am b/data/Makefile.am
index ca180ca..51b1cd4 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -1,4 +1,4 @@
 
-SUBDIRS = src PVE
+SUBDIRS = src PVE test
 
 CLEANFILES = *~
diff --git a/data/test/Makefile b/data/test/Makefile
new file mode 100644
index 0000000..c3df52a
--- /dev/null
+++ b/data/test/Makefile
@@ -0,0 +1,9 @@
+all:
+
+.PHONY: check install clean distclean
+check:
+	./corosync_parser_test.pl
+
+install: check
+distclean: clean
+clean:
diff --git a/data/test/corosync_configs/multiple-nodes.conf b/data/test/corosync_configs/multiple-nodes.conf
new file mode 100644
index 0000000..a67cc0c
--- /dev/null
+++ b/data/test/corosync_configs/multiple-nodes.conf
@@ -0,0 +1,56 @@
+logging {
+  debug: off
+  to_syslog: yes
+}
+
+nodelist {
+  node {
+    name: prox1
+    nodeid: 1
+    quorum_votes: 1
+    ring0_addr: prox1
+    ring1_addr: prox1-ring1
+  }
+  node {
+    name: prox2
+    nodeid: 1
+    quorum_votes: 1
+    ring0_addr: prox2
+    ring1_addr: prox2-ring1
+  }
+  node {
+    name: prox3
+    nodeid: 1
+    quorum_votes: 1
+    ring0_addr: prox3
+    ring1_addr: prox3-ring1
+  }
+  node {
+    name: prox4
+    nodeid: 1
+    quorum_votes: 1
+    ring0_addr: prox4
+    ring1_addr: prox4-ring1
+  }
+}
+
+quorum {
+  provider: corosync_votequorum
+}
+
+totem {
+  cluster_name: cloud
+  config_version: 1
+  ip_version: ipv4
+  secauth: on
+  version: 2
+  interface {
+    bindnetaddr: 192.168.0.42
+    ringnumber: 0
+  }
+  interface {
+    bindnetaddr: 192.168.1.42
+    ringnumber: 1
+  }
+}
+
diff --git a/data/test/corosync_configs/single-node.conf b/data/test/corosync_configs/single-node.conf
new file mode 100644
index 0000000..7a23f9a
--- /dev/null
+++ b/data/test/corosync_configs/single-node.conf
@@ -0,0 +1,30 @@
+logging {
+  debug: off
+  to_syslog: yes
+}
+
+nodelist {
+  node {
+    name: prox1
+    nodeid: 1
+    quorum_votes: 1
+    ring0_addr: prox1
+  }
+}
+
+quorum {
+  provider: corosync_votequorum
+}
+
+totem {
+  cluster_name: cloud
+  config_version: 1
+  ip_version: ipv4
+  secauth: on
+  version: 2
+  interface {
+    bindnetaddr: 192.168.0.42
+    ringnumber: 0
+  }
+}
+
diff --git a/data/test/corosync_parser_test.pl b/data/test/corosync_parser_test.pl
new file mode 100755
index 0000000..18ee4f7
--- /dev/null
+++ b/data/test/corosync_parser_test.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+use lib '..';
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use PVE::Corosync;
+
+sub parser_self_check {
+    my $cfg_fn = shift;
+
+    my $outfile = "$cfg_fn.write";
+    my ($config1, $config2, $raw1, $raw2);
+
+    eval {
+	# read first time
+	$raw1 = PVE::Tools::file_get_contents($cfg_fn);
+	$config1 = PVE::Corosync::parse_conf($cfg_fn, $raw1);
+
+	# write config
+	$raw2 = PVE::Corosync::write_conf(undef, $config1);
+	# do not actually write cfg, but you can outcomment to do so, e.g. if
+	# you want to use diff for easy comparision
+	#PVE::Tools::file_set_contents($outfile, $raw2);
+
+	# reparse written config (must be the same as config1)
+	$config2 = PVE::Corosync::parse_conf(undef, $raw2);
+    }; warn $@ if $@;
+
+    # do not care for whitespace differences
+    delete $config1->{digest};
+    delete $config2->{digest};
+
+    is_deeply($config1, $config2, "self check hash: $cfg_fn");
+
+    # do not care about extra new lines
+    $raw1 =~ s/^\s*\n+//mg;
+    $raw2 =~ s/^\s*\n+//mg;
+
+    is($raw1, $raw2, "self check raw: $cfg_fn");
+}
+
+# exec tests
+if (my $file = shift) {
+    parser_self_check($file);
+} else {
+    foreach my $file (<corosync_configs/*.conf>) {
+	parser_self_check($file);
+    }
+}
+
+done_testing();
-- 
2.11.0





More information about the pve-devel mailing list