From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH guest-common v7 09/24] tests: add tests for used-guest-ids max file size handling
Date: Mon, 5 Oct 2026 16:47:50 +0200 [thread overview]
Message-ID: <20261005144805.825538-10-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20261005144805.825538-1-m.koeppl@proxmox.com>
The test cases cover whether a file exceeding the thresholds is
correctly detected and also cover multiple scenarios for merging ranges
with gaps between them until file size is below the threshold again.
Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
src/tests/guest-id-tests.pl | 96 +++++++++++++++++++++++++++++++++++++
1 file changed, 96 insertions(+)
diff --git a/src/tests/guest-id-tests.pl b/src/tests/guest-id-tests.pl
index 5cf796b..f5091af 100755
--- a/src/tests/guest-id-tests.pl
+++ b/src/tests/guest-id-tests.pl
@@ -4,11 +4,16 @@ use v5.36;
use lib qw(..);
+use List::Util qw(min sum0);
use Test::MockModule;
use Test::More;
my $FILE = '/etc/pve/virtual-guest/used-guest-ids';
+# must match the thresholds in PVE::GuestID
+my $SIZE_WARN = 512 * 1024;
+my $SIZE_MAX = 768 * 1024;
+
my ($parse, $write);
my $raw; # contents of the mocked used-guest-ids file
my $writes = 0;
@@ -44,6 +49,38 @@ sub range_to_vmlist(@ids) {
return { ids => { map { $_ => { node => 'localhost', type => 'qemu' } } @ids } };
}
+# single IDs from 100 upwards, with the gaps between them cycling through @gaps
+sub single_ids($count, @gaps) {
+ my @lines;
+ my $id = 100;
+ for my $i (0 .. $count - 1) {
+ push @lines, "$id\n";
+ $id += 1 + $gaps[$i % scalar(@gaps)];
+ }
+ return join('', @lines);
+}
+
+sub gap_sizes($ranges) {
+ my @gap_sizes;
+ for my $i (1 .. $#$ranges) {
+ push @gap_sizes, $ranges->[$i]->[0] - $ranges->[$i - 1]->[1] - 1;
+ }
+ return @gap_sizes;
+}
+
+# whether every range in $inner lies within a range in $outer, both sorted
+sub covers($outer, $inner) {
+ my $i = 0;
+ for my $range ($inner->@*) {
+ while ($i < scalar(@$outer) && $outer->[$i]->[1] < $range->[0]) {
+ $i++;
+ }
+ return 0 if $i >= scalar(@$outer);
+ return 0 if $outer->[$i]->[0] > $range->[0] || $outer->[$i]->[1] < $range->[1];
+ }
+ return 1;
+}
+
subtest 'parse' => sub {
my $tests = [
['canonical file', "100-250\n300\n900-1200\n", [[100, 250], [300, 300], [900, 1200]]],
@@ -109,6 +146,65 @@ subtest 'write' => sub {
}
};
+subtest 'write size limit' => sub {
+ my sub write_with_warnings($input) {
+ my @warnings;
+ local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+ my $output = $write->($FILE, $parse->($FILE, $input));
+ return ($output, \@warnings);
+ }
+
+ my $small = single_ids(50_000, 1);
+ ok(length($small) < $SIZE_WARN, 'small list is below the warning threshold');
+ my ($output, $warnings) = write_with_warnings($small);
+ is($output, $small, 'below the warning threshold: written unchanged');
+ is_deeply($warnings, [], 'below the warning threshold: no warning');
+
+ my $medium = single_ids(90_000, 1);
+ ok(length($medium) > $SIZE_WARN && length($medium) < $SIZE_MAX,
+ 'medium list is in between');
+ ($output, $warnings) = write_with_warnings($medium);
+ is($output, $medium, 'above the warning threshold: written unchanged');
+ is(scalar($warnings->@*), 1, 'above the warning threshold: one warning');
+ like(
+ $warnings->[0],
+ qr/^\Q$FILE\E is \d+ KiB, .* closed beyond 768 KiB\n\z/,
+ 'above the warning threshold: size is warned about',
+ );
+
+ # above the hard limit, equal gaps
+ my $large = single_ids(150_000, 1);
+ ok(length($large) > $SIZE_MAX, 'large list is above the limit');
+ my $input = $parse->($FILE, $large);
+ ($output, $warnings) = write_with_warnings($large);
+ my $closed = $parse->($FILE, $output);
+ ok(length($output) <= $SIZE_MAX, 'above the limit: output fits');
+ ok(covers($closed, $input), 'above the limit: every used ID is still recorded');
+ is(scalar($warnings->@*), 1, 'above the limit: one warning');
+ my ($count) =
+ ($warnings->[0] // '') =~ m/would exceed 768 KiB, recorded (\d+) unused guest IDs? /;
+ is(
+ $count,
+ sum0(gap_sizes($input)) - sum0(gap_sizes($closed)),
+ 'above the limit: number of IDs marked as used is reported',
+ );
+
+ # above the hard limit, mixed gaps
+ my $mixed = single_ids(200_000, 1, 2, 5, 50);
+ $input = $parse->($FILE, $mixed);
+ ($output, $warnings) = write_with_warnings($mixed);
+ $closed = $parse->($FILE, $output);
+ my @remaining = gap_sizes($closed);
+ ok(length($output) <= $SIZE_MAX, 'mixed gaps: output fits');
+ ok(covers($closed, $input), 'mixed gaps: every used ID is still recorded');
+ is(min(@remaining), 5, 'mixed gaps: all gaps of 1 and 2 are closed first');
+ is(
+ scalar(grep { $_ == 50 } @remaining),
+ scalar(grep { $_ == 50 } gap_sizes($input)),
+ 'mixed gaps: the largest gaps are kept',
+ );
+};
+
subtest 'get_next_unused_id' => sub {
my $tests = [
# name, file contents, existing guests, input ID, expected ID
--
2.47.3
next prev parent reply other threads:[~2026-10-05 14:49 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-05 14:47 [PATCH many v7 00/24] add option to prevent suggesting previously used VMIDs Michael Köppl
2026-10-05 14:47 ` [PATCH cluster v7 01/24] cluster files: add virtual-guest/used-guest-ids Michael Köppl
2026-10-05 14:47 ` [PATCH cluster v7 02/24] datacenter config: add unique subproperty to next-id Michael Köppl
2026-10-05 14:47 ` [PATCH cluster v7 03/24] datacenter config: next-id: add enforce subproperty Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 04/24] src/makefile: order files alphabetically Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 05/24] add module to track previously used guest IDs Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 06/24] tests: add tests for used guest ID tracking Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 07/24] abstract config: register used guest ID when creating config Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 08/24] guest id: keep used ID list below the pmxcfs file size limit Michael Köppl
2026-10-05 14:47 ` Michael Köppl [this message]
2026-10-05 14:47 ` [PATCH guest-common v7 10/24] guest id: optionally enforce the next-id range and uniqueness Michael Köppl
2026-10-05 14:47 ` [PATCH guest-common v7 11/24] guest id: add helper to check guest IDs against next-id enforcement Michael Köppl
2026-10-05 14:47 ` [PATCH qemu-server v7 12/24] api: record VM ID as used on destruction and remote migration Michael Köppl
2026-10-05 14:47 ` [PATCH qemu-server v7 13/24] api, remote migrate: exempt existing VMs from next-id enforcement Michael Köppl
2026-10-05 14:47 ` [PATCH container v7 14/24] api: record CT ID as used on destruction and remote migration Michael Köppl
2026-10-05 14:47 ` [PATCH container v7 15/24] api, migrate: exempt existing CTs from next-id enforcement Michael Köppl
2026-10-05 14:47 ` [PATCH manager v7 16/24] ui: guest ID selector: rename exists flag to rejected Michael Köppl
2026-10-05 14:47 ` [PATCH manager v7 17/24] ui: guest ID selector: show the error returned by nextid Michael Köppl
2026-10-05 14:47 ` [PATCH manager v7 18/24] fix #4369: api: optionally only suggest unique IDs Michael Köppl
2026-10-05 14:48 ` [PATCH manager v7 19/24] ui: dc options: rename VMID to guest ID Michael Köppl
2026-10-05 14:48 ` [PATCH manager v7 20/24] fix #4369: ui: dc options: add option for unique VM/CT IDs Michael Köppl
2026-10-05 14:48 ` [PATCH manager v7 21/24] api: nextid: reject IDs forbidden by next-id enforcement Michael Köppl
2026-10-05 14:48 ` [PATCH manager v7 22/24] ui: dc options: add option to enforce next free guest ID settings Michael Köppl
2026-10-05 14:48 ` [PATCH docs v7 23/24] pmxcfs: files: add virtual-guest/used-guest-ids Michael Köppl
2026-10-05 14:48 ` [PATCH docs v7 24/24] pvecm: next-id: document unique and enforce options 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=20261005144805.825538-10-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