add basic abstract config test system

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2019-10-18 16:42:36 +02:00
parent 550010301d
commit fe82406191
3 changed files with 111 additions and 0 deletions

View File

@ -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

8
tests/Makefile Normal file
View File

@ -0,0 +1,8 @@
all:
.PHONY: check abstract-config
check: abstract-config
abstract-config: abstract-config-tests.pl
perl -I.. ./abstract-config-tests.pl

99
tests/abstract-config-tests.pl Executable file
View File

@ -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();