PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/smolder_smoke_signal

http://github.com/PerlGameDev/SDL
Perl | 241 lines | 122 code | 15 blank | 104 comment | 26 complexity | e1e74c33c03b3c53ee89a51e694ea408 MD5 | raw file
  1#!/usr/bin/perl 
  2
  3eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
  4	if 0; # not running under some shell
  5use strict;
  6use warnings;
  7use Getopt::Long;
  8use Pod::Usage;
  9
 10BEGIN {
 11	eval { require WWW::Mechanize };
 12	if ($@) {
 13		warn "\nCannot load WWW::Mechanize. " . "\nPlease install it before using smolder_smoke_signal.\n";
 14		exit(1);
 15	}
 16}
 17
 18=pod
 19
 20=head1 NAME
 21
 22smolder_smoke_signal
 23
 24=head1 SYNOPSIS
 25
 26    ./bin/smolder_smoke_signal --server smolder.foo.com \
 27        --username myself --password s3cr3t --file test_report.xml \
 28        --project MyProject
 29
 30=head1 DESCRIPTION
 31
 32Script used to upload a Smoke test report to a running smolder server.
 33This is extremely useful for scripted/automatic test runs but also
 34helpful when using a CLI makes things faster.
 35
 36=head1 OPTIONS
 37
 38=head2 REQUIRED
 39
 40=over
 41
 42=item server
 43
 44This is the hostname (and port if not 80) of the running Smolder server.
 45
 46=item project
 47
 48The name of the Smolder project to use for the upload.
 49
 50=item username
 51
 52The name of the Smolder user to use for the upload.
 53
 54=item password
 55
 56The password for the Smolder user given by C<username>.
 57
 58=item file
 59
 60The name of the file to upload. Please see F<docs/upload_file_format.pod>
 61for more details about the format that Smolder expects this file to
 62take.
 63
 64=back
 65
 66=head2 OPTIONAL
 67
 68=over
 69
 70=item port
 71
 72If your Smolder server is running on a port other than 80, then you
 73can specify it here.
 74
 75=item architecture
 76
 77The architecture for the given smoke test run. If none is given
 78it will use the default architecture for the project.
 79
 80=item platform
 81
 82The platform for the given smoke test run. If none is given
 83it will use the default platform for the project.
 84
 85=item revision
 86
 87The revision control number for this test run. Only applies to
 88projects that use revision control (shouldn't they all) and only
 89applies to tests run against a checkout from revision control.
 90
 91This is just a free form text option so it will work with any
 92revision number that your preferred revision control system uses.
 93
 94=item tags
 95
 96A comma separated list of tags that are given for this smoke report run.
 97
 98    ./bin/smolder_smoke_signal --server smolder.foo.com \
 99        --username myself --password s3cr3t --file test_report.xml \
100        --project MyProject --tags "Foo, My Bar"
101
102=item comments
103
104Any comments that you want to associate with the smoke test run.
105
106=item verbose
107
108Print verbose output of our actions to STDOUT.
109
110=cut
111
112# default options
113my ($server,   $project, $user,     $pw,      $file, $arch,
114	$platform, $tags,    $comments, $verbose, $rev,  $port
115);
116my ( $help, $man );
117
118GetOptions(
119	'server=s'       => \$server,
120	'port=s'         => \$port,
121	'project=s'      => \$project,
122	'username=s'     => \$user,
123	'password=s'     => \$pw,
124	'file=s'         => \$file,
125	'architecture=s' => \$arch,
126	'platform=s'     => \$platform,
127	'tags=s'         => \$tags,
128	'comments=s'     => \$comments,
129	'revision=s'     => \$rev,
130	'verbose!'       => \$verbose,
131	'help'           => \$help,
132	'man'            => \$man,
133) || pod2usage();
134
135if ($help) {
136	pod2usage(
137		-exitval => 0,
138		-verbose => 1,
139	);
140} elsif ($man) {
141	pod2usage(
142		-exitval => 0,
143		-verbose => 2,
144	);
145}
146
147# make sure all the required fields are there
148_missing_required('server')   unless $server;
149_missing_required('project')  unless $project;
150_missing_required('username') unless $user;
151_missing_required('password') unless $pw;
152_missing_required('file')     unless $file;
153
154# make sure our file is there and is of the right type
155if ( -r $file ) {
156	unless ( $file =~ /\.tar(\.gz)?$/ ) {
157		warn "File '$file' is not of the correct type!\n";
158		exit(1);
159	}
160} else {
161	warn "File '$file' does not exist, or is not readable!\n";
162	exit(1);
163}
164
165# try and reach the smolder server
166print "Trying to reach Smolder server at $server.\n" if ($verbose);
167$port ||= 80;
168my $mech     = WWW::Mechanize->new();
169my $base_url = "http://$server:$port/app";
170eval { $mech->get($base_url) };
171unless ( $mech->status eq '200' ) {
172	warn "Could not reach $server:$port successfully. Received status " . $mech->status . "\n";
173	exit(1);
174}
175
176# now login
177print "Trying to login with username '$user'.\n" if ($verbose);
178$mech->get( $base_url . '/public_auth/login' );
179my $form = $mech->form_name('login');
180if ( $mech->status ne '200' || !$form ) {
181	warn "Could not reach Smolder login form. Are you sure $server:$port is a Smolder server?\n";
182	exit(1);
183}
184$mech->set_fields(
185	username => $user,
186	password => $pw,
187);
188$mech->submit();
189my $content = $mech->content;
190if ( $mech->status ne '200' || $content !~ /Welcome \Q$user\E/ ) {
191	warn "Could not login with username '$user' and password '$pw'!\n";
192	exit(1);
193}
194
195# now go to this project's page
196print "Retrieving project listing for user '$user'.\n" if ($verbose);
197$mech->get( $base_url . '/developer_projects' );
198$content = $mech->content;
199$content =~ />\Q$project\E<!--ID:(\d+)-->/;
200my $project_id = $1;
201if ( $mech->status ne '200' || !$project_id ) {
202	warn "Could not get your project listing, or you are not a member of the '$project' project!\n";
203	exit(1);
204}
205
206# now go to the add-smoke-report page for this project
207print "Adding smoke report to project '$project'.\n" if ($verbose);
208$mech->get( $base_url . "/developer_projects/add_report/$project_id" );
209$content = $mech->content;
210if ( $mech->status ne '200' || $content !~ /New Smoke Report/ ) {
211	warn "Could not reach the Add Smoke Report form!\n";
212	exit(1);
213}
214$mech->form_name('add_report');
215my %fields = ( report_file => $file );
216$fields{platform}     = $platform if $platform;
217$fields{architecture} = $arch     if $arch;
218$fields{tags}         = $tags     if $tags;
219$fields{comments}     = $comments if $comments;
220$fields{revision}     = $rev      if $rev;
221$mech->set_fields(%fields);
222$mech->submit();
223
224$content = $mech->content;
225if ( $mech->status ne '200' || $content !~ /Recent Smoke Reports/ ) {
226	warn "Could not upload smoke report with the given information!\n";
227	exit(1);
228}
229$content =~ /#(\d+) Added/;
230my $report_id = $1;
231
232print "\nReport successfully uploaded as #$report_id.\n";
233
234##########################################################
235# helper methods
236##########################################################
237sub _missing_required {
238	my $field = shift;
239	warn "Missing required field '$field'!\n";
240	pod2usage();
241}