summaryrefslogtreecommitdiff
path: root/morphlib/artifactresolver_tests.py
blob: 141ff948e113050d9855cac631bcaf57703711ee (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
# Copyright (C) 2012-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 itertools
import unittest
import yaml

import morphlib


def get_chunk_morphology(name, artifact_names=[]):
    assert(isinstance(artifact_names, list))

    if artifact_names:
        # fake a list of artifacts
        artifacts = []
        for artifact_name in artifact_names:
            artifacts.append({'artifact': artifact_name,
                              'include': [artifact_name]})
        text = yaml.dump({"name": name,
                          "kind": "chunk",
                          "products": artifacts}, default_flow_style=False)
    else:
        text = yaml.dump({'name': name,
                          'kind': 'chunk'}, default_flow_style=False)

    loader = morphlib.morphloader.MorphologyLoader()
    morph = loader.load_from_string(text)
    return morph

def get_stratum_morphology(name, chunks=[], build_depends=[]):
    assert(isinstance(chunks, list))
    assert(isinstance(build_depends, list))

    chunks_list = []
    for source_name, morph, repo, ref in chunks:
        chunks_list.append({
            'name': source_name,
            'morph': morph,
            'repo': repo,
            'ref': ref,
            'build-depends': [],
        })
    build_depends_list = []
    for morph in build_depends:
        build_depends_list.append({
            'morph': morph,
        })
    if chunks_list:
        text = yaml.dump({"name": name,
                          "kind": "stratum",
                          "build-depends": build_depends_list,
                          "chunks": chunks_list,}, default_flow_style=False)
    else:
        text = yaml.dump({"name": name,
                          "kind": "stratum",
                          "build-depends": build_depends_list},
                         default_flow_style=False)

    loader = morphlib.morphloader.MorphologyLoader()
    morph = loader.load_from_string(text)
    return morph


class ArtifactResolverTests(unittest.TestCase):

    def setUp(self):
        self.resolver = morphlib.artifactresolver.ArtifactResolver()

    def test_resolve_artifacts_using_an_empty_pool(self):
        pool = morphlib.sourcepool.SourcePool()
        artifacts = self.resolver._resolve_artifacts(pool)
        self.assertEqual(len(artifacts), 0)

    def test_resolve_single_chunk_with_no_subartifacts(self):
        pool = morphlib.sourcepool.SourcePool()

        morph = get_chunk_morphology('chunk')
        sources = morphlib.source.make_sources('repo', 'ref',
                                               'chunk.morph', 'sha1',
                                               'tree', morph)
        for source in sources:
            pool.add(source)

        artifacts = self.resolver._resolve_artifacts(pool)

        self.assertEqual(len(artifacts),
                         sum(len(s.split_rules.artifacts) for s in pool))

        for artifact in artifacts:
            self.assertEqual(artifact.source, source)
            self.assertTrue(artifact.name.startswith('chunk'))
            self.assertEqual(source.dependencies, [])
            self.assertEqual(artifact.dependents, [])

    def test_resolve_single_chunk_with_one_new_artifact(self):
        pool = morphlib.sourcepool.SourcePool()

        morph = get_chunk_morphology('chunk', ['chunk-foobar'])
        sources = morphlib.source.make_sources('repo', 'ref',
                                               'chunk.morph', 'sha1',
                                               'tree', morph)
        for source in sources:
            pool.add(source)

        artifacts = self.resolver._resolve_artifacts(pool)

        self.assertEqual(len(artifacts),
                         sum(len(s.split_rules.artifacts) for s in pool))

        foobartifact, = (a for a in artifacts if a.name == 'chunk-foobar')
        self.assertEqual(foobartifact.source, source)
        self.assertEqual(foobartifact.source.dependencies, [])
        self.assertEqual(foobartifact.dependents, [])

    def test_resolve_single_chunk_with_two_new_artifacts(self):
        pool = morphlib.sourcepool.SourcePool()

        morph = get_chunk_morphology('chunk', ['chunk-baz', 'chunk-qux'])
        sources = morphlib.source.make_sources('repo', 'ref',
                                               'chunk.morph', 'sha1',
                                               'tree', morph)
        for source in sources:
            pool.add(source)

        artifacts = self.resolver._resolve_artifacts(pool)
        artifacts.sort(key=lambda a: a.name)

        self.assertEqual(len(artifacts),
                         sum(len(s.split_rules.artifacts) for s in pool))

        for name in ('chunk-baz', 'chunk-qux'):
            artifact, = (a for a in artifacts if a.name == name)
            self.assertEqual(artifact.source, source)
            self.assertEqual(artifact.source.dependencies, [])
            self.assertEqual(artifact.dependents, [])

    def test_resolve_stratum_and_chunk(self):
        pool = morphlib.sourcepool.SourcePool()

        morph = get_chunk_morphology('chunk')
        sources = morphlib.source.make_sources('repo', 'ref',
                                               'chunk.morph', 'sha1',
                                               'tree', morph)
        for chunk in sources:
            pool.add(chunk)

        morph = get_stratum_morphology(
            'stratum', chunks=[('chunk', 'chunk', 'repo', 'ref')])
        stratum_sources = set(morphlib.source.make_sources('repo', 'ref',
                                                           'stratum.morph',
                                                           'sha1', 'tree',
                                                           morph))
        for stratum in stratum_sources:
            pool.add(stratum)

        artifacts = self.resolver._resolve_artifacts(pool)

        all_artifacts = set()
        for s in pool: all_artifacts.update(s.split_rules.artifacts)

        self.assertEqual(set(a.name for a in artifacts), all_artifacts)
        self.assertEqual(len(artifacts),
                         len(all_artifacts))


        stratum_artifacts = set(a for a in artifacts
                                if a.source in stratum_sources)
        chunk_artifacts = set(a for a in artifacts if a.source == chunk)

        for stratum_artifact in stratum_artifacts:
            self.assertTrue(stratum_artifact.name.startswith('stratum'))
            self.assertEqual(stratum_artifact.dependents, [])
            self.assertTrue(
                any(dep in chunk_artifacts
                    for dep in stratum_artifact.source.dependencies))

        for chunk_artifact in chunk_artifacts:
            self.assertTrue(chunk_artifact.name.startswith('chunk'))
            self.assertEqual(chunk_artifact.source.dependencies, [])
            self.assertTrue(any(dep in stratum_sources
                                for dep in chunk_artifact.dependents))

    def test_resolve_stratum_and_chunk_with_two_new_artifacts(self):
        pool = morphlib.sourcepool.SourcePool()

        morph = get_chunk_morphology('chunk', ['chunk-foo', 'chunk-bar'])
        sources = morphlib.source.make_sources('repo', 'ref',
                                               'chunk.morph', 'sha1',
                                               'tree', morph)
        for chunk in sources:
            pool.add(chunk)

        morph = get_stratum_morphology(
            'stratum',
            chunks=[
                ('chunk', 'chunk', 'repo', 'ref'),
            ])
        stratum_sources = set(morphlib.source.make_sources('repo', 'ref',
                                                           'stratum.morph',
                                                           'sha1', 'tree',
                                                           morph))
        for stratum in stratum_sources:
            pool.add(stratum)

        artifacts = self.resolver._resolve_artifacts(pool)

        self.assertEqual(
            set(artifacts),
            set(itertools.chain.from_iterable(
                    s.artifacts.itervalues()
                    for s in pool)))

        stratum_artifacts = set(a for a in artifacts
                                if a.source in stratum_sources)
        chunk_artifacts = set(a for a in artifacts if a.source == chunk)

        for stratum_artifact in stratum_artifacts:
            self.assertTrue(stratum_artifact.name.startswith('stratum'))
            self.assertEqual(stratum_artifact.dependents, [])
            self.assertTrue(
                any(dep in chunk_artifacts
                    for dep in stratum_artifact.source.dependencies))

        for chunk_artifact in chunk_artifacts:
            self.assertTrue(chunk_artifact.name.startswith('chunk'))
            self.assertEqual(chunk_artifact.source.dependencies, [])
            self.assertTrue(any(dep in stratum_sources
                                for dep in chunk_artifact.dependents))

    def test_detection_of_mutual_dependency_between_two_strata(self):
        loader = morphlib.morphloader.MorphologyLoader()
        pool = morphlib.sourcepool.SourcePool()

        chunk = get_chunk_morphology('chunk1')
        chunk1, = morphlib.source.make_sources(
            'repo', 'original/ref', 'chunk1.morph', 'sha1', 'tree', chunk)
        pool.add(chunk1)

        morph = get_stratum_morphology(
            'stratum1',
            chunks=[(loader.save_to_string(chunk), 'chunk1.morph',
                     'repo', 'original/ref')],
            build_depends=['stratum2'])
        sources = morphlib.source.make_sources('repo', 'original/ref',
                                               'stratum1.morph', 'sha1',
                                               'tree', morph)
        for stratum1 in sources:
            pool.add(stratum1)

        chunk = get_chunk_morphology('chunk2')
        chunk2, = morphlib.source.make_sources(
            'repo', 'original/ref', 'chunk2.morph', 'sha1', 'tree', chunk)
        pool.add(chunk2)

        morph = get_stratum_morphology(
            'stratum2',
            chunks=[(loader.save_to_string(chunk), 'chunk2.morph',
                     'repo', 'original/ref')],
            build_depends=['stratum1'])
        sources = morphlib.source.make_sources('repo', 'original/ref',
                                               'stratum2.morph', 'sha1',
                                               'tree', morph)
        for stratum2 in sources:
            pool.add(stratum2)

        self.assertRaises(morphlib.artifactresolver.MutualDependencyError,
                          self.resolver._resolve_artifacts, pool)

    def test_detection_of_chunk_dependencies_in_invalid_order(self):
        pool = morphlib.sourcepool.SourcePool()

        loader = morphlib.morphloader.MorphologyLoader()
        morph = loader.load_from_string(
            '''
                name: stratum
                kind: stratum
                build-depends: []
                chunks:
                    - name: chunk1
                      repo: repo
                      ref: original/ref
                      build-depends:
                          - chunk2
                    - name: chunk2
                      repo: repo
                      ref: original/ref
                      build-depends: []
            ''')
        sources = morphlib.source.make_sources('repo', 'original/ref',
                                               'stratum.morph', 'sha1',
                                               'tree', morph)
        for stratum in sources:
            pool.add(stratum)

        morph = get_chunk_morphology('chunk1')
        sources = morphlib.source.make_sources('repo', 'original/ref',
                                               'chunk1.morph', 'sha1',
                                               'tree', morph)
        for chunk1 in sources:
            pool.add(chunk1)

        morph = get_chunk_morphology('chunk2')
        sources = morphlib.source.make_sources('repo', 'original/ref',
                                               'chunk2.morph', 'sha1',
                                               'tree', morph)
        for chunk2 in sources:
            pool.add(chunk2)

        self.assertRaises(morphlib.artifactresolver.DependencyOrderError,
                          self.resolver._resolve_artifacts, pool)


# TODO: Expand test suite to include better dependency checking, many
#       tests were removed due to the fundamental change in how artifacts
#       and dependencies are constructed