public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH e2e-tests 2/6] lib: add function for drawing a random unused VMID
Date: Thu, 17 Sep 2026 18:23:20 +0200	[thread overview]
Message-ID: <20260917162324.1926056-3-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20260917162324.1926056-1-m.koeppl@proxmox.com>

`random_vmid` covers 2 problems:
- selecting a random VMID to avoid cases where /cluster/nextid returns
  a VMID that is still locked (e.g. from a cleanup process). So this
  mostly covers same-node runs.
- selecting the same VMID on parallel runs on different nodes of the
  same cluster. Drawing from a range of 9 million makes two runs
  picking the same VMID vanishingly unlikely.

PROXMOX_TEST_VMID_RANGE_START and PROXMOX_TEST_VMID_RANGE_END override
the range, so parallel runs can be given disjoint ranges to rule out a
collision entirely.

Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
 Proxmox/Test/Util.pm | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/Proxmox/Test/Util.pm b/Proxmox/Test/Util.pm
index 9b38924..43efd6b 100644
--- a/Proxmox/Test/Util.pm
+++ b/Proxmox/Test/Util.pm
@@ -3,12 +3,47 @@ use strict;
 use warnings FATAL => 'all';
 
 use parent 'Exporter';
-our @EXPORT_OK =
-    qw(poll_until retry skip_unsupported skip_prerequisite guest_name node_from_upid backup_target);
+our @EXPORT_OK = qw(random_vmid
+    poll_until
+    retry
+    skip_unsupported
+    skip_prerequisite
+    guest_name
+    node_from_upid
+    backup_target);
 
 use Time::HiRes qw(time sleep);
 use Test::More ();
 
+my ($vmid_range_start_default, $vmid_range_end_default) = (1_000_000, 9_999_999);
+
+# An unused VMID for a test guest, drawn at random from a range above the default next-id range.
+#
+# The VMID selection is random because there are cases where the freshly freed VMID cannot be used
+# right away, e.g. if qmeventd runs `qm cleanup`, holding a lock on the VM config for up to 30
+# seconds, making every guest re-created under the freshly freed VMID run into its own lock timeout.
+# This is of course not a strict guarantee that this cannot happen, but makes it highly unlikely.
+sub random_vmid {
+    my ($client) = @_;
+
+    my $vmid_range_start = $ENV{PROXMOX_TEST_VMID_RANGE_START} // $vmid_range_start_default;
+    my $vmid_range_end = $ENV{PROXMOX_TEST_VMID_RANGE_END} // $vmid_range_end_default;
+
+    my $span = $vmid_range_end - $vmid_range_start + 1;
+
+    for (1 .. 100) {
+        my $vmid = $vmid_range_start + int(rand($span));
+
+        if (!eval { $client->get('/cluster/nextid', { vmid => $vmid }) }) {
+            next;
+        }
+
+        return $vmid;
+    }
+
+    die "could not find a free VMID in range [$vmid_range_start, $vmid_range_end]\n";
+}
+
 # Poll $cb until it returns a truthy value or $timeout seconds elapse.
 #
 # Returns the truthy value. Dies on timeout. Exceptions thrown by $cb are treated as "not ready
-- 
2.47.3





  parent reply	other threads:[~2026-09-17 16:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17 16:23 [PATCH test-tools,e2e-tests 0/6] randomly select VMIDs for test Michael Köppl
2026-09-17 16:23 ` [PATCH test-tools 1/6] runner: add vmid-range option for test guest VMIDs Michael Köppl
2026-09-17 16:23 ` Michael Köppl [this message]
2026-09-17 16:23 ` [PATCH e2e-tests 3/6] storage-plugin: use random_vmid function for getting VMIDs Michael Köppl
2026-09-17 16:23 ` [PATCH e2e-tests 4/6] hw-validation: " Michael Köppl
2026-09-17 16:23 ` [PATCH e2e-tests 5/6] tests: " Michael Köppl
2026-09-17 16:23 ` [PATCH e2e-tests 6/6] storage-plugin: README: document the vmid-range option Michael Köppl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260917162324.1926056-3-m.koeppl@proxmox.com \
    --to=m.koeppl@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal