summaryrefslogtreecommitdiff
path: root/morphlib/morphology_tests.py
blob: 7a38bff4c857a86abf06c44697e03d02203aba5d (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# Copyright (C) 2011  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; either 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import json
import StringIO
import unittest

import morphlib


class MockFile(StringIO.StringIO):

    def __init__(self, *args, **kwargs):
        StringIO.StringIO.__init__(self, *args, **kwargs)
        self.name = 'mockfile'


class MorphologyTests(unittest.TestCase):

    def assertRaisesSchemaError(self, morph_dict):
        f = MockFile(json.dumps(morph_dict))
        self.assertRaises(morphlib.morphology.SchemaError,
                          morphlib.morphology.Morphology, f)

    def test_raises_exception_for_empty_file(self):
        self.assertRaises(ValueError, 
                          morphlib.morphology.Morphology,
                          MockFile())

    def test_raises_exception_for_file_without_kind_field(self):
        self.assertRaisesSchemaError({})

    def test_raises_exception_for_chunk_with_unknown_keys_only(self):
        self.assertRaisesSchemaError({ 'x': 'y' })

    def test_raises_exception_if_name_only(self):
        self.assertRaisesSchemaError({ 'name': 'hello' })

    def test_raises_exception_if_name_is_empty(self):
        self.assertRaisesSchemaError({ 'name': '', 'kind': 'chunk',
                                       'sources': { 'repo': 'x', 'ref': 'y' }})

    def test_raises_exception_if_kind_only(self):
        self.assertRaisesSchemaError({ 'kind': 'chunk' })

    def test_raises_exception_for_kind_that_has_unknown_kind(self):
        self.assertRaisesSchemaError({ 'name': 'hello', 'kind': 'x' })

    def test_raises_exception_for_chunk_without_source(self):
        self.assertRaisesSchemaError({ 'name': 'hello', 'kind': 'chunk' })

    def test_raises_exception_for_chunk_with_nondict_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': [],
        })

    def test_raises_exception_for_chunk_with_empty_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {},
        })

    def test_raises_exception_for_chunk_without_repo_in_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'x': 'y'
            },
        })

    def test_raises_exception_for_chunk_with_empty_repo_in_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': '',
                'ref': 'master'
            },
        })

    def test_raises_exception_for_chunk_without_ref_in_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
            },
        })

    def test_raises_exception_for_chunk_with_empty_ref_in_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': ''
            },
        })

    def test_raises_exception_for_chunk_with_unknown_keys_in_source(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master',
                'x': 'y'
            },
        })

    def test_raises_exception_for_chunk_with_unknown_keys(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'x': 'y'
        })

    def test_raises_exception_for_nonlist_configure_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'configure-commands': 0,
        })

    def test_raises_exception_for_list_of_nonstring_configure_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'configure-commands': [0],
        })

    def test_raises_exception_for_nonlist_build_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'build-commands': 0,
        })

    def test_raises_exception_for_list_of_nonstring_build_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'build-commands': [0],
        })

    def test_raises_exception_for_nonlist_test_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'test-commands': 0,
        })

    def test_raises_exception_for_list_of_nonstring_test_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'build-commands': [0],
        })

    def test_raises_exception_for_nonlist_install_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'install-commands': 0,
        })

    def test_raises_exception_for_list_of_nonstring_install_commands(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'chunk',
            'source': {
                'repo': 'foo',
                'ref': 'master'
            },
            'install-commands': [0],
        })

    def test_accepts_valid_chunk_morphology(self):
        chunk = morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "chunk", 
                                "source": 
                                    {
                                        "repo": "foo",
                                        "ref": "ref"
                                    },
                                "configure-commands": ["./configure"],
                                "build-commands": ["make"],
                                "test-commands": ["make check"],
                                "install-commands": ["make install"]
                            }'''))
        self.assertEqual(chunk.kind, 'chunk')
        self.assertEqual(chunk.filename, 'mockfile')

    def test_raises_exception_for_stratum_without_sources(self):
        self.assertRaisesSchemaError({ 'name': 'hello', 'kind': 'stratum' })

    def test_raises_exception_for_stratum_with_nondict_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': [],
        })

    def test_raises_exception_for_stratum_with_empty_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {},
        })
        
    def test_raises_exception_for_stratum_with_bad_children_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': 0,
            },
        })

    def test_raises_exception_for_stratum_without_repo_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'ref': 'master'
                }
            },
        })

    def test_raises_exception_for_stratum_with_empty_repo_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': '',
                    'ref': 'master'
                }
            },
        })

    def test_raises_exception_for_stratum_with_nonstring_repo_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 0,
                    'ref': 'master'
                }
            },
        })

    def test_raises_exception_for_stratum_without_ref_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 'foo',
                }
            },
        })

    def test_raises_exception_for_stratum_with_empty_ref_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 'foo',
                    'ref': ''
                }
            },
        })

    def test_raises_exception_for_stratum_with_nonstring_ref_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 'foo',
                    'ref': 0
                }
            },
        })

    def test_raises_exception_for_stratum_with_unknown_keys_in_sources(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 'foo',
                    'ref': 'master',
                    'x': 'y'
                }
            },
        })

    def test_raises_exception_for_stratum_with_unknown_keys(self):
        self.assertRaisesSchemaError({
            'name': 'hello', 
            'kind': 'stratum',
            'sources': {
                'foo': {
                    'repo': 'foo',
                    'ref': 'master'
                }
            },
            'x': 'y'
        })

    def test_accepts_valid_stratum_morphology(self):
        morph = morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "stratum", 
                                "sources": 
                                    {
                                        "foo": {
                                            "repo": "foo",
                                            "ref": "ref"
                                        }
                                    }
                            }'''))
        self.assertEqual(morph.kind, 'stratum')
        self.assertEqual(morph.filename, 'mockfile')


class ChunkRepoTests(unittest.TestCase):

    def chunk(self, repo):
        return morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "chunk", 
                                "source": 
                                    {
                                        "repo": "%s",
                                        "ref": "HEAD"
                                    },
                                "configure-commands": ["./configure"],
                                "build-commands": ["make"],
                                "test-commands": ["make check"],
                                "install-commands": ["make install"]
                            }''' % repo),
                            baseurl='git://git.baserock.org/')

    def test_returns_repo_with_schema_as_is(self):
        self.assertEqual(self.chunk('git://git.baserock.org/foo/').manifest,
                         [('git://git.baserock.org/foo/', 'HEAD')])

    def test_prepends_baseurl_to_repo_without_schema(self):
        self.assertEqual(self.chunk('foo').manifest,
                         [('git://git.baserock.org/foo/', 'HEAD')])

    def test_leaves_absolute_repo_in_source_dict_as_is(self):
        chunk = self.chunk('git://git.baserock.org/foo/')
        self.assertEqual(chunk.source['repo'], 'git://git.baserock.org/foo/')

    def test_makes_relative_repo_url_absolute_in_source_dict(self):
        chunk = self.chunk('foo')
        self.assertEqual(chunk.source['repo'], 'git://git.baserock.org/foo/')


class StratumRepoTests(unittest.TestCase):

    def stratum(self, repo):
        return morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "stratum", 
                                "sources": 
                                    {
                                        "foo": {
                                            "repo": "%s",
                                            "ref": "HEAD"
                                        }
                                    }
                            }''' % repo),
                            baseurl='git://git.baserock.org/')

    def test_returns_repo_with_schema_as_is(self):
        self.assertEqual(self.stratum('git://git.baserock.org/foo/').manifest,
                         [('git://git.baserock.org/foo/', 'HEAD')])

    def test_prepends_baseurl_to_repo_without_schema(self):
        self.assertEqual(self.stratum('foo').manifest,
                         [('git://git.baserock.org/foo/', 'HEAD')])

    def test_leaves_absolute_repo_in_source_dict_as_is(self):
        stratum = self.stratum('git://git.baserock.org/foo/')
        self.assertEqual(stratum.sources['foo']['repo'], 
                         'git://git.baserock.org/foo/')

    def test_makes_relative_repo_url_absolute_in_source_dict(self):
        stratum = self.stratum('foo')
        self.assertEqual(stratum.sources['foo']['repo'], 
                         'git://git.baserock.org/foo/')