bf33125844e1b422bb848ef5b783868d163211a2
[bookreader.git] / BookReaderIA / testswarm-jquery.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 # CONFIGURE
7
8 # The location of the TestSwarm that you're going to run against.
9
10 my $SWARM = "http://dev01.sf.archive.org:8080";
11 my $SWARM_INJECT = "/js/inject.js";
12
13 # Your TestSwarm username.
14
15 my $USER = "testflip";
16
17 # Your authorization token.
18
19 my $AUTH_TOKEN = ""; # $$$ Fill this in
20
21 # The maximum number of times you want the tests to be run.
22
23 my $MAX_RUNS = 5;
24
25 # The type of revision control system being used.
26 # Currently "svn" or "git" are supported.
27
28 my $RCS_TYPE = "git";
29
30 # The URL from which a copy will be checked out.
31
32 my $RCS_URL = "/home/testflip/bookreader/.git";
33
34 # The directory in which the checkouts will occur.
35
36 my $BASE_DIR = "/home/testflip/public_html/changeset";
37
38 # A script tag loading in the TestSwarm injection script will
39 # be added at the bottom of the <head> in the following file.
40
41 my $INJECT_FILE = "BookReaderIA/test/index.html";
42
43 # Any build commands that need to happen.
44
45 my $BUILD = "(cd BookReaderIA; make test)";
46
47 # The name of the job that will be submitted
48 # (pick a descriptive, but short, name to make it easy to search)
49
50 # Note: The string {REV} will be replaced with the current
51 #       commit number/hash.
52
53 my $JOB_NAME = "BookReader Commit #{REV}";
54
55 # The browsers you wish to run against. Options include:
56 #  - "all" all available browsers.
57 #  - "popular" the most popular browser (99%+ of all browsers in use)
58 #  - "current" the current release of all the major browsers
59 #  - "gbs" the browsers currently supported in Yahoo's Graded Browser Support
60 #  - "beta" upcoming alpha/beta of popular browsers
61 #  - "popularbeta" the most popular browser and their upcoming releases
62
63 my $BROWSERS = "popularbeta";
64
65 # All the suites that you wish to run within this job
66 # (can be any number of suites)
67
68 #my %SUITES = ('first' => 'http://test.archive.org/changeset/{REV}/test/index.html');
69 my %SUITES = ();
70
71 # Comment these out if you wish to define a custom set of SUITES above
72 #my $SUITE = "http://dev.jquery.com/~john/changeset/{REV}";
73 my $SUITE = "http://home.us.archive.org/~testflip/changeset/{REV}";
74 sub BUILD_SUITES {
75         %SUITES = map { /(\w+).js$/; $1 => "$SUITE/test/?$1%20module"; } glob("test/unit/*.js");
76 }
77
78 ########### NO NEED TO CONFIGURE BELOW HERE ############
79
80 my $DEBUG = 1;
81 my $curdate = time;
82 my $co_dir = "tmp-$curdate";
83
84 print "chdir $BASE_DIR\n" if ( $DEBUG );
85 chdir( $BASE_DIR );
86
87 # Check out a specific revision
88 if ( $RCS_TYPE eq "svn" ) {
89         print "svn co $RCS_URL $co_dir\n" if ( $DEBUG );
90         `svn co $RCS_URL $co_dir`;
91 } elsif ( $RCS_TYPE eq "git" ) {
92         print "git clone $RCS_URL $co_dir\n" if ( $DEBUG );
93         `git clone $RCS_URL $co_dir`;
94 }
95
96 if ( ! -e $co_dir ) {
97         die "Problem checking out source.";
98 }
99
100 print "chdir $co_dir\n" if ( $DEBUG );
101 chdir( $co_dir );
102
103 my $rev;
104
105 # Figure out the revision of the checkout
106 if ( $RCS_TYPE eq "svn" ) {
107         print "svn info | grep Revision\n" if ( $DEBUG );
108         $rev = `svn info | grep Revision`;
109         $rev =~ s/Revision: //;
110 } elsif ( $RCS_TYPE eq "git" ) {
111     my $cmd = "git rev-parse --short HEAD";
112     print "$cmd\n" if ( $DEBUG );
113     $rev = `$cmd`;
114 }
115
116 $rev =~ s/\s*//g;
117
118 print "Revision: $rev\n" if ( $DEBUG );
119
120 if ( ! $rev ) {
121         remove_tmp();
122         die "Revision information not found.";
123
124 } elsif ( ! -e "../$rev" ) {
125         print "chdir $BASE_DIR\n" if ( $DEBUG );
126         chdir( $BASE_DIR );
127
128         print "rename $co_dir $rev\n" if ( $DEBUG );
129         rename( $co_dir, $rev );
130
131         print "chdir $rev\n" if ( $DEBUG );
132         chdir ( $rev );
133
134         if ( $BUILD ) {
135                 print "$BUILD\n" if ( $DEBUG );
136                 `$BUILD`;
137         }
138
139         if ( exists &BUILD_SUITES ) {
140                 &BUILD_SUITES();
141         }
142
143         foreach my $file ( glob($INJECT_FILE) ) {
144                 my $inject_file = `cat $file`;
145
146                 # Inject the TestSwarm injection script into the test suite
147                 $inject_file =~ s/<\/head>/<script>document.write("<scr" + "ipt src='$SWARM$SWARM_INJECT?" + (new Date).getTime() + "'><\/scr" + "ipt>");<\/script><\/head>/;
148
149                 open( my $fh, '>', $file ) or die "$file : $!";
150                 print $fh $inject_file;
151                 close( $fh );
152         }
153
154         my %props = (
155                 "state" => "addjob",
156                 "output" => "dump",
157                 "user" => $USER,
158                 "max" => $MAX_RUNS,
159                 "job_name" => $JOB_NAME,
160                 "browsers" => $BROWSERS,
161                 "auth" => $AUTH_TOKEN
162         );
163
164         my $query = "";
165
166         foreach my $prop ( keys %props ) {
167                 $query .= ($query ? "&" : "") . $prop . "=" . clean($props{$prop});
168         }
169
170         foreach my $suite ( sort keys %SUITES ) {
171                 $query .= "&suites[]=" . clean($suite) .
172                           "&urls[]=" . clean($SUITES{$suite});
173         }
174
175         # XXX hacking in host header for now
176         #print "curl -d \"$query\" $SWARM\n" if ( $DEBUG );
177         #my $results = `curl -d "$query" $SWARM`;
178         my $cmd = 'curl  -H "Host: test.archive.org" -d "' . $query . '" ' . $SWARM;
179         print $cmd . '\n' if ( $DEBUG );
180         my $results = `$cmd`;
181
182         print "Results: $results\n" if ( $DEBUG );
183
184         if ( $results ) {
185                 open( my $fh, '>', "$rev/results.txt" ) or die "$rev/results.txt : $!";
186                 print $fh "$SWARM$results";
187                 close( $fh );
188
189         } else {
190                 die "Job not submitted properly.";
191         }
192
193 # Otherwise, give up and clean up
194 } else {
195         remove_tmp();
196 }
197
198 sub remove_tmp {
199         chdir( $BASE_DIR );
200         `rm -rf $co_dir`;
201 }
202
203 sub clean {
204   my $str = shift;
205         $str =~ s/{REV}/$rev/g;
206         $str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
207         $str;
208 }