]> git.proxmox.com Git - proxmox-perl-rs.git/blob - pve-rs/test/resource_scheduling.pl
pve: test: resource scheduling: add test with overcommitted node
[proxmox-perl-rs.git] / pve-rs / test / resource_scheduling.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use PVE::RS::ResourceScheduling::Static;
9
10 sub test_basic {
11 my $static = PVE::RS::ResourceScheduling::Static->new();
12 is(scalar($static->list_nodes()->@*), 0, 'node list empty');
13 $static->add_node("A", 10, 100_000_000_000);
14 is(scalar($static->list_nodes()->@*), 1, '1 node added');
15 $static->add_node("B", 20, 200_000_000_000);
16 is(scalar($static->list_nodes()->@*), 2, '2nd node');
17 $static->add_node("C", 30, 300_000_000_000);
18 is(scalar($static->list_nodes()->@*), 3, '3rd node');
19 $static->remove_node("C");
20 is(scalar($static->list_nodes()->@*), 2, '3rd removed should be 2');
21 ok($static->contains_node("A"), 'should contain a node A');
22 ok($static->contains_node("B"), 'should contain a node B');
23 ok(!$static->contains_node("C"), 'should not contain a node C');
24 }
25
26 sub test_balance {
27 my $static = PVE::RS::ResourceScheduling::Static->new();
28 $static->add_node("A", 10, 100_000_000_000);
29 $static->add_node("B", 20, 200_000_000_000);
30
31 my $service = {
32 maxcpu => 4,
33 maxmem => 20_000_000_000,
34 };
35
36 for (my $i = 0; $i < 15; $i++) {
37 my $score_list = $static->score_nodes_to_start_service($service);
38
39 # imitate HA manager
40 my $scores = { map { $_->[0] => -$_->[1] } $score_list->@* };
41 my @nodes = sort {
42 $scores->{$a} <=> $scores->{$b} || $a cmp $b
43 } keys $scores->%*;
44
45 if ($i % 3 == 2) {
46 is($nodes[0], "A", 'first should be A');
47 is($nodes[1], "B", 'second should be A');
48 } else {
49 is($nodes[0], "B", 'first should be B');
50 is($nodes[1], "A", 'second should be A');
51 }
52
53 $static->add_service_usage_to_node($nodes[0], $service);
54 }
55 }
56
57 sub test_overcommitted {
58 my $static = PVE::RS::ResourceScheduling::Static->new();
59 $static->add_node("A", 4, 4_102_062_080);
60 $static->add_node("B", 4, 4_102_062_080);
61 $static->add_node("C", 4, 4_102_053_888);
62 $static->add_node("D", 4, 4_102_053_888);
63
64 my $service = {
65 maxcpu => 1,
66 maxmem => 536_870_912,
67 };
68
69 $static->add_service_usage_to_node("A", $service);
70 $static->add_service_usage_to_node("A", $service);
71 $static->add_service_usage_to_node("A", $service);
72 $static->add_service_usage_to_node("B", $service);
73 $static->add_service_usage_to_node("A", $service);
74
75 my $score_list = $static->score_nodes_to_start_service($service);
76
77 # imitate HA manager
78 my $scores = { map { $_->[0] => -$_->[1] } $score_list->@* };
79 my @nodes = sort {
80 $scores->{$a} <=> $scores->{$b} || $a cmp $b
81 } keys $scores->%*;
82
83 is($nodes[0], "C", 'first should be C');
84 is($nodes[1], "D", 'second should be D');
85 is($nodes[2], "B", 'third should be B');
86 is($nodes[3], "A", 'fourth should be A');
87 }
88
89 test_basic();
90 test_balance();
91 test_overcommitted();
92
93 done_testing();