]> git.proxmox.com Git - pve-guest-common.git/commitdiff
add basic abstract config test system
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 18 Oct 2019 14:42:36 +0000 (16:42 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 18 Oct 2019 15:48:03 +0000 (17:48 +0200)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Makefile
tests/Makefile [new file with mode: 0644]
tests/abstract-config-tests.pl [new file with mode: 0755]

index 47ac8a6f146a905738d5038dd2075eccf56be990..79653effa509789d24190f745b9164007f6481aa 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -43,6 +43,10 @@ install: PVE
        install -m 0644 PVE/VZDump/Plugin.pm ${PERL5DIR}/PVE/VZDump/
        install -m 0644 PVE/VZDump/Common.pm ${PERL5DIR}/PVE/VZDump/
 
+.PHONY: check
+check:
+       $(MAKE) -C tests check
+
 .PHONY: upload
 upload: ${DEB}
        tar cf - ${DEB} | ssh repoman@repo.proxmox.com -- upload --product pve --dist buster
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644 (file)
index 0000000..2fec185
--- /dev/null
@@ -0,0 +1,8 @@
+
+all:
+
+.PHONY: check abstract-config
+check: abstract-config
+
+abstract-config: abstract-config-tests.pl
+       perl -I.. ./abstract-config-tests.pl
diff --git a/tests/abstract-config-tests.pl b/tests/abstract-config-tests.pl
new file mode 100755 (executable)
index 0000000..f640171
--- /dev/null
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use Test::More;
+#use Test::MockModule;
+
+use PVE::AbstractConfig;
+
+
+# tests for different top level method implementations of AbstractConfig
+# tests need to specify the method, the parameter and expected result
+# for neatly doing more tests per single method you can specifiy a subtests
+# array, which then only has params and expected result
+
+# note that the indentaion level below is "wrong" by design
+my $tests = [
+{
+    method => 'parse_pending_delete',
+    subtests => [
+       {
+           params => [ "memory,cpu" ],
+           expect => {
+               cpu => { force => 0, },
+               memory => { force => 0, },
+           },
+       },
+       {
+           params => [ "memory;cpu,!mp0" ],
+           expect => {
+               cpu => { force => 0, },
+               memory => { force => 0, },
+               mp0 => { force => 1, },
+           },
+       },
+       {
+           params => [ "  memory ; cpu, !mp0, !mp1" ],
+           expect => { # can separate with comma, semicolon, spaces
+               cpu => { force => 0, },
+               memory => { force => 0, },
+               mp0 => { force => 1, },
+               mp1 => { force => 1, },
+           },
+       },
+       {
+           params => [ "!!memory" ],
+           expect => { # we have no double negation, only simple stuff
+               '!memory' => { force => 1, },
+           },
+       },
+       {
+           params => [ " mem ory" ],
+           expect => { # we do not support keys with spaces, seens as two different ones
+               'mem' => { force => 0, },
+               'ory' => { force => 0, },
+           },
+       },
+    ]
+},
+]; # tests definition end
+
+
+sub do_test($$;$) {
+    my ($method, $test, $name) = @_;
+
+    fail("incomplete test, params or expected missing")
+       if !exists $test->{params} || !exists $test->{expect};
+
+    my ($params, $expect) = $test->@{qw(params expect)};
+
+    my $res = eval { PVE::AbstractConfig->$method(@$params) };
+    if (my $err = $@) {
+       is ($err, $expect, $name);
+    } else {
+       is_deeply($res, $expect, $name);
+    }
+}
+
+for my $test (@$tests) {
+    my $method = $test->{method} or fail("missing AbstractConfig method to test") and next;
+
+    my $name = $test->{name} // $method;
+
+    if (defined(my $subtests = $test->{subtests})) {
+       subtest $name => sub {
+           for my $subtest (@$subtests) {
+               do_test($method, $subtest);
+           }
+       }
+    } else {
+       do_test($method, $test, $name);
+    }
+    #my $expected = $test->{expect};
+}
+
+done_testing();