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 16/38] tests: container: verify snapshots, clones, templates
Date: Wed,  4 Nov 2026 20:03:23 +0100	[thread overview]
Message-ID: <20261104190345.2573464-17-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20261104190345.2573464-1-m.koeppl@proxmox.com>

Snapshot a running container and roll it back with 'start' set and
create a full clone, which must keep the data but get its own hostname
and machine ID. Also create a linked clone of a template.

On LVM-thin, a linked clone does not depend on its origin, so the
template can be destroyed while the clone exists. Check that the clone
keeps working afterwards.

Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
 tests/pve_ct_snapshot_clone.pl | 267 +++++++++++++++++++++++++++++++++
 tests/testcases.ron            |   9 ++
 2 files changed, 276 insertions(+)
 create mode 100755 tests/pve_ct_snapshot_clone.pl

diff --git a/tests/pve_ct_snapshot_clone.pl b/tests/pve_ct_snapshot_clone.pl
new file mode 100755
index 0000000..e9119e8
--- /dev/null
+++ b/tests/pve_ct_snapshot_clone.pl
@@ -0,0 +1,267 @@
+#!/usr/bin/perl
+
+=head1 NAME
+
+C<pve_ct_snapshot_clone> - Test snapshots, clones and templates of a container.
+
+=head1 DESCRIPTION
+
+This testcase creates an unprivileged container on local-lvm, writes a marker
+file and takes a snapshot of the running container. After changing the marker,
+it rolls the snapshot back and verifies that the original content is restored,
+then deletes the snapshot.
+
+Afterwards, it creates a full clone and checks that the clone carries the data
+but gets its own hostname and machine ID. Finally, it converts the container to
+a template and creates a linked clone backed by the template's base volume. The
+template can then be destroyed and the linked clone must keep its data.
+
+=cut
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib '.';
+use Proxmox::Test::Helper qw(CONTAINER_TEMPLATE get_pve_instance);
+use Proxmox::Test::Testcase;
+use Proxmox::Test::Util qw(poll_until);
+
+my $pve = get_pve_instance('pve1');
+my $node = $pve->get_nodename();
+my $client = $pve->client();
+
+my $snapname = 'e2esnap';
+
+my $test_container = {
+    vmid => $client->get('/cluster/nextid'),
+    node => $node,
+    ostemplate => CONTAINER_TEMPLATE,
+    rootfs => 'local-lvm:2',
+    storage => 'local-lvm',
+    memory => 256,
+    unprivileged => 1,
+    features => 'nesting=1',
+    hostname => 'ct-snapshot',
+};
+
+my $vmid = $test_container->{vmid};
+my $full_clone = { vmid => undef, hostname => 'ct-clone-full' };
+my $linked_clone = { vmid => undef, hostname => 'ct-clone-linked' };
+my $machine_id;
+
+my $snapshot_names = sub {
+    my $snapshots = $client->get("/nodes/$node/lxc/$vmid/snapshot", {});
+    return map { $_->{name} } grep { $_->{name} ne 'current' } $snapshots->@*;
+};
+
+my $create_clone = sub {
+    my ($clone, $full) = @_;
+
+    $clone->{vmid} = $client->get('/cluster/nextid');
+    my $upid = $client->post(
+        "/nodes/$node/lxc/$vmid/clone",
+        {
+            newid => $clone->{vmid},
+            hostname => $clone->{hostname},
+            full => $full,
+            ($full ? (storage => 'local-lvm') : ()),
+        },
+    );
+    $pve->task_ok($upid, ($full ? 'full' : 'linked') . " clone $clone->{vmid} created")
+        or die "cloning container $vmid failed\n";
+};
+
+Proxmox::Test::Testcase->new('pve_ct_snapshot_clone')->with([{}])->step(
+    'create container',
+    sub {
+        my $upid = $client->post("/nodes/$node/lxc", $test_container);
+        $pve->task_ok($upid, "created container $vmid")
+            or die "creating container $vmid failed\n";
+    },
+    sub {
+        $pve->destroy_guest('lxc', $vmid);
+    },
+)->step(
+    'start container and write marker',
+    sub {
+        $pve->start_guest('lxc', $vmid);
+        $pve->ct_exec($vmid, "sh -c 'echo before-snapshot > /root/marker'");
+
+        # Required later to check if the full clone has a different machine ID
+        $machine_id = $pve->ct_exec($vmid, 'cat /etc/machine-id');
+    },
+)->step(
+    'create snapshot of running container',
+    sub {
+        my $upid = $client->post("/nodes/$node/lxc/$vmid/snapshot", { snapname => $snapname });
+        $pve->task_ok($upid, "created snapshot '$snapname'")
+            or die "creating snapshot '$snapname' failed\n";
+
+        ok((grep { $_ eq $snapname } $snapshot_names->()), "snapshot '$snapname' is listed");
+    },
+)->step(
+    'rollback snapshot',
+    sub {
+        $pve->ct_exec($vmid, "sh -c 'echo after-snapshot > /root/marker'");
+
+        # Rolling back stops the running container, 'start' brings it back afterwards in a separate
+        # task.
+        my $upid =
+            $client->post("/nodes/$node/lxc/$vmid/snapshot/$snapname/rollback", { start => 1 });
+        $pve->task_ok($upid, "rolled back to snapshot '$snapname'")
+            or die "rolling back snapshot '$snapname' failed\n";
+
+        my $start_upid = $pve->find_spawned_task($upid, 'vzstart');
+        $pve->task_ok($start_upid, "started container $vmid after the rollback")
+            or die "starting container $vmid after the rollback failed\n";
+
+        poll_until(
+            sub {
+                $client->get("/nodes/$node/lxc/$vmid/status/current", {})->{status} eq
+                    'running';
+            },
+            # A status query can take some time after teh start, so allow more than usual.
+            timeout => 120,
+            description => "container $vmid to run again after the rollback",
+        );
+        is(
+            $pve->ct_exec($vmid, 'cat /root/marker'),
+            "before-snapshot\n",
+            'rollback restored the content from the time of the snapshot',
+        );
+    },
+)->step(
+    'delete snapshot',
+    sub {
+        my $upid = $client->delete("/nodes/$node/lxc/$vmid/snapshot/$snapname", {});
+        $pve->task_ok($upid, "deleted snapshot '$snapname'")
+            or die "deleting snapshot '$snapname' failed\n";
+
+        ok(!(grep { $_ eq $snapname } $snapshot_names->()), "snapshot '$snapname' is gone");
+    },
+)->step(
+    'stop container',
+    sub {
+        $pve->stop_guest('lxc', $vmid);
+    },
+)->step(
+    'create full clone',
+    sub {
+        $create_clone->($full_clone, 1);
+    },
+    sub {
+        $pve->destroy_guest('lxc', $full_clone->{vmid}) if defined($full_clone->{vmid});
+    },
+)->step(
+    'verify full clone',
+    sub {
+        my $id = $full_clone->{vmid};
+
+        my $conf = $client->get("/nodes/$node/lxc/$id/config", {});
+        is($conf->{hostname}, $full_clone->{hostname}, 'full clone has its own hostname');
+        like(
+            $conf->{rootfs} // '',
+            qr/^local-lvm:vm-$id-disk-\d+\b/,
+            'full clone has own rootfs',
+        );
+
+        $pve->start_guest('lxc', $id);
+        is(
+            $pve->ct_exec($id, 'cat /root/marker'),
+            "before-snapshot\n",
+            'full clone has the data',
+        );
+        is(
+            $pve->ct_exec($id, 'cat /etc/hostname'),
+            "$full_clone->{hostname}\n",
+            'hostname is set up inside the full clone',
+        );
+
+        # Cloning clears the machine ID, so the guest generates a new one on its first boot.
+        my $clone_machine_id = $pve->ct_exec($id, 'cat /etc/machine-id');
+        like($clone_machine_id, qr/^[0-9a-f]{32}$/, 'full clone has a machine ID');
+        isnt($clone_machine_id, $machine_id, 'full clone has a different machine ID');
+
+        $pve->stop_guest('lxc', $id);
+    },
+)->step(
+    'convert container to template',
+    sub {
+        # Conversion runs in a worker task, but the API call does not return its task ID.
+        $client->post("/nodes/$node/lxc/$vmid/template", {});
+
+        my $conf;
+        eval {
+            poll_until(
+                sub {
+                    $conf = $client->get("/nodes/$node/lxc/$vmid/config", {});
+                    return $conf->{template};
+                },
+                timeout => 60,
+                interval => 2,
+                description => "container $vmid to be converted to a template",
+            );
+        };
+        is($conf->{template}, 1, 'container is marked as template');
+        like(
+            $conf->{rootfs} // '',
+            qr/^local-lvm:base-$vmid-disk-\d+\b/,
+            'template rootfs was converted to a base volume',
+        );
+    },
+)->step(
+    'create linked clone',
+    sub {
+        $create_clone->($linked_clone, 0);
+    },
+    sub {
+        $pve->destroy_guest('lxc', $linked_clone->{vmid}) if defined($linked_clone->{vmid});
+    },
+)->step(
+    'verify linked clone',
+    sub {
+        my $id = $linked_clone->{vmid};
+
+        my $conf = $client->get("/nodes/$node/lxc/$id/config", {});
+        my ($volname) = ($conf->{rootfs} // '') =~ m/^local-lvm:([^,]+)/
+            or die "linked clone $id has no rootfs on local-lvm\n";
+
+        my $vgname = $client->get('/storage/local-lvm', {})->{vgname};
+        my ($origin) = $pve->run_command("lvs --noheadings -o origin $vgname/$volname");
+        like(
+            $origin // '',
+            qr/^\s*base-$vmid-disk-\d+\s*$/,
+            'linked clone rootfs is a thin snapshot of the template base volume',
+        );
+
+        $pve->start_guest('lxc', $id);
+        is(
+            $pve->ct_exec($id, 'cat /root/marker'),
+            "before-snapshot\n",
+            'linked clone has the data',
+        );
+        $pve->stop_guest('lxc', $id);
+    },
+)->step(
+    'linked clone survives destroying the template',
+    sub {
+        # A snapshot on LVM-thin does not depend on its origin volume, so the template can be
+        # destroyed while linked clones of it exist and they must keep working.
+        my $upid = $client->delete("/nodes/$node/lxc/$vmid", {});
+        $pve->task_ok($upid, "destroyed template $vmid")
+            or die "destroying the template failed\n";
+
+        my $id = $linked_clone->{vmid};
+        $pve->start_guest('lxc', $id);
+        is(
+            $pve->ct_exec($id, 'cat /root/marker'),
+            "before-snapshot\n",
+            'linked clone still has the data after the template is gone',
+        );
+        $pve->stop_guest('lxc', $id);
+    },
+)->run();
+
+done_testing();
diff --git a/tests/testcases.ron b/tests/testcases.ron
index 79841ca..c0369e4 100644
--- a/tests/testcases.ron
+++ b/tests/testcases.ron
@@ -160,6 +160,15 @@ TestcaseConfig(
             ],
             tag: "extended",
         ),
+        Testcase(
+            name: "pve_ct_snapshot_clone",
+            run: "./tests/pve_ct_snapshot_clone.pl",
+            timeout: 600,
+            instances: [
+                "pve1",
+            ],
+            tag: "extended",
+        ),
         Testcase(
             name: "pve_ha_resource_config",
             run: "./tests/pve_ha_resource_config.pl",
-- 
2.47.3





  parent reply	other threads:[~2026-11-04 19:06 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 19:03 [PATCH e2e-tests/test-tools 00/38] tests: add tests for guests, Ceph, PBS and cluster workflows Michael Köppl
2026-11-04 19:03 ` [PATCH test-tools 01/38] ansible: rename snd_test bridge to tt_test as multi-purpose bridge Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 02/38] plans: rename sdn_test bridge to tt_test Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 03/38] debian: add missing jq dependency Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 04/38] test framework: move the default container template to the helper Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 05/38] lib: instance: add helper to check if an address answers ping Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 06/38] lib: pveinstance: look up disks by their SCSI serial Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 07/38] lib: pveinstance: add helper to find a task started by another task Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 08/38] lib: pveinstance: add helpers to start and stop guests Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 09/38] lib: pveinstance: add helper to run commands in containers Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 10/38] lib: pveinstance: add helper to wait for qmeventd's cleanup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 11/38] lib: pveinstance: add helper to check if guest exists Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 12/38] lib: add helpers to use a PBS datastore as PVE storage Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 13/38] scripts: retry container template download, fail if it fails Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 14/38] tests: container: verify lifecycle and guest setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 15/38] tests: container: verify volume and bind mountpoints Michael Köppl
2026-11-04 19:03 ` Michael Köppl [this message]
2026-11-04 19:03 ` [PATCH e2e-tests 17/38] tests: container: verify device passthrough Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 18/38] plans,scripts: build a minimal OCI image onto pve1 Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 19/38] tests: container: verify application containers from OCI images Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 20/38] tests: vm: verify snapshots, clones, templates Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 21/38] tests: vm: verify adding, resizing, and removing disks Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 22/38] plans,scripts: download a Debian cloud image onto pve1 Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 23/38] tests: vm: verify lifecycle operations and cloud-init guest setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 24/38] plans,scripts: install Ceph Tentacle on the cluster nodes Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 25/38] test framework: add Ceph test helpers Michael Köppl
2026-11-06 12:24   ` Kefu Chai
2026-11-06 13:04     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 26/38] tests: ceph: verify setting up a hyper-converged cluster Michael Köppl
2026-11-06 12:50   ` Kefu Chai
2026-11-06 13:05     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 27/38] tests: ceph: verify guests on RBD storage Michael Köppl
2026-11-06 13:01   ` Kefu Chai
2026-11-06 13:07     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 28/38] tests: ceph: verify setting up, using and destroying a CephFS Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 29/38] tests: ceph: verify replacing an OSD Michael Köppl
2026-11-06 13:14   ` Kefu Chai
2026-11-04 19:03 ` [PATCH e2e-tests 30/38] tests: ceph: drop the outdated Ceph setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 31/38] tests: pbs: verify container backup and restore Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 32/38] tests: backup: verify VM backup and restore, locally and on PBS Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 33/38] tests: pbs: verify single file restore from a container backup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 34/38] tests: storage: verify moving guest volumes between storages Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 35/38] tests: migration: verify container migration between nodes Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 36/38] tests: migration: verify VM migration with local disks Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 37/38] tests: ha: verify moving guests off a node for maintenance Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 38/38] tests: migration: verify migrating guests in bulk Michael Köppl
2026-11-06 13:25 ` [PATCH e2e-tests/test-tools 00/38] tests: add tests for guests, Ceph, PBS and cluster workflows Kefu Chai

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=20261104190345.2573464-17-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