summaryrefslogtreecommitdiff
path: root/morphlib/sourceresolver_tests.py
blob: 298dae5d1fce00558b7f825a667c8499ca9c344b (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Copyright (C) 2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import shutil
import tempfile
import unittest

import morphlib
from morphlib.sourceresolver import (SourceResolver,
                                     PickleCacheManager,
                                     MorphologyNotFoundError)
from morphlib.remoterepocache import CatFileError, LsTreeError


class FakeRemoteRepoCache(object):

    def cat_file(self, reponame, sha1, filename):
        if filename.endswith('.morph'):
            return '''{
                 "name": "%s",
                 "kind": "chunk",
                 "build-system": "dummy"
             }''' % filename[:-len('.morph')]
        return 'text'

    def ls_tree(self, reponame, sha1):
        return []


class FakeLocalRepo(object):

    morphologies = {
        'chunk.morph': '''
                name: chunk
                kind: chunk
                build-system: dummy
            ''',
        'chunk-split.morph': '''
                name: chunk-split
                kind: chunk
                build-system: dummy
                products:
                    - artifact: chunk-split-runtime
                      include: []
                    - artifact: chunk-split-devel
                      include: []
            ''',
        'stratum.morph': '''
                name: stratum
                kind: stratum
                chunks:
                    - name: chunk
                      repo: test:repo
                      ref: sha1
                      build-mode: bootstrap
                      build-depends: []
            ''',
        'stratum-no-bdeps-no-bootstrap.morph': '''
                name: stratum-no-bdeps-no-bootstrap
                kind: stratum
                chunks:
                    - name: chunk
                      repo: test:repo
                      ref: sha1
                      build-depends: []
            ''',
        'stratum-bdeps-no-bootstrap.morph': '''
                name: stratum-bdeps-no-bootstrap
                kind: stratum
                build-depends:
                    - morph: stratum
                chunks:
                    - name: chunk
                      repo: test:repo
                      ref: sha1
                      build-depends: []
            ''',
        'stratum-empty.morph': '''
                name: stratum-empty
                kind: stratum
            ''',
        'system.morph': '''
                name: system
                kind: system
                arch: %(arch)s
                strata:
                    - morph: stratum
            ''',
        'parse-error.morph': ''' name''',
        'name-mismatch.morph': '''
                name: fred
                kind: stratum
            ''',
    }

    def __init__(self):
        self.arch = 'x86_64'

    def read_file(self, filename, ref):
        if filename in self.morphologies:
            values = {
                'arch': self.arch,
            }
            return self.morphologies[filename] % values
        elif filename.endswith('.morph'):
            return '''name: %s
                kind: chunk
                build-system: dummy''' % filename[:-len('.morph')]
        return 'text'

    def list_files(self, ref, recurse):
        return self.morphologies.keys()

    def update(self):
        pass


class FakeLocalRepoCache(object):

    def __init__(self, lr):
        self.lr = lr

    def has_repo(self, reponame):
        return True

    def get_repo(self, reponame):
        return self.lr

    def cache_repo(self, reponame):
        return self.lr


class SourceResolverTests(unittest.TestCase):

    def setUp(self):
        # create temp "definitions" repo
        # set self.sr._definitions_repo to that
        # trick it into presenting temp repo using FakeLocalRepoCache
        # magic
        self.lr = FakeLocalRepo()
        self.lrc = FakeLocalRepoCache(self.lr)
        self.rrc = FakeRemoteRepoCache()

        self.cachedir = tempfile.mkdtemp()
        buildsystem_cache_file = os.path.join(self.cachedir,
            'detected-chunk-buildsystems.cache.pickle')
        buildsystem_cache_manager = PickleCacheManager(
            buildsystem_cache_file, 1000)

        tree_cache_file = os.path.join(self.cachedir, 'trees.cache.pickle')
        tree_cache_manager = PickleCacheManager(tree_cache_file, 1000)

        def status(msg='', **kwargs):
            pass

        self.sr = SourceResolver(self.lrc, self.rrc, tree_cache_manager,
                                 buildsystem_cache_manager, True, status)
        self.lsr = SourceResolver(self.lrc, None, tree_cache_manager,
                                  buildsystem_cache_manager, True, status)

        self.sr._definitions_repo = None
        self.lsr._definitions_repo = None

    def tearDown(self):
        shutil.rmtree(self.cachedir)

    def nolocalfile(self, *args):
        raise IOError('File not found')

    def noremotefile(self, *args):
        raise CatFileError('reponame', 'ref', 'filename')

    def noremoterepo(self, *args):
        raise LsTreeError('reponame', 'ref')

    def localmorph(self, *args):
        return ['chunk.morph']

    def nolocalmorph(self, *args):
        if args[0].endswith('.morph'):
            raise IOError('File not found')
        return 'text'

    def autotoolsbuildsystem(self, *args, **kwargs):
        return ['configure.in']

    def emptytree(self, *args, **kwargs):
        return []

    def remotemorph(self, *args, **kwargs):
        return ['remote-chunk.morph']

    def noremotemorph(self, *args):
        if args[-1].endswith('.morph'):
            raise CatFileError('reponame', 'ref', 'filename')
        return 'text'

    def doesnothaverepo(self, reponame):
        return False

    def test_gets_morph_from_local_repo(self):
        self.lr.list_files = self.localmorph
        morph = self.sr._get_morphology('reponame', 'sha1',
                                       'chunk.morph')
        self.assertEqual('chunk', morph['name'])

    def test_gets_morph_from_cache(self):
        self.lr.list_files = self.localmorph
        morph_from_repo = self.sr._get_morphology('reponame', 'sha1',
                                                  'chunk.morph')
        morph_from_cache = self.sr._get_morphology('reponame', 'sha1',
                                                   'chunk.morph')
        self.assertEqual(morph_from_repo, morph_from_cache)

    def test_gets_morph_from_remote_repo(self):
        self.rrc.ls_tree = self.remotemorph
        self.lrc.has_repo = self.doesnothaverepo
        morph = self.sr._get_morphology('reponame', 'sha1',
                                       'remote-chunk.morph')
        self.assertEqual('remote-chunk', morph['name'])

    def test_autodetects_local_morphology(self):
        self.lr.read_file = self.nolocalmorph
        self.lr.list_files = self.autotoolsbuildsystem
        name = self.sr._detect_build_system('reponame', 'sha1',
                                            'assumed-local.morph')
        self.assertEqual('autotools', name)

    def test_cache_repo_if_not_in_either_cache(self):
        self.lrc.has_repo = self.doesnothaverepo
        self.lr.read_file = self.nolocalmorph
        self.lr.list_files = self.autotoolsbuildsystem
        self.rrc.ls_tree = self.noremoterepo
        name = self.sr._detect_build_system('reponame', 'sha1',
                                            'assumed-local.morph')
        self.assertEqual('autotools', name)

    def test_autodetects_remote_morphology(self):
        self.lrc.has_repo = self.doesnothaverepo
        self.rrc.cat_file = self.noremotemorph
        self.rrc.ls_tree = self.autotoolsbuildsystem
        name = self.sr._detect_build_system('reponame', 'sha1',
                                            'assumed-remote.morph')
        self.assertEqual('autotools', name)

    def test_returns_none_when_no_local_morph(self):
        self.lr.read_file = self.nolocalfile
        morph = self.sr._get_morphology('reponame', 'sha1', 'unreached.morph')
        self.assertEqual(morph, None)

    def test_returns_none_when_fails_no_remote_morph(self):
        self.lrc.has_repo = self.doesnothaverepo
        self.rrc.cat_file = self.noremotefile
        morph = self.sr._get_morphology('reponame', 'sha1', 'unreached.morph')
        self.assertEqual(morph, None)

    def test_raises_error_when_repo_does_not_exist(self):
        self.lrc.has_repo = self.doesnothaverepo
        self.assertRaises(MorphologyNotFoundError,
                          self.lsr._detect_build_system,
                          'reponame', 'sha1', 'non-existent.morph')

    def test_raises_error_when_failed_to_detect_build_system(self):
        self.lr.read_file = self.nolocalfile
        self.lr.list_files = self.emptytree
        self.assertRaises(MorphologyNotFoundError,
                          self.sr._detect_build_system,
                          'reponame', 'sha1', 'undetected.morph')

    def test_raises_error_when_name_mismatches(self):
        self.assertRaises(morphlib.Error, self.sr._get_morphology,
                          'reponame', 'sha1', 'name-mismatch.morph')

    def test_looks_locally_with_no_remote(self):
        self.lr.list_files = self.localmorph
        morph = self.lsr._get_morphology('reponame', 'sha1',
                                         'chunk.morph')
        self.assertEqual('chunk', morph['name'])

    def test_autodetects_locally_with_no_remote(self):
        self.lr.read_file = self.nolocalmorph
        self.lr.list_files = self.autotoolsbuildsystem
        name = self.sr._detect_build_system('reponame', 'sha1',
                                            'assumed-local.morph')
        self.assertEqual('autotools', name)

    def test_succeeds_when_local_not_cached_and_no_remote(self):
        self.lrc.has_repo = self.doesnothaverepo
        self.lr.list_files = self.localmorph
        morph = self.sr._get_morphology('reponame', 'sha1',
                                       'chunk.morph')
        self.assertEqual('chunk', morph['name'])

    def test_arch_is_validated(self):
        self.lr.arch = 'unknown'
        self.assertRaises(morphlib.Error, self.sr._get_morphology,
                          'reponame', 'sha1', 'system.morph')

    def test_arch_arm_defaults_to_le(self):
        self.lr.arch = 'armv7'
        morph = self.sr._get_morphology('reponame', 'sha1', 'system.morph')
        self.assertEqual(morph['arch'], 'armv7l')

    def test_fails_on_parse_error(self):
        self.assertRaises(morphlib.Error, self.sr._get_morphology,
                          'reponame', 'sha1', 'parse-error.morph')

    def test_fails_on_no_bdeps_or_bootstrap(self):
        self.assertRaises(
            morphlib.morphloader.NoStratumBuildDependenciesError,
            self.sr._get_morphology, 'reponame', 'sha1',
            'stratum-no-bdeps-no-bootstrap.morph')

    def test_succeeds_on_bdeps_no_bootstrap(self):
        self.sr._get_morphology(
            'reponame', 'sha1',
            'stratum-bdeps-no-bootstrap.morph')

    def test_fails_on_empty_stratum(self):
        self.assertRaises(
            morphlib.morphloader.EmptyStratumError,
            self.sr._get_morphology, 'reponame', 'sha1', 'stratum-empty.morph')