From 85bc5755c0bbb971ccaa1b0781a1b19d0192ca9d Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 9 Nov 2022 15:35:01 +0100 Subject: [PATCH] add section config tests Signed-off-by: Wolfgang Bumiller --- test/Makefile | 2 +- test/section_config_test.pl | 224 ++++++++++++++++++++++++++++++++++++ 2 files changed, 225 insertions(+), 1 deletion(-) create mode 100755 test/section_config_test.pl diff --git a/test/Makefile b/test/Makefile index 27489de..5c64867 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,7 +6,7 @@ all: export PERLLIB=../src -check: lock_file.test calendar_event_test.test convert_size_test.test procfs_tests.test format_test.test +check: lock_file.test calendar_event_test.test convert_size_test.test procfs_tests.test format_test.test section_config_test.test for d in $(SUBDIRS); do $(MAKE) -C $$d check; done %.test: %.pl diff --git a/test/section_config_test.pl b/test/section_config_test.pl new file mode 100755 index 0000000..f703fa4 --- /dev/null +++ b/test/section_config_test.pl @@ -0,0 +1,224 @@ +#!/usr/bin/perl + +use lib '../src'; + +package Conf; +use strict; +use warnings; + +use Test::More; + +use base qw(PVE::SectionConfig); + +my $defaultData = { + propertyList => { + type => { description => "Section type." }, + id => { + description => "ID", + type => 'string', + format => 'pve-configid', + maxLength => 64, + }, + common => { + type => 'string', + description => 'common value', + maxLength => 512, + }, + }, +}; + +sub private { + return $defaultData; +} + +sub expect_success { + my ($class, $filename, $expected, $raw, $allow_unknown) = @_; + + my $res = $class->parse_config($filename, $raw, $allow_unknown); + delete $res->{digest}; + + is_deeply($res, $expected, $filename); + + my $written = $class->write_config($filename, $res, $allow_unknown); + my $res2 = $class->parse_config($filename, $written, $allow_unknown); + delete $res2->{digest}; + + is_deeply($res, $res2, "$filename - verify rewritten data"); +} + +sub expect_fail { + my ($class, $filename, $expected, $raw) = @_; + + eval { $class->parse_config($filename, $raw) }; + die "test '$filename' succeeded unexpectedly\n" if !$@; + ok(1, "$filename should fail to parse"); +} + +package Conf::One; +use strict; +use warnings; + +use base 'Conf'; + +sub type { + return 'one'; +} + +sub properties { + return { + field1 => { + description => 'Field One', + type => 'integer', + minimum => 3, + maximum => 9, + }, + another => { + description => 'Another field', + type => 'string', + }, + }; +} + +sub options { + return { + common => { optional => 1 }, + field1 => {}, + another => { optional => 1 }, + }; +} + +package Conf::Two; +use strict; +use warnings; + +use base 'Conf'; + +sub type { + return 'two'; +} + +sub properties { + return { + field2 => { + description => 'Field Two', + type => 'integer', + minimum => 3, + maximum => 9, + }, + }; +} + +sub options { + return { + common => { optional => 1 }, + field2 => {}, + another => {}, + }; +} + +package main; + +use strict; +use warnings; + +use Test::More; + +Conf::One->register(); +Conf::Two->register(); +Conf->init(); + +local $SIG{__WARN__} = sub { die @_; }; + +my sub enum { + my $n = 1; + return { map { $_ => $n++ } @_ }; +} + +Conf->expect_success( + 'test1', + { + ids => { + t1 => { + type => 'one', + common => 'foo', + field1 => 3, + }, + t2 => { + type => 'one', + common => 'foo2', + field1 => 4, + another => 'more-text', + }, + t3 => { + type => 'two', + field2 => 5, + another => 'even more text', + }, + }, + order => { t1 => 1, t2 => 2, t3 => 3 }, + }, + <<"EOF"); +one: t1 + common foo + field1 3 + +one: t2 + common foo2 + field1 4 + another more-text + +two: t3 + field2 5 + another even more text +EOF + +my $with_unknown_data = { + ids => { + t1 => { + type => 'one', + common => 'foo', + field1 => 3, + }, + t2 => { + type => 'one', + common => 'foo2', + field1 => 4, + another => 'more-text', + }, + t3 => { + type => 'two', + field2 => 5, + another => 'even more text', + }, + invalid => { + type => 'bad', + common => 'omg', + }, + }, + order => enum(qw(t1 t2 invalid t3)), +}; +my $with_unknown_text = <<"EOF"; +one: t1 + common foo + field1 3 + +one: t2 + common foo2 + field1 4 + another more-text + +bad: invalid + common omg + +two: t3 + field2 5 + another even more text +EOF + +Conf->expect_fail('unknown-forbidden', $with_unknown_data, $with_unknown_text); +Conf->expect_success('unknown-allowed', $with_unknown_data, $with_unknown_text, 1); + + +done_testing(); + +1;