1
from __future__ import print_function
3
from datetime import timedelta
7
from unittest import TestCase
14
from jujuci import Credentials
15
from schedule_hetero_control import (
22
from utility import temp_dir
23
from test_utility import write_config
29
class TestScheduleHeteroControl(TestCase):
31
def test_get_args_credentials(self):
32
args, credentials = get_args(['root', '--user', 'jrandom',
33
'--password', 'password'])
34
self.assertEqual(args.user, 'jrandom')
35
self.assertEqual(args.password, 'password')
36
self.assertEqual(credentials, Credentials('jrandom', 'password'))
38
def test_get_releases(self):
39
with temp_dir() as root:
40
old_juju_dir = os.path.join(root, 'old-juju')
41
supported_juju = os.path.join(old_juju_dir, '1.24.5')
42
devel_juju = os.path.join(old_juju_dir, '1.25-alpha1')
43
test_juju = os.path.join(old_juju_dir, '1.24.5~')
44
for path in (old_juju_dir, supported_juju, devel_juju, test_juju):
46
found = get_releases(root)
47
self.assertEqual(['1.24.5'], list(found))
50
class TestBuildJobs(TestCase):
52
def test_build_jobs_credentials(self):
53
credentials = Credentials('jrandom', 'password1')
54
with temp_dir() as root:
55
write_config(root, 'compatibility-control', 'asdf')
56
with patch('schedule_hetero_control.Jenkins',
57
autospec=True) as jenkins_mock:
58
build_jobs(credentials, root, [])
59
jenkins_mock.assert_called_once_with(
60
'http://juju-ci.vapour.ws:8080', 'jrandom', 'password1')
62
def test_build_jobs(self):
63
credentials = Credentials('jrandom', 'password1')
65
for os_str in ('ubuntu', 'osx', 'windows'):
67
'old_version': "1.18.4",
68
'candidate': "1.24.5",
70
'candidate_path': "1.24.5",
72
'revision_build': "2999"
75
call('compatibility-control',
76
{'candidate_path': '1.24.5', 'candidate': '1.24.5',
77
'new_to_old': 'true', 'revision_build': '2999',
78
'old_version': '1.18.4', 'client_os': 'ubuntu'}),
79
call('compatibility-control-osx',
80
{'candidate_path': '1.24.5', 'candidate': '1.24.5',
81
'new_to_old': 'true', 'revision_build': '2999',
82
'old_version': '1.18.4', 'client_os': 'osx'}),
83
call('compatibility-control-windows',
84
{'candidate_path': '1.24.5', 'candidate': '1.24.5',
85
'new_to_old': 'true', 'revision_build': '2999',
86
'old_version': '1.18.4', 'client_os': 'windows'})]
87
with temp_dir() as root:
88
write_config(root, 'compatibility-control', 'asdf')
89
with patch('schedule_hetero_control.Jenkins',
90
autospec=True) as jenkins_mock:
91
build_jobs(credentials, root, jobs)
92
jenkins_mock.assert_called_once_with(
93
'http://juju-ci.vapour.ws:8080', 'jrandom', 'password1')
95
jenkins_mock.return_value.build_job.call_args_list, calls)
98
class TestGetCandidateInfo(TestCase):
99
def test_get_candidate_info(self):
100
with temp_dir() as dir_path:
101
make_build_var_file(dir_path, version='1.24.3')
102
version, revision = get_candidate_info(dir_path)
103
self.assertEqual(version, '1.24.3')
104
self.assertEqual(revision, '2870')
107
class CalculateJobs(TestCase):
108
def test_calculate_jobs(self):
109
with temp_dir() as root:
110
release_path = os.path.join(root, 'old-juju', '1.20.11')
111
os.makedirs(release_path)
112
candidate_path = os.path.join(root, 'candidate', '1.24')
113
os.makedirs(candidate_path)
114
make_build_var_file(candidate_path, version='1.24.3')
115
jobs = list(calculate_jobs(root))
116
expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
117
self.assertItemsEqual(jobs, expected)
119
def test_calculate_jobs_schedule_all(self):
120
with temp_dir() as root:
121
release_path = os.path.join(root, 'old-juju', '1.20.11')
122
os.makedirs(release_path)
123
candidate_path = os.path.join(root, 'candidate', '1.24')
124
os.makedirs(candidate_path)
125
make_build_var_file(candidate_path, '1.24.3')
126
candidate_path_2 = os.path.join(root, 'candidate', '1.23')
127
os.makedirs(candidate_path_2)
128
buildvars_path = make_build_var_file(candidate_path_2, '1.23.3')
129
a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds()
130
os.utime(buildvars_path, (time(), a_week_ago))
131
jobs = list(calculate_jobs(root, schedule_all=False))
132
jobs_schedule_all = list(calculate_jobs(root, schedule_all=True))
133
expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
134
expected.extend(self.make_jobs('1.23.3', '1.20.11', '1.23'))
135
self.assertItemsEqual(jobs, expected[:6])
136
self.assertItemsEqual(jobs_schedule_all, expected)
138
def test_calculate_jobs_ignore_1_26(self):
139
with temp_dir() as root:
140
release_path = os.path.join(root, 'old-juju', '1.20.11')
141
os.makedirs(release_path)
142
candidate_path = os.path.join(root, 'candidate', '1.24')
143
os.makedirs(candidate_path)
144
make_build_var_file(candidate_path, '1.24.3')
145
candidate_path_2 = os.path.join(root, 'candidate', '1.26')
146
os.makedirs(candidate_path_2)
147
make_build_var_file(candidate_path_2, '1.26-aloha1')
148
jobs = list(calculate_jobs(root))
149
expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
150
self.assertItemsEqual(jobs, expected)
152
def test_calculate_jobs_osx(self):
153
with temp_dir() as root:
154
release_path = os.path.join(root, 'old-juju', '1.20.11')
155
os.makedirs(release_path)
156
candidate_path = os.path.join(root, 'candidate', '1.24.4')
157
os.makedirs(candidate_path)
158
make_build_var_file(candidate_path, '1.24.4')
159
jobs = list(calculate_jobs(root, schedule_all=False))
160
expected = self.make_jobs('1.24.4', '1.20.11')
161
self.assertItemsEqual(jobs, expected)
163
def test_calculate_jobs_candidate_v2(self):
164
with temp_dir() as root:
165
release_path = os.path.join(root, 'old-juju', '1.20.11')
166
os.makedirs(release_path)
167
release_path = os.path.join(root, 'old-juju', '2.0.0')
168
os.makedirs(release_path)
169
candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
170
os.makedirs(candidate_path_2)
171
make_build_var_file(candidate_path_2, '2.0.1')
172
jobs = list(calculate_jobs(root))
173
expected = self.make_jobs('2.0.1', '2.0.0')
174
self.assertItemsEqual(jobs, expected)
176
def test_calculate_jobs_candiade_v1_and_v2(self):
177
with temp_dir() as root:
178
release_path = os.path.join(root, 'old-juju', '1.20.11')
179
os.makedirs(release_path)
180
release_path = os.path.join(root, 'old-juju', '2.0.0')
181
os.makedirs(release_path)
182
candidate_path = os.path.join(root, 'candidate', '1.24.3')
183
os.makedirs(candidate_path)
184
make_build_var_file(candidate_path, '1.24.3')
185
candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
186
os.makedirs(candidate_path_2)
187
make_build_var_file(candidate_path_2, '2.0.1')
188
jobs = list(calculate_jobs(root))
189
expected = self.make_jobs('2.0.1', '2.0.0')
190
expected.extend(self.make_jobs('1.24.3', '1.20.11'))
191
self.assertItemsEqual(jobs, expected)
193
def make_jobs(self, candidate, old_version, candidate_path=None):
195
for client_os in ('ubuntu', 'osx', 'windows'):
196
for new_to_old in ('false', 'true'):
198
'candidate': candidate,
199
'candidate_path': candidate_path or candidate,
200
'client_os': client_os,
201
'new_to_old': new_to_old,
202
'old_version': old_version,
203
'revision_build': '2870'})
207
def make_build_var_file(dir_path, version, revision_build="2870"):
208
build_vars = {"version": version, "revision_build": revision_build}
209
file_path = os.path.join(dir_path, 'buildvars.json')
210
with open(file_path, 'w') as json_file:
211
json.dump(build_vars, json_file)