chiark / gitweb /
[PATCH] added ability to put format specifiers in the CALLOUT program string.
[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 = `pwd`;
25 chomp($PWD);
26
27 my $sysfs     = "sys/";
28 my $udev_bin  = "../udev";
29 my $udev_root = "udev-root/"; # !!! directory will be removed !!!
30 my $udev_db   = "udev.tdb";
31 my $perm      = "udev.permissions";
32 my $conf_tmp  = "udev-test.config";
33
34
35 my @tests = (
36         {
37                 desc     => "label test of scsi disc",
38                 subsys   => "block",
39                 devpath  => "block/sda",
40                 expected => "boot_disk" ,
41                 conf     => <<EOF
42 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
43 REPLACE, KERNEL="ttyUSB0", NAME="visor""
44 EOF
45         },
46         {
47                 desc     => "label test of scsi partition",
48                 subsys   => "block",
49                 devpath  => "block/sda/sda1",
50                 expected => "boot_disk1" ,
51                 conf     => <<EOF
52 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
53 EOF
54         },
55         {
56                 desc     => "replace kernel name",
57                 subsys   => "tty",
58                 devpath  => "class/tty/ttyUSB0",
59                 expected => "visor" ,
60                 conf     => <<EOF
61 REPLACE, KERNEL="ttyUSB0", NAME="visor"
62 EOF
63         },
64         {
65                 desc     => "place on bus of scsi partition",
66                 subsys   => "block",
67                 devpath  => "block/sda/sda3",
68                 expected => "first_disk3" ,
69                 conf     => <<EOF
70 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
71 EOF
72         },
73         {
74                 desc     => "test NAME substitution chars",
75                 subsys   => "block",
76                 devpath  => "block/sda/sda3",
77                 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
78                 conf     => <<EOF
79 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
80 EOF
81         },
82         {
83                 desc     => "callout result subtitution, only last should match",
84                 subsys   => "block",
85                 devpath  => "block/sda/sda3",
86                 expected => "special-device-3" ,
87                 conf     => <<EOF
88 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
89 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
90 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
91 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
92 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
93 EOF
94         },
95         {
96                 desc     => "callout program subtitution",
97                 subsys   => "block",
98                 devpath  => "block/sda/sda3",
99                 expected => "test-0:0:0:0" ,
100                 conf     => <<EOF
101 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
102 EOF
103         },
104 );
105
106 # set env
107 $ENV{UDEV_TEST} = "yes";
108 $ENV{SYSFS_PATH} = $sysfs;
109 $ENV{UDEV_CONFIG_DIR} = "./";
110 $ENV{UDEV_ROOT} = $udev_root;
111 $ENV{UDEV_DB} = $udev_db;
112 $ENV{UDEV_PERMISSION_FILE} = $perm;
113
114
115 sub udev {
116         my ($action, $subsys, $devpath, $config) = @_;
117
118         $ENV{DEVPATH} = $devpath;
119         $ENV{UDEV_CONFIG_FILE} = $conf_tmp;
120
121         # create temporary config
122         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
123         print CONF $$config;
124         close CONF;
125
126         $ENV{ACTION} = $action;
127         system("$udev_bin $subsys");
128 }
129
130
131 # prepare
132 system("rm -rf $udev_root");
133 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
134
135 # test
136 my $error = 0;
137 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
138
139 foreach my $config (@tests) {
140         $config->{conf} =~ m/^([A-Z]*).*/;
141         my $method  = $1;
142
143         print "TEST: $config->{desc}\n";
144         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
145
146         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
147         if (-e "$PWD/$udev_root$config->{expected}") {
148                 print "add: ok    ";
149         } else {
150                 print "add: error\n";
151                 system("tree $udev_root");
152                 print "\n";
153                 $error++;
154 #               next;
155         }
156
157         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
158         if (-e "$PWD/$udev_root$config->{expected}") {
159                 print "remove: error\n\n";
160                 system("tree $udev_root");
161                 $error++;
162         } else {
163                 print "remove: ok\n\n";
164         }
165 }
166
167 print "$error errors occured\n\n";
168
169 # cleanup
170 system("rm -rf $udev_root");
171 unlink($conf_tmp);
172 unlink($udev_db);
173