testing/mozbase/mozdevice/sut_tests/runtests.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozdevice/sut_tests/runtests.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +from optparse import OptionParser
     1.9 +import os
    1.10 +import re
    1.11 +import sys
    1.12 +import unittest
    1.13 +
    1.14 +import mozlog
    1.15 +
    1.16 +import dmunit
    1.17 +import genfiles
    1.18 +
    1.19 +
    1.20 +def main(ip, port, heartbeat_port, scripts, directory, isTestDevice, verbose):
    1.21 +    dmunit.ip = ip
    1.22 +    dmunit.port = port
    1.23 +    dmunit.heartbeat_port = heartbeat_port
    1.24 +    if verbose:
    1.25 +        dmunit.log_level = mozlog.DEBUG
    1.26 +
    1.27 +    suite = unittest.TestSuite()
    1.28 +
    1.29 +    genfiles.gen_test_files()
    1.30 +
    1.31 +    if scripts:
    1.32 +        # Ensure the user didn't include the .py on the name of the test file
    1.33 +        # (and get rid of it if they did)
    1.34 +        scripts = map(lambda x: x.split('.')[0], scripts)
    1.35 +    else:
    1.36 +        # Go through the directory and pick up everything
    1.37 +        # named test_*.py and run it
    1.38 +        testfile = re.compile('^test_.*\.py$')
    1.39 +        files = os.listdir(directory)
    1.40 +
    1.41 +        for f in files:
    1.42 +            if testfile.match(f):
    1.43 +                scripts.append(f.split('.')[0])
    1.44 +
    1.45 +    testLoader = dmunit.DeviceManagerTestLoader(isTestDevice)
    1.46 +    for s in scripts:
    1.47 +        suite.addTest(testLoader.loadTestsFromModuleName(s))
    1.48 +    unittest.TextTestRunner(verbosity=2).run(suite)
    1.49 +
    1.50 +    genfiles.clean_test_files()
    1.51 +
    1.52 +
    1.53 +if  __name__ == "__main__":
    1.54 +
    1.55 +    default_ip = '127.0.0.1'
    1.56 +    default_port = 20701
    1.57 +
    1.58 +    env_ip, _, env_port = os.getenv('TEST_DEVICE', '').partition(':')
    1.59 +    if env_port:
    1.60 +        try:
    1.61 +            env_port = int(env_port)
    1.62 +        except ValueError:
    1.63 +            print >> sys.stderr, "Port in TEST_DEVICE should be an integer."
    1.64 +            sys.exit(1)
    1.65 +
    1.66 +    # Deal with the options
    1.67 +    parser = OptionParser()
    1.68 +    parser.add_option("--ip", action="store", type="string", dest="ip",
    1.69 +                      help="IP address for device running SUTAgent, defaults "
    1.70 +                      "to what's provided in $TEST_DEVICE or 127.0.0.1",
    1.71 +                      default=(env_ip or default_ip))
    1.72 +
    1.73 +    parser.add_option("--port", action="store", type="int", dest="port",
    1.74 +                      help="Port of SUTAgent on device, defaults to "
    1.75 +                      "what's provided in $TEST_DEVICE or 20701",
    1.76 +                      default=(env_port or default_port))
    1.77 +
    1.78 +    parser.add_option("--heartbeat", action="store", type="int",
    1.79 +                      dest="heartbeat_port", help="Port for heartbeat/data "
    1.80 +                      "channel, defaults to 20700", default=20700)
    1.81 +
    1.82 +    parser.add_option("--script", action="append", type="string",
    1.83 +                      dest="scripts", help="Name of test script to run, "
    1.84 +                      "can be specified multiple times", default=[])
    1.85 +
    1.86 +    parser.add_option("--directory", action="store", type="string", dest="dir",
    1.87 +                      help="Directory to look for tests in, defaults to "
    1.88 +                      "current directory", default=os.getcwd())
    1.89 +
    1.90 +    parser.add_option("--testDevice", action="store_true", dest="isTestDevice",
    1.91 +                      help="Specifies that the device is a local test agent",
    1.92 +                      default=False)
    1.93 +
    1.94 +    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
    1.95 +                      help="Verbose DeviceManager output", default=False)
    1.96 +
    1.97 +    (options, args) = parser.parse_args()
    1.98 +
    1.99 +    main(options.ip, options.port, options.heartbeat_port, options.scripts,
   1.100 +         options.dir, options.isTestDevice, options.verbose)

mercurial