From 6c3191e979165700f98903b76621c214186a110c Mon Sep 17 00:00:00 2001
From: Martin Wilck <mwilck@suse.com>
Date: Wed, 25 Apr 2018 09:54:26 +0200
Subject: [PATCH] test/udev-test.pl: generator for large list of block devices

Manually listing all devices in the test definition becomes cumbersome with
lots of devices. Add a function that scans on all block devices in
the test sysfs and generates a list of devices to test.

(cherry picked from commit eb44d715ebee2fe11288433b99f8e1dc5fdac84a)

Related: #1642728
---
 test/udev-test.pl | 60 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

diff --git a/test/udev-test.pl b/test/udev-test.pl
index 8b1ab3c06c..2866fdb77a 100755
--- a/test/udev-test.pl
+++ b/test/udev-test.pl
@@ -50,6 +50,50 @@ for (my $i = 1; $i < 10000; ++$i) {
 }
 $rules_10k_tags_continuation .= "TAG+=\"test10000\"\\n";
 
+# Create a device list with all block devices under /sys
+# (except virtual devices and cd-roms)
+# the optional argument exp_func returns expected and non-expected
+# symlinks for the device.
+sub all_block_devs {
+        my ($exp_func) = @_;
+        my @devices;
+
+        foreach my $bd (glob "$udev_sys/dev/block/*") {
+                my $tgt = readlink($bd);
+                my ($exp, $notexp) = (undef, undef);
+
+                next if ($tgt =~ m!/virtual/! || $tgt =~ m!/sr[0-9]*$!);
+
+                $tgt =~ s!^\.\./\.\.!!;
+                ($exp, $notexp) = $exp_func->($tgt) if defined($exp_func);
+                my $device = {
+                        devpath => $tgt,
+                        exp_links => $exp,
+                        not_exp_links => $notexp,
+                };
+                push(@devices, $device);
+        }
+        return \@devices;
+}
+
+# This generator returns a suitable exp_func for use with
+# all_block_devs().
+sub expect_for_some {
+        my ($pattern, $links, $donot) = @_;
+        my $_expect = sub {
+                my ($name) = @_;
+
+                if ($name =~ /$pattern/) {
+                        return ($links, undef);
+                } elsif ($donot) {
+                        return (undef, $links);
+                } else {
+                        return (undef, undef);
+                }
+        };
+        return $_expect;
+}
+
 my @tests = (
         {
                 desc            => "no rules",
@@ -2257,6 +2301,15 @@ SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partitio
 KERNEL=="*7", OPTIONS+="link_priority=10"
 EOF
         },
+        {
+                desc           => 'all_block_devs',
+                generator      => expect_for_some("\\/sda6\$", ["blockdev"]),
+                repeat         => 10,
+                rules          => <<EOF
+SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
+KERNEL=="sda6", OPTIONS+="link_priority=10"
+EOF
+        }
 );
 
 sub create_rules {
@@ -2631,7 +2684,12 @@ sub fork_and_run_udev {
 sub run_test {
         my ($rules, $number, $sema) = @_;
         my $rc;
-        my @devices = @{$rules->{devices}};
+        my @devices;
+
+        if (!defined $rules->{devices}) {
+                $rules->{devices} = all_block_devs($rules->{generator});
+        }
+        @devices = @{$rules->{devices}};
 
         print "TEST $number: $rules->{desc}\n";
         create_rules(\$rules->{rules});
