chiark / gitweb /
b1757ee1bb587a972ad7a14ca9371e0a6a1e4bf5
[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
186 # set env
187 $ENV{UDEV_TEST} = "yes";
188 $ENV{SYSFS_PATH} = $sysfs;
189 $ENV{UDEV_CONFIG_FILE} = $main_conf;
190
191
192 sub udev {
193         my ($action, $subsys, $devpath, $config) = @_;
194
195         $ENV{DEVPATH} = $devpath;
196
197         # create temporary config
198         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
199         print CONF $$config;
200         close CONF;
201
202         $ENV{ACTION} = $action;
203         system("$udev_bin $subsys");
204 }
205
206
207 # prepare
208 system("rm -rf $udev_root");
209 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
210
211 # test
212 my $error = 0;
213 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
214
215 # create initial config file
216 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
217 print CONF "udev_root=\"$udev_root\"\n";
218 print CONF "udev_db=\"$udev_db\"\n";
219 print CONF "udev_rules=\"$conf_tmp\"\n";
220 print CONF "udev_permissions=\"$perm\"\n";
221 close CONF;
222
223 foreach my $config (@tests) {
224         $config->{conf} =~ m/^([A-Z]*).*/;
225         my $method  = $1;
226
227         print "TEST: $config->{desc}\n";
228         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
229
230         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
231         if (-e "$PWD/$udev_root$config->{expected}") {
232                 print "add: ok    ";
233         } else {
234                 print "add: error\n";
235                 system("tree $udev_root");
236                 print "\n";
237                 $error++;
238         }
239
240         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
241         if (-e "$PWD/$udev_root$config->{expected}") {
242                 print "remove: error\n\n";
243                 system("tree $udev_root");
244                 $error++;
245         } else {
246                 print "remove: ok\n\n";
247         }
248 }
249
250 print "$error errors occured\n\n";
251
252 # cleanup
253 unlink($udev_db);
254 system("rm -rf $udev_root");
255 unlink($conf_tmp);
256 unlink($main_conf);
257