From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 264391FF0B3 for ; Thu, 26 Nov 2026 16:12:48 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B28502190F; Thu, 26 Nov 2026 16:11:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neatech-ar.20260707.gappssmtp.com; s=20260707; t=1795705819; x=1796310619; darn=lists.proxmox.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/FhF3og5lmZs9QpJNMHKJzr62Vw0FC18oWgUtkAadqQ=; b=s77mOtRO18zxK0MVxZC+dWA9uzmLT+SEA7Ky6nN93qgv1uki6hDj7FulPBMJ/4nuA5 L0GUb3kXsOOCSvC1UGFt9CHzw2hd6/DXlE4Q/XP0ZOdHWQs6MkGkR+TfN4/Fet59kp5l D7uWKnWj/4TRI1iG5f1q89yEFjVRwHGvbIq87TqWQ7Kt9XOmJCRqv4wGWQazRcRiFLx2 NKr6K41zT/2Jngrb/zE1YdD4gPq9JaQ8mM8TOHBfxwoA+Mz32DcDjmZ6YZ29HOiCKHEF 2/rG5RKvMqXC0C7qtfdRPkdwVAJf5OcWQYRrEktMELDAa+DdHTbBfskTaBXrflOlpg0B X3Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795705819; x=1796310619; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/FhF3og5lmZs9QpJNMHKJzr62Vw0FC18oWgUtkAadqQ=; b=lZjEAE2/PzePqttzhDvgIsVSWesSmDYJh+y/XXEWXUJuf3/WvvFaOkvbQB3Y+LJSPA 5FDUWBs8NxSFC2zaLugyh6ewCVF/fVOTTvRzExzRU7O9z2mFLUr4HL3dScOQYgzQwu6K EXQz+mVgWemmTybJ4XZOrIyvf0tiA7V6otb6aNf0WO2gdssJ6GUHGzgsLfQbtqEHbY6T 24FeqwSR0d4owZ90ETku+3PuEU8lqUMbEad2o+t7h2QlmnmYyucY+qKELJVbHNlXbb+Y 5IMDDSSxWVxNWPONdzWCT2bMtnit1R3RFtRzUfOQPkFiXH399jsEF83/UIfq8EwPIWaS lHRQ== X-Gm-Message-State: APuU4Z/6BIYvHHdr8KoXh4dqjceDGY7v/mw8fv+vQ3/ZvOEAEBzWyHxi +eVJvjfXuFHY0mFXBjEhNyHlOoXuqd0lxZoVhxBgEFO349NcYU/BHwTwa9tYia2I+DSkxOIMbx1 F6z8IVdw= X-Gm-Gg: AZFdTZcYlGnM6Eg8JdvhwQlRauE0umPQKLuJgiPhafk1H12CWfghXbELnuEzPDBQIkp OxdGAgDJ92OZugHODFaXNwgRMwbVRIPAB70d7CHiPXchcAdVSrPXhEJu35yWwChxn7jbRNhjYod VECKixDGldVzx4kGLBaMLxU/w40sUPn9bA7/xvMKtb0eWiu873Vrnuh686NVArLD0aLH6XhvBi7 MsMBoo/G0NhNCvYc8UQ1Y48YKW21Dwn807o8+lsIWZ+3GlJ+ozlGAcScfEoEyBfjFP63onXAC/k PWlmZQG5jHc20TRV/YYleaOVPKClAokTspCr+bnRvJyelQCtOJ6nzKVSbuhNWiGmFKafhZnUoaW EXYko4vNr9thDjKj+TXkkAy6up+1P7izSHPXt0US+3fdoGUTd9Nr4CKSC7XguKsF1oM5oZfGysZ ECA4oD4bI1Xz0pV44+SD3nUtPjJrENaGyUYCi0YJSSCKWpe9LWwr/Cz/Sec3sy2ghlhqq4+nSfs +Zlo0ZTIeWlwhnybUnbqpMKpRQs4m2n0rh5E5f0gJBSbO2skFPQI4hwVza2+0igWv00+/yz717k 2m11jO4puzGiwMDiWv/AJwGkI/UY9NML8f51jjRfOfcOLTpEL6vkl6h/g8Cmhn/6JO2ORXDww1Y Z0Vn4sWY6UIX+c5qZnk/dg7OQir0svHV5mYHvnfuHjD9SUX0YXoY= X-Received: by 2002:a67:fe89:0:b0:7cc:174b:f7b7 with SMTP id ada2fe7eead31-7d71e3327f4mr13848568137.0.1795705817515; Thu, 26 Nov 2026 07:10:17 -0800 (PST) From: Joaquin Varela To: pve-devel@lists.proxmox.com Subject: [PATCH storage v5 11/13] test: zfsnvme: test the target flows Date: Thu, 26 Nov 2026 12:09:52 -0300 Message-ID: <20261126150956.310593-12-joaquinvarela@neatech.ar> X-Mailer: git-send-email 2.54.0.windows.1 In-Reply-To: <20261126150956.310593-1-joaquinvarela@neatech.ar> References: <20261126150956.310593-1-joaquinvarela@neatech.ar> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.055 Adjusted score from AWL reputation of From: address DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DMARC_PASS -0.1 DMARC pass policy POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes RCVD_IN_DNSWL_NONE -0.0001 Sender listed at https://www.dnswl.org/, no 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: 66QCCEEJ6XJOIZKA2PRYJG6GKTO3N2EA X-Message-ID-Hash: 66QCCEEJ6XJOIZKA2PRYJG6GKTO3N2EA X-MailFrom: joaquinvarela@neatech.ar 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: Run every target flow against FakeTarget, including interrupted ones, and check the identities, the exports and that other storages on the same target stay untouched. Signed-off-by: Joaquin Varela --- src/test/zfsnvme_target_test.pm | 1258 +++++++++++++++++++++++++++++++ 1 file changed, 1258 insertions(+) diff --git a/src/test/zfsnvme_target_test.pm b/src/test/zfsnvme_target_test.pm index a47a4d03..4ea7057b 100644 --- a/src/test/zfsnvme_target_test.pm +++ b/src/test/zfsnvme_target_test.pm @@ -2487,4 +2487,1262 @@ subtest 'template name and chunking' => sub { ); }; +my %ACT = ( + activate => sub { activate() }, + alloc => sub { $PLUGIN->alloc_image('st', scfg(), 200, 'raw', undef, 1024) }, + clone => sub { $PLUGIN->clone_image(scfg(), 'st', 'base-102-disk-0', 201) }, + free => sub { $PLUGIN->free_image('st', scfg(), 'vm-101-disk-0') }, + rollback => + sub { $PLUGIN->volume_snapshot_rollback(scfg(), 'st', 'vm-100-disk-0', 'snap1') }, + template => sub { $PLUGIN->create_base('st', scfg(), 'vm-101-disk-0') }, + resize => sub { $PLUGIN->volume_resize(scfg(), 'st', 'vm-100-disk-0', 2 * 1024**3) }, +); + +sub run_on($action, %opts) { + my $fake = lifecycle_fake(%opts); + return ($fake, flow($fake, $ACT{$action})); +} + +# An action (a name of %ACT or a sub) that is refused without changing the target. +sub refused($fake, $action, $error, $name) { + my $before = dclone($fake->{m}); + my $res = flow($fake, ref($action) ? $action : $ACT{$action}); + like($res->{error}, $error, "refuses $name"); + ok(!changes($res) && same($before, $fake->{m}), "without changes for $name"); + return $res; +} + +sub ns_of($fake, $nsid, $nqn = $NQN) { + return ($fake->{m}->{subsystems}->{$nqn} // {})->{ns}->{$nsid}; +} + +sub exported($fake, $nsid, $uuid, $dev) { + my $ns = ns_of($fake, $nsid) // return 0; + return $ns->{enable} eq '1' && $ns->{device_uuid} eq $uuid && $ns->{device_path} eq $dev; +} + +subtest 'path() is one read-only ZFS query' => sub { + my $fake = lifecycle_fake(); + my $res = flow($fake, sub { [$PLUGIN->path(scfg(), 'vm-100-disk-0', 'st')] }); + is($res->{error}, '', 'lookup succeeds'); + is_deeply( + $res->{result}, + ["/dev/disk/by-id/nvme-uuid.$U{1}", 100, 'vm-vol'], + 'the UUID link', + ); + is_deeply(ops($res), ['read ZFS inventory'], 'one call'); + is(scalar($res->{locks}->@*), 0, 'without the lock'); + $res = flow($fake, sub { [$PLUGIN->path(scfg(), 'vm-100-disk-0', 'st', 'snap1')] }); + like($res->{error}, qr/direct access to snapshots not implemented/, 'snapshots'); + is(scalar($res->{calls}->@*), 0, 'are refused locally'); +}; + +subtest 'a failed read is retried' => sub { + local $SLEPT = 0; + my $fake = lifecycle_fake(read_fault => { after => -1, mode => 'before' }); + my $res = flow($fake, sub { [$PLUGIN->path(scfg(), 'vm-100-disk-0', 'st')] }); + is($res->{error}, '', 'a read that fails once'); + is_deeply(ops($res), ['read ZFS inventory', 'read ZFS inventory'], 'is repeated'); + is($SLEPT, 0.2, 'after 200 ms'); + + $fake = lifecycle_fake(fail => [['zfs', 'get'], 'I/O error']); + $res = flow($fake, sub { [$PLUGIN->path(scfg(), 'vm-100-disk-0', 'st')] }); + is($res->{error}, "cannot read NVMe target state: I/O error\n", + 'a read that keeps failing'); + is(scalar($res->{calls}->@*), 3, 'is tried three times'); + + $fake = lifecycle_fake(read_fault => { after => -1, mode => 'unreachable' }); + $res = flow($fake, sub { [$PLUGIN->path(scfg(), 'vm-100-disk-0', 'st')] }); + like($res->{error}, qr/NVMe target '192\.0\.2\.10' is unreachable/, + 'an unreachable target'); + is(scalar($res->{calls}->@*), 1, 'is not tried again'); +}; + +subtest 'activate_volume' => sub { + my %forced; + my $activations = 0; + my $activation = sub($class, $storeid, $scfg, $cache = undef) { + $activations++; + $forced{$storeid} = $cache->{'zfsnvme-force-reconcile'}->{$storeid}; + die "activation attempted\n"; + }; + $plugin_mock->redefine(activate_storage => $activation); + + my $fake = lifecycle_fake(); + delete $fake->{m}->{subsystems}->{$NQN}->{ns}->{2}; + my $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-101-disk-0') }); + like($res->{error}, qr/activation attempted/, 'a missing namespace forces an activation'); + is($forced{st}, 1, 'the activation is forced past its fast path'); + is_deeply(ops($res), ['read target state'], 'after one read'); + ok(!$res->{calls}->[0]->{mutating} && !$res->{locks}->@*, 'without a change or the lock'); + + $fake = lifecycle_fake(); + ns_of($fake, 2)->{device_uuid} = $U{9}; + $activations = 0; + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-101-disk-0') }); + like( + $res->{error}, + qr/NVMe namespace ID '2' has a different identity/, + 'refuses an enabled namespace with another identity', + ); + is($activations, 0, 'without activating'); + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-101-disk-0', 'snap') }); + like($res->{error}, qr/unable to activate snapshot/, 'snapshots are refused'); + + # The local device: its udev link and whether it is the namespace. + my $ok = 1; + $plugin_mock->redefine(_nvmet_local_namespace_ok => sub(@args) { return $ok }); + my $link = "/dev/disk/by-id/nvme-uuid.$U{1}"; + local %BLOCK = ($link => 1); + $fake = target_fake(); + add_volume($fake, 'vm-100-disk-0', 1, $U{1}); + $activations = 0; + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-100-disk-0') }); + is($res->{error}, '', 'a converged volume activates'); + is_deeply(ops($res), ['read target state'], 'with one read'); + is(scalar($res->{locks}->@*) + $activations, 0, 'without lock or activation'); + + for my $case ( + ['disabled', sub($f) { ns_of($f, 1)->{enable} = '0' }], + ['absent', sub($f) { delete $f->{m}->{subsystems}->{$NQN}->{ns}->{1} }], + ['without subsystem', sub($f) { delete $f->{m}->{subsystems}->{$NQN} }], + [ + 'on the template name of the zvol', + sub($f) { ns_of($f, 1)->{device_path} = '/dev/zvol/tank/base-100-disk-0' }, + ], + [ + 'being built', + sub($f) { + $f->{m}->{subsystems}->{$NQN}->{ns}->{1} = ns_model($U{9}, '(null)', '0'); + }, + ], + ) { + my ($name, $change) = $case->@*; + $fake = target_fake(); + add_volume($fake, 'vm-100-disk-0', 1, $U{1}); + $change->($fake); + %forced = (); + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-100-disk-0') }); + is($forced{st}, 1, "a namespace that is $name on the target is exported again"); + } + + $fake = target_fake(); + add_volume($fake, 'vm-100-disk-0', 1, $U{1}); + $ok = 0; + $activations = 0; + $plugin_mock->redefine(activate_storage => sub(@args) { $activations++; return 1 }); + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-100-disk-0') }); + like( + $res->{error}, + qr/NVMe namespace for 'vm-100-disk-0' has an unexpected identity/, + 'a local device of another namespace is never used', + ); + is($activations, 1, 'after one forced activation'); + is_deeply(ops($res), ['read target state'], 'and one read'); + $plugin_mock->unmock('_nvmet_local_namespace_ok'); + + # While the device is missing, live controllers are rescanned. + my $file_mock = Test::MockModule->new('PVE::File'); + $file_mock->redefine( + dir_glob_foreach => sub($dir, $regex, $func) { + return if $dir ne '/sys/class/nvme'; + $func->($_) for qw(nvme7 nvme8 nvme9); + }, + ); + local %FILES = ( + '/sys/class/nvme/nvme7/subsysnqn' => $NQN, + '/sys/class/nvme/nvme7/state' => 'live', + '/sys/class/nvme/nvme8/subsysnqn' => $NQN, + '/sys/class/nvme/nvme8/state' => 'connecting', + '/sys/class/nvme/nvme9/subsysnqn' => $FOREIGN_NQN, + '/sys/class/nvme/nvme9/state' => 'live', + ); + $plugin_mock->redefine(activate_storage => sub(@args) { return 1 }); + $fake = lifecycle_fake(); + delete $fake->{m}->{subsystems}->{$NQN}->{ns}->{2}; + my $start = $NOW; + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-101-disk-0') }); + like( + $res->{error}, + qr/NVMe namespace for 'vm-101-disk-0' did not appear/, + 'a missing device', + ); + is($NOW - $start, 10, 'after 10 seconds'); + my $rescan = ['/sys/class/nvme/nvme7/rescan_controller', "1\n"]; + is_deeply( + $res->{sysfs_writes}, + [($rescan) x 5], + 'the live controller is rescanned every 2 s', + ); + is_deeply($res->{warnings}, [], 'without a warning'); + + # The stock file_write on files that fail: a failed rescan is a task warning with the errno + # text, and a controller that went away since it was listed needs none. + my $file_write = $sysfs_mock->original('file_write'); + my $dir = tempdir(CLEANUP => 1); + my @perl_warnings; + local $SIG{__WARN__} = sub($warning) { push @perl_warnings, $warning }; + for my $case ( + ['/dev/full', 'No space left on device'], + [$dir, 'Is a directory'], + ["$dir/gone/rescan_controller", undef], + ) { + my ($path, $reason) = $case->@*; + $sysfs_mock->redefine(file_write => sub($file, @args) { $file_write->($path, @args) }); + $res = flow($fake, sub { $PLUGIN->activate_volume('st', scfg(), 'vm-101-disk-0') }); + is_deeply( + $res->{warnings}, + defined($reason) ? [("cannot rescan NVMe controller 'nvme7': $reason") x 5] : [], + defined($reason) + ? "a failed rescan is a task warning with the errno text ($reason)" + : 'a controller that went away is not', + ); + } + is_deeply(\@perl_warnings, [], 'and file_write warns about none'); + $sysfs_mock->redefine(file_write => $sysfs_write); + $file_mock->unmock_all(); + $plugin_mock->unmock('activate_storage'); +}; + +subtest 'activation of an empty target' => sub { + my $fake = FakeTarget->new(pools => ['tank']); + $fake->add_zvol('tank/vm-100-disk-0', identity => [$NQN, 1, $U{1}]); + my $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'the target is configured'); + is_deeply( + [map { $_->{locked} } $res->{calls}->@*], + [0, 1, 1, 1, 1, 1], + 'read, then lock, read, apply, verify and publish', + ); + is_deeply( + [map { [$_->@{qw(name timeout)}] } $res->{locks}->@*], + [['zfsnvme-192.0.2.10', 30]], + 'under the domain lock of the target, waiting up to 30 seconds for it', + ); + my $m = $fake->{m}; + my $subsys = $m->{subsystems}->{$NQN}; + is_deeply( + $subsys->{attr}, + { + attr_model => $MODEL, + attr_serial => nv('_nvmet_serial', $NQN), + attr_allow_any_host => '0', + }, + 'the subsystem carries our marker', + ); + is_deeply($subsys->{acl}, { map { $_ => 1 } @HOSTS }, 'every host has an ACL'); + is_deeply([map { $m->{hosts}->{$_}->{key} } @HOSTS], [$KEY, $KEY], 'and the key'); + ok(exported($fake, 1, $U{1}, '/dev/zvol/tank/vm-100-disk-0'), 'the volume is exported'); + + $res = flow($fake, $ACT{activate}); + is_deeply(ops($res), ['read target state'], 'a converged target costs one read'); + ok(!$res->{locks}->@* && !$res->{calls}->[0]->{mutating}, 'without lock or change'); +}; + +subtest 'activation refusals change nothing' => sub { + for my $case ( + [ + 'a foreign subsystem at our NQN', + sub($f) { $f->add_subsystem($NQN, model => 'Other Storage', serial => 'abc') }, + qr/refusing to take over existing NVMe subsystem/, + ], + [ + 'a used subsystem with the default model', + sub($f) { + $f->add_host($HOSTS[0], $KEY); + $f->add_subsystem($NQN, model => 'Linux', serial => 'abc', acl => [$HOSTS[0]]); + }, + qr/refusing to take over existing NVMe subsystem/, + ], + [ + 'a duplicate UUID on a published target', + sub($f) { + $f->{m} = lifecycle_fake()->{m}; + $f->add_zvol('tank/vm-150-disk-0', identity => [$NQN, 7, $U{5}]); + $f->add_zvol('tank/vm-151-disk-0', identity => [$NQN, 8, $U{5}]); + }, + qr/duplicate namespace UUID '\Q@{[ $U{5} ]}\E'/, + ], + [ + 'an enabled namespace of another volume', + sub($f) { + $f->add_zvol('tank/vm-100-disk-0', identity => [$NQN, 1, $U{1}]); + $f->add_subsystem($NQN); + $f->add_namespace($NQN, 1, $U{9}, '/dev/zvol/tank/other'); + }, + qr/NVMe namespace ID '1' has a different identity/, + ], + ) { + my ($name, $setup, $error) = $case->@*; + my $fake = FakeTarget->new(pools => ['tank']); + $setup->($fake); + my $res = refused($fake, 'activate', $error, $name); + unlike($res->{error}, qr/DHHC-1/, 'without key material in the message'); + } +}; + +subtest 'nothing is published unless a fresh read converges' => sub { + my $acl = "$S/allowed_hosts/$HOSTS[1]"; + my $failing = sub($limit) { + my $failures = 0; + return sub($step, $call) { + return undef + if !step_is($step, 'ln', '-s') || $step->[3] ne $acl || $failures >= $limit; + $failures++; + return "ln: failed to create symbolic link '$acl': Invalid argument"; + }; + }; + my $fake = FakeTarget->new(pools => ['tank'], step_fault => $failing->(99)); + my $res = flow($fake, $ACT{activate}); + like( + $res->{error}, + qr/NVMe target did not converge: ln: failed to create symbolic link/, + 'a plan that fails twice stops', + ); + is( + scalar(grep { $_->{op} eq 'configure NVMe target' } $res->{calls}->@*), + 2, + 'in two rounds', + ); + is(scalar(grep { $_->{op} =~ /\Alink / } $res->{calls}->@*), 0, 'and publishes nothing'); + ok(!grep({ $_->{links}->%* } values $fake->{m}->{ports}->%*), + 'no port links the subsystem'); + + $fake = FakeTarget->new(pools => ['tank'], step_fault => $failing->(1)); + $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'a transient failure is repaired by the second round'); + is(scalar(grep { $_->{op} =~ /\Alink / } $res->{calls}->@*), 2, 'which then publishes'); + + $fake = FakeTarget->new(pools => ['tank']); + $fake->{m}->{mounted} = 0; + $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'configfs is mounted when it is missing'); + my @seen = grep { /mount/ } ops($res)->@*; + is_deeply(\@seen, ['read mounts', 'mount configfs'], 'after reading the mount table'); + is( + $res->{calls}->[-1]->{op}, + 'link NVMe subsystem on port 2', + 'and the target is published', + ); + + $fake = FakeTarget->new(pools => ['tank']); + $fake->{step_fault} = sub($step, $call) { + return step_is($step, 'env') ? "find: '$ROOT/hosts': Permission denied" : undef; + }; + $res = flow($fake, $ACT{activate}); + like( + $res->{error}, + qr/cannot read NVMe target state: find: '\Q$ROOT\E\/hosts': Permission denied/, + 'an unreadable, mounted configfs stops the activation', + ); + ok(!grep({ $_ eq 'mount configfs' } ops($res)->@*), 'without mounting'); + is(changes($res), 0, 'and without changes'); +}; + +subtest 'an interrupted subsystem creation' => sub { + # The chain that creates the subsystem stops after its mkdir or after the model write, for + # example when the remote shell is killed. Call 2 is the configuration, its outcome is + # unknown and the next activation repairs it. + my $marker = { + attr_model => $MODEL, + attr_serial => nv('_nvmet_serial', $NQN), + attr_allow_any_host => '0', + }; + my $completed = sub($fake, $what) { + is_deeply($fake->{m}->{subsystems}->{$NQN}->{attr}, $marker, "$what: our marker"); + ok( + exported($fake, 1, $U{1}, '/dev/zvol/tank/vm-100-disk-0'), + "$what: exports the volume", + ); + ok( + $fake->{m}->{ports}->{1}->{links}->{$NQN} + && $fake->{m}->{ports}->{2}->{links}->{$NQN}, + "$what: and publishes it", + ); + is_deeply($fake->{violations}, [], "$what: no protocol violation"); + }; + my $new = sub(%faults) { + my $fake = FakeTarget->new(pools => ['tank'], faults => {%faults}); + $fake->add_zvol('tank/vm-100-disk-0', identity => [$NQN, 1, $U{1}]); + return $fake; + }; + + # After the model write, only this plugin can have created the subsystem. + my $fake = $new->(2 => 'cut:2'); + my $res = flow($fake, $ACT{activate}); + like( + $res->{error}, + qr/'configure NVMe target' did not complete .* state is unknown\n\z/, + 'a cut after the model write', + ); + ok(!grep({ $_->{links}->%* } values $fake->{m}->{ports}->%*), 'is unpublished'); + $fake->{faults} = {}; + $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'the next activation completes the subsystem'); + my ($configure) = grep { $_->{op} eq 'configure NVMe target' } $res->{calls}->@*; + is_deeply( + $configure->{steps}->[0], + write_step("$S/attr_serial", $marker->{attr_serial}), + 'by writing the serial', + ); + $completed->($fake, 'next activation after the model write'); + + # Right after the mkdir, the subsystem looks like one of another tool. + $fake = $new->(2 => 'cut:1'); + $res = flow($fake, $ACT{activate}); + like($res->{error}, qr/state is unknown\n\z/, 'a cut right after the mkdir'); + $fake->{faults} = {}; + $res = flow($fake, $ACT{activate}); + like( + $res->{error}, + qr/refusing to take over existing NVMe subsystem .* not created by Proxmox VE/, + 'is refused by the next activation', + ); + is($fake->{m}->{subsystems}->{$NQN}->{attr}->{attr_model}, 'Linux', + 'the subsystem is kept'); + ok(!grep({ $_->{links}->%* } values $fake->{m}->{ports}->%*), 'and unpublished'); + delete $fake->{m}->{subsystems}->{$NQN}; + $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'once an administrator removed it, the next activation creates it'); + $completed->($fake, 'activation after the removal'); +}; + +subtest 'quorum and reachability' => sub { + my $fake = FakeTarget->new(pools => ['tank']); + local $QUORATE = 0; + local @QUORUM = (); + my $res = flow($fake, $ACT{activate}); + like($res->{error}, qr/cluster not quorate - refusing NVMe target changes/, 'no quorum'); + is_deeply(\@QUORUM, [1], 'with a quorum check that does not wait'); + is(scalar($res->{locks}->@*), 0, 'before requesting the lock'); + is_deeply(ops($res), ['read target state'], 'after the lockless read'); + $QUORATE = 1; + + $fake->{unreachable} = 1; + $res = flow($fake, $ACT{activate}); + like( + $res->{error}, + qr/NVMe target '192\.0\.2\.10' is unreachable: ssh: connect/, + 'an unreachable target fails in the lockless read', + ); + ok(!$res->{locks}->@* && $res->{calls}->@* == 1, 'without lock or retry'); + + $fake = lifecycle_fake(); + local $QUORATE = 0; + $res = flow($fake, $ACT{activate}); + is($res->{error}, '', 'a converged target needs no quorum'); +}; + +subtest 'create' => sub { + my ($fake, $res) = run_on('alloc'); + is($res->{error}, '', 'allocation succeeds'); + is($res->{result}, 'vm-200-disk-0', 'with the next free name'); + is_deeply( + [map { $_->{locked} } $res->{calls}->@*], + [0, 1, 1, 1, 1, 1, 1], + 'the name lookup without the lock, the rest under it', + ); + my $uuid = $fake->{m}->{ds}->{'tank/vm-200-disk-0'}->{props}->{'proxmox:nvme-uuid'}; + like($uuid, $UUID_RE, 'a new UUID'); + ok(exported($fake, 4, $uuid, '/dev/zvol/tank/vm-200-disk-0'), 'exported at the next NSID'); + + my $thick = scfg(); + delete $thick->@{qw(sparse blocksize)}; + $fake = lifecycle_fake(); + $res = + flow($fake, sub { $PLUGIN->alloc_image('st', $thick, 200, 'raw', 'vm-200-disk-1', 4000) }); + is($res->{result}, 'vm-200-disk-1', 'an explicit name is used'); + my ($create) = grep { $_->{op} eq 'create zvol' } $res->{calls}->@*; + is_deeply( + [grep { /\A(?:-[sb]|[0-9]+k)\z/ } $create->{steps}->[0]->@*], + ['4096k'], + 'the size is rounded up to KiB; without sparse and blocksize the zvol is thick', + ); + + ($fake, $res) = run_on('alloc', udev_delay => 3); + is($res->{error}, '', 'a slow udev'); + is(scalar(grep { $_ eq 'wait for zvol' } ops($res)->@*), 4, 'adds polls'); + + local @UUIDS = ($U{1}, $U{9}); + ($fake, $res) = run_on('alloc'); + is( + $fake->{m}->{ds}->{'tank/vm-200-disk-0'}->{props}->{'proxmox:nvme-uuid'}, + $U{9}, + 'a UUID that is in use is drawn again', + ); + local @UUIDS = ($U{1}) x 10; + ($fake, $res) = run_on('alloc'); + is($res->{error}, "cannot generate an unused namespace UUID\n", 'but not forever'); + is(changes($res), 0, 'and nothing changes'); + @UUIDS = (); + + ($fake, $res) = run_on('alloc', faults => { 3 => 'after' }); + like( + $res->{error}, + qr/'create zvol' did not complete .*state is unknown\n\z/, + 'a lost reply aborts even when the create reached the target', + ); + is($res->{calls}->[-1]->{op}, 'create zvol', 'no read tries to settle it'); + ok(!ns_of($fake, 4), 'the abandoned operation does not export it'); + $fake->{faults} = {}; + $res = flow($fake, $ACT{activate}); + ok(!$res->{error} && ns_of($fake, 4), 'the next activation exports the owned zvol'); + + ($fake, $res) = run_on('alloc', faults => { 5 => 'before' }); + like($res->{error}, qr/'export namespace' failed: injected failure/, 'a failed export'); + ok(!$fake->{m}->{ds}->{'tank/vm-200-disk-0'} && !ns_of($fake, 4), 'destroys the new zvol'); + is($res->{locks}->@* + $res->{nested}->@*, 1, 'the compensation reuses the lock'); + + # The export may still run on the target after its connection broke. + ($fake, $res) = run_on('alloc', faults => { 5 => 'after' }); + like( + $res->{error}, + qr/\A.*'export namespace' did not complete \(Connection.*state is unknown\n\z/, + 'a lost reply of the export leaves the state unknown', + ); + ok(!grep({ /destroy|remove/ } ops($res)->@*), 'and is not compensated'); + ok($fake->{m}->{ds}->{'tank/vm-200-disk-0'} && ns_of($fake, 4), 'so the disk is kept'); + + ($fake, $res) = run_on( + 'alloc', + before_call => sub($f, $call) { + $f->add_zvol('tank/vm-777-disk-0', identity => [$NQN, 4, $U{9}]) + if $call->{index} == 6 && !$f->{m}->{ds}->{'tank/vm-777-disk-0'}; + }, + ); + like( + $res->{error}, + qr/duplicate NVMe identity on 'tank\/vm-200-disk-0'/, + 'a duplicate that appears before the verify read is refused', + ); + ok( + !$fake->{m}->{ds}->{'tank/vm-200-disk-0'} && !ns_of($fake, 4), + 'the new zvol is destroyed', + ); + ok($fake->{m}->{ds}->{'tank/vm-777-disk-0'}, 'the other zvol is kept'); + + ($fake, $res) = run_on( + 'alloc', + before_call => sub($f, $call) { + my $ds = $f->{m}->{ds}->{'tank/vm-200-disk-0'}; + $ds->{props}->{'proxmox:nvme-uuid'} = $U{9} if $ds && $call->{index} == 6; + }, + ); + is( + $res->{error}, + "NVMe identity of zvol 'tank/vm-200-disk-0' changed\n", + 'an identity that changes before the verify read is refused', + ); + ok(!ns_of($fake, 4), 'the new namespace is removed'); + ok($fake->{m}->{ds}->{'tank/vm-200-disk-0'}, 'but a zvol without our identity is kept'); + + # A namespace of another zvol that was renamed away while exported uses + # the device path of the new zvol: only what this call built is removed. + ($fake, $res) = run_on( + 'alloc', + faults => { 5 => 'before' }, + before_call => sub($f, $call) { + $f->add_namespace($NQN, 9, $U{9}, '/dev/zvol/tank/vm-200-disk-0') + if $call->{index} == 0; + }, + ); + like($res->{error}, qr/'export namespace' failed: injected failure/, 'a failed export'); + ok( + exported($fake, 9, $U{9}, '/dev/zvol/tank/vm-200-disk-0'), + 'keeps an enabled namespace of another UUID with the same device path', + ); + ok( + !grep({ grep { write_to($_, '/namespaces/9/enable') } $_->{steps}->@* } + $res->{calls}->@*), + 'without touching it', + ); + + ($fake, $res) = run_on( + 'alloc', + faults => { 5 => 'before' }, + fail => [['zfs', 'destroy'], "cannot destroy 'tank/vm-200-disk-0': dataset is busy"], + ); + like( + $res->{error}, + qr/'export namespace' failed: injected failure/, + 'keeps the first error', + ); + my $cleanup = qr/\Afailed to clean up zvol 'vm-200-disk-0': NVMe target operation/; + is( + scalar(grep { /$cleanup 'destroy zvol' failed: cannot destroy/ } $res->{warnings}->@*), + 1, + 'a failed cleanup is reported', + ); + + # ssh gives up on a call, which the target still runs to its end later + ($fake, $res) = run_on('alloc', faults => { 3 => 'late' }); + is( + $res->{error}, + "NVMe target operation 'create zvol' did not complete (timeout);" + . " the target state is unknown\n", + 'a create that did not complete in time', + ); + is($res->{calls}->[-1]->{op}, 'create zvol', 'is not followed by anything'); + my $second = + flow($fake, sub { $PLUGIN->alloc_image('st', scfg(), 201, 'raw', undef, 1024) }); + is($second->{error}, '', 'another allocation meanwhile'); + $fake->land; + is_deeply( + [map { owned_volumes($fake->{m})->{"tank/vm-$_-disk-0"}->[0] } 200, 201], + [4, 5], + 'never gets the NSID of the create that completes afterwards', + ); + $res = flow($fake, $ACT{activate}); + ok( + !$res->{error} && ns_of($fake, 4) && ns_of($fake, 5), + 'and the next activation exports both', + ); + + ($fake, $res) = run_on( + 'alloc', + before_call => + sub($f, $call) { die "received interrupt\n" if $call->{op} eq 'export namespace' }, + ); + is($res->{error}, "received interrupt\n", 'a stopped task'); + ok(!grep({ /destroy|remove/ } ops($res)->@*), 'is not compensated either'); + + ($fake, $res) = run_on('alloc', read_fault => { after => 5, mode => 'failed' }); + is( + $res->{error}, + "NVMe target operation 'read target state' did not complete (ssh failed);" + . " the target state is unknown\n", + 'a read under the lock that ssh did not run to its end', + ); + ok(!grep({ /destroy|remove/ } ops($res)->@*), 'is not followed by a compensation'); + + $fake = lifecycle_fake(); + $fake->add_zvol('tank/vm-200-disk-0'); + my $named = sub { $PLUGIN->alloc_image('st', scfg(), 200, 'raw', 'vm-200-disk-0', 1024) }; + refused($fake, $named, qr/zvol 'tank\/vm-200-disk-0' already exists/, 'an existing zvol'); + $fake = lifecycle_fake(); + delete $fake->{m}->{subsystems}->{$NQN}; + refused( + $fake, + 'alloc', + qr/NVMe subsystem does not exist; activate the storage first/, + 'an allocation without the subsystem', + ); + $res = + flow($fake, sub { $PLUGIN->alloc_image('st', scfg(), 200, 'raw', 'vm-201-disk-0', 1024) }); + like($res->{error}, qr/illegal name 'vm-201-disk-0'/, 'names must belong to the VM'); +}; + +subtest 'clone' => sub { + my ($fake, $res) = run_on('clone'); + is($res->{error}, '', 'a linked clone'); + is($res->{result}, 'base-102-disk-0/vm-201-disk-0', 'is named after its base'); + my $clone = $fake->{m}->{ds}->{'tank/vm-201-disk-0'}; + is($clone->{origin}, 'tank/base-102-disk-0@__base__', 'from the base snapshot'); + my $uuid = $clone->{props}->{'proxmox:nvme-uuid'}; + ok(exported($fake, 4, $uuid, '/dev/zvol/tank/vm-201-disk-0'), 'with its own identity'); + + $fake = lifecycle_fake(); + $res = + flow($fake, sub { $PLUGIN->clone_image(scfg(), 'st', 'base-102-disk-0', 201, 'missing') }); + like( + $res->{error}, + qr/'create zvol' failed: cannot open 'tank\/base-102-disk-0\@missing'/, + 'a missing snapshot', + ); + ok(!$fake->{m}->{ds}->{'tank/vm-201-disk-0'}, 'leaves nothing behind'); + $res = flow($fake, sub { $PLUGIN->clone_image(scfg(), 'st', 'base-102-disk-0', 201, '') }); + is($res->{error}, '', 'an empty snapshot name'); + is( + $fake->{m}->{ds}->{'tank/vm-201-disk-0'}->{origin}, + 'tank/base-102-disk-0@__base__', + 'means the base snapshot', + ); + + for my $case ( + ['basevol-102-disk-0', qr/clone_image requires a ZFS volume/], + ['vm-100-disk-0', qr/clone_image only works on base images/], + ) { + $res = flow($fake, sub { $PLUGIN->clone_image(scfg(), 'st', $case->[0], 201) }); + like($res->{error}, $case->[1], "refuses $case->[0]"); + is(scalar($res->{calls}->@*), 0, 'locally'); + } + $res = + flow($fake, sub { $PLUGIN->clone_image(scfg(), 'st', 'base-102-disk-0', 201, 'a b') }); + like($res->{error}, qr/invalid snapshot name/, 'refuses a malformed snapshot name'); +}; + +subtest 'destroy' => sub { + my $dev = '/dev/zvol/tank/vm-101-disk-0'; + my ($fake, $res) = run_on('free'); + is($res->{error}, '', 'a volume is freed'); + is_deeply(ops($res), ['read target state', 'destroy zvol'], 'in two calls'); + ok(!$fake->{m}->{ds}->{'tank/vm-101-disk-0'} && !ns_of($fake, 2), 'both are gone'); + $res = flow($fake, $ACT{free}); + is($res->{error}, '', 'a missing volume is already freed'); + is(changes($res), 0, 'without changes'); + + for my $case ( + [ + 'a volume that is not owned', + 'vm-900-disk-0', + undef, + qr/is not owned by NVMe subsystem/, + ], + [ + 'a UUID exported at another NSID', + 'vm-101-disk-0', + sub($f) { $f->add_namespace($NQN, 9, $U{2}, '/dev/zvol/tank/x', 0) }, + qr/namespace UUID '\Q@{[ $U{2} ]}\E' is already in use/, + ], + [ + 'another identity at its NSID', + 'vm-101-disk-0', + sub($f) { ns_of($f, 2)->{device_uuid} = $U{9} }, + qr/NVMe namespace ID '2' has a different identity/, + ], + [ + 'a duplicate identity', + 'vm-101-disk-0', + sub($f) { $f->add_zvol('tank/vm-104-disk-0', identity => [$NQN, 2, $U{9}]) }, + qr/duplicate NVMe identity/, + ], + ) { + my ($name, $volume, $change, $error) = $case->@*; + $fake = lifecycle_fake(); + $change->($fake) if $change; + refused($fake, sub { $PLUGIN->free_image('st', scfg(), $volume) }, $error, $name); + } + $res = flow($fake, sub { $PLUGIN->free_image('st', scfg(), 'subvol-100-disk-0') }); + like($res->{error}, qr/free_image requires a ZFS volume/, 'subvolumes are refused'); + + my $busy = 'Device or resource busy'; + ($fake, $res) = + run_on('free', fail => [{ write => '/namespaces/2/enable', value => 0 }, $busy]); + like($res->{error}, qr/cannot disable namespace '2': .*busy/, 'a failed disable'); + ok(exported($fake, 2, $U{2}, $dev), 'keeps the namespace'); + + ($fake, $res) = run_on('free', fail => [['rmdir', "$S/namespaces/2"], $busy]); + like( + $res->{error}, + qr/cannot remove namespace '2': Device or resource busy\n\z/, + 'a failed rmdir', + ); + ok(exported($fake, 2, $U{2}, $dev), 're-enables the namespace'); + is($res->{calls}->[-1]->{op}, 'enable namespace', 'from a fresh read'); + + my $rmdir_and_enable = sub($s) { + step_is($s, 'rmdir', "$S/namespaces/2") || write_to($s, '/namespaces/2/enable', 1); + }; + ($fake, $res) = run_on('free', fail => [$rmdir_and_enable, $busy]); + like( + $res->{error}, + qr/cannot remove namespace '2': .*; cannot restore namespace: /, + 'a failed restore is reported too', + ); + + my $destroy = ['zfs', 'destroy', '-r', 'tank/vm-101-disk-0']; + local $SLEPT = 0; + ($fake, $res) = run_on('free', fail => [$destroy, $busy]); + like( + $res->{error}, + qr/\Afailed to destroy 'tank\/vm-101-disk-0': Device or resource busy; namespace restored/, + 'a zvol that stays busy', + ); + is(scalar(grep { $_ eq 'destroy zvol' } ops($res)->@*), 6, 'is destroyed six times'); + is($SLEPT, 5, 'a second apart'); + ok(exported($fake, 2, $U{2}, $dev), 'and its namespace is restored'); + + ($fake, $res) = run_on('free', fail => [$destroy, $busy, 3]); + is($res->{error}, '', 'a zvol that becomes free is destroyed by a retry'); + ok(!$fake->{m}->{ds}->{'tank/vm-101-disk-0'}, 'and is gone'); + + ($fake, $res) = run_on('free', fail => [$destroy, $busy, 1], faults => { 3 => 'after' }); + like( + $res->{error}, + qr/'destroy zvol' did not complete .* state is unknown\n\z/, + 'a retry whose reply is lost', + ); + is($res->{calls}->[-1]->{op}, 'destroy zvol', 'is not followed by anything'); + + ($fake, $res) = run_on( + 'free', + fail => [$destroy, $busy], + read_fault => { after => 1, mode => 'unreachable' }, + ); + is( + $res->{error}, + "failed to destroy 'tank/vm-101-disk-0': Device or resource busy\n", + 'a failed destroy whose outcome cannot be read is a failure', + ); + ok($fake->{m}->{ds}->{'tank/vm-101-disk-0'}, 'and the zvol is still there'); + + ($fake, $res) = run_on( + 'free', + step_fault => sub($step, $call) { + return 'dataset is busy' if step_is($step, $destroy->@*); + return 'File exists' if step_is($step, 'mkdir', "$S/namespaces/2"); + return undef; + }, + ); + like( + $res->{error}, + qr/failed to destroy 'tank\/vm-101-disk-0': dataset is busy; namespace restoration also/, + 'a failed restore after the retries', + ); + + $fake = lifecycle_fake(fail => [$destroy, $busy]); + delete $fake->{m}->{subsystems}->{$NQN}->{ns}->{2}; + $res = flow($fake, $ACT{free}); + like( + $res->{error}, + qr/\Afailed to destroy 'tank\/vm-101-disk-0': Device or resource busy\n\z/, + 'an unexported busy zvol is reported without restore', + ); + ok(!ns_of($fake, 2), 'and stays unexported'); + + $fake = lifecycle_fake(); + delete $fake->{m}->{subsystems}->{$NQN}; + delete $_->{links}->{$NQN} for values $fake->{m}->{ports}->%*; + $res = flow($fake, $ACT{free}); + is($res->{error}, '', 'a volume is freed without subsystem'); + ok(!$fake->{m}->{ds}->{'tank/vm-101-disk-0'}, 'nothing exports it'); +}; + +subtest 'rollback' => sub { + my $dev = '/dev/zvol/tank/vm-100-disk-0'; + my ($fake, $res) = run_on('rollback'); + is($res->{error}, '', 'a rollback'); + is_deeply( + ops($res), + ['read target state', 'rollback zvol', 'wait for zvol', 'restore namespace'], + 'unexports, rolls back and exports again', + ); + ok(exported($fake, 1, $U{1}, $dev), 'with the same identity'); + + # A snapshot without the identity properties: the rollback loses them. + my $bare = sub(%opts) { + my $f = lifecycle_fake(%opts); + $f->add_snapshot('tank/vm-100-disk-0@bare', props => {}); + return $f; + }; + for my $case ( + ['the disable', { write => '/namespaces/1/enable', value => 0 }], + ['the rmdir', ['rmdir', "$S/namespaces/1"]], + ['the rollback', ['zfs', 'rollback']], + ['the identity', ['zfs', 'set']], + ) { + my ($name, $match) = $case->@*; + $fake = $bare->(fail => [$match, "injected $name failure", 1]); + $res = flow( + $fake, + sub { $PLUGIN->volume_snapshot_rollback(scfg(), 'st', 'vm-100-disk-0', 'bare') }, + ); + like( + $res->{error}, + qr/\ANVMe target operation 'rollback zvol' failed: injected \Q$name\E failure\n\z/, + "a failure at $name is reported", + ); + ok(exported($fake, 1, $U{1}, $dev), "the namespace is restored after $name"); + is_deeply( + owned_volumes($fake->{m})->{'tank/vm-100-disk-0'}, + [1, $U{1}], + 'with identity', + ); + } + my @restore = grep { $_->{op} eq 'restore namespace' } $res->{calls}->@*; + ok(step_is($restore[0]->{steps}->[0], 'zfs', 'set'), 'a lost identity is written first'); + + ($fake, $res) = run_on( + 'rollback', + step_fault => sub($step, $call) { + return 'rollback failed' if step_is($step, 'zfs', 'rollback'); + return 'mkdir failed' if step_is($step, 'mkdir'); + return undef; + }, + ); + like( + $res->{error}, + qr/ + \Arollback\ failed:\ NVMe\ target\ operation\ 'rollback\ zvol'\ failed: + \ rollback\ failed; + \ namespace\ restoration\ failed:\ NVMe\ target\ operation\ 'restore\ namespace' + \ failed:\ mkdir\ failed\n\z + /x, + 'both failures are reported', + ); + ($fake, $res) = run_on('rollback', fail => [['mkdir'], 'mkdir failed']); + like( + $res->{error}, + qr/\Anamespace restoration failed after rollback: .*mkdir failed\n\z/, + 'a failed export after a good rollback is reported', + ); + + $fake = lifecycle_fake(); + for my $case ( + ['a malformed snapshot name', 'vm-100-disk-0', 'a b', qr/invalid snapshot name/], + [ + 'a subvolume', + 'subvol-100-disk-0', + 'snap1', + qr/snapshot rollback requires a ZFS volume/, + ], + ['a volume that is not owned', 'vm-900-disk-0', 'snap1', qr/is not owned/], + ) { + my ($name, $volume, $snap, $error) = $case->@*; + my $rollback = sub { $PLUGIN->volume_snapshot_rollback(scfg(), 'st', $volume, $snap) }; + refused($fake, $rollback, $error, $name); + } + my $taken = lifecycle_fake(); + $taken->add_namespace($NQN, 1, $U{9}, '/dev/zvol/tank/vm-900-disk-0'); + refused( + $taken, + 'rollback', + qr/NVMe namespace ID '1' has a different identity/, + 'a namespace ID that holds another identity', + ); + delete $fake->{m}->{subsystems}->{$NQN}; + refused( + $fake, + 'rollback', + qr/NVMe subsystem does not exist/, + 'a rollback without the subsystem', + ); +}; + +subtest 'template' => sub { + my $old = '/dev/zvol/tank/vm-101-disk-0'; + my $new = '/dev/zvol/tank/base-101-disk-0'; + my ($fake, $res) = run_on('template'); + is($res->{result}, 'base-101-disk-0', 'a template'); + is_deeply( + ops($res), + ['read target state', 'rename zvol', 'wait for zvol', 'create template'], + 'is renamed, exported and snapshotted', + ); + ok(exported($fake, 2, $U{2}, $new), 'under the same identity'); + ok($fake->{m}->{snaps}->{'tank/base-101-disk-0@__base__'}, 'with its base snapshot'); + + my $restored = sub($f) { + return + exported($f, 2, $U{2}, $old) + && $f->{m}->{ds}->{'tank/vm-101-disk-0'} + && !$f->{m}->{ds}->{'tank/base-101-disk-0'}; + }; + for my $case ( + ['the unexport', sub($s, $c) { write_to($s, '/namespaces/2/enable', 0) }], + ['the rename', sub($s, $c) { step_is($s, 'zfs', 'rename', 'tank/vm-101-disk-0') }], + ['the export', sub($s, $c) { step_is($s, 'mkdir') && $c->{op} eq 'create template' }], + ['the snapshot', sub($s, $c) { step_is($s, 'zfs', 'snapshot') }], + ) { + my ($name, $match) = $case->@*; + my $fault = + sub($step, $call) { $match->($step, $call) ? "injected $name failure" : undef }; + ($fake, $res) = run_on('template', step_fault => $fault); + my $prefix = qr/\Atemplate conversion failed: NVMe target operation '[a-z ]+' failed:/; + like( + $res->{error}, + qr/$prefix injected \Q$name\E failure\n\z/, + "a failure at $name is reported", + ); + ok($restored->($fake), "the old name is exported again after $name"); + } + + ($fake, $res) = run_on( + 'template', + after_call => sub($f, $call) { + $f->{m}->{ds}->{'tank/base-101-disk-0'}->{devwait} = 1000 + if $call->{op} eq 'rename zvol'; + }, + ); + like( + $res->{error}, + qr/\Atemplate conversion failed: zvol '\Q$new\E' is not a block device after 10 seconds\n/, + 'a device that never appears', + ); + ok($restored->($fake), 'is renamed back'); + + ($fake, $res) = run_on('template', faults => { 1 => 'late' }); + like( + $res->{error}, + qr/\ANVMe target operation 'rename zvol' did not complete .* state is unknown\n\z/, + 'a rename that does not complete in time is passed on as it is', + ); + ok(!grep({ $_->{op} =~ /back|restore/ } $res->{calls}->@*), 'is not compensated'); + $fake->land; + $res = flow($fake, $ACT{activate}); + ok(!$res->{error} && exported($fake, 2, $U{2}, $new), 'the next activation exports it'); + + my $taken = lifecycle_fake(); + $taken->add_namespace($NQN, 2, $U{9}, '/dev/zvol/tank/vm-900-disk-0'); + refused( + $taken, + 'template', + qr/NVMe namespace ID '2' has a different identity/, + 'a namespace ID that holds another identity', + ); + $fake = lifecycle_fake(); + $fake->add_zvol('tank/base-101-disk-0'); + refused( + $fake, + 'template', + qr/template zvol 'tank\/base-101-disk-0' already exists/, + 'a taken name', + ); + $res = flow($fake, sub { $PLUGIN->create_base('st', scfg(), 'base-102-disk-0') }); + like($res->{error}, qr/create_base not possible with base image/, 'refuses a base'); + refused( + $fake, + sub { $PLUGIN->create_base('st', scfg(), 'vm-900-disk-0') }, + qr/is not owned/, + 'a volume that is not owned', + ); +}; + +subtest 'resize' => sub { + my ($fake, $res) = run_on('resize'); + is($res->{error}, '', 'an exported volume grows'); + is($res->{result}, 2 * 1024**2, 'the new size in KiB is returned'); + is_deeply(ops($res), ['read target state', 'resize zvol'], 'read, then resize'); + ok(!grep({ !$_->{locked} } $res->{calls}->@*), 'under the lock'); + is($fake->{revalidated}->{"$NQN/1"}, 1, + 'the namespace reads the new size in the same call'); + + $fake = lifecycle_fake(); + delete $fake->{m}->{subsystems}->{$NQN}->{ns}->{1}; + $res = flow($fake, $ACT{resize}); + is($res->{error}, '', 'an unexported volume grows'); + is(scalar($res->{calls}->[1]->{steps}->@*), 1, 'without revalidation'); + $fake = lifecycle_fake(); + ns_of($fake, 1)->{enable} = '0'; + $res = flow($fake, $ACT{resize}); + is(scalar($res->{calls}->[1]->{steps}->@*), 1, 'a disabled namespace is not revalidated'); + $fake = lifecycle_fake()->reboot(0); + $res = flow($fake, $ACT{resize}); + is($res->{error}, '', 'a volume grows after a target restart'); + is($fake->{m}->{ds}->{'tank/vm-100-disk-0'}->{volsize}, 2 * 1024**3, + 'once nvmet is loaded'); + + for my $case ( + [ + 'a duplicate UUID', + 'vm-100-disk-0', + sub($f) { $f->add_namespace($NQN, 9, $U{1}, '/dev/zvol/tank/x', 0) }, + qr/duplicate namespace UUID/, + ], + ['a volume that is not owned', 'vm-900-disk-0', sub($f) { }, qr/is not owned/], + ['a volume of another subsystem', 'vm-901-disk-0', sub($f) { }, qr/is not owned/], + ) { + my ($name, $volname, $change, $error) = $case->@*; + $fake = lifecycle_fake(); + $change->($fake); + my $resize = sub { $PLUGIN->volume_resize(scfg(), 'st', $volname, 2 * 1024**3) }; + refused($fake, $resize, $error, $name); + } + ($fake, $res) = run_on('resize', fail => [['zfs', 'set'], 'out of space']); + like($res->{error}, qr/'resize zvol' failed: out of space/, 'a failed resize'); + is_deeply(ops($res), ['read target state', 'resize zvol'], 'stops there'); + ($fake, $res) = + run_on('resize', fail => [{ write => 'revalidate_size' }, 'Invalid argument']); + like($res->{error}, qr/'resize zvol' failed: Invalid argument/, 'a failed revalidation'); + is($fake->{m}->{ds}->{'tank/vm-100-disk-0'}->{volsize}, 2 * 1024**3, 'keeps the new size'); +}; + +# Another node read the key before the storage was removed, which deletes the key file, and its +# activation waited for the target lock. +subtest 'an activation of a storage whose key file is gone' => sub { + my $fake = FakeTarget->new(pools => ['tank']); + my $res = + flow($fake, sub { nv('_nvmet_activate_target', 'st', scfg(), $PORTALS, [@HOSTS], $KEY) }); + is($res->{error}, "storage 'st' is being removed or its key changed\n", 'is refused'); + ok(!changes($res) && !$fake->{m}->{subsystems}->{$NQN}, + 'and does not configure the target'); +}; + +subtest 'snapshots and the generic ZFS calls' => sub { + my $fake = lifecycle_fake(); + my $res = + flow($fake, sub { $PLUGIN->volume_snapshot(scfg(), 'st', 'vm-100-disk-0', 'snap_2.0') }); + is($res->{error}, '', 'a snapshot'); + ok($fake->{m}->{snaps}->{'tank/vm-100-disk-0@snap_2.0'}, 'is taken'); + is(scalar($res->{locks}->@*), 1, 'under the target lock'); + $res = flow( + $fake, + sub { $PLUGIN->volume_rollback_is_possible(scfg(), 'st', 'vm-100-disk-0', 'snap1') }, + ); + like( + $res->{error}, + qr/'snap1' is not most recent snapshot/, + 'only from the latest snapshot', + ); + $res = flow( + $fake, + sub { $PLUGIN->volume_snapshot_delete(scfg(), 'st', 'vm-100-disk-0', 'snap_2.0') }, + ); + ok( + !$res->{error} && !$fake->{m}->{snaps}->{'tank/vm-100-disk-0@snap_2.0'}, + 'deleted by name', + ); + is(scalar($res->{locks}->@*), 1, 'under the target lock too'); + $res = flow( + $fake, + sub { $PLUGIN->volume_rollback_is_possible(scfg(), 'st', 'vm-100-disk-0', 'snap1') }, + ); + is($res->{result}, 1, 'now the rollback is possible'); + $res = flow( + $fake, + sub { $PLUGIN->volume_rollback_is_possible(scfg(), 'st', 'vm-100-disk-0', 'gone') }, + ); + like($res->{error}, qr/snapshot 'gone' does not exist/, 'and needs an existing snapshot'); + $res = flow($fake, sub { $PLUGIN->volume_snapshot_info(scfg(), 'st', 'vm-100-disk-0') }); + is_deeply([sort keys $res->{result}->%*], ['snap1'], 'snapshot information'); + + $res = flow($fake, sub { [$PLUGIN->volume_size_info(scfg(), 'st', 'vm-100-disk-0', 7)] }); + is_deeply($res->{result}, [1073741824, 'raw', 4096, undef], 'size information'); + is($res->{calls}->[0]->{timeout}, 7, 'with the timeout of the caller'); + $res = + flow($fake, sub { scalar($PLUGIN->volume_size_info(scfg(), 'st', 'vm-100-disk-0')) }); + is($res->{result}, 1073741824, 'the size alone in scalar context'); + + # Snapshot names are validated before zfs reads '%' and ',' in them as a + # range and a list, and the pool name wherever a call uses it. + for my $case ( + ['volume_snapshot', 'a b'], + ['volume_snapshot_delete', 'snap1%'], + ['volume_rollback_is_possible', 'snap1,snap2'], + ) { + my ($method, $snap) = $case->@*; + $res = refused( + $fake, + sub { $PLUGIN->$method(scfg(), 'st', 'vm-100-disk-0', $snap) }, + qr/\Ainvalid snapshot name\n\z/, + "$method with the snapshot name '$snap'", + ); + is(scalar($res->{calls}->@*), 0, 'locally'); + } + my $bad_pool = scfg(pool => '-rH'); + for my $case ( + ['list_images', sub { $PLUGIN->list_images('st', $bad_pool) }], + [ + 'volume_size_info', + sub { $PLUGIN->volume_size_info($bad_pool, 'st', 'vm-100-disk-0') }, + ], + [ + 'volume_snapshot_info', + sub { $PLUGIN->volume_snapshot_info($bad_pool, 'st', 'vm-100-disk-0') }, + ], + [ + 'volume_snapshot', + sub { $PLUGIN->volume_snapshot($bad_pool, 'st', 'vm-100-disk-0', 'snap2') }, + ], + ) { + $res = refused( + $fake, + $case->[1], + qr/\Ainvalid ZFS pool name\n\z/, + "$case->[0] with an invalid pool name", + ); + is(scalar($res->{calls}->@*), 0, 'locally'); + } + my @warnings; + { + local $SIG{__WARN__} = sub($warning) { push @warnings, $warning }; + $res = flow($fake, sub { [$PLUGIN->status('st', $bad_pool)] }); + } + is_deeply( + [$res->{result}, $res->{calls}, \@warnings], + [[0, 0, 0, 0], [], ["storage 'st': invalid ZFS pool name\n"]], + 'status with an invalid pool name is inactive, locally', + ); + + # The plugin creates zvols only. + for my $case ( + [ + 'volume_snapshot', + sub { $PLUGIN->volume_snapshot(scfg(), 'st', 'subvol-100-disk-0', 's') }, + ], + [ + 'volume_size_info', + sub { $PLUGIN->volume_size_info(scfg(), 'st', 'subvol-100-disk-0') }, + ], + ) { + $res = refused( + $fake, + $case->[1], + qr/\A$case->[0] requires a ZFS volume\n\z/, + "$case->[0] of a subvolume", + ); + is(scalar($res->{calls}->@*), 0, 'locally'); + } +}; + +subtest 'lock ownership' => sub { + my $fake = lifecycle_fake(); + pipe(my $reader, my $writer) or die "pipe: $!\n"; + my $child; + $fake->{before_call} = sub($f, $call) { + return if $child || $call->{op} ne 'create zvol'; + $child = fork() // die "fork: $!\n"; + return if $child; + close($reader); + my $res = flow( + $f, sub { $PLUGIN->volume_resize(scfg(), 'st', 'vm-100-disk-0', 2 * 1024**3, 0) }, + ); + my $locked = grep { $_->{pid} == $$ } $res->{locks}->@*; + my @counts = ($locked, scalar($res->{nested}->@*), scalar($res->{calls}->@*)); + print {$writer} "@counts\n"; + close($writer); + POSIX::_exit(0); + }; + my $res = flow($fake, sub { $PLUGIN->alloc_image('st', scfg(), 200, 'raw', undef, 1024) }); + close($writer); + my $line = <$reader>; + waitpid($child, 0); + is($res->{error}, '', 'the parent keeps its lock'); + is($line, "1 1 0\n", 'a child requests the lock itself, waits and changes nothing'); + + local $QUORATE = 0; + $fake = lifecycle_fake(); + for my $action (qw(alloc free rollback template resize)) { + $res = flow($fake, $ACT{$action}); + like($res->{error}, qr/cluster not quorate/, "$action needs quorum"); + ok(!$res->{locks}->@* && !changes($res), 'and fails before the lock'); + } +}; + +subtest 'target call timeouts' => sub { + my ($fake, $res) = run_on('alloc', after_call => sub($f, $call) { $NOW += 100 }); + is($res->{error}, '', 'slow target calls do not end an operation'); + is_deeply( + [map { $_->{timeout} } grep { $_->{locked} } $res->{calls}->@*], + [15, 60, 60, 15, 30, 15], + 'reads take up to 15 s, zfs changes 60 s and configfs changes 30 s', + ); + + # in a worker, a zfs change may take an hour, like in the other ZFS plugins + my $rpcenv_mock = Test::MockModule->new('PVE::RPCEnvironment'); + $rpcenv_mock->redefine(is_worker => sub(@args) { return 1 }); + my %timeouts; + for my $action (qw(alloc template resize rollback free)) { + (undef, $res) = run_on($action); + $timeouts{ $_->{op} } = $_->{timeout} for $res->{calls}->@*; + } + is_deeply( + [ + @timeouts{ + 'reserve namespace ID', + 'create zvol', + 'rename zvol', + 'create template', + 'resize zvol', + 'rollback zvol', + 'restore namespace', + 'destroy zvol', + }, + ], + [(3600) x 8], + 'every zfs change', + ); + $rpcenv_mock->unmock_all(); +}; + done_testing(); -- 2.54.0.windows.1