dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
21255d
From 6c3191e979165700f98903b76621c214186a110c Mon Sep 17 00:00:00 2001
21255d
From: Martin Wilck <mwilck@suse.com>
21255d
Date: Wed, 25 Apr 2018 09:54:26 +0200
21255d
Subject: [PATCH] test/udev-test.pl: generator for large list of block devices
21255d
21255d
Manually listing all devices in the test definition becomes cumbersome with
21255d
lots of devices. Add a function that scans on all block devices in
21255d
the test sysfs and generates a list of devices to test.
21255d
21255d
(cherry picked from commit eb44d715ebee2fe11288433b99f8e1dc5fdac84a)
21255d
21255d
Related: #1642728
21255d
---
21255d
 test/udev-test.pl | 60 ++++++++++++++++++++++++++++++++++++++++++++++-
21255d
 1 file changed, 59 insertions(+), 1 deletion(-)
21255d
21255d
diff --git a/test/udev-test.pl b/test/udev-test.pl
21255d
index 8b1ab3c06c..2866fdb77a 100755
21255d
--- a/test/udev-test.pl
21255d
+++ b/test/udev-test.pl
21255d
@@ -50,6 +50,50 @@ for (my $i = 1; $i < 10000; ++$i) {
21255d
 }
21255d
 $rules_10k_tags_continuation .= "TAG+=\"test10000\"\\n";
21255d
 
21255d
+# Create a device list with all block devices under /sys
21255d
+# (except virtual devices and cd-roms)
21255d
+# the optional argument exp_func returns expected and non-expected
21255d
+# symlinks for the device.
21255d
+sub all_block_devs {
21255d
+        my ($exp_func) = @_;
21255d
+        my @devices;
21255d
+
21255d
+        foreach my $bd (glob "$udev_sys/dev/block/*") {
21255d
+                my $tgt = readlink($bd);
21255d
+                my ($exp, $notexp) = (undef, undef);
21255d
+
21255d
+                next if ($tgt =~ m!/virtual/! || $tgt =~ m!/sr[0-9]*$!);
21255d
+
21255d
+                $tgt =~ s!^\.\./\.\.!!;
21255d
+                ($exp, $notexp) = $exp_func->($tgt) if defined($exp_func);
21255d
+                my $device = {
21255d
+                        devpath => $tgt,
21255d
+                        exp_links => $exp,
21255d
+                        not_exp_links => $notexp,
21255d
+                };
21255d
+                push(@devices, $device);
21255d
+        }
21255d
+        return \@devices;
21255d
+}
21255d
+
21255d
+# This generator returns a suitable exp_func for use with
21255d
+# all_block_devs().
21255d
+sub expect_for_some {
21255d
+        my ($pattern, $links, $donot) = @_;
21255d
+        my $_expect = sub {
21255d
+                my ($name) = @_;
21255d
+
21255d
+                if ($name =~ /$pattern/) {
21255d
+                        return ($links, undef);
21255d
+                } elsif ($donot) {
21255d
+                        return (undef, $links);
21255d
+                } else {
21255d
+                        return (undef, undef);
21255d
+                }
21255d
+        };
21255d
+        return $_expect;
21255d
+}
21255d
+
21255d
 my @tests = (
21255d
         {
21255d
                 desc            => "no rules",
21255d
@@ -2257,6 +2301,15 @@ SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partitio
21255d
 KERNEL=="*7", OPTIONS+="link_priority=10"
21255d
 EOF
21255d
         },
21255d
+        {
21255d
+                desc           => 'all_block_devs',
21255d
+                generator      => expect_for_some("\\/sda6\$", ["blockdev"]),
21255d
+                repeat         => 10,
21255d
+                rules          => <
21255d
+SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
21255d
+KERNEL=="sda6", OPTIONS+="link_priority=10"
21255d
+EOF
21255d
+        }
21255d
 );
21255d
 
21255d
 sub create_rules {
21255d
@@ -2631,7 +2684,12 @@ sub fork_and_run_udev {
21255d
 sub run_test {
21255d
         my ($rules, $number, $sema) = @_;
21255d
         my $rc;
21255d
-        my @devices = @{$rules->{devices}};
21255d
+        my @devices;
21255d
+
21255d
+        if (!defined $rules->{devices}) {
21255d
+                $rules->{devices} = all_block_devs($rules->{generator});
21255d
+        }
21255d
+        @devices = @{$rules->{devices}};
21255d
 
21255d
         print "TEST $number: $rules->{desc}\n";
21255d
         create_rules(\$rules->{rules});