summaryrefslogtreecommitdiffstats
path: root/apex/tests/test_apex_build.py
blob: a55f790e23e01fad1517ae3ac6d0ac1e2b27924d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
##############################################################################
# Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import os
import subprocess
import unittest

from mock import patch
from mock import mock_open
from argparse import ArgumentParser

from apex.build import ApexBuildException
from apex.build import build
from apex.build import build_cache
from apex.build import create_build_parser
from apex.build import get_cache_file
from apex.build import get_journal
from apex.build import main
from apex.build import prune_cache
from apex.build import unpack_cache

from nose.tools import (
    assert_is_none,
    assert_raises,
    assert_is_instance)

a_mock_open = mock_open(read_data=None)


class TestBuild(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        """This method is run once for each class before any tests are run"""
        cls.repo_name = 'test_repo'
        cls.repo_url = 'https://gerrit.opnfv.org/gerrit/' + cls.repo_name
        cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
        cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
        cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
        cls.sys_argv_debug = ['deploy.py', '--debug']

    @classmethod
    def teardown_class(cls):
        """This method is run once for each class _after_ all tests are run"""

    def setup(self):
        """This method is run once before _each_ test method is executed"""

    def teardown(self):
        """This method is run once after _each_ test method is executed"""

    def test_create_build_parser(self):
        assert_is_instance(create_build_parser(), ArgumentParser)

    @patch('apex.build.yaml')
    @patch('apex.build.os.path')
    @patch('builtins.open', a_mock_open, create=True)
    def test_get_journal_exists(self, mock_os_path, mock_yaml):
        # setup mock
        mock_os_path.isfile.return_value = True
        mock_yaml.safe_load.return_value = ['a', 'list']
        # execute
        assert_is_instance(get_journal('test_dir'), list)
        # assert
        mock_os_path.isfile.assert_called_with('test_dir/cache_journal.yaml')
        mock_yaml.safe_load.assert_called_with(a_mock_open.return_value)

    @patch('apex.build.os.path')
    def test_get_journal_notexist(self, mock_os_path):
        # setup mock
        mock_os_path.isfile.return_value = False
        # execute
        assert_is_none(get_journal('test_dir'))

    @patch('apex.build.os.path')
    @patch('apex.build.get_journal')
    def test_get_cache_file(self, mock_get_journal, mock_os_path):
        mock_get_journal.return_value = ['journal_contents']
        mock_os_path.isfile.return_value = True
        get_cache_file('test_dir')

    def test_unpack_cache_no_cache_dir(self):
        unpack_cache('dest_dir', cache_dir=None)

    @patch('apex.build.os.path')
    def test_unpack_cache_not_isdir(self, mock_os_path):
        mock_os_path.isdir.return_value = False
        unpack_cache('dest_dir', cache_dir='cache_dir')

    @patch('apex.build.get_cache_file')
    @patch('apex.build.os.path')
    def test_unpack_cache_cache_file_none(self, mock_os_path, mock_cache_file):
        mock_os_path.isdir.return_value = True
        mock_cache_file.return_value = None
        unpack_cache('dest_dir', cache_dir='cache_dir')

    @patch('apex.build.subprocess.check_call')
    @patch('apex.build.get_cache_file')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    def test_unpack_cache_tar_failure(self, mock_os, mock_os_path,
                                      mock_cache_file,
                                      mock_subprocess):
        mock_os_path.isdir.return_value = True
        mock_cache_file.return_value = 'cache_file'
        mock_os_path.exists.return_value = False
        mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
        unpack_cache('dest_dir', cache_dir='cache_dir')

    @patch('apex.build.subprocess')
    @patch('apex.build.get_cache_file')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    def test_unpack_cache_cache_dest_not_exist(self, mock_os, mock_os_path,
                                               mock_cache_file,
                                               mock_subprocess):
        mock_os_path.isdir.return_value = True
        mock_cache_file.return_value = 'cache_file'
        mock_os_path.exists.return_value = False
        mock_os.listdir.return_value = ['listdir', 'is', 'Mocked']
        unpack_cache('dest_dir', cache_dir='cache_dir')

    @patch('apex.build.subprocess')
    def test_build(self, mock_subprocess):
        build('build_root', None)

    @patch('apex.build.subprocess.check_call')
    def test_build_check_call_raises(self, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
        assert_raises(subprocess.CalledProcessError, build, 'build_root', None)

    @patch('apex.build.subprocess.check_output')
    @patch('apex.build.subprocess.check_call')
    def test_build_check_output_raises(self, mock_check_call, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
        assert_raises(subprocess.CalledProcessError, build, 'build_root', None)

    @patch('apex.build.subprocess')
    def test_build_rpms(self, mock_subprocess):
        build('build_root', 'v123', rpms=True)

    @patch('apex.build.subprocess')
    def test_build_iso(self, mock_subprocess):
        build('build_root', 'v123', iso=True)

    def test_build_cache_none(self):
        build_cache('cache_source', None)

    @patch('apex.build.get_journal')
    @patch('apex.build.yaml')
    @patch('apex.build.os')
    @patch('apex.build.subprocess')
    @patch('builtins.open', a_mock_open, create=True)
    def test_build_cache(self, mock_subprocess, mock_os,
                         mock_yaml, mock_get_journal):
        build_cache('cache_source', 'cache_dir')
        mock_yaml.safe_dump.assert_called_with(mock_get_journal.return_value,
                                               a_mock_open.return_value,
                                               default_flow_style=False)

    @patch('apex.build.get_journal')
    @patch('apex.build.uuid')
    @patch('apex.build.yaml')
    @patch('apex.build.os')
    @patch('apex.build.subprocess')
    @patch('builtins.open', a_mock_open, create=True)
    def test_build_cache_get_journal_none(self, mock_subprocess, mock_os,
                                          mock_yaml, mock_uuid,
                                          mock_get_journal):
        uuid = '73b18d27-8d25-4e02-a937-cb08609b6911'
        mock_get_journal.return_value = None
        mock_uuid.uuid4.return_value = uuid
        build_cache('cache_source', 'cache_dir')
        mock_yaml.safe_dump.assert_called_with(['apex-cache-{}.tgz'.format(
                                                uuid)],
                                               a_mock_open.return_value,
                                               default_flow_style=False)

    @patch('apex.build.get_journal')
    @patch('apex.build.yaml')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    @patch('apex.build.subprocess.check_call')
    @patch('builtins.open', mock_open(read_data=None), create=True)
    def test_build_cache_tar_fails(self, mock_subprocess, mock_os,
                                   mock_os_path, mock_yaml, mock_get_journal):
        mock_subprocess.side_effect = BaseException()
        mock_os_path.isfile.return_value = True
        assert_raises(BaseException, build_cache, 'cache_source', 'cache_dir')

    @patch('apex.build.get_journal')
    @patch('apex.build.yaml')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    @patch('apex.build.subprocess.check_call')
    @patch('builtins.open', mock_open(read_data=None), create=True)
    def test_build_cache_cache_full_path_false(self, mock_subprocess, mock_os,
                                               mock_os_path, mock_yaml,
                                               mock_get_journal):
        mock_os_path.isfile.return_value = False
        build_cache('cache_source', 'cache_dir')
        mock_yaml.safe_dump.assert_not_called()

    def test_prune_cache_none(self):
        prune_cache(None)

    @patch('apex.build.get_journal')
    def test_prune_cache_empty_journal(self, mock_get_journal):
        mock_get_journal.return_value = []
        prune_cache('not-none')

    @patch('apex.build.get_journal')
    @patch('apex.build.yaml')
    @patch('apex.build.os')
    @patch('builtins.open', mock_open(read_data=None), create=True)
    def test_prune_cache_os_remove_error(self, mock_os, mock_yaml,
                                         mock_get_journal):
        # setup Mock
        mock_get_journal.return_value = ['more', 'than', 'two']
        rm = mock_os.remove
        rm.side_effect = OSError()
        # execute
        prune_cache('not-none')

    @patch('apex.build.get_journal')
    @patch('apex.build.yaml')
    @patch('apex.build.os')
    @patch('builtins.open', a_mock_open, create=True)
    def test_prune_cache(self, mock_os, mock_yaml, mock_get_journal):
        # setup Mock
        mock_get_journal.return_value = ['more', 'than', 'two']
        # execute
        prune_cache('not-none')
        # assert
        mock_yaml.safe_dump.assert_called_with(['than', 'two'],
                                               a_mock_open.return_value,
                                               default_flow_style=False)

    @patch('apex.build.create_build_parser')
    @patch('apex.build.subprocess.check_output')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    @patch('apex.build.utils')
    @patch('apex.build.unpack_cache')
    @patch('apex.build.build_cache')
    @patch('apex.build.prune_cache')
    @patch('apex.build.build')
    def test_main(self, mock_build, mock_prune_cache,
                  mock_build_cache, mock_unpack_cache,
                  mock_utils, mock_os, mock_os_path,
                  mock_subprocess, mock_parser):
        # setup mock
        mbc = mock_parser.return_value
        args = mbc.parse_args.return_value
        args.debug = False
        mock_os_path.isdir.return_value = True
        # execute
        main()
        # assert
        # TODO

    @patch('apex.build.create_build_parser')
    @patch('apex.build.subprocess.check_output')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    @patch('apex.build.utils')
    @patch('apex.build.unpack_cache')
    @patch('apex.build.build_cache')
    @patch('apex.build.prune_cache')
    @patch('apex.build.build')
    def test_main_throw_build_except(self, mock_build, mock_prune_cache,
                                     mock_build_cache, mock_unpack_cache,
                                     mock_utils, mock_os, mock_os_path,
                                     mock_subprocess, mock_parser):
        # setup mock
        mbc = mock_parser.return_value
        args = mbc.parse_args.return_value
        args.debug = True
        mock_os_path.isdir.return_value = False
        # execute and assert
        assert_raises(ApexBuildException, main)

    @patch('apex.build.create_build_parser')
    @patch('apex.build.subprocess.check_output')
    @patch('apex.build.os.path')
    @patch('apex.build.os')
    @patch('apex.build.utils')
    @patch('apex.build.unpack_cache')
    @patch('apex.build.build_cache')
    @patch('apex.build.prune_cache')
    @patch('apex.build.build')
    def test_main_throw_subprocess_except(self, mock_build, mock_prune_cache,
                                          mock_build_cache, mock_unpack_cache,
                                          mock_utils, mock_os, mock_os_path,
                                          mock_subprocess, mock_parser):
        # setup mock
        mbc = mock_parser.return_value
        args = mbc.parse_args.return_value
        args.debug = False
        mock_os_path.isdir.return_value = True
        mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
        # execute and assert
        assert_raises(subprocess.CalledProcessError, main)