~free.ekanayaka/landscape-client/jaunty-updates-1.4.4-0ubuntu0.9.04

« back to all changes in this revision

Viewing changes to test

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2009-09-21 17:59:31 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090921175931-4ucv40j9ro26i3lm
Tags: 1.3.2.3-0ubuntu0.9.04.0
* New upstream release (LP: #347983):
  - Don't clear the hash_id_requests table upon resynchronize (LP #417122)
  - Include the README file in landscape-client (LP: #396260)
  - Fix client capturing stderr from run_command when constructing
    hash-id-databases url (LP: #397480)
  - Use substvars to conditionally depend on update-motd or
    libpam-modules (LP: #393454)
  - Fix reporting wrong version to the server (LP: #391225)
  - The init script does not wait for the network to be available
    before checking for EC2 user data (LP: #383336)
  - When the broker is restarted by the watchdog, the state of the client
    is inconsistent (LP: #380633)
  - Package stays unknown forever in the client with hash-id-databases
    support (LP: #381356)
  - Standard error not captured when calling smart-update (LP: #387441)
  - Changer calls reporter without switching groups, just user (LP: #388092)
  - Run smart update in the package-reporter instead of having a cronjob (LP: #362355)
  - Package changer does not inherit proxy settings (LP: #381241)
  - The ./test script doesn't work in landscape-client (LP: #381613)
  - The source package should build on all supported releases (LP: #385098)
  - Strip smart update's output (LP: #387331)
  - The fetch() timeout isn't based on activity (#389224)
  - Client can use a UUID of "None" when fetching the hash-id-database (LP: #381291)
  - Registration should use the fqdn rather than just the hostname (LP: #385730)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
import tempfile
3
 
import optparse
4
 
import unittest
5
 
import doctest
6
 
import shutil
7
 
import new
8
2
import sys
9
3
import os
10
4
 
11
 
lib_dir = os.path.abspath(os.path.dirname(__file__))
12
 
sys.path.insert(0, lib_dir)
13
 
 
14
 
# This is needed for DBUS listeners to work.
15
 
from twisted.internet.glib2reactor import install
16
 
install()
17
 
 
18
 
import landscape
19
 
 
20
 
 
21
 
def find_tests(testpaths=()):
22
 
    """Find all test paths, or test paths contained in the provided sequence.
23
 
 
24
 
    @param testpaths: If provided, only tests in the given sequence will
25
 
                      be considered.  If not provided, all tests are
26
 
                      considered.
27
 
    @return: (unittests, doctests) tuple, with lists of unittests and
28
 
             doctests found, respectively.
29
 
    """
30
 
    topdir = os.path.abspath(os.path.dirname(__file__))
31
 
    testdir = os.path.dirname(landscape.__file__)
32
 
    testpaths = set(testpaths)
33
 
    unittests = []
34
 
    doctests = []
35
 
    for root, dirnames, filenames in os.walk(testdir):
36
 
        for filename in filenames:
37
 
            filepath = os.path.join(root, filename)
38
 
            relpath = filepath[len(topdir)+1:]
39
 
 
40
 
            if (filename == "__init__.py" or filename.endswith(".pyc") or
41
 
                relpath == "landscape/conftest.py" or "/tests/" not in relpath):
42
 
                # Skip non-tests.
43
 
                continue
44
 
 
45
 
            if testpaths:
46
 
                # Skip any tests not in testpaths.
47
 
                for testpath in testpaths:
48
 
                    if relpath.startswith(testpath):
49
 
                        break
50
 
                else:
51
 
                    continue
52
 
 
53
 
            if filename.endswith(".py"):
54
 
                unittests.append(relpath)
55
 
            elif filename.endswith(".txt"):
56
 
                doctests.append(relpath)
57
 
 
58
 
    return unittests, doctests
59
 
 
60
 
 
61
 
def parse_sys_argv():
62
 
    """Extract any arguments not starting with '-' from sys.argv."""
63
 
    testpaths = []
64
 
    for i in range(len(sys.argv)-1,0,-1):
65
 
        arg = sys.argv[i]
66
 
        if not arg.startswith("-"):
67
 
            testpaths.append(arg)
68
 
            del sys.argv[i]
69
 
    return testpaths
70
 
 
71
 
 
72
 
def test_with_trial():
73
 
    from twisted.scripts import trial
74
 
    unittests, doctests = find_tests(parse_sys_argv())
75
 
    sys.argv.extend(unittests)
76
 
    trial.run()
77
 
 
78
 
 
79
 
def test_with_py_test():
80
 
    import py
81
 
    dirname = os.path.dirname(__file__)
82
 
    unittests, doctests = find_tests(parse_sys_argv())
83
 
    sys.argv.extend(unittests)
84
 
    sys.argv.extend(doctests)
85
 
    # For timestamp checking when looping:
86
 
    sys.argv.append(os.path.join(os.path.dirname(__file__), "landscape/"))
87
 
    py.test.cmdline.main()
88
 
 
89
 
 
90
 
def test_with_unittest():
91
 
 
92
 
    usage = "test.py [options] [<test filename>, ...]"
93
 
 
94
 
    parser = optparse.OptionParser(usage=usage)
95
 
 
96
 
    parser.add_option('--verbose', action='store_true')
97
 
    opts, args = parser.parse_args()
98
 
 
99
 
    runner = unittest.TextTestRunner()
100
 
 
101
 
    if opts.verbose:
102
 
        runner.verbosity = 2
 
5
 
 
6
def main(args):
 
7
    for arg in args:
 
8
        if arg.startswith("landscape"):
 
9
            break
 
10
    else:
 
11
        args.append("landscape")
 
12
    os.execvp("trial", ["trial", "-r", "glib2"] + args)
103
13
 
104
 
    loader = unittest.TestLoader()
105
 
 
106
 
    unittests, doctests = find_tests(args)
107
 
 
108
 
    class Summary:
109
 
        def __init__(self):
110
 
            self.total_failures = 0
111
 
            self.total_errors = 0
112
 
            self.total_tests = 0
113
 
        def __call__(self, tests, failures, errors):
114
 
            self.total_tests += tests
115
 
            self.total_failures += failures
116
 
            self.total_errors += errors
117
 
            print "(tests=%d, failures=%d, errors=%d)" % \
118
 
                  (tests, failures, errors)
119
 
 
120
 
    unittest_summary = Summary()
121
 
    doctest_summary = Summary()
122
 
 
123
 
    if unittests:
124
 
        print "Running unittests..."
125
 
        for relpath in unittests:
126
 
            print "[%s]" % relpath
127
 
            modpath = relpath.replace('/', '.')[:-3]
128
 
            module = __import__(modpath, None, None, [""])
129
 
            test = loader.loadTestsFromModule(module)
130
 
            result = runner.run(test)
131
 
            unittest_summary(test.countTestCases(),
132
 
                             len(result.failures), len(result.errors))
133
 
            print
134
 
 
135
 
    if doctests:
136
 
        print "Running doctests..."
137
 
        doctest_flags = doctest.ELLIPSIS
138
 
        for relpath in doctests:
139
 
            print "[%s]" % relpath
140
 
            failures, total = doctest.testfile(relpath,
141
 
                                               optionflags=doctest_flags)
142
 
            doctest_summary(total, failures, 0)
143
 
            print
144
 
 
145
 
    print "Total test cases: %d" % unittest_summary.total_tests
146
 
    print "Total doctests: %d" % doctest_summary.total_tests
147
 
    print "Total failures: %d" % (unittest_summary.total_failures +
148
 
                                  doctest_summary.total_failures)
149
 
    print "Total errors: %d" % (unittest_summary.total_errors +
150
 
                                doctest_summary.total_errors)
151
 
 
152
 
    failed = bool(unittest_summary.total_failures or
153
 
                  unittest_summary.total_errors or
154
 
                  doctest_summary.total_failures or
155
 
                  doctest_summary.total_errors)
156
 
 
157
 
    sys.exit(failed)
158
14
 
159
15
if __name__ == "__main__":
160
 
    runner = os.environ.get("LANDSCAPE_TEST_RUNNER")
161
 
    if not runner:
162
 
        runner = "unittest"
163
 
    runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
164
 
    if not runner_func:
165
 
        sys.exit("Test runner not found: %s" % runner)
166
 
    runner_func()
 
16
    main(sys.argv[1:])
 
17
 
167
18
 
168
19
# vim:ts=4:sw=4:et