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