chiark / gitweb /
[PATCH] experimental (very simple) SYMLINK creation
[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/disk" ,
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
222 # set env
223 $ENV{UDEV_TEST} = "yes";
224 $ENV{SYSFS_PATH} = $sysfs;
225 $ENV{UDEV_CONFIG_FILE} = $main_conf;
226
227
228 sub udev {
229         my ($action, $subsys, $devpath, $config) = @_;
230
231         $ENV{DEVPATH} = $devpath;
232
233         # create temporary config
234         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
235         print CONF $$config;
236         close CONF;
237
238         $ENV{ACTION} = $action;
239         system("$udev_bin $subsys");
240 }
241
242
243 # prepare
244 system("rm -rf $udev_root");
245 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
246
247 # test
248 my $error = 0;
249 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
250
251 # create initial config file
252 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
253 print CONF "udev_root=\"$udev_root\"\n";
254 print CONF "udev_db=\"$udev_db\"\n";
255 print CONF "udev_rules=\"$conf_tmp\"\n";
256 print CONF "udev_permissions=\"$perm\"\n";
257 close CONF;
258
259 foreach my $config (@tests) {
260         $config->{conf} =~ m/^([A-Z]*).*/;
261         my $method  = $1;
262
263         print "TEST: $config->{desc}\n";
264         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
265
266         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
267         if (-e "$PWD/$udev_root$config->{expected}") {
268                 print "add: ok    ";
269         } else {
270                 print "add: error\n";
271                 system("tree $udev_root");
272                 print "\n";
273                 $error++;
274         }
275
276         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
277         if ((-e "$PWD/$udev_root$config->{expected}") ||
278             (-l "$PWD/$udev_root$config->{expected}")) {
279                 print "remove: error\n\n";
280                 system("tree $udev_root");
281                 $error++;
282         } else {
283                 print "remove: ok\n\n";
284         }
285 }
286
287 print "$error errors occured\n\n";
288
289 # cleanup
290 unlink($udev_db);
291 system("rm -rf $udev_root");
292 unlink($conf_tmp);
293 unlink($main_conf);
294