]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
[PATCH] fix up the tests to work without all of the environ variables.
[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"
42REPLACE, KERNEL="ttyUSB0", NAME="visor""
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"
0db6d4cc
KS
52EOF
53 },
54 {
9f1da361 55 desc => "catch device by *",
0db6d4cc
KS
56 subsys => "tty",
57 devpath => "class/tty/ttyUSB0",
58 expected => "visor/0" ,
59 conf => <<EOF
60REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
9f1da361
KS
61EOF
62 },
63 {
64 desc => "catch device by ?",
65 subsys => "tty",
66 devpath => "class/tty/ttyUSB0",
67 expected => "visor/0" ,
68 conf => <<EOF
69REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
70REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
71REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
72EOF
73 },
74 {
75 desc => "catch device by character class",
76 subsys => "tty",
77 devpath => "class/tty/ttyUSB0",
78 expected => "visor/0" ,
79 conf => <<EOF
80REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
81REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
82REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
a367f04e
GKH
83EOF
84 },
85 {
86 desc => "replace kernel name",
87 subsys => "tty",
88 devpath => "class/tty/ttyUSB0",
89 expected => "visor" ,
90 conf => <<EOF
91REPLACE, KERNEL="ttyUSB0", NAME="visor"
5499d319
KS
92EOF
93 },
94 {
95 desc => "subdirectory handling",
96 subsys => "tty",
97 devpath => "class/tty/ttyUSB0",
98 expected => "sub/direct/ory/visor" ,
99 conf => <<EOF
100REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
a367f04e
GKH
101EOF
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
109TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
110EOF
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
118TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
119EOF
120 },
121 {
9f1da361 122 desc => "callout result substitution",
a367f04e
GKH
123 subsys => "block",
124 devpath => "block/sda/sda3",
125 expected => "special-device-3" ,
126 conf => <<EOF
127CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
128CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
129CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
130CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
131CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
f3b04a2e
GKH
132EOF
133 },
134 {
f8f00338 135 desc => "callout program substitution",
f3b04a2e
GKH
136 subsys => "block",
137 devpath => "block/sda/sda3",
138 expected => "test-0:0:0:0" ,
139 conf => <<EOF
140CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
36043f84
GKH
141EOF
142 },
143 {
144 desc => "devfs disk naming substitution",
145 subsys => "block",
146 devpath => "block/sda",
147 expected => "lun0/disk" ,
148 conf => <<EOF
149LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
150EOF
151 },
152 {
153 desc => "devfs disk naming substitution",
154 subsys => "block",
155 devpath => "block/sda/sda2",
156 expected => "lun0/part2" ,
157 conf => <<EOF
158LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D"
772558f4
GKH
159EOF
160 },
161 {
162 desc => "callout bus type",
163 subsys => "block",
164 devpath => "block/sda",
165 expected => "scsi-0:0:0:0" ,
166 conf => <<EOF
167CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
168CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
169CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
a367f04e
GKH
170EOF
171 },
172);
173
174# set env
175$ENV{UDEV_TEST} = "yes";
176$ENV{SYSFS_PATH} = $sysfs;
72ffa78d 177$ENV{UDEV_CONFIG_FILE} = $main_conf;
a367f04e
GKH
178
179
180sub udev {
181 my ($action, $subsys, $devpath, $config) = @_;
182
183 $ENV{DEVPATH} = $devpath;
a367f04e
GKH
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
196system("rm -rf $udev_root");
197mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
198
199# test
e5fbfe0a 200my $error = 0;
a367f04e
GKH
201print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
202
72ffa78d
GKH
203# create initial config file
204open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
205print CONF "udev_root=\"$udev_root\"\n";
206print CONF "udev_db=\"$udev_db\"\n";
207print CONF "udev_rules=\"$conf_tmp\"\n";
208print CONF "udev_permissions=\"$perm\"\n";
209close CONF;
210
a367f04e
GKH
211foreach 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++;
a367f04e
GKH
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
238print "$error errors occured\n\n";
239
240# cleanup
36043f84 241unlink($udev_db);
a367f04e
GKH
242system("rm -rf $udev_root");
243unlink($conf_tmp);
72ffa78d 244unlink($main_conf);
a367f04e 245