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