109
102
class TestFindCandidates(TestCase):
111
def test__find_candidates_artifacts_default(self):
112
with temp_dir() as root:
113
make_candidate_dir(root, 'master-artifacts')
114
make_candidate_dir(root, '1.25')
115
candidate = os.path.join(root, 'candidate', '1.25')
116
self.assertEqual(list(_find_candidates(root)), [
117
(candidate, os.path.join(candidate, 'buildvars.json'))])
119
def test__find_candidates_artifacts_enabled(self):
120
with temp_dir() as root:
121
make_candidate_dir(root, 'master-artifacts')
122
make_candidate_dir(root, '1.25')
123
candidate = os.path.join(root, 'candidate', 'master-artifacts')
124
self.assertEqual(list(_find_candidates(root, artifacts=True)), [
125
(candidate, os.path.join(candidate, 'buildvars.json'))])
127
104
def test_find_candidates(self):
128
105
with temp_dir() as root:
129
106
master_path = make_candidate_dir(root, 'master')
176
153
def test_find_latest_branch_candidates(self):
177
154
with temp_dir() as root:
178
master_path = make_candidate_dir(root, 'master-artifacts')
155
master_path = make_candidate_dir(root, 'master')
179
156
self.assertEqual(find_latest_branch_candidates(root),
180
[(master_path, 1234)])
182
159
def test_find_latest_branch_candidates_old_buildvars(self):
183
160
with temp_dir() as root:
184
161
a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds()
185
make_candidate_dir(root, 'master-artifacts', modified=a_week_ago)
162
make_candidate_dir(root, 'master', modified=a_week_ago)
186
163
self.assertEqual(find_latest_branch_candidates(root), [])
188
165
def test_ignore_older_revision_build(self):
189
166
with temp_dir() as root:
190
167
path_1234 = make_candidate_dir(
191
root, '1234-artifacts', 'mybranch', '1234')
168
root, '1234', 'mybranch', '1234')
192
169
make_candidate_dir(root, '1233', 'mybranch', '1233')
193
self.assertEqual(find_latest_branch_candidates(root), [
170
self.assertEqual(find_latest_branch_candidates(root), [path_1234])
196
172
def test_include_older_revision_build_different_branch(self):
197
173
with temp_dir() as root:
198
174
path_1234 = make_candidate_dir(
199
root, '1234-artifacts', 'branch_foo', '1234')
175
root, '1234', 'branch_foo', '1234')
200
176
path_1233 = make_candidate_dir(
201
root, '1233-artifacts', 'branch_bar', '1233')
177
root, '1233', 'branch_bar', '1233')
202
178
self.assertItemsEqual(
203
find_latest_branch_candidates(root), [
204
(path_1233, 1233), (path_1234, 1234)])
207
class TestAsLiteralAddress(TestCase):
209
def test_hostname(self):
210
self.assertEqual("name.testing", as_literal_address("name.testing"))
213
self.assertEqual("127.0.0.2", as_literal_address("127.0.0.2"))
216
self.assertEqual("[2001:db8::7]", as_literal_address("2001:db8::7"))
219
class TestIsIPv6Address(TestCase):
221
def test_hostname(self):
222
self.assertIs(False, is_ipv6_address("name.testing"))
225
self.assertIs(False, is_ipv6_address("127.0.0.2"))
228
self.assertIs(True, is_ipv6_address("2001:db8::4"))
230
def test_ipv6_missing_support(self):
231
with patch('utility.socket', wraps=socket) as wrapped_socket:
232
del wrapped_socket.inet_pton
233
result = is_ipv6_address("2001:db8::4")
234
# Would use expectedFailure here, but instead just assert wrong result.
235
self.assertIs(False, result)
238
class TestSplitAddressPort(TestCase):
240
def test_hostname(self):
242
("name.testing", None), split_address_port("name.testing"))
246
("127.0.0.2", "17017"), split_address_port("127.0.0.2:17017"))
250
("2001:db8::7", "17017"), split_address_port("2001:db8::7:17017"))
179
find_latest_branch_candidates(root), [path_1233, path_1234])
253
182
class TestWaitForPort(TestCase):
298
227
connect_mock.assert_called_once_with(('192.168.8.3', 27))
300
229
def test_wait_for_port_no_address_closed(self):
301
error = socket.gaierror(socket.EAI_NODATA, 'What address?')
302
230
with patch('socket.getaddrinfo', autospec=True,
303
side_effect=error) as gai_mock:
231
side_effect=socket.error(-5, None)) as gai_mock:
304
232
with patch('socket.socket') as socket_mock:
305
233
wait_for_port('asdf', 26, closed=True)
306
234
gai_mock.assert_called_once_with('asdf', 26, socket.AF_INET,
329
257
self.assertEqual(socket_mock.call_count, 0)
331
def test_ipv6_open(self):
332
gai_result = [(23, 0, 0, '', ('2001:db8::2', 22, 0, 0))]
333
with patch('socket.getaddrinfo', autospec=True,
334
return_value=gai_result) as gai_mock:
335
with patch('socket.socket') as socket_mock:
336
wait_for_port('2001:db8::2', 22, closed=False)
337
gai_mock.assert_called_once_with(
338
'2001:db8::2', 22, socket.AF_INET6, socket.SOCK_STREAM)
339
socket_mock.assert_called_once_with(23, 0, 0)
340
connect_mock = socket_mock.return_value.connect
341
connect_mock.assert_called_once_with(('2001:db8::2', 22, 0, 0))
344
260
class TestExtractDeb(TestCase):
372
288
upload_tools=False, bootstrap_host=None, machine=[], region=None)
373
289
self.assertEqual(args, expected)
375
def test_positional_args_add_juju_bin_name(self):
376
cmd_line = ['local', '/juju', '/tmp/logs', 'testtest']
377
parser = add_basic_testing_arguments(ArgumentParser())
378
args = parser.parse_args(cmd_line)
379
self.assertEqual(args.juju_bin, '/juju')
381
def test_positional_args_accepts_juju_exe(self):
382
cmd_line = ['local', 'c:\\juju.exe', '/tmp/logs', 'testtest']
383
parser = add_basic_testing_arguments(ArgumentParser())
384
args = parser.parse_args(cmd_line)
385
self.assertEqual(args.juju_bin, 'c:\\juju.exe')
387
def test_warns_on_dirty_logs(self):
388
with warnings.catch_warnings(record=True) as warned:
389
with temp_dir() as log_dir:
390
open(os.path.join(log_dir, "existing.log"), "w").close()
391
cmd_line = ['local', '/a/juju', log_dir, 'testtest']
392
parser = add_basic_testing_arguments(ArgumentParser())
393
parser.parse_args(cmd_line)
394
self.assertEqual(len(warned), 1)
395
self.assertRegexpMatches(
396
str(warned[0].message),
397
r"^Directory '.*' has existing contents.$")
398
self.assertEqual("", self.log_stream.getvalue())
400
def test_no_warn_on_empty_logs(self):
401
"""Special case a file named 'empty' doesn't make log dir dirty"""
402
with warnings.catch_warnings(record=True) as warned:
403
with temp_dir() as log_dir:
404
open(os.path.join(log_dir, "empty"), "w").close()
405
cmd_line = ['local', '/a/juju', log_dir, 'testtest']
406
parser = add_basic_testing_arguments(ArgumentParser())
407
parser.parse_args(cmd_line)
408
self.assertEqual(warned, [])
409
self.assertEqual("", self.log_stream.getvalue())
411
291
def test_debug(self):
412
292
cmd_line = ['local', '/foo/juju', '/tmp/logs', 'testtest', '--debug']
413
293
parser = add_basic_testing_arguments(ArgumentParser())