chiark / gitweb /
[PATCH] fix up the tests to work without all of the environ variables.
[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     => "catch device by *",
56                 subsys   => "tty",
57                 devpath  => "class/tty/ttyUSB0",
58                 expected => "visor/0" ,
59                 conf     => <<EOF
60 REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
61 EOF
62         },
63         {
64                 desc     => "catch device by ?",
65                 subsys   => "tty",
66                 devpath  => "class/tty/ttyUSB0",
67                 expected => "visor/0" ,
68                 conf     => <<EOF
69 REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
70 REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
71 REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
72 EOF
73         },
74         {
75                 desc     => "catch device by character class",
76                 subsys   => "tty",
77                 devpath  => "class/tty/ttyUSB0",
78                 expected => "visor/0" ,
79                 conf     => <<EOF
80 REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
81 REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
82 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
83 EOF
84         },
85         {
86                 desc     => "replace kernel name",
87                 subsys   => "tty",
88                 devpath  => "class/tty/ttyUSB0",
89                 expected => "visor" ,
90                 conf     => <<EOF
91 REPLACE, KERNEL="ttyUSB0", NAME="visor"
92 EOF
93         },
94         {
95                 desc     => "subdirectory handling",
96                 subsys   => "tty",
97                 devpath  => "class/tty/ttyUSB0",
98                 expected => "sub/direct/ory/visor" ,
99                 conf     => <<EOF
100 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
101 EOF
102         },
103         {
104                 desc     => "place on bus of scsi partition",
105                 subsys   => "block",
106                 devpath  => "block/sda/sda3",
107                 expected => "first_disk3" ,
108                 conf     => <<EOF
109 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
110 EOF
111         },
112         {
113                 desc     => "test NAME substitution chars",
114                 subsys   => "block",
115                 devpath  => "block/sda/sda3",
116                 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
117                 conf     => <<EOF
118 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
119 EOF
120         },
121         {
122                 desc     => "callout result substitution",
123                 subsys   => "block",
124                 devpath  => "block/sda/sda3",
125                 expected => "special-device-3" ,
126                 conf     => <<EOF
127 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
128 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
129 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
130 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
131 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
132 EOF
133         },
134         {
135                 desc     => "callout program substitution",
136                 subsys   => "block",
137                 devpath  => "block/sda/sda3",
138                 expected => "test-0:0:0:0" ,
139                 conf     => <<EOF
140 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
141 EOF
142         },
143         {
144                 desc     => "devfs disk naming substitution",
145                 subsys   => "block",
146                 devpath  => "block/sda",
147                 expected => "lun0/disk" ,
148                 conf     => <<EOF
149 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
150 EOF
151         },
152         {
153                 desc     => "devfs disk naming substitution",
154                 subsys   => "block",
155                 devpath  => "block/sda/sda2",
156                 expected => "lun0/part2" ,
157                 conf     => <<EOF
158 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
159 EOF
160         },
161         {
162                 desc     => "callout bus type",
163                 subsys   => "block",
164                 devpath  => "block/sda",
165                 expected => "scsi-0:0:0:0" ,
166                 conf     => <<EOF
167 CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
168 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
169 CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
170 EOF
171         },
172 );
173
174 # set env
175 $ENV{UDEV_TEST} = "yes";
176 $ENV{SYSFS_PATH} = $sysfs;
177 $ENV{UDEV_CONFIG_FILE} = $main_conf;
178
179
180 sub udev {
181         my ($action, $subsys, $devpath, $config) = @_;
182
183         $ENV{DEVPATH} = $devpath;
184
185         # create temporary config
186         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
187         print CONF $$config;
188         close CONF;
189
190         $ENV{ACTION} = $action;
191         system("$udev_bin $subsys");
192 }
193
194
195 # prepare
196 system("rm -rf $udev_root");
197 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
198
199 # test
200 my $error = 0;
201 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
202
203 # create initial config file
204 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
205 print CONF "udev_root=\"$udev_root\"\n";
206 print CONF "udev_db=\"$udev_db\"\n";
207 print CONF "udev_rules=\"$conf_tmp\"\n";
208 print CONF "udev_permissions=\"$perm\"\n";
209 close CONF;
210
211 foreach my $config (@tests) {
212         $config->{conf} =~ m/^([A-Z]*).*/;
213         my $method  = $1;
214
215         print "TEST: $config->{desc}\n";
216         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
217
218         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
219         if (-e "$PWD/$udev_root$config->{expected}") {
220                 print "add: ok    ";
221         } else {
222                 print "add: error\n";
223                 system("tree $udev_root");
224                 print "\n";
225                 $error++;
226         }
227
228         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
229         if (-e "$PWD/$udev_root$config->{expected}") {
230                 print "remove: error\n\n";
231                 system("tree $udev_root");
232                 $error++;
233         } else {
234                 print "remove: ok\n\n";
235         }
236 }
237
238 print "$error errors occured\n\n";
239
240 # cleanup
241 unlink($udev_db);
242 system("rm -rf $udev_root");
243 unlink($conf_tmp);
244 unlink($main_conf);
245