~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/goose/test.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
import subprocess
 
5
import sys
 
6
 
 
7
 
 
8
KNOWN_LIVE_SUITES = [
 
9
    'client',
 
10
    'glance',
 
11
    'identity',
 
12
    'nova',
 
13
    'swift',
 
14
]
 
15
 
 
16
 
 
17
def ensure_tarmac_log_dir():
 
18
    """Hack-around tarmac not creating its own log directory."""
 
19
    try:
 
20
        os.makedirs(os.path.expanduser("~/logs/"))
 
21
    except OSError:
 
22
        # Could be already exists, or cannot create, either way, just continue
 
23
        pass
 
24
 
 
25
 
 
26
def create_tarmac_repository():
 
27
    """Try to ensure a shared repository for the code."""
 
28
    try:
 
29
        from bzrlib import (
 
30
            branch,
 
31
            controldir,
 
32
            errors,
 
33
            transport,
 
34
            repository,
 
35
            reconfigure,
 
36
            )
 
37
    except ImportError:
 
38
        sys.stderr.write('Could not import bzrlib to ensure a repository\n')
 
39
        return
 
40
    try:
 
41
        b, _ = branch.Branch.open_containing('.')
 
42
    except:
 
43
        sys.stderr.write('Could not open local branch\n')
 
44
        return
 
45
    # By the time we get here, we've already branched everything from
 
46
    # launchpad. So if we aren't in a shared repository, we create one, and
 
47
    # fetch all the data into it, so it doesn't have to be fetched again.
 
48
    if b.repository.is_shared():
 
49
        return
 
50
    pwd = os.getcwd()
 
51
    expected_dir = 'src/launchpad.net/'
 
52
    offset = pwd.rfind(expected_dir)
 
53
    if offset == -1:
 
54
        sys.stderr.write('Could not find %r to create a shared repo\n')
 
55
        return
 
56
    path = pwd[:offset+len(expected_dir)]
 
57
    try:
 
58
        repository.Repository.open(path)
 
59
    except (errors.NoRepositoryPresent, errors.NotBranchError):
 
60
        pass # Good, the repo didn't exist
 
61
    else:
 
62
        # We must have already created the repo.
 
63
        return
 
64
    repo_fmt = controldir.format_registry.make_bzrdir('default')
 
65
    trans = transport.get_transport(path)
 
66
    info = repo_fmt.initialize_on_transport_ex(trans, create_prefix=False,
 
67
        make_working_trees=True, shared_repo=True, force_new_repo=True,
 
68
        use_existing_dir=True,
 
69
        repo_format_name=repo_fmt.repository_format.get_format_string())
 
70
    repo = info[0]
 
71
    sys.stderr.write('Reconfiguring to use a shared repository\n')
 
72
    reconfiguration = reconfigure.Reconfigure.to_use_shared(b.bzrdir)
 
73
    try:
 
74
        reconfiguration.apply(False)
 
75
    except errors.NoRepositoryPresent:
 
76
        sys.stderr.write('tarmac did a lightweight checkout,'
 
77
                         ' not fetching into the repo.\n')
 
78
 
 
79
 
 
80
def ensure_juju_core_dependencies():
 
81
    """Ensure that juju-core and all dependencies have been installed."""
 
82
    # Note: This potentially overwrites goose while it is updating the world.
 
83
    # However, if we are targetting the trunk branch of goose, that should have
 
84
    # already been updated to the latest version by tarmac.
 
85
    # I don't quite see a way to reconcile that we want the latest juju-core
 
86
    # and all of the other dependencies, but we don't want to touch goose
 
87
    # itself. One option would be to have a split GOPATH. One installs the
 
88
    # latest juju-core and everything else. The other is where the
 
89
    # goose-under-test resides. So we don't add the goose-under-test to GOPATH,
 
90
    # call "go get", then add it to the GOPATH for the rest of the testing.
 
91
    cmd = ['go', 'get', '-u', '-x', 'launchpad.net/juju-core/...']
 
92
    sys.stderr.write('Running: %s\n' % (' '.join(cmd),))
 
93
    retcode = subprocess.call(cmd)
 
94
    if retcode != 0:
 
95
        sys.stderr.write('WARN: Failed to update launchpad.net/juju-core\n')
 
96
 
 
97
 
 
98
def tarmac_setup(opts):
 
99
    """Do all the bits of setup that need to happen for the tarmac bot."""
 
100
    ensure_tarmac_log_dir()
 
101
    create_tarmac_repository()
 
102
    ensure_juju_core_dependencies()
 
103
 
 
104
 
 
105
def setup_gopath():
 
106
    pwd = os.getcwd()
 
107
    if sys.platform == 'win32':
 
108
        pwd = pwd.replace('\\', '/')
 
109
    offset = pwd.rfind('src/launchpad.net/goose')
 
110
    if offset == -1:
 
111
        sys.stderr.write('Could not find "src/launchpad.net/goose" in cwd: %s\n'
 
112
                         % (pwd,))
 
113
        sys.stderr.write('Unable to automatically set GOPATH\n')
 
114
        return
 
115
    add_gopath = pwd[:offset].rstrip('/')
 
116
    gopath = os.environ.get("GOPATH")
 
117
    if gopath:
 
118
        if add_gopath in gopath:
 
119
            return
 
120
        # Put this path first, so we know we are running these tests
 
121
        gopath = add_gopath + os.pathsep + gopath
 
122
    else:
 
123
        gopath = add_gopath
 
124
    sys.stderr.write('Setting GOPATH to: %s\n' % (gopath,))
 
125
    os.environ['GOPATH'] = gopath
 
126
 
 
127
 
 
128
def run_cmd(cmd):
 
129
    cmd_str = ' '.join(cmd)
 
130
    sys.stderr.write('Running: %s\n' % (cmd_str,))
 
131
    retcode = subprocess.call(cmd)
 
132
    if retcode != 0:
 
133
        sys.stderr.write('FAIL: failed running: %s\n' % (cmd_str,))
 
134
    return retcode
 
135
 
 
136
 
 
137
def run_go_fmt(opts):
 
138
    return run_cmd(['go', 'fmt', './...'])
 
139
 
 
140
 
 
141
def run_go_build(opts):
 
142
    return run_cmd(['go', 'build', './...'])
 
143
 
 
144
 
 
145
def run_go_test(opts):
 
146
    # Note: I wish we could run this with '-gocheck.v'
 
147
    return run_cmd(['go', 'test', './...'])
 
148
 
 
149
 
 
150
def run_juju_core_tests(opts):
 
151
    """Run the juju-core test suite"""
 
152
    orig_wd = os.getcwd()
 
153
    try:
 
154
        sys.stderr.write('Switching to juju-core')
 
155
        os.chdir('../juju-core')
 
156
        retval = run_cmd(['go', 'build', './...'])
 
157
        if retval != 0:
 
158
            return retval
 
159
        return run_cmd(['go', 'test', './...'])
 
160
    finally:
 
161
        os.chdir(orig_wd)
 
162
 
 
163
 
 
164
def run_live_tests(opts):
 
165
    """Run all of the live tests."""
 
166
    orig_wd = os.getcwd()
 
167
    final_retcode = 0
 
168
    for d in KNOWN_LIVE_SUITES:
 
169
        try:
 
170
            cmd = ['go', 'test', '-live', '-gocheck.v']
 
171
            sys.stderr.write('Running: %s in %s\n' % (' '.join(cmd), d))
 
172
            os.chdir(d)
 
173
            retcode = subprocess.call(cmd)
 
174
            if retcode != 0:
 
175
                sys.stderr.write('FAIL: Running live tests in %s\n' % (d,))
 
176
                final_retcode = retcode
 
177
        finally:
 
178
            os.chdir(orig_wd)
 
179
 
 
180
 
 
181
def main(args):
 
182
    import argparse
 
183
    p = argparse.ArgumentParser(description='Run the goose test suite')
 
184
    p.add_argument('--verbose', action='store_true', help='Be chatty')
 
185
    p.add_argument('--version', action='version', version='%(prog)s 0.1')
 
186
    p.add_argument('--tarmac', action='store_true',
 
187
        help="Pass this if the script is running as the tarmac bot."
 
188
             " This is used for stuff like ensuring repositories and"
 
189
             " logging directories are initialized.")
 
190
    p.add_argument('--juju-core', action='store_true',
 
191
        help="Run the juju-core trunk tests as well as the goose tests.")
 
192
    p.add_argument('--live', action='store_true',
 
193
        help="Run tests against a live service.")
 
194
 
 
195
    opts = p.parse_args(args)
 
196
    setup_gopath()
 
197
    if opts.tarmac:
 
198
        tarmac_setup(opts)
 
199
    to_run = [run_go_fmt, run_go_build, run_go_test]
 
200
    if opts.juju_core:
 
201
        to_run.append(run_juju_core_tests)
 
202
    if opts.live:
 
203
        to_run.append(run_live_tests)
 
204
    for func in to_run:
 
205
        retcode = func(opts)
 
206
        if retcode != 0:
 
207
            return retcode
 
208
 
 
209
 
 
210
if __name__ == '__main__':
 
211
    import sys
 
212
    main(sys.argv[1:])
 
213