use Test::More for resource_scheduling tests

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-11-15 11:10:32 +01:00
parent 79552120c6
commit 221be779c4

View File

@ -3,6 +3,8 @@
use strict; use strict;
use warnings; use warnings;
use Test::More;
# FIXME ensure that the just built library is loaded rather than the installed one and add a test # FIXME ensure that the just built library is loaded rather than the installed one and add a test
# target to pve-rs/Makefile afterwards. Issue is that the loader looks into an $PATH/auto directory, # target to pve-rs/Makefile afterwards. Issue is that the loader looks into an $PATH/auto directory,
# so it's not enough to use lib qw(../target/release) # so it's not enough to use lib qw(../target/release)
@ -12,37 +14,19 @@ use warnings;
#use lib qw(../target/release); #use lib qw(../target/release);
use PVE::RS::ResourceScheduling::Static; use PVE::RS::ResourceScheduling::Static;
sub assert_num_eq {
my ($left, $right) = @_;
my (undef, undef, $line) = caller();
die "assertion failed: '$left != $right' at line $line\n" if $left != $right;
}
sub assert_str_eq {
my ($left, $right) = @_;
my (undef, undef, $line) = caller();
die "assertion failed: '$left ne $right' at line $line\n" if $left ne $right;
}
sub assert {
my ($bool) = @_;
my (undef, undef, $line) = caller();
die "assertion failed at line $line\n" if !$bool;
}
my $static = PVE::RS::ResourceScheduling::Static->new(); my $static = PVE::RS::ResourceScheduling::Static->new();
assert_num_eq(scalar($static->list_nodes()->@*), 0); is(scalar($static->list_nodes()->@*), 0, 'node list empty');
$static->add_node("A", 10, 100_000_000_000); $static->add_node("A", 10, 100_000_000_000);
assert_num_eq(scalar($static->list_nodes()->@*), 1); is(scalar($static->list_nodes()->@*), 1, '1 node added');
$static->add_node("B", 20, 200_000_000_000); $static->add_node("B", 20, 200_000_000_000);
assert_num_eq(scalar($static->list_nodes()->@*), 2); is(scalar($static->list_nodes()->@*), 2, '2nd node');
$static->add_node("C", 30, 300_000_000_000); $static->add_node("C", 30, 300_000_000_000);
assert_num_eq(scalar($static->list_nodes()->@*), 3); is(scalar($static->list_nodes()->@*), 3, '3rd node');
$static->remove_node("C"); $static->remove_node("C");
assert_num_eq(scalar($static->list_nodes()->@*), 2); is(scalar($static->list_nodes()->@*), 2, '3rd removed should be 2');
assert($static->contains_node("A")); ok($static->contains_node("A"), 'should contain a node A');
assert($static->contains_node("B")); ok($static->contains_node("B"), 'should contain a node B');
assert(!$static->contains_node("C")); ok(!$static->contains_node("C"), 'should not contain a node C');
my $service = { my $service = {
maxcpu => 4, maxcpu => 4,
@ -59,12 +43,14 @@ for (my $i = 0; $i < 15; $i++) {
} keys $scores->%*; } keys $scores->%*;
if ($i % 3 == 2) { if ($i % 3 == 2) {
assert_str_eq($nodes[0], "A"); is($nodes[0], "A", 'first should be A');
assert_str_eq($nodes[1], "B"); is($nodes[1], "B", 'second should be A');
} else { } else {
assert_str_eq($nodes[0], "B"); is($nodes[0], "B", 'first should be B');
assert_str_eq($nodes[1], "A"); is($nodes[1], "A", 'second should be A');
} }
$static->add_service_usage_to_node($nodes[0], $service); $static->add_service_usage_to_node($nodes[0], $service);
} }
done_testing();