/tools/smolder_smoke_signal

http://github.com/PerlGameDev/SDL · Perl · 241 lines · 122 code · 15 blank · 104 comment · 25 complexity · e1e74c33c03b3c53ee89a51e694ea408 MD5 · raw file

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