fix dumptool on macos
[openafs.git] / src / tests / run-suite.pl
1 #!/usr/bin/env perl
2 # Copyright (C) 2000 by Sam Hartman
3 # This file may be copied either under the terms of the GNU GPL or the IBM Public License
4 # either version 2 or later of the GPL or version 1.0 or later of the IPL.
5
6 use Term::ReadLine;
7 use strict;
8 use OpenAFS::ConfigUtils;
9 use OpenAFS::Dirpath;
10 use OpenAFS::OS;
11 use OpenAFS::Auth;
12 use File::Copy;
13 use Getopt::Long;
14 use vars qw($admin $server $cellname $cachesize $part
15           $shutdown_needed $csdb);
16 my $rl = new Term::ReadLine('run-suite');
17
18 =head1  NAME
19
20    run-suite - Set up AFS cell and test.
21
22 =head1 SYNOPSIS
23
24 B<run-suite> [B<--cellname> cellname] [B<--cachesize> size]
25
26 =head1 DESCRIPTION
27
28
29 This script sets up an AFS cell, then runs a suite of tests against the 
30 cell to verify the build.
31
32 The B<cellname> option specifies the name of the cell.
33
34 The B<cachesize> option specifies the size of the AFS cache.
35
36 =cut
37
38 # main script
39
40 # mkvol(volume, mount)
41 sub mkvol($$) {
42     my ($vol, $mnt) = @_;
43     run("$openafsdirpath->{'afssrvsbindir'}/vos create $server $part $vol -localauth");
44     unwind("$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part $vol -localauth");
45     run("$openafsdirpath->{'afssrvbindir'}/fs mkm $mnt $vol ");
46     run("$openafsdirpath->{'afssrvbindir'}/fs sa $mnt system:anyuser rl");
47 }
48
49 GetOptions (
50            "cellname=s" => \$cellname, 
51            "cachesize=s" => \$cachesize,
52            "partition=s" => \$part,
53            "admin=s" => \$admin);
54
55 if ($> != 0) {
56   die "This script should almost always be run as root.\n";
57 }
58
59 open(MOUNT, "mount |") or die "Failed to run mount: $!\n";
60 while(<MOUNT>) {
61   if(m:^AFS:) {
62     print "The AFS client is currently running on this workstation.\n";
63     print "Please restart this script after running $openafsinitcmd->{'client-stop'}\n";
64     exit(1);
65   }
66   if(m:^/afs on AFS:) {
67     print "The AFS client is currently running on this workstation.\n";
68     print "Please restart this script after running $openafsinitcmd->{'client-stop'}\n";
69     exit(1);
70   }
71 }
72 close MOUNT;
73
74 print "If the fileserver is not running, this may hang for 30 seconds.\n";
75 run("$openafsinitcmd->{'filesrv-stop'}");
76 $server = `hostname`;
77 chomp $server;
78 $admin = "admin" unless $admin;
79 $admin =~ s:/:.:g;
80 if($admin =~ /@/) {
81 die "The administrative user must be in the same realm as the cell and no realm may be specified.\n";
82 }
83
84 $cellname = $rl->readline("What cellname should be used? ") unless $cellname;
85 die "Please specify a cellname\n" unless $cellname;
86
87 unlink "$openafsdirpath->{'viceetcdir'}/CellServDB";
88 unlink "$openafsdirpath->{'viceetcdir'}/ThisCell";
89 if ($cellname eq "this.thirty.nine.character.name.for.sed") {
90   &uudecode("kaserver.DB0.uu");
91   &uudecode("kaserver.DBSYS1.uu");
92   &uudecode("KeyFile.uu");
93   copy("kaserver.DB0","$openafsdirpath->{'afsdbdir'}/kaserver.DB0");
94   copy("kaserver.DBSYS1","$openafsdirpath->{'afsdbdir'}/kaserver.DBSYS1");
95   copy("KeyFile","$openafsdirpath->{'afsconfdir'}/KeyFile");
96 }
97
98 unless ( -f "$openafsdirpath->{'afsconfdir'}/KeyFile") {
99   print "You do not have an AFS keyfile.  Please create this using asetkey from openafs-krb5 or the bos addkey command";
100   exit(1);
101 }
102
103 my $lcell = "${cellname}";
104
105 #let bosserver create symlinks
106 run("$openafsinitcmd->{'filesrv-start'}");
107 unwind("$openafsinitcmd->{'filesrv-stop'}");
108 unwind("/bin/rm -f $openafsdirpath->{'afslocaldir'}/BosConfig");
109 $shutdown_needed = 1;
110
111 open(THISCELL, ">$openafsdirpath->{'afsconfdir'}/ThisCell");
112 print THISCELL $cellname;
113 close THISCELL;
114 open(THISCELL, ">$openafsdirpath->{'afsconfdir'}/CellServDB");
115 print THISCELL ">$cellname #test cell";
116 close THISCELL;
117
118 run ("$openafsdirpath->{'afssrvbindir'}/bos setcellname $server $lcell -localauth ||true");
119 run ("$openafsdirpath->{'afssrvbindir'}/bos addhost $server $server -localauth ||true");
120 run("$openafsdirpath->{'afssrvbindir'}/bos adduser $server $admin -localauth");
121 unwind("$openafsdirpath->{'afssrvbindir'}/bos removeuser $server $admin -localauth");
122 if ( -f "$openafsdirpath->{'afsdbdir'}/prdb.DB0" ) {
123   die "Protection database already exists; cell already partially created\n";
124  }
125 open(PRDB, "|$openafsdirpath->{'afssrvsbindir'}/pt_util -p $openafsdirpath->{'afsdbdir'}/prdb.DB0 -w ")
126 or die "Unable to start pt_util: $!\n";
127 print PRDB "$admin 128/20 1 -204 -204\n";
128 print PRDB "system:administrators 130/20 -204 -204 -204\n";
129 print PRDB" $admin 1\n";
130 close PRDB;
131 unwind( "rm $openafsdirpath->{'afsdbdir'}/prdb.DB* ");
132 # Start up ptserver and vlserver
133 run("$openafsdirpath->{'afssrvbindir'}/bos create $server ptserver simple $openafsdirpath->{'afssrvlibexecdir'}/ptserver -localauth");
134 unwind("$openafsdirpath->{'afssrvbindir'}/bos delete $server ptserver -localauth");
135 unwind("$openafsdirpath->{'afssrvbindir'}/bos stop $server ptserver -localauth -wait");
136
137 run("$openafsdirpath->{'afssrvbindir'}/bos create $server vlserver simple $openafsdirpath->{'afssrvlibexecdir'}/vlserver -localauth");
138 unwind( "rm $openafsdirpath->{'afsdbdir'}/vldb.DB* ");
139 unwind("$openafsdirpath->{'afssrvbindir'}/bos delete $server vlserver -localauth");
140 unwind("$openafsdirpath->{'afssrvbindir'}/bos stop $server vlserver -localauth -wait");
141
142 run("$openafsdirpath->{'afssrvbindir'}/bos create $server kaserver simple $openafsdirpath->{'afssrvlibexecdir'}/kaserver -localauth");
143 unwind( "rm $openafsdirpath->{'afsdbdir'}/kaserver.DB* ");
144 unwind("$openafsdirpath->{'afssrvbindir'}/bos delete $server kaserver -localauth");
145 unwind("$openafsdirpath->{'afssrvbindir'}/bos stop $server kaserver -localauth -wait");
146
147 run( "$openafsdirpath->{'afssrvbindir'}/bos create $server fs fs ".
148      "-cmd $openafsdirpath->{'afssrvlibexecdir'}/fileserver ".
149      "-cmd $openafsdirpath->{'afssrvlibexecdir'}/volserver ".
150      "-cmd $openafsdirpath->{'afssrvlibexecdir'}/salvager -localauth");
151 unwind( "$openafsdirpath->{'afssrvbindir'}/bos delete $server fs -localauth ");
152 unwind( "$openafsdirpath->{'afssrvbindir'}/bos stop $server fs -localauth -wait");
153 run("$openafsdirpath->{'afssrvbindir'}/bos restart $server -all -bosserver -localauth");
154
155 print "Waiting for database elections: ";
156 sleep(90);
157 print "done.\n";
158 # Past this point we want to control when bos shutdown happens
159 $shutdown_needed = 0;
160 $part = "a" unless $part;
161
162 unwind( "$openafsdirpath->{'afssrvbindir'}/bos shutdown $server -localauth ");
163 run("$openafsdirpath->{'afssrvsbindir'}/vos create $server $part root.afs -localauth");
164 unwind("$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part root.afs -localauth");
165
166 # bring up client
167
168 $cachesize = $rl->readline("What size cache (in 1k blocks)? ") unless $cachesize;
169 die "Please specify a cache size\n" unless $cachesize;
170
171 mkdir "/usr/vice/cache", 0700;
172 mkdir "/afs", 0777;
173
174 run("echo /afs:/usr/vice/cache:${cachesize} >$openafsdirpath->{'viceetcdir'}/cacheinfo");
175 run("$openafsinitcmd->{'client-forcestart'}");
176 my $afs_running = 0;
177 open(MOUNT, "mount |") or die "Failed to run mount: $!\n";
178   if(m:^AFS:) {
179     print "The AFS client is currently running on this workstation.\n";
180     print "Please restart this script after running $openafsinitcmd->{'client-stop'}\n";
181     exit(1);
182   }
183 while(<MOUNT>) {
184   if(m:^AFS:) {
185     $afs_running = 1;
186   }
187   if(m:^/afs on AFS:) {
188     $afs_running = 1;
189   }
190 }
191 unless ($afs_running) {
192 print "*** The AFS client failed to start.\n";
193 print  "Please fix whatever problem kept it from running.\n";
194        exit(1);
195 }
196 unwind("$openafsinitcmd->{'client-stop'}");
197
198 &OpenAFS::Auth::authadmin();
199
200 run("$openafsdirpath->{'afssrvbindir'}/fs sa /afs system:anyuser rl");
201
202 run("$openafsdirpath->{'afssrvsbindir'}/vos create $server $part root.cell -localauth");
203 unwind("$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part root.cell -localauth");
204 # We make root.cell s:anyuser readable after we mount in the next
205 # loop.
206 open(CELLSERVDB, "$openafsdirpath->{'viceetcdir'}/CellServDB")
207     or die "Unable to open $openafsdirpath->{'viceetcdir'}/CellServDB: $!\n";
208 while(<CELLSERVDB>) {
209     chomp;
210     if (/^>\s*([a-z0-9_\-.]+)/ ) {
211        run("$openafsdirpath->{'afssrvbindir'}/fs mkm /afs/$1 root.cell -cell $1 -fast");
212        unwind ("$openafsdirpath->{'afssrvbindir'}/fs rmm /afs/$1");
213    }
214 }
215
216 run("$openafsdirpath->{'afssrvbindir'}/fs sa /afs/$lcell system:anyuser rl");
217 run ("$openafsdirpath->{'afssrvbindir'}/fs mkm /afs/.$lcell root.cell -cell $lcell -rw");
218 unwind ("$openafsdirpath->{'afssrvbindir'}/fs rmm /afs/.$lcell");
219 run("$openafsdirpath->{'afssrvbindir'}/fs mkm /afs/.root.afs root.afs -rw");
220 unwind ("$openafsdirpath->{'afssrvbindir'}/fs rmm /afs/.root.afs");
221
222 mkvol( "user", "/afs/$lcell/user" );
223 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part user -localauth ");
224
225 mkvol( "service", "/afs/$lcell/service" );
226 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part service -localauth ");
227
228 mkvol( "rep", "/afs/$lcell/.replicated" );
229 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part rep -localauth ");
230 run( "$openafsdirpath->{'afssrvbindir'}/fs mkm /afs/$lcell/replicated rep.readonly " );
231
232 run( "$openafsdirpath->{'afssrvsbindir'}/vos addsite $server $part rep -localauth" );
233 run( "$openafsdirpath->{'afssrvsbindir'}/vos release rep -localauth" );
234 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part rep.readonly -localauth ");
235
236 mkvol( "unrep", "/afs/$lcell/unreplicated" );
237 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part unrep -localauth ");
238
239 $lcell =~ /^([^.]*)/;
240 my $cellpart = $1;
241 run("ln -s /afs/$lcell /afs/$cellpart");
242 unwind ("rm /afs/$cellpart");
243 run( "ln -s /afs/.$lcell /afs/.$cellpart" );
244 unwind ("rm /afs/.$cellpart");
245
246 run( "$openafsdirpath->{'afssrvsbindir'}/vos addsite $server $part root.afs -localauth" );
247 run( "$openafsdirpath->{'afssrvsbindir'}/vos addsite $server $part root.cell -localauth" );
248 run( "$openafsdirpath->{'afssrvsbindir'}/vos release root.afs -localauth" );
249 run( "$openafsdirpath->{'afssrvsbindir'}/vos release root.cell -localauth" );
250 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part root.cell.readonly -localauth ");
251 unwind( "$openafsdirpath->{'afssrvsbindir'}/vos remove $server $part root.afs.readonly -localauth ");
252
253 run("$openafsinitcmd->{'client-restart'}");
254
255 `cp ./t.uniq-bad /usr/tmp`;
256
257 system ("pagsh -c './test-front.sh $lcell'");
258
259 @unwinds = ();
260 END {
261 # If we fail before all the instances are created, we need to perform 
262 # our own bos shutdown
263     system("$openafsdirpath->{'afssrvbindir'}/bos shutdown $server -localauth") if $shutdown_needed;
264   run(pop @unwinds) while @unwinds;
265   }
266
267 # perl uudecode implementation originally from 
268 # http://www.perl.com/language/ppt/src/uudecode/uudecode.nick
269 sub uudecode {
270     my ( $filename ) = @_;
271     open(ENCODED, "$filename");
272     FILESPEC :
273     while (<ENCODED>) {
274         my ($mode, $file);
275         next FILESPEC unless ($mode,$file) = /^begin\s+(\d+)\s+(\S+)/;
276         open(OUT, "> $file") or die "can't create $file: $!";
277         binmode(OUT);       # winsop
278         # Quickly protect file before data is written.  
279         # XXX: Does this break on sub-Unix systems, like if 
280         #      it's a mode 400 or 000 file? If so, then we must 
281         #      wait until after the close.
282         chmod oct($mode), $file or die "can't chmod $file to mode $mode: $!";
283         my $ended = 0;
284       LINE:
285         while (<ENCODED>) {
286             if (/^end$/) {
287                 $ended = 1;
288                 last LINE;
289             } 
290             next LINE if /[a-z]/;
291             next LINE unless int((((ord() - 32) & 077) + 2) / 3) 
292                 == int(length() / 4);
293             print OUT unpack("u", $_)
294                 or die "can't write $file: $!";
295             
296         }
297         close(OUT) or die "can't close $file: $!";
298         $ended or die "missing end; $file may be truncated";
299     }
300     close(ENCODED);
301 }
302