tests: lvm-mapping - convert to python

Signed-off-by: Susant Sahani <ssahani@redhat.com>
This commit is contained in:
Susant Sahani
2025-12-03 08:23:19 +05:30
committed by rwmjones
parent 8f187bf97b
commit 1f739979bb
3 changed files with 81 additions and 94 deletions

View File

@@ -445,10 +445,10 @@ EXTRA_DIST += \
TESTS += \
lvm/test-lvm-filtering.sh \
lvm/test-lvm-mapping.pl
lvm/test-lvm-mapping.py
EXTRA_DIST += \
lvm/test-lvm-filtering.sh \
lvm/test-lvm-mapping.pl
lvm/test-lvm-mapping.py
TESTS += \
md/test-inspect-fstab.sh \

View File

@@ -1,92 +0,0 @@
#!/usr/bin/env perl
# Copyright (C) 2010 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Test the discovery of relationships between LVM PVs, VGs and LVs.
use strict;
use warnings;
use Sys::Guestfs;
exit 77 if $ENV{SKIP_TEST_LVM_MAPPING_PL};
my $g = Sys::Guestfs->new ();
$g->add_drive_scratch (256 * 1024 * 1024);
$g->launch ();
# Create an arrangement of PVs, VGs and LVs.
$g->part_init ("/dev/sda", "mbr");
$g->part_add ("/dev/sda", "p", 2048, 128*1024/2-1);
$g->part_add ("/dev/sda", "p", 128*1024/2, -64);
$g->pvcreate ("/dev/sda1");
$g->pvcreate ("/dev/sda2");
$g->vgcreate ("VG", ["/dev/sda1", "/dev/sda2"]);
$g->lvcreate ("LV1", "VG", 32);
$g->lvcreate ("LV2", "VG", 32);
$g->lvcreate ("LV3", "VG", 32);
# Now let's get the arrangement.
my @pvs = $g->pvs ();
my @lvs = $g->lvs ();
my %pvuuids;
foreach my $pv (@pvs) {
my $uuid = $g->pvuuid ($pv);
$pvuuids{$uuid} = $pv;
}
my %lvuuids;
foreach my $lv (@lvs) {
my $uuid = $g->lvuuid ($lv);
$lvuuids{$uuid} = $lv;
}
# In this case there is only one VG, called "VG", but in a real
# program you'd want to repeat these steps for each VG that you found.
my @pvuuids_in_VG = $g->vgpvuuids ("VG");
my @lvuuids_in_VG = $g->vglvuuids ("VG");
my @pvs_in_VG;
foreach my $uuid (@pvuuids_in_VG) {
push @pvs_in_VG, $pvuuids{$uuid};
}
@pvs_in_VG = sort @pvs_in_VG;
my @lvs_in_VG;
foreach my $uuid (@lvuuids_in_VG) {
push @lvs_in_VG, $lvuuids{$uuid};
}
@lvs_in_VG = sort @lvs_in_VG;
unless (@pvs_in_VG == 2 &&
$pvs_in_VG[0] =~ m{/dev/[abce-ln-z]+da1} &&
$pvs_in_VG[1] =~ m{/dev/[abce-ln-z]+da2}) {
die "unexpected set of PVs for volume group VG: [",
join (", ", @pvs_in_VG), "]\n"
}
unless (@lvs_in_VG == 3 &&
$lvs_in_VG[0] eq "/dev/VG/LV1" &&
$lvs_in_VG[1] eq "/dev/VG/LV2" &&
$lvs_in_VG[2] eq "/dev/VG/LV3") {
die "unexpected set of LVs for volume group VG: [",
join (", ", @lvs_in_VG), "]\n"
}
undef $g;

79
tests/lvm/test-lvm-mapping.py Executable file
View File

@@ -0,0 +1,79 @@
#!/usr/bin/env python3
# Copyright (C) 2025 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Test the discovery of relationships between LVM PVs, VGs and LVs.
import os
import sys
import re
import guestfs
if os.environ.get('SKIP_TEST_LVM_MAPPING_PY'):
sys.exit(77)
g = guestfs.GuestFS()
g.add_drive_scratch(256 * 1024 * 1024)
g.launch()
# Create an arrangement of PVs, VGs and LVs.
g.part_init("/dev/sda", "mbr")
g.part_add("/dev/sda", "p", 2048, 128 * 1024 // 2 - 1)
g.part_add("/dev/sda", "p", 128 * 1024 // 2, -64)
g.pvcreate("/dev/sda1")
g.pvcreate("/dev/sda2")
g.vgcreate("VG", ["/dev/sda1", "/dev/sda2"])
g.lvcreate("LV1", "VG", 32)
g.lvcreate("LV2", "VG", 32)
g.lvcreate("LV3", "VG", 32)
# Now let's get the arrangement.
pvs = g.pvs()
lvs = g.lvs()
pvuuids = {}
for pv in pvs:
uuid = g.pvuuid(pv)
pvuuids[uuid] = pv
lvuuids = {}
for lv in lvs:
uuid = g.lvuuid(lv)
lvuuids[uuid] = lv
# In this case there is only one VG, called "VG", but in a real
# program we'd want to repeat these steps for each VG that you found.
pvuuids_in_VG = g.vgpvuuids("VG")
lvuuids_in_VG = g.vglvuuids("VG")
pvs_in_VG = [pvuuids[uuid] for uuid in pvuuids_in_VG]
pvs_in_VG.sort()
lvs_in_VG = [lvuuids[uuid] for uuid in lvuuids_in_VG]
lvs_in_VG.sort()
if not (len(pvs_in_VG) == 2 and
re.match(r'/dev/[abce-ln-z]+da1$', pvs_in_VG[0]) and
re.match(r'/dev/[abce-ln-z]+da2$', pvs_in_VG[1])):
raise Exception("unexpected set of PVs for volume group VG: [" + ", ".join(pvs_in_VG) + "]")
if not (len(lvs_in_VG) == 3 and
lvs_in_VG[0] == "/dev/VG/LV1" and
lvs_in_VG[1] == "/dev/VG/LV2" and
lvs_in_VG[2] == "/dev/VG/LV3"):
raise Exception("unexpected set of LVs for volume group VG: [" + ", ".join(lvs_in_VG) + "]")