chiark / gitweb /
[PATCH] udev-test.pl add subdir test
[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 $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     => "replace kernel name",
55                 subsys   => "tty",
56                 devpath  => "class/tty/ttyUSB0",
57                 expected => "visor" ,
58                 conf     => <<EOF
59 REPLACE, KERNEL="ttyUSB0", NAME="visor"
60 EOF
61         },
62         {
63                 desc     => "subdirectory handling",
64                 subsys   => "tty",
65                 devpath  => "class/tty/ttyUSB0",
66                 expected => "sub/direct/ory/visor" ,
67                 conf     => <<EOF
68 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
69 EOF
70         },
71         {
72                 desc     => "place on bus of scsi partition",
73                 subsys   => "block",
74                 devpath  => "block/sda/sda3",
75                 expected => "first_disk3" ,
76                 conf     => <<EOF
77 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
78 EOF
79         },
80         {
81                 desc     => "test NAME substitution chars",
82                 subsys   => "block",
83                 devpath  => "block/sda/sda3",
84                 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
85                 conf     => <<EOF
86 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
87 EOF
88         },
89         {
90                 desc     => "callout result substitution, only last should match",
91                 subsys   => "block",
92                 devpath  => "block/sda/sda3",
93                 expected => "special-device-3" ,
94                 conf     => <<EOF
95 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
96 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
97 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
98 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
99 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
100 EOF
101         },
102         {
103                 desc     => "callout program substitution",
104                 subsys   => "block",
105                 devpath  => "block/sda/sda3",
106                 expected => "test-0:0:0:0" ,
107                 conf     => <<EOF
108 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
109 EOF
110         },
111 );
112
113 # set env
114 $ENV{UDEV_TEST} = "yes";
115 $ENV{SYSFS_PATH} = $sysfs;
116 $ENV{UDEV_CONFIG_DIR} = "./";
117 $ENV{UDEV_ROOT} = $udev_root;
118 $ENV{UDEV_DB} = $udev_db;
119 $ENV{UDEV_PERMISSION_FILE} = $perm;
120
121
122 sub udev {
123         my ($action, $subsys, $devpath, $config) = @_;
124
125         $ENV{DEVPATH} = $devpath;
126         $ENV{UDEV_CONFIG_FILE} = $conf_tmp;
127
128         # create temporary config
129         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
130         print CONF $$config;
131         close CONF;
132
133         $ENV{ACTION} = $action;
134         system("$udev_bin $subsys");
135 }
136
137
138 # prepare
139 system("rm -rf $udev_root");
140 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
141
142 # test
143 my $error = 0;
144 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
145
146 foreach my $config (@tests) {
147         $config->{conf} =~ m/^([A-Z]*).*/;
148         my $method  = $1;
149
150         print "TEST: $config->{desc}\n";
151         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
152
153         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
154         if (-e "$PWD/$udev_root$config->{expected}") {
155                 print "add: ok    ";
156         } else {
157                 print "add: error\n";
158                 system("tree $udev_root");
159                 print "\n";
160                 $error++;
161         }
162
163         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
164         if (-e "$PWD/$udev_root$config->{expected}") {
165                 print "remove: error\n\n";
166                 system("tree $udev_root");
167                 $error++;
168         } else {
169                 print "remove: ok\n\n";
170         }
171 }
172
173 print "$error errors occured\n\n";
174
175 # cleanup
176 system("rm -rf $udev_root");
177 unlink($conf_tmp);
178 unlink($udev_db);
179