]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
[PATCH] allow multiple symlinks
[thirdparty/systemd.git] / test / udev-test.pl
CommitLineData
a367f04e
GKH
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
21use warnings;
22use strict;
23
f8f00338 24my $PWD = $ENV{PWD};
a367f04e
GKH
25my $sysfs = "sys/";
26my $udev_bin = "../udev";
27my $udev_root = "udev-root/"; # !!! directory will be removed !!!
36043f84 28my $udev_db = ".udev.tdb";
a367f04e 29my $perm = "udev.permissions";
72ffa78d
GKH
30my $main_conf = "udev-test.conf";
31my $conf_tmp = "udev-test.rules";
a367f04e
GKH
32
33
34my @tests = (
35 {
36 desc => "label test of scsi disc",
37 subsys => "block",
38 devpath => "block/sda",
39 expected => "boot_disk" ,
40 conf => <<EOF
41LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
3d150dfb 42REPLACE, KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
43EOF
44 },
45 {
46 desc => "label test of scsi partition",
47 subsys => "block",
48 devpath => "block/sda/sda1",
49 expected => "boot_disk1" ,
50 conf => <<EOF
51LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
83be97ba
KS
52EOF
53 },
54 {
55 desc => "label test of pattern match",
56 subsys => "block",
57 devpath => "block/sda/sda1",
58 expected => "boot_disk1" ,
59 conf => <<EOF
60LABEL, BUS="scsi", vendor="?IBM-ESXS", NAME="boot_disk%n-1"
61LABEL, BUS="scsi", vendor="IBM-ESXS?", NAME="boot_disk%n-2"
62LABEL, BUS="scsi", vendor="IBM-ES??", NAME="boot_disk%n"
63LABEL, BUS="scsi", vendor="IBM-ESXSS", NAME="boot_disk%n-3"
0db6d4cc
KS
64EOF
65 },
66 {
9f1da361 67 desc => "catch device by *",
0db6d4cc
KS
68 subsys => "tty",
69 devpath => "class/tty/ttyUSB0",
70 expected => "visor/0" ,
71 conf => <<EOF
72REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
9f1da361
KS
73EOF
74 },
75 {
76 desc => "catch device by ?",
77 subsys => "tty",
78 devpath => "class/tty/ttyUSB0",
79 expected => "visor/0" ,
80 conf => <<EOF
81REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
82REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
83REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
84EOF
85 },
86 {
87 desc => "catch device by character class",
88 subsys => "tty",
89 devpath => "class/tty/ttyUSB0",
90 expected => "visor/0" ,
91 conf => <<EOF
92REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
93REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
94REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
a367f04e
GKH
95EOF
96 },
97 {
98 desc => "replace kernel name",
99 subsys => "tty",
100 devpath => "class/tty/ttyUSB0",
101 expected => "visor" ,
102 conf => <<EOF
103REPLACE, KERNEL="ttyUSB0", NAME="visor"
5499d319
KS
104EOF
105 },
106 {
107 desc => "subdirectory handling",
108 subsys => "tty",
109 devpath => "class/tty/ttyUSB0",
110 expected => "sub/direct/ory/visor" ,
111 conf => <<EOF
112REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
a367f04e
GKH
113EOF
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
121TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
122EOF
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
130TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
131EOF
132 },
133 {
9f1da361 134 desc => "callout result substitution",
a367f04e
GKH
135 subsys => "block",
136 devpath => "block/sda/sda3",
137 expected => "special-device-3" ,
138 conf => <<EOF
139CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
140CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
141CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
142CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
143CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
f3b04a2e
GKH
144EOF
145 },
146 {
f8f00338 147 desc => "callout program substitution",
f3b04a2e
GKH
148 subsys => "block",
149 devpath => "block/sda/sda3",
150 expected => "test-0:0:0:0" ,
151 conf => <<EOF
152CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
36043f84
GKH
153EOF
154 },
155 {
156 desc => "devfs disk naming substitution",
157 subsys => "block",
158 devpath => "block/sda",
159 expected => "lun0/disk" ,
160 conf => <<EOF
161LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
162EOF
163 },
164 {
165 desc => "devfs disk naming substitution",
166 subsys => "block",
167 devpath => "block/sda/sda2",
168 expected => "lun0/part2" ,
169 conf => <<EOF
170LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
772558f4
GKH
171EOF
172 },
173 {
174 desc => "callout bus type",
175 subsys => "block",
176 devpath => "block/sda",
177 expected => "scsi-0:0:0:0" ,
178 conf => <<EOF
179CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
180CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
181CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
3d150dfb
KS
182EOF
183 },
184 {
185 desc => "symlink creation (same directory)",
186 subsys => "tty",
187 devpath => "class/tty/ttyUSB0",
188 expected => "visor0" ,
189 conf => <<EOF
190REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
191EOF
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
199LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
200EOF
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
208LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
209EOF
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
217LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
4763256c
KS
218EOF
219 },
220 {
221 desc => "multiple symlinks",
222 subsys => "tty",
223 devpath => "class/tty/ttyUSB0",
224 expected => "second-0" ,
225 conf => <<EOF
226REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
a367f04e
GKH
227EOF
228 },
229);
230
231# set env
232$ENV{UDEV_TEST} = "yes";
233$ENV{SYSFS_PATH} = $sysfs;
72ffa78d 234$ENV{UDEV_CONFIG_FILE} = $main_conf;
a367f04e
GKH
235
236
237sub udev {
238 my ($action, $subsys, $devpath, $config) = @_;
239
240 $ENV{DEVPATH} = $devpath;
a367f04e
GKH
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
253system("rm -rf $udev_root");
254mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
255
256# test
e5fbfe0a 257my $error = 0;
a367f04e
GKH
258print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
259
72ffa78d
GKH
260# create initial config file
261open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
262print CONF "udev_root=\"$udev_root\"\n";
263print CONF "udev_db=\"$udev_db\"\n";
264print CONF "udev_rules=\"$conf_tmp\"\n";
265print CONF "udev_permissions=\"$perm\"\n";
266close CONF;
267
a367f04e
GKH
268foreach 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++;
a367f04e
GKH
283 }
284
285 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
3d150dfb
KS
286 if ((-e "$PWD/$udev_root$config->{expected}") ||
287 (-l "$PWD/$udev_root$config->{expected}")) {
a367f04e
GKH
288 print "remove: error\n\n";
289 system("tree $udev_root");
290 $error++;
291 } else {
292 print "remove: ok\n\n";
293 }
294}
295
296print "$error errors occured\n\n";
297
298# cleanup
36043f84 299unlink($udev_db);
a367f04e
GKH
300system("rm -rf $udev_root");
301unlink($conf_tmp);
72ffa78d 302unlink($main_conf);
a367f04e 303