sbsv2/raptor/test/common/run_tests.py
author Zheng Shen <zheng.shen@nokia.com>
Tue, 26 Oct 2010 11:12:31 +0800
changeset 654 7c11c3d8d025
parent 625 a1925fb7753a
permissions -rw-r--r--
romtools 13.2.0.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
591
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     1
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     3
# All rights reserved.
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     4
# This component and the accompanying materials are made available
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     6
# which accompanies this distribution, and is available
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     8
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     9
# Initial Contributors:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    11
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    12
# Contributors:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    13
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    14
# Description: 
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    15
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    16
# Runs the specified suite of raptor tests
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    17
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    18
import os
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    19
import sys
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    20
import re
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    21
import imp
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    22
import datetime
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    23
import traceback
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    24
raptor_tests = imp.load_source("raptor_tests", "common/raptor_tests.py")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    25
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    26
# Command line options ########################################################
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    27
from optparse import OptionParser
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    28
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    29
parser = OptionParser(
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    30
		prog = "run",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    31
		usage = "%prog [Options]")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    32
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    33
parser.add_option("-u", "--upload", action = "store", type = "string",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    34
		dest = "upload", default = None,
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    35
		help = "Path for uploading results (Can be UNC path)")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    36
parser.add_option("-b", "--branch", action = "store", type = "choice",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    37
		dest = "branch", choices = ["master", "m", "fix", "f", "wip", "w"],
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    38
		help = "string indicating which branch is being tested:\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    39
		"master, fix or wip. Default is 'fix'")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    40
parser.add_option("-s", "--suite", action = "store", type = "string",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    41
		dest = "suite", help = "regex to use for selecting test suites")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    42
parser.add_option("-t", "--tests", action = "store", type = "string",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    43
		dest = "tests", help = "regex to use for selecting tests")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    44
parser.add_option("-d", "--debug", action = "store_true", dest = "debug_mode",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    45
		default = False, help = "Turns on debug-mode")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    46
parser.add_option("--test-home", action = "store", type = "string",
616
24e4ef208cca sbs version 2.14.2
Richard Taylor <richard.i.taylor@nokia.com>
parents: 591
diff changeset
    47
		dest = "test_home", default="default",
591
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    48
		help = "Location of custom .sbs_init.xml (name of directory in " +
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    49
		"'custom_options'): test/custom_options/<test_home>/.sbs_init.xml")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    50
parser.add_option("--what-failed", action = "store_true", dest = "what_failed",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    51
		help = "Re-run all the tests that failed in the previous test run")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    52
parser.add_option("--clean", action = "store_true", dest = "clean",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    53
		help = "Clean EPOCROOT after each test is run")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    54
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    55
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    56
(options, args) = parser.parse_args()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    57
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    58
# Check for --what-failed and override '-s' and '-t' (including flagless regex)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    59
if options.what_failed:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    60
	try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    61
		what_failed_file = open("what_failed", "r")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    62
		what_failed = what_failed_file.readline()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    63
		what_failed_file.close()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    64
		print "Running: run " + what_failed
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    65
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    66
		first = what_failed.find('"')
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    67
		second = what_failed.find('"', (first + 1))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    68
		options.suite = what_failed[(first + 1):second]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    69
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    70
		first = what_failed.find('"', (second + 1))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    71
		second = what_failed.find('"', (first + 1))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    72
		options.tests = what_failed[(first + 1):second]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    73
	except:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    74
		# If no file exists, nothing failed, so run as usual
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    75
		pass
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    76
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    77
# Allow flagless test regex
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    78
if (options.tests == None) and (len(args) > 0):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    79
	options.tests = args[len(args) - 1]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    80
	
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    81
if options.upload != None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    82
	if options.branch != None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    83
		if options.branch == "m":
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    84
			branch = "master"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    85
		elif options.branch == "f":
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    86
			branch = "fix"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    87
		elif options.branch == "w":
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    88
			branch = "wip"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    89
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    90
			branch = options.branch
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    91
	else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    92
		print "Warning: Test branch not set - Use " + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    93
				"'-b [master|fix|wip]'\n Using default of 'Fix'..."
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    94
		branch = "fix"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    95
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    96
if options.debug_mode:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    97
	raptor_tests.activate_debug()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    98
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    99
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   100
# Set $HOME environment variable for finding a custom .sbs_init.xml 
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   101
if options.test_home != None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   102
	home_dir = options.test_home
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   103
	if home_dir in os.listdir("./custom_options"):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   104
		os.environ["HOME"] = os.environ["SBS_HOME"] + "/test/custom_options/" \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   105
				+ home_dir + "/"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   106
	else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   107
		print "Warning: Path to custom .sbs_init.xml file not found (" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   108
				home_dir + ")\nUsing defaults..."
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   109
		options.test_home = None
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   110
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   111
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   112
def format_milliseconds(microseconds):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   113
	""" format a microsecond time in milliseconds """
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   114
	milliseconds = (microseconds / 1000)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   115
	if milliseconds == 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   116
		return "000"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   117
	elif milliseconds < 10:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   118
		return "00" + str(milliseconds)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   119
	elif milliseconds < 100:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   120
		return "0" + str(milliseconds)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   121
	return milliseconds
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   122
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   123
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   124
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   125
class TestRun(object):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   126
	"""Represents any series of tests"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   127
	def __init__(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   128
		self.test_set = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   129
		self.failed_tests = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   130
		self.error_tests = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   131
		self.pass_total = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   132
		self.fail_total = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   133
		self.skip_total = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   134
		self.exception_total = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   135
		self.test_total = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   136
		# For --what-failed:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   137
		self.suites_failed = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   138
		self.tests_failed = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   139
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   140
	def aggregate(self, atestrun):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   141
		""" Aggregate other test results into this one """
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   142
		self.test_set.append(atestrun)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   143
		self.test_total += len(atestrun.test_set)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   144
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   145
	def show(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   146
		for test_set in self.test_set:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   147
			print "\n\n" + str(test_set.suite_dir) + ":\n"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   148
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   149
			# If a suite has failed/erroneous tests, add it to what_failed
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   150
			if (test_set.fail_total + test_set.exception_total) > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   151
				self.suites_failed.append(test_set.suite_dir)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   152
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   153
			if len(test_set.test_set) < 1:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   154
				print "No tests run"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   155
			else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   156
				print "PASSED: " + str(test_set.pass_total)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   157
				print "FAILED: " + str(test_set.fail_total)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   158
				if test_set.skip_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   159
					print "SKIPPED: " + str(test_set.skip_total)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   160
				if test_set.exception_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   161
					print "EXCEPTIONS: " + str(test_set.exception_total)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   162
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   163
				if test_set.fail_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   164
					print "\nFAILED TESTS:"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   165
					
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   166
					# Add each failed test to what_failed and print it
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   167
					for test in test_set.failed_tests:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   168
						self.tests_failed.append("^" + test + ".py")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   169
						print "\t", test
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   170
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   171
				if test_set.exception_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   172
					print "\nERRONEOUS TESTS:"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   173
					
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   174
					# Add each erroneous test to what_failed and print it
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   175
					for test in test_set.error_tests:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   176
						first = test.find("'")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   177
						second = test.find("'", (first + 1))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   178
						self.tests_failed.append("^" +
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   179
								test[(first + 1):second] + ".py")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   180
						print "\t", test
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   181
						
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   182
	def what_failed(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   183
		"Create the file for --what-failed if there were failing tests"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   184
		if len(self.suites_failed) > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   185
			self.what_failed = open("what_failed", "w")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   186
			# Add the suites and tests to the file as command-line options
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   187
			self.what_failed.write('-s "')
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   188
			loop_number = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   189
			for suite in self.suites_failed:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   190
				loop_number += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   191
				self.what_failed.write(suite)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   192
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   193
				# If this is not the last suite, prepare to add another
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   194
				if loop_number < len(self.suites_failed):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   195
					self.what_failed.write("|")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   196
					
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   197
			self.what_failed.write('" -t "')
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   198
			loop_number = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   199
			for test in self.tests_failed:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   200
				loop_number += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   201
				self.what_failed.write(test)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   202
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   203
				# If this is not the last test, prepare to add another
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   204
				if loop_number < len(self.tests_failed):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   205
					self.what_failed.write("|")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   206
			self.what_failed.write('"')
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   207
			self.what_failed.close()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   208
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   209
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   210
			# If there were no failing tests this time, remove any previous file
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   211
			try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   212
				os.remove("what_failed")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   213
			except:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   214
				try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   215
					os.chmod("what_failed", stat.S_IRWXU)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   216
					os.remove("what_failed")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   217
				except:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   218
					pass
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   219
					
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   220
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   221
class Suite(TestRun):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   222
	"""A test suite"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   223
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   224
	python_file_regex = re.compile("(.*)\.py$", re.I)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   225
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   226
	def __init__(self, dir, parent):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   227
		TestRun.__init__(self)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   228
		self.suite_dir = dir
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   229
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   230
		# Upload directory (if set)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   231
		self.upload_location = parent.upload_location
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   232
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   233
		# Regex for searching for tests
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   234
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   235
		self.test_file_regex = parent.test_file_regex
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   236
		self.test_pattern = parent.testpattern
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   237
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   238
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   239
	def run(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   240
		"""run the suite"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   241
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   242
		self.time_stamp = datetime.datetime.now()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   243
		self.results = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   244
		self.start_times = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   245
		self.end_times = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   246
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   247
		print "\n\nRunning " + str(self.suite_dir) + "..."
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   248
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   249
		# Iterate through all files in specified directory
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   250
		for test in os.listdir(self.suite_dir):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   251
			# Only check '*.py' files
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   252
			name_match = self.python_file_regex.match(test)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   253
			if name_match is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   254
				if self.test_file_regex is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   255
					# Each file that matches -t input is imported if any
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   256
					name_match = self.test_file_regex.match(test)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   257
				else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   258
					name_match = 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   259
				if name_match is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   260
					import_name = test[:-3]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   261
					try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   262
						self.test_set.append(imp.load_source(import_name,
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   263
								(raptor_tests.ReplaceEnvs(self.suite_dir
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   264
								+ "/" + test))))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   265
					except:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   266
						print "\n", (sys.exc_type.__name__ + ":"), \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   267
								sys.exc_value, "\n", \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   268
								traceback.print_tb(sys.exc_traceback)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   269
	
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   270
		test_number = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   271
		test_total = len(self.test_set)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   272
		if test_total < 1:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   273
			print "No tests in suite "+self.suite_dir+" matched by specification '"+self.test_pattern+"' (regex: /.*"+self.test_pattern+".*/)\n";
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   274
		# Run each test, capturing all its details and its results
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   275
		for test in self.test_set:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   276
			test_number += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   277
			# Save start/end times and save in dictionary for TMS
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   278
			start_time = datetime.datetime.now()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   279
			try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   280
				test_number_text = "\n\nTEST " + str(test_number) + "/" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   281
						str(test_total) + ":"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   282
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   283
				if self.fail_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   284
					test_number_text += "    So far " + str(self.fail_total) + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   285
							" FAILED"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   286
				if self.exception_total > 0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   287
					test_number_text += "    So far " + str(self.exception_total) + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   288
							" ERRONEOUS"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   289
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   290
				print test_number_text
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   291
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   292
				test_object = test.run()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   293
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   294
				end_time = datetime.datetime.now()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   295
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   296
				# Add leading 0s
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   297
				test_object.id = raptor_tests.fix_id(test_object.id)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   298
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   299
				# No millisecond function, so need to use microseconds/1000
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   300
				start_milliseconds = start_time.microsecond
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   301
				end_milliseconds = end_time.microsecond
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   302
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   303
				# Add trailing 0's if required
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   304
				start_milliseconds = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   305
						format_milliseconds(start_milliseconds)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   306
				end_milliseconds = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   307
						format_milliseconds(end_milliseconds)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   308
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   309
				self.start_times[test_object.id] = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   310
						start_time.strftime("%H:%M:%S:" +
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   311
						str(start_milliseconds))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   312
				self.end_times[test_object.id] = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   313
						end_time.strftime("%H:%M:%S:" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   314
						str(end_milliseconds))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   315
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   316
				run_time = (end_time - start_time)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   317
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   318
				run_time_seconds = (str(run_time.seconds) + "." + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   319
						str(format_milliseconds(run_time.microseconds)))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   320
				print ("RunTime: " + run_time_seconds + "s")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   321
				# Add to pass/fail count and save result to dictionary
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   322
				if test_object.result == raptor_tests.SmokeTest.PASS:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   323
					self.pass_total += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   324
					self.results[test_object.id] = "Passed"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   325
				elif test_object.result == raptor_tests.SmokeTest.FAIL:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   326
					self.fail_total += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   327
					self.results[test_object.id] = "Failed"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   328
					self.failed_tests.append(test_object.name)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   329
				elif test_object.result == raptor_tests.SmokeTest.SKIP:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   330
					self.skip_total += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   331
				# Clean epocroot after running each test if --clean option is specified
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   332
				if options.clean:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   333
					print "\nCLEANING TEST RESULTS..."
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   334
					raptor_tests.clean_epocroot()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   335
					
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   336
			except:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   337
				print "\nTEST ERROR:"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   338
				print (sys.exc_type.__name__ + ":"), \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   339
						sys.exc_value, "\n", \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   340
						traceback.print_tb(sys.exc_traceback)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   341
				self.exception_total += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   342
				self.error_tests.append(str(self.test_set[test_number - 1]))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   343
								
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   344
				
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   345
		if self.upload_location != None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   346
			self.create_csv()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   347
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   348
		end_time_stamp = datetime.datetime.now()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   349
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   350
		runtime = end_time_stamp - self.time_stamp
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   351
		seconds = (str(runtime.seconds) + "." + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   352
				str(format_milliseconds(runtime.microseconds)))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   353
		if options.upload:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   354
			self.create_tri(seconds)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   355
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   356
		print ("\n" + str(self.suite_dir) + " RunTime: " + seconds + "s")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   357
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   358
	def create_csv(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   359
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   360
		This method will create a CSV file with the smoke test's output
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   361
				in order to successfully upload results to TMS QC
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   362
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   363
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   364
		# This sorts the dictionaries by their key values (Test IDs)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   365
		id_list = run_tests.sort_dict(self.results)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   366
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   367
		self.test_file_name = (self.suite_dir + "_" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   368
				self.time_stamp.strftime("%Y-%m-%d_%H-%M-%S") + "_" +
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   369
				branch + "_results.csv")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   370
		# This is the path for file-creation on the server. Includes
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   371
		self.test_path = (self.upload_location + "/csv/" + self.suite_dir + "/"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   372
				+ self.test_file_name)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   373
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   374
		try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   375
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   376
			if not os.path.isdir(self.upload_location + "/csv/" +
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   377
					self.suite_dir):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   378
				os.makedirs(self.upload_location + "/csv/" + self.suite_dir)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   379
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   380
			csv_file = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   381
					open(raptor_tests.ReplaceEnvs(os.path.normpath(self.test_path)),
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   382
					"w")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   383
			csv_file.write("TestCaseID,StartTime,EndTime,Result\n")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   384
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   385
			for test_id in id_list:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   386
				csv_file.write("PCT-SBSV2-" + self.suite_dir + "-" + test_id + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   387
						"," + str(self.start_times[test_id]) + "," + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   388
						str(self.end_times[test_id]) + "," + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   389
						self.results[test_id] + "\n")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   390
			csv_file.close()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   391
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   392
		except OSError, e:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   393
			print "SBS_TESTS: Error:", e
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   394
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   395
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   396
	def create_tri(self, overall_seconds):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   397
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   398
		This method will create a TRI (xml) file containing the location of the
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   399
				CSV file in order to successfully upload results to TMS QC
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   400
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   401
		# Path for the tri file
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   402
		tri_path = (self.upload_location + "/new/" + self.suite_dir + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   403
				"_" + self.time_stamp.strftime("%Y-%m-%d_%H-%M-%S") + ".xml")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   404
		run_name_timestamp = self.time_stamp.strftime(self.suite_dir + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   405
				"%Y-%m-%d_%H-%M-%S")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   406
		date_time_timestamp = self.time_stamp.strftime("%d.%m.%Y %H:%M:%S")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   407
		test_set_name = "Root\\Product Creation Tools\\Regression\\" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   408
				"SBS v2 (Raptor)\\" + self.suite_dir + "_"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   409
		if sys.platform.startswith("win"):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   410
			test_set_name += ("WinXP_" + branch)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   411
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   412
			test_set_name += ("Linux_" + branch)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   413
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   414
		# /mnt/ -> // Fixes the difference in paths for lon-rhdev mounts vs. win
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   415
		if not sys.platform.startswith("win"):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   416
			if self.test_path.startswith("/mnt/"):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   417
				self.test_path = self.test_path.replace("mnt", "", 1)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   418
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   419
		try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   420
			tri_file = \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   421
					open(raptor_tests.ReplaceEnvs(os.path.normpath(tri_path)), \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   422
					"w")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   423
			tri_file.write(
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   424
					"<TestRunInfo>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   425
						"\t<RunName>\n\t\t" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   426
							run_name_timestamp + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   427
						"\n\t</RunName>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   428
						"\t<TestGroup>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   429
							"\t\tSBSv2 (Non-SITK)\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   430
						"\t</TestGroup>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   431
						"\t<DateTime>\n\t\t" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   432
							date_time_timestamp + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   433
						"\n\t</DateTime>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   434
						"\t<RunDuration>\n\t\t" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   435
							overall_seconds + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   436
						"\n\t</RunDuration>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   437
						'\t<TestSet name="' + test_set_name + '">\n' + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   438
							"\t\t<TestResults>\n\t\t\t" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   439
								self.test_path + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   440
							"\n\t\t</TestResults>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   441
						"\t</TestSet>\n" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   442
					"</TestRunInfo>")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   443
			tri_file.close()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   444
			print "Tests uploaded to '" + self.upload_location + "' (" + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   445
					branch + ")"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   446
		except OSError, e:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   447
			print "SBS_TESTS: Error:", e
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   448
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   449
class SuiteRun(TestRun):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   450
	""" Represents a 'run' of a number of test suites """
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   451
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   452
	def __init__(self, suitepattern = None, testpattern = None,
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   453
			upload_location = None):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   454
		TestRun.__init__(self)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   455
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   456
		# Add common directory to list of paths to search for modules
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   457
		sys.path.append(raptor_tests.ReplaceEnvs("$(SBS_HOME)/test/common"))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   458
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   459
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   460
		if suitepattern:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   461
			self.suite_regex = re.compile(".*" + suitepattern + ".*", re.I)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   462
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   463
			self.suite_regex = re.compile(".*\_suite$", re.I)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   464
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   465
		if testpattern:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   466
			self.test_file_regex = re.compile(".*" + testpattern + ".*",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   467
					re.I)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   468
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   469
			self.test_file_regex = None
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   470
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   471
		self.suitepattern = suitepattern
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   472
		self.testpattern = testpattern
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   473
		self.upload_location = upload_location
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   474
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   475
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   476
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   477
	def run_tests(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   478
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   479
		Run all the tests in the specified suite (directory)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   480
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   481
	
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   482
		suites = []
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   483
		for dir in os.listdir("."):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   484
			name_match = self.suite_regex.match(dir)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   485
			# Each folder that matches the suite pattern will be looked into
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   486
			# Also checks to make sure the found entry is actually a directory
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   487
			if name_match is not None and os.path.isdir(dir):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   488
				s = Suite(dir, self)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   489
				s.run()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   490
				self.aggregate(s)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   491
				suites.append(dir)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   492
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   493
		# Print which options were used
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   494
		if options.test_home == None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   495
			options_dir = "defaults)"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   496
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   497
			options_dir = "'" + options.test_home + "' options file)"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   498
		print "\n(Tests run using %s" %options_dir
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   499
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   500
		# Summarise the entire test run
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   501
		if self.suitepattern and (len(suites) < 1):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   502
			print "\nNo suites matched specification '" + self.suitepattern + \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   503
					"'\n"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   504
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   505
			print "Overall summary (%d suites, %d tests):" \
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   506
					%(len(suites), self.test_total)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   507
			self.show()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   508
			self.what_failed()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   509
	        
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   510
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   511
	def sort_dict(self, input_dict):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   512
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   513
		This method sorts values in a dictionary
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   514
		"""
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   515
		keys = input_dict.keys()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   516
		keys.sort()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   517
		return keys
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   518
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   519
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   520
# Make SBS_HOME, EPOCROOT have uppercase drive letters to match os.getcwd() and
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   521
# thus stop all those insane test problems which result from one being uppercase
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   522
# and the other lowercase
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   523
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   524
if sys.platform.startswith("win"):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   525
	sh = os.environ['SBS_HOME']
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   526
	if sh[1] == ':':
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   527
		os.environ['SBS_HOME'] = sh[0].upper() + sh[1:]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   528
	er = os.environ['EPOCROOT']
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   529
	if er[1] == ':':
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   530
		os.environ['EPOCROOT'] = er[0].upper() + er[1:]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   531
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   532
# Clean epocroot before running tests
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   533
raptor_tests.clean_epocroot()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   534
run_tests = SuiteRun(suitepattern = options.suite, testpattern = options.tests,
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   535
		upload_location = options.upload)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   536
run_tests.run_tests()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   537
625
a1925fb7753a sbs version 2.15.0
Richard Taylor <richard.i.taylor@nokia.com>
parents: 616
diff changeset
   538
if run_tests.suites_failed:
a1925fb7753a sbs version 2.15.0
Richard Taylor <richard.i.taylor@nokia.com>
parents: 616
diff changeset
   539
	sys.exit(1)
a1925fb7753a sbs version 2.15.0
Richard Taylor <richard.i.taylor@nokia.com>
parents: 616
diff changeset
   540