chiark / gitweb /
[PATCH] change devfs disk name rule from 'disk' to 'disc'
[elogind.git] / test / udev-test.pl
1 #!/usr/bin/perl
2
3 # udev-test
4 #
5 # Provides automated testing of the udev binary.
6 # The whole test is self contained in this file, except the matching sysfs tree.
7 # Simply extend the @tests array, to add a new test variant.
8 #
9 # Every test is driven by its own temporary config file.
10 # This program prepares the environment, creates the config and calls udev.
11 #
12 # udev reads the config, looks at the provided sysfs and
13 # first creates and then removes the device node.
14 # After creation and removal the result is checked against the
15 # expected value and the result is printed.
16 #
17 # happy testing,
18 # Kay Sievers <kay.sievers@vrfy.org>, 2003
19
20
21 use warnings;
22 use strict;
23
24 my $PWD = $ENV{PWD};
25 my $sysfs     = "sys/";
26 my $udev_bin  = "../udev";
27 my $udev_root = "udev-root/"; # !!! directory will be removed !!!
28 my $udev_db   = ".udev.tdb";
29 my $perm      = "udev.permissions";
30 my $main_conf = "udev-test.conf";
31 my $conf_tmp  = "udev-test.rules";
32
33
34 my @tests = (
35         {
36                 desc     => "label test of scsi disc",
37                 subsys   => "block",
38                 devpath  => "block/sda",
39                 expected => "boot_disk" ,
40                 conf     => <<EOF
41 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
42 REPLACE, KERNEL="ttyUSB0", NAME="visor"
43 EOF
44         },
45         {
46                 desc     => "label test of scsi partition",
47                 subsys   => "block",
48                 devpath  => "block/sda/sda1",
49                 expected => "boot_disk1" ,
50                 conf     => <<EOF
51 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
52 EOF
53         },
54         {
55                 desc     => "label test of pattern match",
56                 subsys   => "block",
57                 devpath  => "block/sda/sda1",
58                 expected => "boot_disk1" ,
59                 conf     => <<EOF
60 LABEL, BUS="scsi", vendor="?IBM-ESXS", NAME="boot_disk%n-1"
61 LABEL, BUS="scsi", vendor="IBM-ESXS?", NAME="boot_disk%n-2"
62 LABEL, BUS="scsi", vendor="IBM-ES??", NAME="boot_disk%n"
63 LABEL, BUS="scsi", vendor="IBM-ESXSS", NAME="boot_disk%n-3"
64 EOF
65         },
66         {
67                 desc     => "catch device by *",
68                 subsys   => "tty",
69                 devpath  => "class/tty/ttyUSB0",
70                 expected => "visor/0" ,
71                 conf     => <<EOF
72 REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
73 EOF
74         },
75         {
76                 desc     => "catch device by ?",
77                 subsys   => "tty",
78                 devpath  => "class/tty/ttyUSB0",
79                 expected => "visor/0" ,
80                 conf     => <<EOF
81 REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
82 REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
83 REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
84 EOF
85         },
86         {
87                 desc     => "catch device by character class",
88                 subsys   => "tty",
89                 devpath  => "class/tty/ttyUSB0",
90                 expected => "visor/0" ,
91                 conf     => <<EOF
92 REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
93 REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
94 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
95 EOF
96         },
97         {
98                 desc     => "replace kernel name",
99                 subsys   => "tty",
100                 devpath  => "class/tty/ttyUSB0",
101                 expected => "visor" ,
102                 conf     => <<EOF
103 REPLACE, KERNEL="ttyUSB0", NAME="visor"
104 EOF
105         },
106         {
107                 desc     => "subdirectory handling",
108                 subsys   => "tty",
109                 devpath  => "class/tty/ttyUSB0",
110                 expected => "sub/direct/ory/visor" ,
111                 conf     => <<EOF
112 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
113 EOF
114         },
115         {
116                 desc     => "place on bus of scsi partition",
117                 subsys   => "block",
118                 devpath  => "block/sda/sda3",
119                 expected => "first_disk3" ,
120                 conf     => <<EOF
121 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
122 EOF
123         },
124         {
125                 desc     => "test NAME substitution chars",
126                 subsys   => "block",
127                 devpath  => "block/sda/sda3",
128                 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
129                 conf     => <<EOF
130 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
131 EOF
132         },
133         {
134                 desc     => "callout result substitution",
135                 subsys   => "block",
136                 devpath  => "block/sda/sda3",
137                 expected => "special-device-3" ,
138                 conf     => <<EOF
139 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
140 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
141 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
142 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
143 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
144 EOF
145         },
146         {
147                 desc     => "callout program substitution",
148                 subsys   => "block",
149                 devpath  => "block/sda/sda3",
150                 expected => "test-0:0:0:0" ,
151                 conf     => <<EOF
152 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
153 EOF
154         },
155         {
156                 desc     => "devfs disk naming substitution",
157                 subsys   => "block",
158                 devpath  => "block/sda",
159                 expected => "lun0/disc" ,
160                 conf     => <<EOF
161 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
162 EOF
163         },
164         {
165                 desc     => "devfs disk naming substitution",
166                 subsys   => "block",
167                 devpath  => "block/sda/sda2",
168                 expected => "lun0/part2" ,
169                 conf     => <<EOF
170 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
171 EOF
172         },
173         {
174                 desc     => "callout bus type",
175                 subsys   => "block",
176                 devpath  => "block/sda",
177                 expected => "scsi-0:0:0:0" ,
178                 conf     => <<EOF
179 CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
180 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
181 CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
182 EOF
183         },
184         {
185                 desc     => "symlink creation (same directory)",
186                 subsys   => "tty",
187                 devpath  => "class/tty/ttyUSB0",
188                 expected => "visor0" ,
189                 conf     => <<EOF
190 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
191 EOF
192         },
193         {
194                 desc     => "symlink creation (relative link back)",
195                 subsys   => "block",
196                 devpath  => "block/sda/sda2",
197                 expected => "1/2/a/b/symlink" ,
198                 conf     => <<EOF
199 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
200 EOF
201         },
202         {
203                 desc     => "symlink creation (relative link forward)",
204                 subsys   => "block",
205                 devpath  => "block/sda/sda2",
206                 expected => "1/2/symlink" ,
207                 conf     => <<EOF
208 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
209 EOF
210         },
211         {
212                 desc     => "symlink creation (relative link back and forward)",
213                 subsys   => "block",
214                 devpath  => "block/sda/sda2",
215                 expected => "1/2/c/d/symlink" ,
216                 conf     => <<EOF
217 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
218 EOF
219         },
220         {
221                 desc     => "multiple symlinks",
222                 subsys   => "tty",
223                 devpath  => "class/tty/ttyUSB0",
224                 expected => "second-0" ,
225                 conf     => <<EOF
226 REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
227 EOF
228         },
229 );
230
231 # set env
232 $ENV{UDEV_TEST} = "yes";
233 $ENV{SYSFS_PATH} = $sysfs;
234 $ENV{UDEV_CONFIG_FILE} = $main_conf;
235
236
237 sub udev {
238         my ($action, $subsys, $devpath, $config) = @_;
239
240         $ENV{DEVPATH} = $devpath;
241
242         # create temporary config
243         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
244         print CONF $$config;
245         close CONF;
246
247         $ENV{ACTION} = $action;
248         system("$udev_bin $subsys");
249 }
250
251
252 # prepare
253 system("rm -rf $udev_root");
254 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
255
256 # test
257 my $error = 0;
258 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
259
260 # create initial config file
261 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
262 print CONF "udev_root=\"$udev_root\"\n";
263 print CONF "udev_db=\"$udev_db\"\n";
264 print CONF "udev_rules=\"$conf_tmp\"\n";
265 print CONF "udev_permissions=\"$perm\"\n";
266 close CONF;
267
268 foreach my $config (@tests) {
269         $config->{conf} =~ m/^([A-Z]*).*/;
270         my $method  = $1;
271
272         print "TEST: $config->{desc}\n";
273         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
274
275         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
276         if (-e "$PWD/$udev_root$config->{expected}") {
277                 print "add: ok    ";
278         } else {
279                 print "add: error\n";
280                 system("tree $udev_root");
281                 print "\n";
282                 $error++;
283         }
284
285         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
286         if ((-e "$PWD/$udev_root$config->{expected}") ||
287             (-l "$PWD/$udev_root$config->{expected}")) {
288                 print "remove: error\n\n";
289                 system("tree $udev_root");
290                 $error++;
291         } else {
292                 print "remove: ok\n\n";
293         }
294 }
295
296 print "$error errors occured\n\n";
297
298 # cleanup
299 unlink($udev_db);
300 system("rm -rf $udev_root");
301 unlink($conf_tmp);
302 unlink($main_conf);
303