michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: from optparse import OptionParser michael@0: import os michael@0: import re michael@0: import sys michael@0: import unittest michael@0: michael@0: import mozlog michael@0: michael@0: import dmunit michael@0: import genfiles michael@0: michael@0: michael@0: def main(ip, port, heartbeat_port, scripts, directory, isTestDevice, verbose): michael@0: dmunit.ip = ip michael@0: dmunit.port = port michael@0: dmunit.heartbeat_port = heartbeat_port michael@0: if verbose: michael@0: dmunit.log_level = mozlog.DEBUG michael@0: michael@0: suite = unittest.TestSuite() michael@0: michael@0: genfiles.gen_test_files() michael@0: michael@0: if scripts: michael@0: # Ensure the user didn't include the .py on the name of the test file michael@0: # (and get rid of it if they did) michael@0: scripts = map(lambda x: x.split('.')[0], scripts) michael@0: else: michael@0: # Go through the directory and pick up everything michael@0: # named test_*.py and run it michael@0: testfile = re.compile('^test_.*\.py$') michael@0: files = os.listdir(directory) michael@0: michael@0: for f in files: michael@0: if testfile.match(f): michael@0: scripts.append(f.split('.')[0]) michael@0: michael@0: testLoader = dmunit.DeviceManagerTestLoader(isTestDevice) michael@0: for s in scripts: michael@0: suite.addTest(testLoader.loadTestsFromModuleName(s)) michael@0: unittest.TextTestRunner(verbosity=2).run(suite) michael@0: michael@0: genfiles.clean_test_files() michael@0: michael@0: michael@0: if __name__ == "__main__": michael@0: michael@0: default_ip = '127.0.0.1' michael@0: default_port = 20701 michael@0: michael@0: env_ip, _, env_port = os.getenv('TEST_DEVICE', '').partition(':') michael@0: if env_port: michael@0: try: michael@0: env_port = int(env_port) michael@0: except ValueError: michael@0: print >> sys.stderr, "Port in TEST_DEVICE should be an integer." michael@0: sys.exit(1) michael@0: michael@0: # Deal with the options michael@0: parser = OptionParser() michael@0: parser.add_option("--ip", action="store", type="string", dest="ip", michael@0: help="IP address for device running SUTAgent, defaults " michael@0: "to what's provided in $TEST_DEVICE or 127.0.0.1", michael@0: default=(env_ip or default_ip)) michael@0: michael@0: parser.add_option("--port", action="store", type="int", dest="port", michael@0: help="Port of SUTAgent on device, defaults to " michael@0: "what's provided in $TEST_DEVICE or 20701", michael@0: default=(env_port or default_port)) michael@0: michael@0: parser.add_option("--heartbeat", action="store", type="int", michael@0: dest="heartbeat_port", help="Port for heartbeat/data " michael@0: "channel, defaults to 20700", default=20700) michael@0: michael@0: parser.add_option("--script", action="append", type="string", michael@0: dest="scripts", help="Name of test script to run, " michael@0: "can be specified multiple times", default=[]) michael@0: michael@0: parser.add_option("--directory", action="store", type="string", dest="dir", michael@0: help="Directory to look for tests in, defaults to " michael@0: "current directory", default=os.getcwd()) michael@0: michael@0: parser.add_option("--testDevice", action="store_true", dest="isTestDevice", michael@0: help="Specifies that the device is a local test agent", michael@0: default=False) michael@0: michael@0: parser.add_option("-v", "--verbose", action="store_true", dest="verbose", michael@0: help="Verbose DeviceManager output", default=False) michael@0: michael@0: (options, args) = parser.parse_args() michael@0: michael@0: main(options.ip, options.port, options.heartbeat_port, options.scripts, michael@0: options.dir, options.isTestDevice, options.verbose)