]> git.proxmox.com Git - pve-guest-common.git/blame - tests/abstract-config-tests.pl
tests: add print_pending_delete ones
[pve-guest-common.git] / tests / abstract-config-tests.pl
CommitLineData
fe824061
TL
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use Test::More;
9#use Test::MockModule;
10
11use PVE::AbstractConfig;
12
13
14# tests for different top level method implementations of AbstractConfig
15# tests need to specify the method, the parameter and expected result
16# for neatly doing more tests per single method you can specifiy a subtests
17# array, which then only has params and expected result
18
19# note that the indentaion level below is "wrong" by design
20my $tests = [
21{
22 method => 'parse_pending_delete',
23 subtests => [
24 {
25 params => [ "memory,cpu" ],
26 expect => {
27 cpu => { force => 0, },
28 memory => { force => 0, },
29 },
30 },
31 {
32 params => [ "memory;cpu,!mp0" ],
33 expect => {
34 cpu => { force => 0, },
35 memory => { force => 0, },
36 mp0 => { force => 1, },
37 },
38 },
39 {
40 params => [ " memory ; cpu, !mp0, !mp1" ],
41 expect => { # can separate with comma, semicolon, spaces
42 cpu => { force => 0, },
43 memory => { force => 0, },
44 mp0 => { force => 1, },
45 mp1 => { force => 1, },
46 },
47 },
48 {
49 params => [ "!!memory" ],
50 expect => { # we have no double negation, only simple stuff
51 '!memory' => { force => 1, },
52 },
53 },
54 {
55 params => [ " mem ory" ],
56 expect => { # we do not support keys with spaces, seens as two different ones
57 'mem' => { force => 0, },
58 'ory' => { force => 0, },
59 },
60 },
61 ]
62},
c29213f0
TL
63{
64 method => 'print_pending_delete',
65 subtests => [
66 {
67 params => [{
68 cpu => { force => 0, },
69 memory => { force => 0, },
70 }],
71 expect => "memory,cpu",
72 },
73 {
74 params => [{ # we have no double negation, only simple stuff
75 '!memory' => { force => 1, },
76 }],
77 expect => "!!memory",
78 },
79 ]
80},
fe824061
TL
81]; # tests definition end
82
83
84sub do_test($$;$) {
85 my ($method, $test, $name) = @_;
86
87 fail("incomplete test, params or expected missing")
88 if !exists $test->{params} || !exists $test->{expect};
89
90 my ($params, $expect) = $test->@{qw(params expect)};
91
92 my $res = eval { PVE::AbstractConfig->$method(@$params) };
93 if (my $err = $@) {
94 is ($err, $expect, $name);
95 } else {
96 is_deeply($res, $expect, $name);
97 }
98}
99
100for my $test (@$tests) {
101 my $method = $test->{method} or fail("missing AbstractConfig method to test") and next;
102
103 my $name = $test->{name} // $method;
104
105 if (defined(my $subtests = $test->{subtests})) {
106 subtest $name => sub {
107 for my $subtest (@$subtests) {
108 do_test($method, $subtest);
109 }
110 }
111 } else {
112 do_test($method, $test, $name);
113 }
114 #my $expected = $test->{expect};
115}
116
117done_testing();