1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
14 from mock import patch
15 from mock import mock_open
16 from argparse import ArgumentParser
18 from apex.build import ApexBuildException
19 from apex.build import build
20 from apex.build import build_cache
21 from apex.build import create_build_parser
22 from apex.build import get_cache_file
23 from apex.build import get_journal
24 from apex.build import main
25 from apex.build import prune_cache
26 from apex.build import unpack_cache
28 from nose.tools import (
33 a_mock_open = mock_open(read_data=None)
36 class TestBuild(unittest.TestCase):
39 """This method is run once for each class before any tests are run"""
40 cls.repo_name = 'test_repo'
41 cls.repo_url = 'https://gerrit.opnfv.org/gerrit/' + cls.repo_name
42 cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
43 cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
44 cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
45 cls.sys_argv_debug = ['deploy.py', '--debug']
48 def teardown_class(cls):
49 """This method is run once for each class _after_ all tests are run"""
52 """This method is run once before _each_ test method is executed"""
55 """This method is run once after _each_ test method is executed"""
57 def test_create_build_parser(self):
58 assert_is_instance(create_build_parser(), ArgumentParser)
60 @patch('apex.build.yaml')
61 @patch('apex.build.os.path')
62 @patch('builtins.open', a_mock_open, create=True)
63 def test_get_journal_exists(self, mock_os_path, mock_yaml):
65 mock_os_path.isfile.return_value = True
66 mock_yaml.safe_load.return_value = ['a', 'list']
68 assert_is_instance(get_journal('test_dir'), list)
70 mock_os_path.isfile.assert_called_with('test_dir/cache_journal.yaml')
71 mock_yaml.safe_load.assert_called_with(a_mock_open.return_value)
73 @patch('apex.build.os.path')
74 def test_get_journal_notexist(self, mock_os_path):
76 mock_os_path.isfile.return_value = False
78 assert_is_none(get_journal('test_dir'))
80 @patch('apex.build.os.path')
81 @patch('apex.build.get_journal')
82 def test_get_cache_file(self, mock_get_journal, mock_os_path):
83 mock_get_journal.return_value = ['journal_contents']
84 mock_os_path.isfile.return_value = True
85 get_cache_file('test_dir')
87 def test_unpack_cache_no_cache_dir(self):
88 unpack_cache('dest_dir', cache_dir=None)
90 @patch('apex.build.os.path')
91 def test_unpack_cache_not_isdir(self, mock_os_path):
92 mock_os_path.isdir.return_value = False
93 unpack_cache('dest_dir', cache_dir='cache_dir')
95 @patch('apex.build.get_cache_file')
96 @patch('apex.build.os.path')
97 def test_unpack_cache_cache_file_none(self, mock_os_path, mock_cache_file):
98 mock_os_path.isdir.return_value = True
99 mock_cache_file.return_value = None
100 unpack_cache('dest_dir', cache_dir='cache_dir')
102 @patch('apex.build.subprocess.check_call')
103 @patch('apex.build.get_cache_file')
104 @patch('apex.build.os.path')
105 @patch('apex.build.os')
106 def test_unpack_cache_tar_failure(self, mock_os, mock_os_path,
109 mock_os_path.isdir.return_value = True
110 mock_cache_file.return_value = 'cache_file'
111 mock_os_path.exists.return_value = False
112 mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
113 unpack_cache('dest_dir', cache_dir='cache_dir')
115 @patch('apex.build.subprocess')
116 @patch('apex.build.get_cache_file')
117 @patch('apex.build.os.path')
118 @patch('apex.build.os')
119 def test_unpack_cache_cache_dest_not_exist(self, mock_os, mock_os_path,
122 mock_os_path.isdir.return_value = True
123 mock_cache_file.return_value = 'cache_file'
124 mock_os_path.exists.return_value = False
125 mock_os.listdir.return_value = ['listdir', 'is', 'Mocked']
126 unpack_cache('dest_dir', cache_dir='cache_dir')
128 @patch('apex.build.subprocess')
129 def test_build(self, mock_subprocess):
130 build('build_root', None)
132 @patch('apex.build.subprocess.check_call')
133 def test_build_check_call_raises(self, mock_subprocess):
134 mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
135 assert_raises(subprocess.CalledProcessError, build, 'build_root', None)
137 @patch('apex.build.subprocess.check_output')
138 @patch('apex.build.subprocess.check_call')
139 def test_build_check_output_raises(self, mock_check_call, mock_subprocess):
140 mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
141 assert_raises(subprocess.CalledProcessError, build, 'build_root', None)
143 @patch('apex.build.subprocess')
144 def test_build_rpms(self, mock_subprocess):
145 build('build_root', 'v123', rpms=True)
147 @patch('apex.build.subprocess')
148 def test_build_iso(self, mock_subprocess):
149 build('build_root', 'v123', iso=True)
151 def test_build_cache_none(self):
152 build_cache('cache_source', None)
154 @patch('apex.build.get_journal')
155 @patch('apex.build.yaml')
156 @patch('apex.build.os')
157 @patch('apex.build.subprocess')
158 @patch('builtins.open', a_mock_open, create=True)
159 def test_build_cache(self, mock_subprocess, mock_os,
160 mock_yaml, mock_get_journal):
161 build_cache('cache_source', 'cache_dir')
162 mock_yaml.safe_dump.assert_called_with(mock_get_journal.return_value,
163 a_mock_open.return_value,
164 default_flow_style=False)
166 @patch('apex.build.get_journal')
167 @patch('apex.build.uuid')
168 @patch('apex.build.yaml')
169 @patch('apex.build.os')
170 @patch('apex.build.subprocess')
171 @patch('builtins.open', a_mock_open, create=True)
172 def test_build_cache_get_journal_none(self, mock_subprocess, mock_os,
173 mock_yaml, mock_uuid,
175 uuid = '73b18d27-8d25-4e02-a937-cb08609b6911'
176 mock_get_journal.return_value = None
177 mock_uuid.uuid4.return_value = uuid
178 build_cache('cache_source', 'cache_dir')
179 mock_yaml.safe_dump.assert_called_with(['apex-cache-{}.tgz'.format(
181 a_mock_open.return_value,
182 default_flow_style=False)
184 @patch('apex.build.get_journal')
185 @patch('apex.build.yaml')
186 @patch('apex.build.os.path')
187 @patch('apex.build.os')
188 @patch('apex.build.subprocess.check_call')
189 @patch('builtins.open', mock_open(read_data=None), create=True)
190 def test_build_cache_tar_fails(self, mock_subprocess, mock_os,
191 mock_os_path, mock_yaml, mock_get_journal):
192 mock_subprocess.side_effect = BaseException()
193 mock_os_path.isfile.return_value = True
194 assert_raises(BaseException, build_cache, 'cache_source', 'cache_dir')
196 @patch('apex.build.get_journal')
197 @patch('apex.build.yaml')
198 @patch('apex.build.os.path')
199 @patch('apex.build.os')
200 @patch('apex.build.subprocess.check_call')
201 @patch('builtins.open', mock_open(read_data=None), create=True)
202 def test_build_cache_cache_full_path_false(self, mock_subprocess, mock_os,
203 mock_os_path, mock_yaml,
205 mock_os_path.isfile.return_value = False
206 build_cache('cache_source', 'cache_dir')
207 mock_yaml.safe_dump.assert_not_called()
209 def test_prune_cache_none(self):
212 @patch('apex.build.get_journal')
213 def test_prune_cache_empty_journal(self, mock_get_journal):
214 mock_get_journal.return_value = []
215 prune_cache('not-none')
217 @patch('apex.build.get_journal')
218 @patch('apex.build.yaml')
219 @patch('apex.build.os')
220 @patch('builtins.open', mock_open(read_data=None), create=True)
221 def test_prune_cache_os_remove_error(self, mock_os, mock_yaml,
224 mock_get_journal.return_value = ['more', 'than', 'two']
226 rm.side_effect = OSError()
228 prune_cache('not-none')
230 @patch('apex.build.get_journal')
231 @patch('apex.build.yaml')
232 @patch('apex.build.os')
233 @patch('builtins.open', a_mock_open, create=True)
234 def test_prune_cache(self, mock_os, mock_yaml, mock_get_journal):
236 mock_get_journal.return_value = ['more', 'than', 'two']
238 prune_cache('not-none')
240 mock_yaml.safe_dump.assert_called_with(['than', 'two'],
241 a_mock_open.return_value,
242 default_flow_style=False)
244 @patch('apex.build.create_build_parser')
245 @patch('apex.build.subprocess.check_output')
246 @patch('apex.build.os.path')
247 @patch('apex.build.os')
248 @patch('apex.build.utils')
249 @patch('apex.build.unpack_cache')
250 @patch('apex.build.build_cache')
251 @patch('apex.build.prune_cache')
252 @patch('apex.build.build')
253 def test_main(self, mock_build, mock_prune_cache,
254 mock_build_cache, mock_unpack_cache,
255 mock_utils, mock_os, mock_os_path,
256 mock_subprocess, mock_parser):
258 mbc = mock_parser.return_value
259 args = mbc.parse_args.return_value
261 mock_os_path.isdir.return_value = True
267 @patch('apex.build.create_build_parser')
268 @patch('apex.build.subprocess.check_output')
269 @patch('apex.build.os.path')
270 @patch('apex.build.os')
271 @patch('apex.build.utils')
272 @patch('apex.build.unpack_cache')
273 @patch('apex.build.build_cache')
274 @patch('apex.build.prune_cache')
275 @patch('apex.build.build')
276 def test_main_throw_build_except(self, mock_build, mock_prune_cache,
277 mock_build_cache, mock_unpack_cache,
278 mock_utils, mock_os, mock_os_path,
279 mock_subprocess, mock_parser):
281 mbc = mock_parser.return_value
282 args = mbc.parse_args.return_value
284 mock_os_path.isdir.return_value = False
286 assert_raises(ApexBuildException, main)
288 @patch('apex.build.create_build_parser')
289 @patch('apex.build.subprocess.check_output')
290 @patch('apex.build.os.path')
291 @patch('apex.build.os')
292 @patch('apex.build.utils')
293 @patch('apex.build.unpack_cache')
294 @patch('apex.build.build_cache')
295 @patch('apex.build.prune_cache')
296 @patch('apex.build.build')
297 def test_main_throw_subprocess_except(self, mock_build, mock_prune_cache,
298 mock_build_cache, mock_unpack_cache,
299 mock_utils, mock_os, mock_os_path,
300 mock_subprocess, mock_parser):
302 mbc = mock_parser.return_value
303 args = mbc.parse_args.return_value
305 mock_os_path.isdir.return_value = True
306 mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
308 assert_raises(subprocess.CalledProcessError, main)