Comments and code tidying for ats3_testdrop.pl
authorMike Kinghan <mikek@symbian.org>
Wed, 03 Jun 2009 11:21:24 +0100
changeset 158 039d0b7e56f3
parent 136 7f0174848f99
child 159 45d06bae9906
Comments and code tidying for ats3_testdrop.pl Adding new ATS support script ats_specialise_test_drop.pl
common/tools/ats/ats3_testdrop.pl
common/tools/ats/ats_specialise_test_drop.pl
--- a/common/tools/ats/ats3_testdrop.pl	Thu May 28 14:55:09 2009 +0100
+++ b/common/tools/ats/ats3_testdrop.pl	Wed Jun 03 11:21:24 2009 +0100
@@ -13,20 +13,26 @@
 # Contributors:
 #
 # Description:
-#   This is a tool for sending an ATS3 testdrop to an ATS3 server.
+#   This is a tool for sending an ATS testdrop to an ATS server.
 
 use strict;
 use Getopt::Long;
 use File::Copy;
 use File::Spec;
 
-my $username = "admin";
-my $password = "admin";
-my $host;
-my $schedule;
-my $host_drop_path;
-my $local_drop_path;
-my $local_test_pkg;
+my $username = "admin"; # ATS server user name
+my $password = "admin"; # ATS server password
+my $host;   # ATS server hostname
+my $schedule;   # Schedule string for test drop, if any
+my $host_drop_path; # Directory path on the ATS server where the test drop will be placed.
+                    # This must be physically the same path as $local_drop_path, via drive mapping. 
+my $local_drop_path; # Local directory path where the test drop will be placed.
+                    # This must be physically the same path as $host_drop_path, via drive mapping.
+my $local_test_pkg; # Local pathname of the test drop (zip or xml file). The test drop will be
+                    # copied from here to $local_drop_path to make it accessible on the server,
+                    # unless $local_test_pkg begins with "...". This indicates the test drop is already
+                    # present in $local_drop_path and does need to be copied there. The remainder
+                    # following "..." is the filename of the test drop.    
 my $help;
 my $dev_null = $^O =~ /^MSWin/ ? "nul" : "/dev/null";
 
@@ -60,7 +66,7 @@
 
 usage_error(), unless (defined($host) && defined($local_test_pkg) && defined($local_drop_path) && defined($host_drop_path));
 
-my $curl_version = $^O =~ /^MSWin/ ? `curl --version 1> $dev_null 2>&1` : `curl --version 1> $dev_null 2>&1`;
+my $curl_version = `curl --version 1> $dev_null 2>&1`;
 die("Need program \"curl\". Not found"), if ($?);
 die("Test drop path \"$local_drop_path\" not found"), unless ( -d "$local_drop_path");
 
@@ -70,41 +76,44 @@
 my $local_test_drop;
 my $host_test_drop;
 
-if ($local_test_pkg =~ /^\.\.\.(.+)/ ) {
-    $pkg = $1;
-    $local_test_drop = File::Spec->catfile($local_drop_path,$pkg);
+if ($local_test_pkg =~ /^\.\.\.(.+)/ ) { # Local test package name begins with "...". No need to copy. 
+    $pkg = $1; # Get the filename of the test package.
+    # Compose the full name of the test drop within the local drop path.
+    $local_test_drop = File::Spec->catfile($local_drop_path,$pkg);    
     die("Test package file \"$local_test_drop\" not found"), unless ( -f "$local_test_drop");        
 }
-else {
+else { # Local test package must be copied to local drop path.
     die("Test package file \"$local_test_pkg\" not found"), unless ( -f "$local_test_pkg");
+    # Compose the full name of the test drop within the local drop path.    
     ($vol,$dir,$pkg) = File::Spec->splitpath($local_test_pkg);
     $local_test_drop = File::Spec->catfile($local_drop_path,$pkg);
     if ( -f "$local_test_drop") {
-        my $cmp_local_test_drop = $local_test_drop;
-        my $cmp_local_test_pkg = $local_test_pkg;
-        if ($^O =~ /^MSWin/) {
-            $cmp_local_test_drop = lc($local_test_drop);
-            $cmp_local_test_pkg = lc($local_test_pkg);        
-        }   
-        if ("$cmp_local_test_drop" eq "$cmp_local_test_pkg") {
-            if (unlink($local_test_drop) == 0) {
-                die("Can't delete stale test drop \"$local_test_drop\". $!");
-            }
-            else {
-                print("A stale test drop \"$local_test_drop\" existed. Deleted\n");
-            }
-        }
+        # A state test drop of the same name exists. Delete.
+        unlink($local_test_drop) or die("Can't delete stale test drop \"$local_test_drop\". $!");
+        print("A stale test drop \"$local_test_drop\" existed. Deleted\n");
     }
+    # Copy the test drop to the local drop path.
     copy("$local_test_pkg","$local_test_drop")
         or die("Cannot copy \"$local_test_pkg\" -> \"$local_test_drop\". $!");
 }
+# Compose the full name of the test drop within the host drop path.
 $host_test_drop = File::Spec->catfile($host_drop_path,$pkg);
+# Compose URL to notify server of test drop, requesting run.
 my $url ="http://$host/ats3/XTestRunExecute.do?username=$username&password=$password&testrunpath=$host_test_drop";
 $url .= "&schedule=$schedule", if (defined($schedule));
+# Post URL to server with cURL.
 my $curl_cmd = "curl \"$url\"";
-system("$curl_cmd");
-die("\nTest drop failed: $!"), if ($?);
-print("\nTest drop done");
+my $curl_response = `$curl_cmd 2>$1`;
+die("\nTest drop failed: $!\n"), if ($?);
+if ($curl_response =~ /(TEST_RUN_REF_ID=\d+)/) {
+    #extract test run id from cURL response.
+    print "$1\n";
+}
+else {
+    print "$curl_response\n";
+    die("Test drop failed\n");
+} 
+print("Test drop done\n");
 
 exit 0;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/tools/ats/ats_specialise_test_drop.pl	Wed Jun 03 11:21:24 2009 +0100
@@ -0,0 +1,202 @@
+#!/usr/bin/perl
+
+# Copyright (c) 2009 Symbian Foundation Ltd
+# This component and the accompanying materials are made available
+# under the terms of the License "Eclipse Public License v1.0"
+# which accompanies this distribution, and is available
+# at the URL "http://www.eclipse.org/legal/epl-v10.html".
+#
+# Initial Contributors:
+# Symbian Foundation Ltd - initial contribution.
+#   Mike Kinghan, mikek@symbian.org
+#
+# Contributors:
+#
+# Description:
+#   This is a tool for setting the name of an ATS test drop, and/or the build id
+#   of the build being tested, and/or the name of the device on which
+#   it is to be run, by modifying the XML test drop specificcation.
+
+use strict;
+use Getopt::Long;
+use XML::Simple;
+use File::Copy;
+use Data::Dumper;
+
+my $test_drop_name;	# Test drop name to be embedded in output XML
+my $build_id;    # Build ID to be embedded in output XML 
+my $device_name; # Device name to be embedded in output XML
+my $srcfile;	# Input test drop file, either .xml or .zip
+my $destfile;	# Leafname for output file. Extension will be .xml or .zip as input.
+				# If unspecified then the input file is updated. 
+my $help;		# Do help?
+my $srctype;	# Type of inpout file, .xml or .zip
+my $dev_null = $^O =~ /^MSWin/ ? "nul" : "/dev/null";	# OS-dependent nul device.
+my $zipper;		# Zip command, depending on whether zip or 7z is available.
+my $unzipper;	# Unzip command, depending on whether [unzip or 7z is available.
+my $xml_in;		# Name of the input xml file. Always 'test.xml' if extracted from zipped test drop.
+my $temp_dest_name; # Leafname of temporary output file, if $destfile undefined.
+
+sub usage($);
+sub help();
+sub usage_error();
+sub silent_command($);
+
+my %optmap = (  'test-drop-name' => \$test_drop_name,
+			    'device-name' => \$device_name,
+			    'build-id' => \$build_id,
+			    'src' => \$srcfile,
+			    'dest' => \$destfile,
+                'help' => \$help);
+
+GetOptions(\%optmap,
+          'test-drop-name=s',
+          'device-name=s',
+          'build-id=s',
+          'src=s',
+          'dest=s',
+          'help!') 
+          or usage_error();
+
+if ($help) {
+	help();
+}
+
+# --src is mandatory.
+usage_error(), unless (defined($srcfile));
+if ($srcfile =~ /(\.xml$)/i) {
+    $srctype = lc($1);
+}
+elsif ($srcfile =~ /(\.zip$)/i) {
+    $srctype = lc($1);
+}
+else {
+	# src file must have extension .xml or .zip
+    usage_error();
+}
+die("File \"$srcfile\" does not exist"), unless -f $srcfile;
+
+if (!$destfile) { # dest file unspecified. We will update the src file.
+	$temp_dest_name = "ats_specialise_test_drop.out";
+	$destfile = $temp_dest_name;
+}
+else {
+    # If dest file has same extension as src then strip off, cos we'll append it anyway.
+    $destfile =~ s/$srctype$//;
+}
+
+
+if ($srctype =~ /^\.zip$/) { # Test drop is zip. Must contain test.xml.
+    $xml_in = "test.xml"; # This is our input XML.
+    if ( -f $xml_in) {
+		# Pre-emptively delete anystale xml file.
+        unlink($xml_in) or die("Cannot delete stale $xml_in: $!\n");
+    }
+    if (!silent_command("zip -v")) { # See if we've got zip
+        $zipper = "zip -m $destfile.zip test.xml";
+        $unzipper = "unzip -o $srcfile $xml_in -d .";
+    }
+    elsif (!silent_command("7z -version")) { # Else see if we've got 7zip
+        $zipper = "7z u $destfile.zip test.xml";
+        $unzipper = "7z e $srcfile $xml_in";
+    }
+    else { # Got no archiver.
+        die ("Need programs zip/unzip or 7z. Not found\n");
+    }
+    # Extract test.xml from zip.
+    system("$unzipper > $dev_null") and die("Could not unzip \"$srcfile\"\n");
+    die("Did not find $xml_in within \"$srcfile\"\n"), unless( -f "$xml_in");
+    
+}
+else {
+    $xml_in = $srcfile;
+}
+
+# Parse the input XML into hashref.
+my $test_drop = XMLin("./$xml_in", keeproot => 1,
+    forcearray => [ 'name', 'id','owner','priority','buildid','target','device', 'property', 'command', 'param'],#
+        keyattr => [] );
+    
+        
+# Insert the specified test drop name, if any.
+$test_drop->{'test'}->{'name'}->[0] = $test_drop_name, if $test_drop_name;
+# Insert the specified build id, if any.
+$test_drop->{'test'}->{'buildid'}->[0] = $build_id, if $build_id;
+
+if ($device_name) { # Also insert sepcified device name.
+	
+	my $device_properties = $test_drop->{'test'}->{'target'}->[0]->{'device'}->[0]->{'property'};
+	my $num_properties = @{$device_properties};
+	$device_properties->[$num_properties]= { 'name' => "NAME", 'value' => "$device_name" };
+	$test_drop->{'test'}->{'target'}->[0]->{'device'}->[0]->{'property'} = $device_properties;
+    #print Dumper($device_properties);
+    #exit(0);		
+}
+
+if ($srctype =~ /^\.xml$/i ) { # Input file was XML
+    # Write new XML to dest file.
+    open OUT,">$destfile.xml" or die("Cannot open file \"$destfile.xml\" for writing. $!\n");
+    print OUT XMLout($test_drop, keeproot => 1);
+    close OUT;    
+}
+else { #Input file was a zip.
+    # Write new XML to test.xml
+    open OUT,">test.xml" or die("Cannot open file \"test.xml\" for writing. $!\n");
+    print OUT XMLout($test_drop, keeproot => 1);
+    close OUT;
+    if ( -f "$destfile.zip") {
+        unlink("$destfile.zip") or die("Could not delete \"$destfile.zip\": $!\n");
+    } 
+    copy("$srcfile","$destfile.zip") or die("Could not copy \"$srcfile\" -> \"$destfile.zip\": $!\n");
+    system("$zipper > $dev_null") and die("Could not zip \"test.xml\" into \"$destfile\"\n");    
+}
+
+if ($temp_dest_name) { # We are to update the src file. dest file is a temporary.
+   # Delete src file 
+   unlink($srcfile) or die("Cannot delete file \"$srcfile\" to replace: $!\n");
+   # Replace with temporary.
+   move("$destfile$srctype","$srcfile") or die("Could not move \"$destfile$srctype\" -> \"$srcfile\": $!\n");
+}
+
+print "OK\n";
+exit 0;
+
+sub usage($)
+{
+    my $error = shift;
+    my $fh = $error == 0 ? *STDOUT : *STDERR;
+    print $fh "ats_specialise_test_drop.pl\n" .
+            "Specify the name, build id and target device in an ATS XML test drop\n" .
+            "synopsis:\n" .
+            "  ats_specialise_test_drop.pl --help\n" .
+            "  ats_specialise_test_drop.pl [--test-drop-name=TESTNAME] [--build-id=BUILDID] [--device-name=DEVICENAME] [--dest=FILE] --src=FILE \n" .
+            "options:\n" .
+            "  --help                        Display this help and exit\n" .
+            "  --test-drop-name=TESTNAME     TESTNAME is the desired name of the test drop. If not specified then the test drop name is not modified.\n" .
+            "  --build-id=BUILDID            BUILDID is id of the build being tested. If not specified then the build id is not modified.\n" .            
+            "  --device-name=DEVICENAME      DEVICENAME  is the name of the device on which the test should be run. " .
+            "If not specified then the test device name is not modified.\n" .
+            "  --src=INFILE                  INFILE is the file containing the test drop XML to be modified, or else a zip file " .
+            "                                containing the test drop XML in the file 'test.xml'. INFILE must have extension .xml or.zip\n" .
+            "  --dest=OUTFILE                The modified output will be written to the file OUTFILE.EXT " .
+            "where EXT is same extention, .xml or .zip, found on INFILE. " .
+            "If OUTFILE is not specified then INFILE is modified\n";              
+    exit $error;            
+}
+
+sub help()
+{
+    usage(0);
+}
+
+sub usage_error()
+{
+    usage(1);
+}             
+
+sub silent_command($)
+{
+    my $cmd = shift;
+    system("$cmd 1> $dev_null 2>&1");
+    return $? >> 8;
+}