From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 2D9DA8E13F
 for <pve-devel@lists.proxmox.com>; Thu, 10 Nov 2022 15:38:15 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id EDCE62892A
 for <pve-devel@lists.proxmox.com>; Thu, 10 Nov 2022 15:38:13 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Thu, 10 Nov 2022 15:38:08 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 060BD44B87
 for <pve-devel@lists.proxmox.com>; Thu, 10 Nov 2022 15:38:06 +0100 (CET)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 10 Nov 2022 15:37:44 +0100
Message-Id: <20221110143800.98047-6-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20221110143800.98047-1-f.ebner@proxmox.com>
References: <20221110143800.98047-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.028 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH proxmox-perl-rs 2/2] add basic test for resource
 scheduling
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 10 Nov 2022 14:38:15 -0000

currently only used to test the installed version and not
automatically during build. See the FIXME note for why.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 pve-rs/test/Makefile               |  4 ++
 pve-rs/test/README                 |  2 +
 pve-rs/test/resource_scheduling.pl | 70 ++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 pve-rs/test/Makefile
 create mode 100644 pve-rs/test/README
 create mode 100755 pve-rs/test/resource_scheduling.pl

diff --git a/pve-rs/test/Makefile b/pve-rs/test/Makefile
new file mode 100644
index 0000000..dc0a5bd
--- /dev/null
+++ b/pve-rs/test/Makefile
@@ -0,0 +1,4 @@
+.PHONY: test
+test:
+	@echo "-- running pve-rs tests --"
+	./resource_scheduling.pl
diff --git a/pve-rs/test/README b/pve-rs/test/README
new file mode 100644
index 0000000..662b131
--- /dev/null
+++ b/pve-rs/test/README
@@ -0,0 +1,2 @@
+Note that these tests are not run during build currently! See the FIXME note in
+resource_scheduling.pl for why.
diff --git a/pve-rs/test/resource_scheduling.pl b/pve-rs/test/resource_scheduling.pl
new file mode 100755
index 0000000..b55843c
--- /dev/null
+++ b/pve-rs/test/resource_scheduling.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# 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,
+# so it's not enough to use lib qw(../target/release)
+# Also might be a good idea to test for existence of the files to avoid surprises if the directory
+# structure changes in the future.
+#use lib qw(..);
+#use lib qw(../target/release);
+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();
+assert_num_eq(scalar($static->list_nodes()->@*), 0);
+$static->add_node("A", 10, 100_000_000_000);
+assert_num_eq(scalar($static->list_nodes()->@*), 1);
+$static->add_node("B", 20, 200_000_000_000);
+assert_num_eq(scalar($static->list_nodes()->@*), 2);
+$static->add_node("C", 30, 300_000_000_000);
+assert_num_eq(scalar($static->list_nodes()->@*), 3);
+$static->remove_node("C");
+assert_num_eq(scalar($static->list_nodes()->@*), 2);
+assert($static->contains_node("A"));
+assert($static->contains_node("B"));
+assert(!$static->contains_node("C"));
+
+my $service = {
+    maxcpu => 4,
+    maxmem => 20_000_000_000,
+};
+
+for (my $i = 0; $i < 15; $i++) {
+    my $score_list = $static->score_nodes_to_start_service($service);
+
+    # imitate HA manager
+    my $scores = { map { $_->[0] => -$_->[1] } $score_list->@* };
+    my @nodes = sort {
+	$scores->{$a} <=> $scores->{$b} || $a cmp $b
+    } keys $scores->%*;
+
+    if ($i % 3 == 2) {
+	assert_str_eq($nodes[0], "A");
+	assert_str_eq($nodes[1], "B");
+    } else {
+	assert_str_eq($nodes[0], "B");
+	assert_str_eq($nodes[1], "A");
+    }
+
+    $static->add_service_usage_to_node($nodes[0], $service);
+}
-- 
2.30.2