summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozdevice/sut_tests/runtests.py
blob: fffc306e3e78a10d015f8319d7ee95d8dad3aa87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from optparse import OptionParser
import logging
import os
import re
import sys
import unittest

import dmunit
import genfiles


def main(ip, port, heartbeat_port, scripts, directory, isTestDevice, verbose):
    dmunit.ip = ip
    dmunit.port = port
    dmunit.heartbeat_port = heartbeat_port
    if verbose:
        dmunit.log_level = logging.DEBUG

    suite = unittest.TestSuite()

    genfiles.gen_test_files()

    if scripts:
        # Ensure the user didn't include the .py on the name of the test file
        # (and get rid of it if they did)
        scripts = map(lambda x: x.split('.')[0], scripts)
    else:
        # Go through the directory and pick up everything
        # named test_*.py and run it
        testfile = re.compile('^test_.*\.py$')
        files = os.listdir(directory)

        for f in files:
            if testfile.match(f):
                scripts.append(f.split('.')[0])

    testLoader = dmunit.DeviceManagerTestLoader(isTestDevice)
    for s in scripts:
        suite.addTest(testLoader.loadTestsFromModuleName(s))
    unittest.TextTestRunner(verbosity=2).run(suite)

    genfiles.clean_test_files()


if __name__ == "__main__":

    default_ip = '127.0.0.1'
    default_port = 20701

    env_ip, _, env_port = os.getenv('TEST_DEVICE', '').partition(':')
    if env_port:
        try:
            env_port = int(env_port)
        except ValueError:
            print >> sys.stderr, "Port in TEST_DEVICE should be an integer."
            sys.exit(1)

    # Deal with the options
    parser = OptionParser()
    parser.add_option("--ip", action="store", type="string", dest="ip",
                      help="IP address for device running SUTAgent, defaults "
                      "to what's provided in $TEST_DEVICE or 127.0.0.1",
                      default=(env_ip or default_ip))

    parser.add_option("--port", action="store", type="int", dest="port",
                      help="Port of SUTAgent on device, defaults to "
                      "what's provided in $TEST_DEVICE or 20701",
                      default=(env_port or default_port))

    parser.add_option("--heartbeat", action="store", type="int",
                      dest="heartbeat_port", help="Port for heartbeat/data "
                      "channel, defaults to 20700", default=20700)

    parser.add_option("--script", action="append", type="string",
                      dest="scripts", help="Name of test script to run, "
                      "can be specified multiple times", default=[])

    parser.add_option("--directory", action="store", type="string", dest="dir",
                      help="Directory to look for tests in, defaults to "
                      "current directory", default=os.getcwd())

    parser.add_option("--testDevice", action="store_true", dest="isTestDevice",
                      help="Specifies that the device is a local test agent",
                      default=False)

    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                      help="Verbose DeviceManager output", default=False)

    (options, args) = parser.parse_args()

    main(options.ip, options.port, options.heartbeat_port, options.scripts,
         options.dir, options.isTestDevice, options.verbose)