~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to tests/test_utility.py

  • Committer: Curtis Hovey
  • Date: 2015-12-20 15:14:05 UTC
  • Revision ID: curtis@canonical.com-20151220151405-pm3dauunjr2978gz
skip any client-server that starts with 1.26.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import os
13
13
import socket
14
14
from time import time
15
 
import warnings
16
15
 
17
16
from mock import (
18
17
    call,
19
18
    patch,
20
19
    )
21
20
 
22
 
from tests import (
23
 
    TestCase,
24
 
)
 
21
from tests import TestCase
25
22
from utility import (
26
23
    add_basic_testing_arguments,
27
24
    as_literal_address,
 
25
    ErrJujuPath,
28
26
    extract_deb,
29
 
    _find_candidates,
30
27
    find_candidates,
31
28
    find_latest_branch_candidates,
32
29
    get_auth_token,
37
34
    quote,
38
35
    run_command,
39
36
    scoped_environ,
40
 
    split_address_port,
41
37
    temp_dir,
42
38
    until_timeout,
43
39
    wait_for_port,
108
104
 
109
105
class TestFindCandidates(TestCase):
110
106
 
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'))])
118
 
 
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'))])
126
 
 
127
107
    def test_find_candidates(self):
128
108
        with temp_dir() as root:
129
109
            master_path = make_candidate_dir(root, 'master')
175
155
 
176
156
    def test_find_latest_branch_candidates(self):
177
157
        with temp_dir() as root:
178
 
            master_path = make_candidate_dir(root, 'master-artifacts')
 
158
            master_path = make_candidate_dir(root, 'master')
179
159
            self.assertEqual(find_latest_branch_candidates(root),
180
 
                             [(master_path, 1234)])
 
160
                             [master_path])
181
161
 
182
162
    def test_find_latest_branch_candidates_old_buildvars(self):
183
163
        with temp_dir() as root:
184
164
            a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds()
185
 
            make_candidate_dir(root, 'master-artifacts', modified=a_week_ago)
 
165
            make_candidate_dir(root, 'master', modified=a_week_ago)
186
166
            self.assertEqual(find_latest_branch_candidates(root), [])
187
167
 
188
168
    def test_ignore_older_revision_build(self):
189
169
        with temp_dir() as root:
190
170
            path_1234 = make_candidate_dir(
191
 
                root, '1234-artifacts', 'mybranch', '1234')
 
171
                root, '1234', 'mybranch', '1234')
192
172
            make_candidate_dir(root, '1233', 'mybranch', '1233')
193
 
            self.assertEqual(find_latest_branch_candidates(root), [
194
 
                (path_1234, 1234)])
 
173
            self.assertEqual(find_latest_branch_candidates(root), [path_1234])
195
174
 
196
175
    def test_include_older_revision_build_different_branch(self):
197
176
        with temp_dir() as root:
198
177
            path_1234 = make_candidate_dir(
199
 
                root, '1234-artifacts', 'branch_foo', '1234')
 
178
                root, '1234', 'branch_foo', '1234')
200
179
            path_1233 = make_candidate_dir(
201
 
                root, '1233-artifacts', 'branch_bar', '1233')
 
180
                root, '1233', 'branch_bar', '1233')
202
181
            self.assertItemsEqual(
203
 
                find_latest_branch_candidates(root), [
204
 
                    (path_1233, 1233), (path_1234, 1234)])
 
182
                find_latest_branch_candidates(root), [path_1233, path_1234])
205
183
 
206
184
 
207
185
class TestAsLiteralAddress(TestCase):
235
213
        self.assertIs(False, result)
236
214
 
237
215
 
238
 
class TestSplitAddressPort(TestCase):
239
 
 
240
 
    def test_hostname(self):
241
 
        self.assertEqual(
242
 
            ("name.testing", None), split_address_port("name.testing"))
243
 
 
244
 
    def test_ipv4(self):
245
 
        self.assertEqual(
246
 
            ("127.0.0.2", "17017"), split_address_port("127.0.0.2:17017"))
247
 
 
248
 
    def test_ipv6(self):
249
 
        self.assertEqual(
250
 
            ("2001:db8::7", "17017"), split_address_port("2001:db8::7:17017"))
251
 
 
252
 
 
253
216
class TestWaitForPort(TestCase):
254
217
 
255
218
    def test_wait_for_port_0000_closed(self):
384
347
        args = parser.parse_args(cmd_line)
385
348
        self.assertEqual(args.juju_bin, 'c:\\juju.exe')
386
349
 
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())
 
350
    def test_positional_args_raises_ErrJujuPath(self):
 
351
        cmd_line = ['local', '/foo', '/tmp/logs', 'testtest']
 
352
        parser = add_basic_testing_arguments(ArgumentParser())
 
353
        with self.assertRaises(ErrJujuPath):
 
354
            parser.parse_args(cmd_line)
399
355
 
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())
 
356
    def test_positional_args_juju_bin_empty(self):
 
357
        cmd_line = ['local', '', '/tmp/logs', 'testtest']
 
358
        parser = add_basic_testing_arguments(ArgumentParser())
 
359
        with self.assertRaises(ErrJujuPath):
 
360
            parser.parse_args(cmd_line)
410
361
 
411
362
    def test_debug(self):
412
363
        cmd_line = ['local', '/foo/juju', '/tmp/logs', 'testtest', '--debug']