1
from argparse import Namespace
2
from contextlib import contextmanager
3
from unittest import TestCase
10
from industrial_test import (
15
from jujuci import Credentials
16
from schedule_reliability_tests import (
21
from test_utility import (
25
from tests import parse_error
26
from utility import temp_dir
29
class TestParseArgs(TestCase):
31
def test_parse_args(self):
32
args, credentials = parse_args([
33
'foo', '--user', 'jrandom', '--password', 'password1'])
36
Namespace(root_dir='foo', suite=[], jobs=None, user='jrandom',
37
password='password1'))
38
self.assertEqual(credentials,
39
Credentials(user='jrandom', password='password1'))
41
def test_parse_args_suite_supports_known_suites(self):
42
for suite in suites.keys():
45
'foo', '--suite', suite, '--user', 'u', '--password',
47
Namespace(root_dir='foo', suite=[suite], jobs=None,
48
user='u', password='p'))
50
def test_parse_args_bad_suite(self):
51
with parse_error(self) as stderr:
52
parse_args(['foo', '--suite', 'foo'])
53
self.assertRegexpMatches(stderr.getvalue(),
54
".*invalid choice: 'foo'.*")
56
def test_parse_args_multi_suite(self):
57
args = parse_args(['foo', '--suite', FULL, '--suite', UPGRADE,
58
'--user=u', '--password=p'])[0]
59
self.assertEqual(args.suite, [FULL, UPGRADE])
61
def test_parse_jobs(self):
63
parse_args(['foo', 'bar', '--user', 'jrandom', '--password',
65
Namespace(root_dir='foo', suite=[], jobs=['bar'], user='jrandom',
66
password='password1'))
68
parse_args(['foo', 'bar', 'baz', '--user', 'jrandom',
69
'--password', 'password1'])[0],
70
Namespace(root_dir='foo', suite=[], jobs=['bar', 'baz'],
71
user='jrandom', password='password1'))
74
class TestBuildJob(TestCase):
76
def test_build_job(self):
77
jenkins_cxt = patch('schedule_reliability_tests.Jenkins')
78
with jenkins_cxt as jenkins_mock, temp_dir() as root:
79
write_config(root, 'foo', 'quxxx')
80
build_job(Credentials('jrandom', 'password1'), root, 'foo',
81
[('bar', 1), ('baz', 2)], ['qux'])
82
jenkins_mock.assert_called_once_with(
83
'http://localhost:8080', 'jrandom', 'password1')
84
calls = jenkins_mock.return_value.build_job.mock_calls
89
'new_juju_dir': 'bar',
90
'revision_build': '1',
95
'new_juju_dir': 'baz',
99
self.assertEqual(calls, expected)
101
def test_build_job_multi_suite(self):
102
jenkins_cxt = patch('schedule_reliability_tests.Jenkins')
103
with jenkins_cxt as jenkins_mock, temp_dir() as root:
104
write_config(root, 'foo', 'bar')
105
build_job(Credentials('jrandom', 'password1'), root, 'foo',
106
[('baz', 1)], ['qux', 'quxx'])
107
jenkins_mock.return_value.build_job.assert_called_once_with(
111
'new_juju_dir': 'baz',
112
'revision_build': '1',
116
class TestMain(TestCase):
119
def build_job_context(self):
120
with temp_dir() as root:
121
write_config(root, 'foo', 'quxxx')
124
def run_main(self, root):
125
with patch('jenkins.Jenkins.build_job') as build_job_mock:
126
main([root, 'foo', '--user', 'bar', '--password', 'baz'])
127
return build_job_mock
129
def test_selects_newest_candidate(self):
130
with self.build_job_context() as root:
131
path_1234 = make_candidate_dir(
132
root, '1234', 'mybranch', '1234')
133
make_candidate_dir(root, '1233', 'mybranch', '1233')
134
build_job_mock = self.run_main(root)
135
build_job_mock.assert_called_once_with('foo', {
136
'new_juju_dir': path_1234,
139
'revision_build': '1234',
142
def test_limit_3(self):
143
# Even though it's only testing the latest, it should only test 3,
144
# even if there are more than 3 latest.
145
with self.build_job_context() as root:
147
root, 'branch1', 'mybranch1')
149
root, 'branch2', 'mybranch2')
151
root, 'branch3', 'mybranch3')
153
root, 'branch4', 'mybranch4')
154
build_job_mock = self.run_main(root)
155
self.assertEqual(build_job_mock.call_count, 3)