Merge lp:~gz/juju-ci-tools/tests_clean_delta into lp:juju-ci-tools
- tests_clean_delta
- Merge into trunk
Proposed by
Martin Packman
Status: | Merged |
---|---|
Merged at revision: | 1181 |
Proposed branch: | lp:~gz/juju-ci-tools/tests_clean_delta |
Merge into: | lp:juju-ci-tools |
Diff against target: |
390 lines (+121/-90) 3 files modified
jujuci.py (+8/-11) tests/test_jujuci.py (+105/-71) tests/test_quickstart_deploy.py (+8/-8) |
To merge this branch: | bzr merge lp:~gz/juju-ci-tools/tests_clean_delta |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Curtis Hovey (community) | code | Approve | |
Review via email: mp+278346@code.launchpad.net |
Commit message
Description of the change
Fix test failure and tidy up some tests
Missed a test issue due to masking by a different failure on my armhf machine. Move two more test files to common framework to clean up.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'jujuci.py' |
2 | --- jujuci.py 2015-11-05 18:35:54 +0000 |
3 | +++ jujuci.py 2015-11-23 17:00:00 +0000 |
4 | @@ -31,6 +31,7 @@ |
5 | extract_deb, |
6 | get_deb_arch, |
7 | get_revision_build, |
8 | + print_now, |
9 | ) |
10 | |
11 | |
12 | @@ -45,10 +46,6 @@ |
13 | Artifact = namedtuple('Artifact', ['file_name', 'location']) |
14 | |
15 | |
16 | -def print_now(string): |
17 | - print(string) |
18 | - |
19 | - |
20 | Credentials = namedtuple('Credentials', ['user', 'password']) |
21 | |
22 | |
23 | @@ -458,7 +455,7 @@ |
24 | try: |
25 | args, credentials = parse_args(argv) |
26 | except CredentialsMissing as e: |
27 | - print(e) |
28 | + print_now(e) |
29 | sys.exit(2) |
30 | try: |
31 | if args.command == 'list': |
32 | @@ -475,11 +472,11 @@ |
33 | args.path, env=args.clean_env, |
34 | dry_run=args.dry_run, verbose=args.verbose) |
35 | elif args.command == 'get-juju-bin': |
36 | - print(get_juju_bin(credentials, args.workspace)) |
37 | + print_now(get_juju_bin(credentials, args.workspace)) |
38 | elif args.command == 'get-certification-bin': |
39 | path = get_certification_bin(credentials, args.version, |
40 | args.workspace) |
41 | - print(path) |
42 | + print_now(path) |
43 | elif args.command == 'get-build-vars': |
44 | text = get_buildvars( |
45 | credentials, args.build, env=args.env, |
46 | @@ -487,17 +484,17 @@ |
47 | version=args.version, short_branch=args.short_branch, |
48 | short_revision=args.short_revision, |
49 | branch=args.branch, revision=args.revision) |
50 | - print(text) |
51 | + print_now(text) |
52 | elif args.command == 'get-package-name': |
53 | - print(PackageNamer.factory().get_release_package(args.version)) |
54 | + print_now(PackageNamer.factory().get_release_package(args.version)) |
55 | |
56 | except Exception as e: |
57 | - print(e) |
58 | + print_now(e) |
59 | if args.verbose: |
60 | traceback.print_tb(sys.exc_info()[2]) |
61 | return 2 |
62 | if args.verbose: |
63 | - print("Done.") |
64 | + print_now("Done.") |
65 | return 0 |
66 | |
67 | |
68 | |
69 | === modified file 'tests/test_jujuci.py' |
70 | --- tests/test_jujuci.py 2015-11-04 20:42:06 +0000 |
71 | +++ tests/test_jujuci.py 2015-11-23 17:00:00 +0000 |
72 | @@ -3,7 +3,6 @@ |
73 | import json |
74 | import os |
75 | from StringIO import StringIO |
76 | -from unittest import TestCase |
77 | import urllib2 |
78 | |
79 | from mock import patch |
80 | @@ -38,7 +37,11 @@ |
81 | ) |
82 | import jujupy |
83 | from utility import temp_dir |
84 | -from tests import parse_error |
85 | +from tests import ( |
86 | + FakeHomeTestCase, |
87 | + parse_error, |
88 | + TestCase, |
89 | +) |
90 | |
91 | |
92 | def make_build_data(number='lastSuccessfulBuild'): |
93 | @@ -196,7 +199,7 @@ |
94 | } |
95 | |
96 | |
97 | -class JujuCITestCase(TestCase): |
98 | +class JujuCITestCase(FakeHomeTestCase): |
99 | |
100 | def test_get_credentials(self): |
101 | self.assertEqual( |
102 | @@ -217,43 +220,55 @@ |
103 | get_credentials(Namespace(user='jrandom', password=None)) |
104 | |
105 | def test_main_list_options(self): |
106 | - with patch('jujuci.list_artifacts') as mock: |
107 | - main(['-d', '-v', 'list', '-b', '1234', 'foo', '*.tar.gz', |
108 | - '--user', 'jrandom', '--password', '1password']) |
109 | - args, kwargs = mock.call_args |
110 | - self.assertEqual((Credentials('jrandom', '1password'), 'foo', |
111 | - '1234', '*.tar.gz'), args) |
112 | - self.assertTrue(kwargs['verbose']) |
113 | + print_list = [] |
114 | + with patch('jujuci.print_now', side_effect=print_list.append): |
115 | + with patch('jujuci.list_artifacts') as mock: |
116 | + main(['-d', '-v', 'list', '-b', '1234', 'foo', '*.tar.gz', |
117 | + '--user', 'jrandom', '--password', '1password']) |
118 | + args, kwargs = mock.call_args |
119 | + self.assertEqual((Credentials('jrandom', '1password'), 'foo', |
120 | + '1234', '*.tar.gz'), args) |
121 | + self.assertTrue(kwargs['verbose']) |
122 | + self.assertEqual(print_list, ['Done.']) |
123 | |
124 | def test_main_get_options(self): |
125 | - with patch('jujuci.get_artifacts') as mock: |
126 | - main(['-d', '-v', |
127 | - 'get', '-a', '-b', '1234', 'foo', '*.tar.gz', 'bar', |
128 | - '--user', 'jrandom', '--password', '1password']) |
129 | - args, kwargs = mock.call_args |
130 | - self.assertEqual((Credentials('jrandom', '1password'), 'foo', |
131 | - '1234', '*.tar.gz', 'bar'), args) |
132 | - self.assertTrue(kwargs['archive']) |
133 | - self.assertTrue(kwargs['verbose']) |
134 | - self.assertTrue(kwargs['dry_run']) |
135 | + print_list = [] |
136 | + with patch('jujuci.print_now', side_effect=print_list.append): |
137 | + with patch('jujuci.get_artifacts') as mock: |
138 | + main(['-d', '-v', |
139 | + 'get', '-a', '-b', '1234', 'foo', '*.tar.gz', 'bar', |
140 | + '--user', 'jrandom', '--password', '1password']) |
141 | + args, kwargs = mock.call_args |
142 | + self.assertEqual((Credentials('jrandom', '1password'), 'foo', |
143 | + '1234', '*.tar.gz', 'bar'), args) |
144 | + self.assertTrue(kwargs['archive']) |
145 | + self.assertTrue(kwargs['verbose']) |
146 | + self.assertTrue(kwargs['dry_run']) |
147 | + self.assertEqual(print_list, ['Done.']) |
148 | |
149 | def test_main_setup_workspace_options(self): |
150 | - with patch('jujuci.setup_workspace', autospec=True) as mock: |
151 | - main( |
152 | - ['-d', '-v', 'setup-workspace', '--clean-env', 'bar', './foo']) |
153 | - args, kwargs = mock.call_args |
154 | - self.assertEqual(('./foo', ), args) |
155 | - self.assertEqual('bar', kwargs['env']) |
156 | - self.assertTrue(kwargs['dry_run']) |
157 | - self.assertTrue(kwargs['verbose']) |
158 | + print_list = [] |
159 | + with patch('jujuci.print_now', side_effect=print_list.append): |
160 | + with patch('jujuci.setup_workspace', autospec=True) as mock: |
161 | + main(['-d', '-v', 'setup-workspace', '--clean-env', 'bar', |
162 | + './foo']) |
163 | + args, kwargs = mock.call_args |
164 | + self.assertEqual(('./foo', ), args) |
165 | + self.assertEqual('bar', kwargs['env']) |
166 | + self.assertTrue(kwargs['dry_run']) |
167 | + self.assertTrue(kwargs['verbose']) |
168 | + self.assertEqual(print_list, ['Done.']) |
169 | |
170 | def test_main_get_buildvars(self): |
171 | - with patch('jujuci.get_buildvars', autospec=True) as mock: |
172 | - main( |
173 | - ['get-build-vars', '--env', 'foo', '--summary', |
174 | - '--revision-build', '--version', '--short-branch', |
175 | - '--short-revision', '--branch', '--revision', '123', |
176 | - '--user', 'jrandom', '--password', '1password']) |
177 | + print_list = [] |
178 | + with patch('jujuci.print_now', side_effect=print_list.append): |
179 | + with patch('jujuci.get_buildvars', autospec=True, |
180 | + return_value='sample buildvars') as mock: |
181 | + main( |
182 | + ['get-build-vars', '--env', 'foo', '--summary', |
183 | + '--revision-build', '--version', '--short-branch', |
184 | + '--short-revision', '--branch', '--revision', '123', |
185 | + '--user', 'jrandom', '--password', '1password']) |
186 | args, kwargs = mock.call_args |
187 | self.assertEqual((Credentials('jrandom', '1password'), '123'), args) |
188 | self.assertEqual('foo', kwargs['env']) |
189 | @@ -264,6 +279,7 @@ |
190 | self.assertTrue(kwargs['short_branch']) |
191 | self.assertTrue(kwargs['branch']) |
192 | self.assertTrue(kwargs['revision']) |
193 | + self.assertEqual(print_list, ['sample buildvars']) |
194 | |
195 | def test_parse_arg_buildvars_common_options(self): |
196 | args, credentials = parse_args( |
197 | @@ -529,9 +545,10 @@ |
198 | |
199 | def test_get_artifacts(self): |
200 | build_data = make_build_data(1234) |
201 | - with patch('jujuci.get_build_data', return_value=build_data): |
202 | - with patch('urllib.urlretrieve') as uo_mock: |
203 | - with patch('jujuci.print_now') as pn_mock: |
204 | + print_list = [] |
205 | + with patch('jujuci.print_now', side_effect=print_list.append): |
206 | + with patch('jujuci.get_build_data', return_value=build_data): |
207 | + with patch('urllib.urlretrieve') as uo_mock: |
208 | found = get_artifacts( |
209 | Credentials('jrandom', '1password'), 'foo', '1234', |
210 | '*.bash', './', verbose=True) |
211 | @@ -546,37 +563,41 @@ |
212 | auth_location = location.replace('http://', |
213 | 'http://jrandom:1password@') |
214 | self.assertEqual((auth_location, local_path), args) |
215 | - messages = sorted(call[1][0] for call in pn_mock.mock_calls) |
216 | - self.assertEqual(1, len(messages)) |
217 | - message = messages[0] |
218 | self.assertEqual( |
219 | - 'Retrieving %s => %s' % (location, local_path), |
220 | - message) |
221 | + print_list, |
222 | + ['Retrieving %s => %s' % (location, local_path)] |
223 | + ) |
224 | |
225 | def test_get_artifacts_with_dry_run(self): |
226 | build_data = make_build_data(1234) |
227 | - with patch('jujuci.get_build_data', return_value=build_data): |
228 | - with patch('urllib.urlretrieve') as uo_mock: |
229 | - get_artifacts( |
230 | - Credentials('jrandom', '1password'), 'foo', '1234', |
231 | - '*.bash', './', dry_run=True) |
232 | + print_list = [] |
233 | + with patch('jujuci.print_now', side_effect=print_list.append): |
234 | + with patch('jujuci.get_build_data', return_value=build_data): |
235 | + with patch('urllib.urlretrieve') as uo_mock: |
236 | + get_artifacts( |
237 | + Credentials('jrandom', '1password'), 'foo', '1234', |
238 | + '*.bash', './', dry_run=True) |
239 | self.assertEqual(0, uo_mock.call_count) |
240 | + self.assertEqual(print_list, ['buildvars.bash']) |
241 | |
242 | def test_get_artifacts_with_archive(self): |
243 | build_data = make_build_data(1234) |
244 | - with patch('jujuci.get_build_data', return_value=build_data): |
245 | - with patch('urllib.urlretrieve'): |
246 | - with temp_dir() as base_dir: |
247 | - path = os.path.join(base_dir, 'foo') |
248 | - os.mkdir(path) |
249 | - old_file_path = os.path.join(path, 'old_file.txt') |
250 | - with open(old_file_path, 'w') as old_file: |
251 | - old_file.write('old') |
252 | - get_artifacts( |
253 | - Credentials('jrandom', '1password'), 'foo', '1234', |
254 | - '*.bash', path, archive=True) |
255 | - self.assertFalse(os.path.isfile(old_file_path)) |
256 | - self.assertTrue(os.path.isdir(path)) |
257 | + print_list = [] |
258 | + with temp_dir() as base_dir: |
259 | + path = os.path.join(base_dir, 'foo') |
260 | + os.mkdir(path) |
261 | + old_file_path = os.path.join(path, 'old_file.txt') |
262 | + with open(old_file_path, 'w') as old_file: |
263 | + old_file.write('old') |
264 | + with patch('jujuci.print_now', side_effect=print_list.append): |
265 | + with patch('jujuci.get_build_data', return_value=build_data): |
266 | + with patch('urllib.urlretrieve'): |
267 | + get_artifacts( |
268 | + Credentials('jrandom', '1password'), 'foo', '1234', |
269 | + '*.bash', path, archive=True) |
270 | + self.assertFalse(os.path.isfile(old_file_path)) |
271 | + self.assertTrue(os.path.isdir(path)) |
272 | + self.assertEqual(print_list, ['buildvars.bash']) |
273 | |
274 | def test_get_artifacts_with_archive_error(self): |
275 | build_data = make_build_data(1234) |
276 | @@ -594,36 +615,49 @@ |
277 | os.makedirs(foo_dir) |
278 | with open(os.path.join(workspace_dir, 'old.txt'), 'w') as of: |
279 | of.write('old') |
280 | - setup_workspace(workspace_dir, dry_run=False, verbose=False) |
281 | + print_list = [] |
282 | + with patch('jujuci.print_now', side_effect=print_list.append): |
283 | + setup_workspace(workspace_dir, dry_run=False, verbose=False) |
284 | self.assertEqual(['artifacts'], os.listdir(workspace_dir)) |
285 | artifacts_dir = os.path.join(workspace_dir, 'artifacts') |
286 | self.assertEqual(['empty'], os.listdir(artifacts_dir)) |
287 | + self.assertEqual( |
288 | + print_list, |
289 | + ['Removing old.txt', 'Removing foo', 'Creating artifacts dir.'] |
290 | + ) |
291 | |
292 | def test_setup_workspace_with_env(self): |
293 | with temp_dir() as base_dir: |
294 | workspace_dir = os.path.join(base_dir, 'workspace') |
295 | os.makedirs(workspace_dir) |
296 | - with patch('jujuci.clean_environment', autospec=True) as mock: |
297 | - setup_workspace( |
298 | - workspace_dir, env='foo', dry_run=False, verbose=False) |
299 | + print_list = [] |
300 | + with patch('jujuci.print_now', side_effect=print_list.append): |
301 | + with patch('jujuci.clean_environment', autospec=True) as mock: |
302 | + setup_workspace( |
303 | + workspace_dir, env='foo', dry_run=False, verbose=False) |
304 | mock.assert_called_once_with('foo', verbose=False) |
305 | + self.assertEqual(print_list, ['Creating artifacts dir.']) |
306 | |
307 | def test_clean_environment_not_dirty(self): |
308 | config = {'environments': {'local': {'type': 'local'}}} |
309 | with jujupy._temp_env(config, set_home=True): |
310 | - with patch('jujuci.destroy_environment') as mock: |
311 | - dirty = clean_environment('foo', verbose=False) |
312 | + with patch('jujuci.destroy_environment', autospec=True) as mock_de: |
313 | + with patch('jujupy.EnvJujuClient.by_version', side_effect=( |
314 | + lambda env: jujupy.EnvJujuClient(env, '1', None))): |
315 | + dirty = clean_environment('foo', verbose=False) |
316 | self.assertFalse(dirty) |
317 | - self.assertEqual(0, mock.call_count) |
318 | + self.assertEqual(0, mock_de.call_count) |
319 | |
320 | def test_clean_environment_dirty(self): |
321 | config = {'environments': {'foo': {'type': 'local'}}} |
322 | with jujupy._temp_env(config, set_home=True): |
323 | - with patch('jujuci.destroy_environment', autospec=True) as mock: |
324 | - dirty = clean_environment('foo', verbose=False) |
325 | + with patch('jujuci.destroy_environment', autospec=True) as mock_de: |
326 | + with patch('jujupy.EnvJujuClient.by_version', side_effect=( |
327 | + lambda env: jujupy.EnvJujuClient(env, '1', None))): |
328 | + dirty = clean_environment('foo', verbose=False) |
329 | self.assertTrue(dirty) |
330 | - self.assertEqual(1, mock.call_count) |
331 | - args, kwargs = mock.call_args |
332 | + self.assertEqual(1, mock_de.call_count) |
333 | + args, kwargs = mock_de.call_args |
334 | self.assertIsInstance(args[0], jujupy.EnvJujuClient) |
335 | self.assertEqual('foo', args[1]) |
336 | |
337 | |
338 | === modified file 'tests/test_quickstart_deploy.py' |
339 | --- tests/test_quickstart_deploy.py 2015-10-22 19:51:15 +0000 |
340 | +++ tests/test_quickstart_deploy.py 2015-11-23 17:00:00 +0000 |
341 | @@ -3,16 +3,16 @@ |
342 | ANY, |
343 | patch |
344 | ) |
345 | -from unittest import TestCase |
346 | |
347 | from jujupy import ( |
348 | EnvJujuClient, |
349 | SimpleEnvironment, |
350 | ) |
351 | from quickstart_deploy import QuickstartTest |
352 | - |
353 | - |
354 | -class TestQuickstartTest(TestCase): |
355 | +from tests import FakeHomeTestCase |
356 | + |
357 | + |
358 | +class TestQuickstartTest(FakeHomeTestCase): |
359 | |
360 | @contextmanager |
361 | def from_args_cxt(self): |
362 | @@ -115,8 +115,7 @@ |
363 | ps_mock.assert_called_once_with(client) |
364 | dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs') |
365 | |
366 | - @patch('sys.stderr') |
367 | - def test_run_exception(self, se_mock): |
368 | + def test_run_exception(self): |
369 | def fake_iter_steps(): |
370 | yield {'bootstrap_host': 'foo'} |
371 | raise Exception() |
372 | @@ -141,7 +140,8 @@ |
373 | steps = quickstart.iter_steps() |
374 | with patch.object(client, 'quickstart') as qs_mock: |
375 | # Test first yield |
376 | - step = steps.next() |
377 | + with patch('jujupy.check_free_disk_space', autospec=True): |
378 | + step = steps.next() |
379 | qs_mock.assert_called_once_with('/tmp/bundle.yaml') |
380 | expected = {'juju-quickstart': 'Returned from quickstart'} |
381 | self.assertEqual(expected, step) |
382 | @@ -149,7 +149,7 @@ |
383 | return_value='mocked_name') as dns_mock: |
384 | # Test second yield |
385 | step = steps.next() |
386 | - dns_mock.assert_called_once_with(client, 0) |
387 | + dns_mock.assert_called_once_with(client, '0') |
388 | self.assertEqual('mocked_name', step['bootstrap_host']) |
389 | with patch.object(client, 'wait_for_deploy_started') as wds_mock: |
390 | # Test third yield |
Thank you.