~nskaggs/juju-ci-tools/add-essential-operations

808.1.11 by Curtis Hovey
Improved verbosity.
1
#!/usr/bin/python
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
2
from __future__ import print_function
3
4
from argparse import ArgumentParser
5
import shutil
6
import os
7
import subprocess
8
import sys
9
import tarfile
923.1.1 by Martin Packman
Set TMP to fresh directory when running unit tests on windows
10
import tempfile
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
11
import traceback
12
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
13
from utility import (
14
    print_now,
15
    temp_dir,
16
)
808.1.3 by Curtis Hovey
Added untar_gopath.
17
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
18
19
class WorkingDirectory:
20
    """Context manager for changing the current working directory"""
21
    def __init__(self, working_path):
22
        self.working_path = working_path
23
24
    def __enter__(self):
25
        self.savedPath = os.getcwd()
26
        os.chdir(self.working_path)
27
28
    def __exit__(self, etype, value, traceback):
29
        os.chdir(self.savedPath)
30
31
928.1.1 by Martin Packman
Futher changes to gotesttarball script to clean up after mongo and re-add windows env hack
32
def run(command, **kwargs):
33
    """Run a command returning exit status."""
34
    proc = subprocess.Popen(command, **kwargs)
35
    proc.communicate()
36
    return proc.returncode
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
37
38
39
def untar_gopath(tarfile_path, gopath, delete=False, verbose=False):
808.1.3 by Curtis Hovey
Added untar_gopath.
40
    """Untar the tarfile to the gopath."""
41
    with temp_dir() as tmp_dir:
808.1.14 by Curtis Hovey
Changes per review.
42
        with tarfile.open(name=tarfile_path, mode='r:gz') as tar:
43
            tar.extractall(path=tmp_dir)
808.1.3 by Curtis Hovey
Added untar_gopath.
44
        if verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
45
            print_now('Extracted the Juju source.')
808.1.14 by Curtis Hovey
Changes per review.
46
        dir_name = os.path.basename(tarfile_path).replace('.tar.gz', '')
808.1.3 by Curtis Hovey
Added untar_gopath.
47
        dir_path = os.path.join(tmp_dir, dir_name)
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
48
        shutil.move(dir_path, gopath)
808.1.3 by Curtis Hovey
Added untar_gopath.
49
        if verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
50
            print_now('Moved %s to %s' % (dir_name, gopath))
808.1.3 by Curtis Hovey
Added untar_gopath.
51
    if delete:
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
52
        os.unlink(tarfile_path)
808.1.3 by Curtis Hovey
Added untar_gopath.
53
        if verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
54
            print_now('Deleted %s' % tarfile_path)
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
55
56
928.1.1 by Martin Packman
Futher changes to gotesttarball script to clean up after mongo and re-add windows env hack
57
def murder_mongo():
58
    """Kill off any lingering mongod processess."""
59
    if sys.platform == 'win32':
60
        return run(["taskkill.exe", "/F", "/FI", "imagename eq mongod.exe"])
61
    return run(["sudo", "killall", "-SIGABRT", "mongod"])
62
63
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
64
def go_test_package(package, go_cmd, gopath, verbose=False):
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
65
    """Run the package unit tests."""
808.1.14 by Curtis Hovey
Changes per review.
66
    # Set GOPATH and GOARCH to ensure the go command tests extracted
67
    # tarfile using the arch the win-agent is compiled with. The
68
    # default go env might be 386 used to create a win client.
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
69
    env = dict(os.environ)
70
    env['GOPATH'] = gopath
71
    env['GOARCH'] = 'amd64'
1489.1.17 by Curtis Hovey
Precompile test deps for win and centos.
72
    build_cmd = [go_cmd, 'test', '-i', './...']
73
    test_cmd = [go_cmd, 'test', '-timeout=1200s', './...']
862 by Curtis Hovey
Ensure win tests don't see OpenSSH, and do see powershell.
74
    if sys.platform == 'win32':
75
        # Ensure OpenSSH is never in the path for win tests.
863 by Curtis Hovey
Sanitise the os.environ['PATH'] for windows, not the sys.path.
76
        sane_path = [p for p in env['PATH'].split(';') if 'OpenSSH' not in p]
77
        env['PATH'] = ';'.join(sane_path)
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
78
        if verbose:
79
            print_now('Setting environ Path to:')
80
            print_now(env['PATH'])
928.1.1 by Martin Packman
Futher changes to gotesttarball script to clean up after mongo and re-add windows env hack
81
        # GZ 2015-04-21: Short-term hack to work around case-insensitive issues
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
82
        env['Path'] = env.pop('PATH')
923.1.1 by Martin Packman
Set TMP to fresh directory when running unit tests on windows
83
        tempdir = tempfile.mkdtemp(prefix="tmp-juju-test", dir=gopath)
84
        env['TMP'] = env['TEMP'] = tempdir
85
        if verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
86
            print_now('Setting environ TMP and TEMP to:')
87
            print_now(env['TEMP'])
1489.1.17 by Curtis Hovey
Precompile test deps for win and centos.
88
        build_cmd = ['powershell.exe', '-Command'] + build_cmd
89
        test_cmd = ['powershell.exe', '-Command'] + test_cmd
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
90
    package_dir = os.path.join(gopath, 'src', package.replace('/', os.sep))
91
    with WorkingDirectory(package_dir):
808.1.11 by Curtis Hovey
Improved verbosity.
92
        if verbose:
1489.1.17 by Curtis Hovey
Precompile test deps for win and centos.
93
            print_now('Building test dependencies')
1489.1.18 by Curtis Hovey
Fixed returncode var.
94
        returncode = run(build_cmd, env=env)
1489.1.19 by Curtis Hovey
Revisions per review.
95
        if returncode != 0:
96
            return returncode
1489.1.17 by Curtis Hovey
Precompile test deps for win and centos.
97
        if verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
98
            print_now('Running unit tests in %s' % package)
1489.1.17 by Curtis Hovey
Precompile test deps for win and centos.
99
        returncode = run(test_cmd, env=env)
808.1.9 by Curtis Hovey
Added tests for main.
100
        if verbose:
808.1.12 by Curtis Hovey
Renamed module and improved verbose.
101
            if returncode == 0:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
102
                print_now('SUCCESS')
808.1.12 by Curtis Hovey
Renamed module and improved verbose.
103
            else:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
104
                print_now('FAIL')
105
            print_now("Killing any lingering mongo processes...")
928.1.1 by Martin Packman
Futher changes to gotesttarball script to clean up after mongo and re-add windows env hack
106
        murder_mongo()
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
107
    return returncode
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
108
1315.2.23 by Aaron Bentley
Introduce set_model_name, update tests, check controller on bootstrap.
109
# suppress nosetests
110
go_test_package.__test__ = False
111
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
112
113
def parse_args(args=None):
808.1.9 by Curtis Hovey
Added tests for main.
114
    """Return parsed args for this program."""
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
115
    parser = ArgumentParser("Run go test against the content of a tarfile.")
116
    parser.add_argument(
117
        '-v', '--verbose', action='store_true', default=False,
118
        help='Increase verbosity.')
119
    parser.add_argument(
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
120
        '-g', '--go', default='go', help='The go comand.')
808.1.3 by Curtis Hovey
Added untar_gopath.
121
    parser.add_argument(
808.1.14 by Curtis Hovey
Changes per review.
122
        '-p', '--package', default='github.com/juju/juju',
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
123
        help='The package to test.')
124
    parser.add_argument(
808.1.8 by Curtis Hovey
Added test for parse_args.
125
        '-r', '--remove-tarfile', action='store_true', default=False,
126
        help='Remove the tarfile after extraction.')
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
127
    parser.add_argument(
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
128
        'tarfile', help='The path to the gopath tarfile.')
129
    return parser.parse_args(args)
130
131
808.1.9 by Curtis Hovey
Added tests for main.
132
def main(argv=None):
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
133
    """Run go test against the content of a tarfile."""
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
134
    returncode = 0
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
135
    args = parse_args(argv)
808.1.9 by Curtis Hovey
Added tests for main.
136
    tarfile_path = args.tarfile
137
    tarfile_name = os.path.basename(tarfile_path)
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
138
    version = tarfile_name.split('_')[-1].replace('.tar.gz', '')
139
    try:
808.1.9 by Curtis Hovey
Added tests for main.
140
        if args.verbose:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
141
            print_now('Testing juju %s from %s' % (version, tarfile_name))
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
142
        with temp_dir() as workspace:
143
            gopath = os.path.join(workspace, 'gogo')
144
            untar_gopath(
145
                tarfile_path, gopath, delete=args.remove_tarfile,
146
                verbose=args.verbose)
147
            returncode = go_test_package(
148
                args.package, args.go, gopath, verbose=args.verbose)
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
149
    except Exception as e:
929.1.1 by Martin Packman
Flush output from script to interleave with child processes, and adjust PATH hack
150
        print_now(str(e))
151
        print_now(traceback.print_exc())
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
152
        return 3
808.1.6 by Curtis Hovey
refactored go_test_package to return the return_code for exit.
153
    return returncode
808.1.1 by Curtis Hovey
Added skeleton on a win test script culled from several scripts.
154
155
156
if __name__ == '__main__':
157
    sys.exit(main())