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