From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id F27051FF0AA for ; Tue, 17 Nov 2026 11:47:49 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id D087721687; Tue, 17 Nov 2026 11:47:37 +0100 (CET) Message-ID: Date: Tue, 17 Nov 2026 11:47:26 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Subject: Re: [PATCH guest-common 09/34] abstract config: assign a guest identity and keep it on config writes To: Thomas Lamprecht , pve-devel@lists.proxmox.com References: <20261116004703.2908294-1-t.lamprecht@proxmox.com> <20261116004703.2908294-10-t.lamprecht@proxmox.com> Content-Language: en-US From: Dominik Csapak In-Reply-To: <20261116004703.2908294-10-t.lamprecht@proxmox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794912446759 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.292 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: YNGYF2KSEJHGIN7SP2TYUPYJET32IRFU X-Message-ID-Hash: YNGYF2KSEJHGIN7SP2TYUPYJET32IRFU X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: some comments inline On 11/16/26 1:50 AM, Thomas Lamprecht wrote: > With identities enabled for a guest type, every config write keeps the > identity that the guest already has, regardless of the identity in the > written config, and assigns a new one if the guest has none. This > covers new guests and guests from older versions without identity > handling in every operation. A stale config, a config copied from a > clone source, or a snapshot rollback can neither change nor duplicate > an identity that way. Taking over the uid of a backup needs a separate > check that no other guest in the cluster uses it. > > Read the existing identity directly from the cluster filesystem, as > the parsed config in the cache can be outdated, and abort the write on > read errors. A failed IPC connection can look like a missing or empty > file, so verify such a result against the mounted filesystem before > assigning an identity. A file that cannot be checked does not count as > missing. > > Some writers, including the container post-stop hook, do not hold the > guest config lock. Read the identity and write the config under a short > local lock, so that concurrent writers cannot assign different > identities. > > Guest types have to enable this explicitly: older qemu-server versions > validate 'meta' strictly on writes and would reject a config with the > identity members. > > Signed-off-by: Thomas Lamprecht > --- > src/PVE/AbstractConfig.pm | 111 ++++++- > src/tests/Makefile | 7 +- > src/tests/config-identity-tests.pl | 488 +++++++++++++++++++++++++++++ > 3 files changed, 603 insertions(+), 3 deletions(-) > create mode 100755 src/tests/config-identity-tests.pl > > diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm > index 7bcae19..e78b8cc 100644 > --- a/src/PVE/AbstractConfig.pm > +++ b/src/PVE/AbstractConfig.pm > @@ -3,6 +3,8 @@ package PVE::AbstractConfig; > use strict; > use warnings; > > +use Errno qw(ENOENT); > + > use PVE::Cluster; > use PVE::INotify; > use PVE::JSONSchema; > @@ -10,6 +12,7 @@ use PVE::Storage; > use PVE::Tools qw(lock_file lock_file_full); > > use PVE::GuestHelpers qw(typesafe_ne); > +use PVE::GuestIdentity; > use PVE::ReplicationConfig; > use PVE::Replication; > > @@ -61,13 +64,119 @@ sub load_config { > return $conf; > } > > +# Guest identity, see PVE::GuestIdentity > + > +# Whether writing a config keeps the published guest identity, or assigns one if there is none, > +# see write_config. Only enable it for a guest type whose config format accepts the identity > +# members, as a strict parser would otherwise reject every written config. > +sub __identity_enabled { > + my ($class) = @_; > + > + return 0; > +} > + > +# Returns the guest identity contained in the parsed config $conf, undef if there is none. Guest > +# types can override this and __identity_set for a different representation of 'meta' in parsed > +# configs, but the config file must have the identity in the top-level 'meta' property string, as > +# that is where the published identity gets read from. > +sub __identity_get { > + my ($class, $conf) = @_; > + > + return PVE::GuestIdentity::parse_identity($conf->{meta}); > +} > + > +# Sets the guest identity $identity in the parsed config $conf, keeping everything else. > +sub __identity_set { > + my ($class, $conf, $identity) = @_; > + > + $conf->{meta} = PVE::GuestIdentity::merge_identity($conf->{meta}, $identity); > +} > + > +# Returns the guest identity contained in the parsed config $conf, for example of a backup, undef > +# if there is none. > +sub get_identity { > + my ($class, $conf) = @_; > + > + return $class->__identity_get($conf); > +} > + > +# Runs $code with the local identity lock of $vmid held and returns its result. Every config write > +# takes it, also ones without the guest config lock like hooks, so never take another lock while > +# holding it. Tests can override it to run without access to the lock directory. a why not simply take the (simple) guest config lock? AFAICS this is only taken in the write_config path where we need to have the lock anyway? > +sub __identity_lock { > + my ($class, $vmid, $code, @param) = @_; > + > + my $lockdir = $PVE::GuestHelpers::lockdir; > + mkdir $lockdir; > + > + my $res = lock_file("$lockdir/pve-guest-identity-$vmid", 10, $code, @param); > + die $@ if $@; > + > + return $res; > +} > + > +# Returns undef if no config file exists for $vmid on this node, otherwise a hash with the identity > +# published in its top-level section, undef if there is none. Reads the file directly, as the > +# parsed configs in the file cache can be outdated. Dies on other read errors. Tests can override how can parsed configs in the file cache be outdated? wouldn't that break many other parts in the code if our config wouldn't be current? > +# it to run without the cluster filesystem. > +sub __identity_read_published { > + my ($class, $vmid) = @_; > + > + my $raw = PVE::Cluster::get_config($class->cfs_config_path($vmid)); > + if (!defined($raw) || $raw eq '') { > + # a failed connection to pmxcfs returns undef with errno left as it was, which can look > + # like a missing file (ENOENT) or an empty one (no error) > + my $path = $class->config_file($vmid); > + my $size = -s $path; > + # only a file known not to exist counts as missing, not one that cannot be checked > + die "unable to check config file '$path' for its guest identity - $!\n" > + if !defined($size) && $! != ENOENT; > + die "unable to read config file '$path' to check its guest identity\n" > + if defined($raw) ? $size : defined($size); > + return undef if !defined($raw); > + } > + > + my $meta; > + for my $line (split(/\n/, $raw)) { > + last if $line =~ m/^\[/; # snapshot or other section > + $meta = $1 if $line =~ m/^meta:\s*(.*?)\s*$/; this code implies the re could be multiple `meta: ` lines before a snapshot or section, but AFAIK this is not the case. so if $meta is defined we could skip reading the rest of the config? > + } > + > + return { identity => PVE::GuestIdentity::parse_identity($meta) }; > +} > + > # Generate and write config file for this VM/CT > +# > +# With guest identities enabled for the class, the identity published in the existing config file > +# gets set in $conf, whatever $conf contains, or a new one if none is published. So a config write > +# never changes a published identity. > sub write_config { > my ($class, $vmid, $conf) = @_; > > my $cfspath = $class->cfs_config_path($vmid); > > - PVE::Cluster::cfs_write_file($cfspath, $conf); > + if (!$class->__identity_enabled()) { > + PVE::Cluster::cfs_write_file($cfspath, $conf); > + return; > + } > + > + $class->__identity_lock( > + $vmid, > + sub { > + my $published = $class->__identity_read_published($vmid); > + my $identity = $published && $published->{identity}; > + > + # a published uid without valid rtime can only come from a manual edit > + if (!$identity || !defined($identity->{rtime})) { the check for rtime here can't ever work since $identity is a boolean? i think it's mean to be `!defined($published->{rtime})` , no ? > + $identity = PVE::GuestIdentity::new_identity($identity && $identity->{uid});\ same here, the $identity->{uid} access can't work... > + } > + > + $class->__identity_set($conf, $identity); > + PVE::Cluster::cfs_write_file($cfspath, $conf); > + }, > + ); > + > + return; > } > > # Pending changes related > diff --git a/src/tests/Makefile b/src/tests/Makefile > index 129a57e..ae66694 100644 > --- a/src/tests/Makefile > +++ b/src/tests/Makefile > @@ -1,12 +1,15 @@ > > all: > > -.PHONY: check abstract-config guest-identity hookscript > -check: abstract-config guest-identity hookscript > +.PHONY: check abstract-config config-identity guest-identity hookscript > +check: abstract-config config-identity guest-identity hookscript > > abstract-config: abstract-config-tests.pl > perl -I.. ./abstract-config-tests.pl > > +config-identity: config-identity-tests.pl > + perl -I.. ./config-identity-tests.pl > + > guest-identity: guest-identity-tests.pl > perl -I.. ./guest-identity-tests.pl > > diff --git a/src/tests/config-identity-tests.pl b/src/tests/config-identity-tests.pl > new file mode 100755 > index 0000000..92864ca > --- /dev/null > +++ b/src/tests/config-identity-tests.pl > @@ -0,0 +1,488 @@ > +#!/usr/bin/perl > + > +use strict; > +use warnings; > + > +use lib qw(..); > + > +use Fcntl qw(:flock); > +use File::Path qw(make_path); > +use File::Temp qw(tempdir); > +use Storable qw(dclone); > +use Test::MockModule; > +use Test::More; > + > +use PVE::AbstractConfig; > +use PVE::GuestHelpers; > +use PVE::GuestIdentity; > + > +my $tmpdir = tempdir('config-identity-XXXXXX', DIR => '.', CLEANUP => 1); > +$PVE::GuestHelpers::lockdir = "$tmpdir/lock"; > + > +# guest config classes using a minimal config file format, see below > + > +package PVE::TestConfig { > + use base qw(PVE::AbstractConfig); > + > + sub guest_type { return 'VM'; } > + > + sub config_file_lock { > + my ($class, $vmid) = @_; > + return "$tmpdir/config-$vmid.lock"; > + } > + > + sub cfs_config_path { > + my ($class, $vmid, $node) = @_; > + return "nodes/local/qemu-server/$vmid.conf"; > + } > + > + # only checked for existence, the fake cluster filesystem below does not create it > + sub config_file { > + my ($class, $vmid, $node) = @_; > + return "$tmpdir/etc-pve/" . $class->cfs_config_path($vmid, $node); > + } > + > + sub __identity_enabled { return 1; } > + > + # just enough for snapshot rollbacks without volumes > + sub valid_volume_keys { return (); } > + sub __snapshot_check_running { return 0; } > + sub __snapshot_rollback_vm_stop { return; } > + sub __snapshot_rollback_get_unused { return []; } > +} > + > +package PVE::TestConfig::CT { > + use base qw(PVE::TestConfig); > + > + sub guest_type { return 'CT'; } > + > + sub cfs_config_path { > + my ($class, $vmid, $node) = @_; > + return "nodes/local/lxc/$vmid.conf"; > + } > +} > + > +package PVE::TestConfig::NoIdentity { > + use base qw(PVE::TestConfig); > + > + sub __identity_enabled { return 0; } > +} > + > +# like the tests of a guest type that run without lock directory and cluster filesystem > +package PVE::TestConfig::Seams { > + use base qw(PVE::TestConfig); > + > + our $published = {}; > + > + sub __identity_lock { > + my ($class, $vmid, $code, @param) = @_; > + return $code->(@param); > + } > + > + sub __identity_read_published { > + my ($class, $vmid) = @_; > + return $published->{$vmid}; > + } > +} > + > +package main; > + > +# fake cluster filesystem with raw config files, indexed by their cfs path, and a file cache that > +# behaves like the one of PVE::Cluster: it only reads a config again after a write of this process, > +# or after cfs_update noticed a newer version > + > +my $files = {}; > +my $versions = {}; > +my $version_counter = 0; > +my $seen_versions = {}; > +my $cache = {}; > + > +our $get_config_error; > +our $check_identity_lock = 1; > + > +my $reads = 0; > +my $writes = 0; > + > +sub parse_test_config { > + my ($raw) = @_; > + > + my $conf = { snapshots => {} }; > + my $section = $conf; > + for my $line (split(/\n/, $raw)) { > + next if $line =~ m/^\s*$/; > + if ($line =~ m/^\[([^\]]+)\]$/) { > + $section = $conf->{snapshots}->{$1} = {}; > + } elsif ($line =~ m/^([a-z][a-z0-9_\-]*):\s*(.*?)\s*$/) { > + $section->{$1} = $2; > + } else { > + die "test error: unable to parse config line '$line'\n"; > + } > + } > + > + return $conf; > +} > + > +sub write_test_config { > + my ($conf) = @_; > + > + my $print_section = sub { > + my ($section) = @_; > + return join('', > + map { "$_: $section->{$_}\n" } grep { !ref($section->{$_}) } sort keys %$section); > + }; > + > + my $raw = $print_section->($conf); > + for my $snapname (sort keys(($conf->{snapshots} // {})->%*)) { > + $raw .= "\n[$snapname]\n" . $print_section->($conf->{snapshots}->{$snapname}); > + } > + > + return $raw; > +} > + > +sub vmid_of_path { > + my ($path) = @_; > + return $path =~ m!/(\d+)\.conf$! ? $1 : die "test error: unexpected path '$path'\n"; > +} > + > +sub identity_lock_held { > + my ($vmid) = @_; > + > + open(my $fh, '>>', "$PVE::GuestHelpers::lockdir/pve-guest-identity-$vmid") or return 0; > + my $free = flock($fh, LOCK_EX | LOCK_NB); > + close($fh); > + > + return !$free; > +} > + > +my $cluster_module = Test::MockModule->new('PVE::Cluster'); > +$cluster_module->redefine( > + cfs_update => sub { > + $seen_versions = { $versions->%* }; > + return; > + }, > + cfs_read_file => sub { > + my ($path) = @_; > + my $version = $seen_versions->{$path}; > + my $entry = $cache->{$path}; > + if (!$entry || !$entry->{version} || !$version || $entry->{version} != $version) { > + my $raw = $files->{$path}; > + my $data = defined($raw) ? parse_test_config($raw) : undef; > + $entry = $cache->{$path} = { version => $version, data => $data }; > + } > + return defined($entry->{data}) ? dclone($entry->{data}) : undef; > + }, > + cfs_write_file => sub { > + my ($path, $conf) = @_; > + my $vmid = vmid_of_path($path); > + die "test error: config of $vmid written without identity lock\n" > + if $check_identity_lock && !identity_lock_held($vmid); > + $writes++; > + $files->{$path} = write_test_config($conf); > + $versions->{$path} = ++$version_counter; > + $cache->{$path}->{version} = undef if $cache->{$path}; > + }, > + get_config => sub { > + my ($path) = @_; > + my $vmid = vmid_of_path($path); > + die "test error: config of $vmid read without identity lock\n" > + if !identity_lock_held($vmid); > + die $get_config_error if $get_config_error; > + $reads++; > + return $files->{$path}; > + }, > +); > + > +# deterministic identities: uids count up, the time is set by the tests > + > +my $uid_counter = 0; > +my $now = 1_700_000_000_000; > + > +my $identity_module = Test::MockModule->new('PVE::GuestIdentity'); > +$identity_module->redefine( > + __random_bytes => sub { > + my ($len) = @_; > + return pack('N', ++$uid_counter) . ("\0" x ($len - 4)); > + }, > + __time_ms => sub { return $now; }, > +); > + > +my sub new_uid { > + return PVE::GuestIdentity::new_identity()->{uid}; > +} > + > +my sub file_of { > + my ($class, $vmid) = @_; > + return $files->{ $class->cfs_config_path($vmid) }; > +} > + > +my sub conf_of { > + my ($class, $vmid) = @_; > + my $raw = file_of($class, $vmid); > + return defined($raw) ? parse_test_config($raw) : undef; > +} > + > +my sub identity_of { > + my ($class, $vmid) = @_; > + my $conf = conf_of($class, $vmid) or return undef; > + return PVE::GuestIdentity::parse_identity($conf->{meta}); > +} > + > +# write of another process, the file cache of this process does not notice it > +my sub set_file { > + my ($class, $vmid, $raw) = @_; > + my $path = $class->cfs_config_path($vmid); > + $files->{$path} = $raw; > + $versions->{$path} = ++$version_counter; > +} > + > +my $foreign_uid = new_uid(); > + > +subtest 'disabled by default' => sub { > + ok(!PVE::AbstractConfig->__identity_enabled(), 'abstract config does not enable it'); > + > + local $check_identity_lock = 0; > + my $class = 'PVE::TestConfig::NoIdentity'; > + my $reads_before = $reads; > + > + my $res = [$class->write_config(100, { name => 'a' })]; > + is(file_of($class, 100), "name: a\n", 'config without identity written as is'); > + is_deeply($res, [], 'nothing is returned'); > + > + my $meta = "ctime=1,rtime=5,uid=$foreign_uid"; > + $class->write_config(100, { name => 'a', meta => $meta }); > + is(file_of($class, 100), "meta: $meta\nname: a\n", 'carried identity written as is'); > + > + is($reads, $reads_before, 'published identity is not read'); > +}; > + > +subtest 'none published' => sub { > + my $class = 'PVE::TestConfig'; > + > + my $conf = { name => 'new' }; > + my $res = [$class->write_config(101, $conf)]; > + my $identity = identity_of($class, 101); > + ok($identity, 'new config gets an identity'); > + is($identity->{rtime}, $now, 'rtime is the current time'); > + is_deeply($class->get_identity($conf), $identity, 'identity is set in the written hash'); > + is_deeply($res, [], 'nothing is returned'); > + > + # copied from a clone source, a snapshot, or a backup > + $class->write_config(102, { name => 'clone', meta => "ctime=7,rtime=5,uid=$foreign_uid" }); > + $identity = identity_of($class, 102); > + isnt($identity->{uid}, $foreign_uid, 'carried identity is not published'); > + is($identity->{rtime}, $now, 'carried rtime is not published'); > + like( > + file_of($class, 102), > + qr/^meta: ctime=7,rtime=$now,uid=[a-z2-7]{20}$/m, > + 'other members are kept', > + ); > + > + set_file($class, 103, "meta: ctime=1\nname: old\n"); > + $class->write_config(103, conf_of($class, 103)); > + $identity = identity_of($class, 103); > + ok($identity, 'existing config without identity gets one'); > + like(file_of($class, 103), qr/^meta: ctime=1,rtime=$now,uid=/m, 'other members are kept'); > + > + set_file($class, 104, "name: old\n\n[snap]\nmeta: rtime=1,uid=$foreign_uid\n"); > + $class->write_config(104, conf_of($class, 104)); > + isnt(identity_of($class, 104)->{uid}, $foreign_uid, 'identity of a snapshot is ignored'); > + like( > + file_of($class, 104), > + qr/^\[snap\]\nmeta: rtime=1,uid=$foreign_uid$/m, > + 'snapshot keeps its copy', > + ); > + > + set_file($class, 105, ''); > + $class->write_config(105, { name => 'empty' }); > + ok(identity_of($class, 105), 'existing empty config gets an identity'); > +}; > + > +subtest 'published identity wins' => sub { > + my $class = 'PVE::TestConfig'; > + > + $class->write_config(110, { name => 'a', meta => 'ctime=1' }); > + my $published = identity_of($class, 110); > + my $stale = new_uid(); > + > + $now += 1000; > + > + my $tests = [ > + [{ name => 'b' }, 'missing identity'], > + [{ name => 'b', meta => 'ctime=1' }, 'missing identity members'], > + [{ name => 'b', meta => "ctime=1,rtime=5,uid=$stale" }, 'stale identity'], > + [{ name => 'b', meta => "ctime=1,rtime=$now,uid=$foreign_uid" }, 'foreign identity'], > + [{ name => 'b', meta => "ctime=1,rtime=5,uid=bogus" }, 'invalid identity'], > + [{ name => 'b', meta => "ctime=1,uid=$published->{uid}" }, 'identity without rtime'], > + ]; > + > + for my $test (@$tests) { > + my ($conf, $name) = @$test; > + my $other_members = $conf->{meta} && $conf->{meta} =~ m/ctime=1/; > + > + $class->write_config(110, $conf); > + is_deeply(identity_of($class, 110), $published, "$name: published identity is kept"); > + is_deeply($class->get_identity($conf), $published, "$name: hash has it too"); > + like(file_of($class, 110), qr/^meta: ctime=1,/m, "$name: other members are kept") > + if $other_members; > + } > + > + set_file($class, 111, "meta: ctime=1,uid=$stale\nname: old\n"); > + $class->write_config(111, conf_of($class, 111)); > + is_deeply( > + identity_of($class, 111), > + { uid => $stale, rtime => $now }, > + 'published uid without rtime is kept and gets the current time', > + ); > + > + my ($first, $last) = (new_uid(), new_uid()); > + set_file($class, 112, "meta: rtime=1,uid=$first\nname: x\nmeta: rtime=2,uid=$last\n"); > + $class->write_config(112, { name => 'y' }); > + is_deeply( > + identity_of($class, 112), > + { uid => $last, rtime => 2 }, > + 'last top-level meta line counts, like for the config parsers', > + ); > +}; > + > +subtest 'read and write under identity lock' => sub { > + my $class = 'PVE::TestConfig'; > + > + # the cluster filesystem mocks die without the lock, the lock is gone afterwards > + $class->write_config(120, { name => 'a' }); > + ok(identity_of($class, 120), 'written'); > + ok(!identity_lock_held(120), 'lock is released'); > + > + $class->lock_config(120, sub { $class->write_config(120, conf_of($class, 120)) }); > + ok(!identity_lock_held(120), 'lock is released when nested in config lock'); > +}; > + > +subtest 'read errors are fatal' => sub { > + my $class = 'PVE::TestConfig'; > + > + $class->write_config(130, { name => 'a' }); > + my $raw = file_of($class, 130); > + > + { > + local $get_config_error = "Connection refused\n"; > + eval { $class->write_config(130, { name => 'b' }) }; > + is($@, "Connection refused\n", 'write dies'); > + is(file_of($class, 130), $raw, 'config is unchanged'); > + > + eval { $class->write_config(131, { name => 'b' }) }; > + is($@, "Connection refused\n", 'write of new config dies'); > + ok(!defined(file_of($class, 131)), 'config is not written'); > + } > + > + # the read reports a missing file, but the mounted filesystem has it > + my $path = $class->config_file(132); > + make_path($path =~ s!/[^/]+$!!r); > + open(my $fh, '>', $path) or die "unable to create '$path' - $!\n"; > + close($fh); > + eval { $class->write_config(132, { name => 'b' }) }; > + like( > + $@, > + qr/^unable to read config file '\Q$path\E'/, > + 'inconsistent missing file is an error', > + ); > + ok(!defined(file_of($class, 132)), 'config is not written'); > + > + # the read reports an empty file, but the mounted filesystem has content > + set_file($class, 133, ''); > + $path = $class->config_file(133); > + open($fh, '>', $path) or die "unable to create '$path' - $!\n"; > + print $fh "name: a\n"; > + close($fh); > + eval { $class->write_config(133, { name => 'b' }) }; > + like( > + $@, > + qr/^unable to read config file '\Q$path\E'/, > + 'inconsistent empty file is an error', > + ); > + is(file_of($class, 133), '', 'config is not written'); > + > + # empty for both, a config without identity > + set_file($class, 134, ''); > + $path = $class->config_file(134); > + open($fh, '>', $path) or die "unable to create '$path' - $!\n"; > + close($fh); > + $class->write_config(134, { name => 'b' }); > + ok(identity_of($class, 134), 'empty file gets an identity'); > + > + # the mounted filesystem cannot be checked either, like while pmxcfs restarts > + my $not_a_dir = "$tmpdir/not-a-dir"; > + open($fh, '>', $not_a_dir) or die "unable to create '$not_a_dir' - $!\n"; > + close($fh); > + my $config_module = Test::MockModule->new('PVE::TestConfig', no_auto => 1); > + $config_module->redefine(config_file => sub { return "$not_a_dir/$_[1].conf"; }); > + > + eval { $class->write_config(135, { name => 'b' }) }; > + like( > + $@, > + qr/^unable to check config file '\Q$not_a_dir\E\/135\.conf' for its guest identity - /, > + 'missing file that cannot be checked is an error', > + ); > + ok(!defined(file_of($class, 135)), 'config is not written'); > + > + set_file($class, 136, ''); > + eval { $class->write_config(136, { name => 'b' }) }; > + like( > + $@, > + qr/^unable to check config file '\Q$not_a_dir\E\/136\.conf' for its guest identity - /, > + 'empty file that cannot be checked is an error', > + ); > + is(file_of($class, 136), '', 'config is not written'); > +}; > + > +subtest 'snapshot rollback' => sub { > + my $class = 'PVE::TestConfig'; > + > + my $storage_module = Test::MockModule->new('PVE::Storage'); > + $storage_module->redefine(config => sub { return {}; }); > + my $replication_module = Test::MockModule->new('PVE::ReplicationConfig'); > + $replication_module->redefine( > + new => sub { return bless({}, 'PVE::ReplicationConfig'); }, > + check_for_existing_jobs => sub { return 0; }, > + ); > + > + $class->write_config(140, { name => 'current', meta => 'ctime=2' }); > + my $published = identity_of($class, 140); > + > + my $conf = conf_of($class, 140); > + $conf->{snapshots}->{old} = { name => 'old', meta => "ctime=1,rtime=5,uid=$foreign_uid" }; > + $conf->{snapshots}->{none} = { name => 'none' }; > + $class->write_config(140, $conf); > + > + $now += 1000; > + > + for my $snapname (qw(old none)) { > + $class->snapshot_rollback(140, $snapname); > + $conf = conf_of($class, 140); > + is($conf->{name}, $snapname, "rolled back to '$snapname'"); > + is_deeply(identity_of($class, 140), $published, > + "'$snapname': current identity is kept"); > + } > + is( > + $conf->{snapshots}->{old}->{meta}, > + "ctime=1,rtime=5,uid=$foreign_uid", > + 'snapshot is unchanged', > + ); > +}; > + > +subtest 'overridden lock and published identity read' => sub { > + my $class = 'PVE::TestConfig::Seams'; > + local $check_identity_lock = 0; > + my $reads_before = $reads; > + > + $class->write_config(160, { name => 'a', meta => "rtime=5,uid=$foreign_uid" }); > + my $identity = identity_of($class, 160); > + isnt($identity->{uid}, $foreign_uid, 'nothing published: new identity'); > + > + local $PVE::TestConfig::Seams::published->{161} = { identity => $identity }; > + $class->write_config(161, { name => 'b' }); > + is_deeply(identity_of($class, 161), $identity, 'published identity is kept'); > + > + is($reads, $reads_before, 'cluster filesystem is not read'); > + ok(!-e "$PVE::GuestHelpers::lockdir/pve-guest-identity-160", 'no lock file is used'); > +}; > + > +done_testing();