Improve command-line interface to run-tests.
[pintos-anon] / grading / userprog / run-tests
1 #! /usr/bin/perl
2
3 # Find the directory that contains the grading files.
4 our ($GRADES_DIR);
5
6 # Add our Perl library directory to the include path. 
7 BEGIN {
8     ($GRADES_DIR = $0) =~ s#/[^/]+$##;
9     -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
10     unshift @INC, "$GRADES_DIR/../lib";
11 }
12
13 use warnings;
14 use strict;
15 use Pintos::Grading;
16
17 our ($hw) = "userprog";
18 our (@TESTS);           # Tests to run.
19 our ($test);
20 our (%extra);
21 our ($action);
22
23 parse_cmd_line qw (args-argc args-argv0 args-argvn args-single args-multiple
24                    args-dbl-space
25                    sc-bad-sp sc-bad-arg sc-boundary
26                    halt exit
27                    create-normal create-empty create-null create-bad-ptr 
28                    create-long create-exists create-bound
29                    open-normal open-missing open-boundary open-empty open-null
30                    open-bad-ptr open-twice
31                    close-normal close-twice close-stdin close-stdout
32                    close-bad-fd
33                    read-normal read-bad-ptr read-boundary read-zero read-stdout
34                    read-bad-fd
35                    write-normal write-bad-ptr write-boundary write-zero
36                    write-stdin write-bad-fd
37                    exec-once exec-arg exec-multiple exec-missing exec-bad-ptr
38                    join-simple join-twice join-killed join-bad-pid
39                    multi-recurse multi-oom multi-child-fd);
40
41 # Default set of tests.
42 @TESTS =  unless @TESTS > 0;
43
44 clean_dir (), exit if $action eq 'clean';
45
46 extract_sources (); 
47 exit if $action eq 'extract';
48
49 build (); 
50 exit if $action eq 'build';
51
52 run_and_grade_tests (); 
53 write_grades (); 
54 write_details ();
55 exit if $action eq 'test';
56
57 assemble_final_grade ();
58 exit if $action eq 'assemble';
59
60 die "Don't know how to '$action'";
61
62 # Runs $test in directory output/$test.
63 # Returns 'ok' if it went ok, otherwise an explanation.
64 sub run_test {
65     xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
66              DIE => "cp failed\n");
67
68     my ($args) = "";
69     $args = 'some arguments for you!'
70         if grep ($_ eq $test, qw(args-argc args-argv0
71                                  args-argvn args-multiple));
72     $args = 'onearg' if $test eq 'args-single';
73     $args = 'two  args' if $test eq 'args-dbl-space';
74     $args = '15' if $test eq 'multi-recurse';
75     $args = '0' if $test eq 'multi-oom';
76     $args = " $args" if $args ne '';
77
78     # Run.
79     my ($timeout) = $test !~ /^multi-/ ? 10 : 600;
80     my ($result) = run_pintos ("pintos "
81                                . "--os-disk=pintos/src/userprog/build/os.dsk "
82                                . "--fs-disk=output/$test/fs.dsk "
83                                . "-v run -q -ex \"$test$args\"",
84                                LOG => "$test/run",
85                                TIMEOUT => $timeout);
86     rename "output/$test/fs.dsk", "output/$test/fs.dsk.keep"
87         if $test eq 'write-normal';
88     return $result;
89 }
90 \f
91 sub grade_write_normal {
92     my (@output) = @_;
93     verify_common (@output);
94     compare_output ("$GRADES_DIR/write-normal.exp", @output);
95     my ($test_txt) = "output/$test/test.txt";
96     get_file ("test.txt", $test_txt) if ! -e $test_txt;
97
98     my (@actual) = snarf ($test_txt);
99     my (@expected) = snarf ("$GRADES_DIR/sample.txt");
100
101     my ($eq);
102     if ($#actual == $#expected) {
103         $eq = 1;
104         for my $i (0...$#actual) {
105             $eq = 0 if $actual[$i] ne $expected[$i];
106         }
107     } else {
108         $eq = 0;
109     }
110     if (!$eq) {
111         my ($details);
112         $details = "Expected file content:\n";
113         $details .= join ('', map ("  $_\n", @expected));
114         $details .= "Actual file content:\n";
115         $details .= join ('', map ("  $_\n", @actual));
116         $extra{$test} = $details;
117
118         die "File written didn't have expected content.\n";
119     }
120 }
121
122 sub grade_multi_oom {
123     my (@output) = @_;
124     verify_common (@output);
125
126     @output = canonicalize_exit_codes (get_core_output (@output));
127     my ($n) = 0;
128     while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
129         die "Child process $m started out of order.\n" if $m != $n;
130         $n = $m + 1;
131         shift @output;
132     }
133     die "Only $n child process(es) started.\n" if $n < 15;
134
135     # There could be a death notice for a process that didn't get
136     # fully loaded, and/or notices from the loader.
137     while (@output > 0
138            && ($output[0] =~ /^multi-oom: exit\(-1\)$/
139                || $output[0] =~ /^load: /)) {
140         shift @output;
141     }
142
143     while (--$n >= 0) {
144         die "Output ended unexpectedly before process $n finished.\n"
145             if @output < 2;
146
147         local ($_);
148         chomp ($_ = shift @output);
149         die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
150         die "Child process $n ended out of order.\n"
151             if !/^\(multi-oom\) end $n$/;
152
153         chomp ($_ = shift @output);
154         die "Kernel didn't print proper exit message for process $n.\n"
155             if !/^multi-oom: exit\($n\)$/;
156     }
157     die "Spurious output at end: '$output[0]'.\n" if @output;
158 }
159
160 sub get_file {
161     my ($guest_fn, $host_fn) = @_;
162     my ($result) = run_pintos ("pintos "
163                                . "--os-disk=pintos/src/userprog/build/os.dsk "
164                                . "--fs-disk=output/$test/fs.dsk.keep "
165                                . "-v get $guest_fn $host_fn",
166                                LOG => "$test/get-$guest_fn",
167                                TIMEOUT => 10,
168                                EXPECT => 0);
169     die "`pintos get $guest_fn' failed - $result\n"
170         if $result ne 'ok';
171 }