summaryrefslogtreecommitdiff
path: root/morphlib/morphology_tests.py
blob: ccec41fcae4632992dcefd7656e9776ffe77b730 (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
# 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; 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 test_accepts_valid_chunk_morphology(self):
        morph = morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "chunk", 
                                "description": "desc",
                                "configure-commands": ["./configure"],
                                "build-commands": ["make"],
                                "test-commands": ["make check"],
                                "install-commands": ["make install"]
                            }'''))
        self.assertEqual(morph.name, 'hello')
        self.assertEqual(morph.kind, 'chunk')
        self.assertEqual(morph.description, 'desc')
        self.assertEqual(morph.filename, 'mockfile')
        self.assertEqual(morph.configure_commands, ['./configure'])
        self.assertEqual(morph.build_commands, ['make'])
        self.assertEqual(morph.test_commands, ['make check'])
        self.assertEqual(morph.install_commands, ['make install'])

    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')
        self.assertEqual(morph.sources,
                         {
                            'foo': { 'repo': 'foo/', 'ref': 'ref' },
                         })

    def test_accepts_valid_system_morphology(self):
        morph = morphlib.morphology.Morphology(
                          MockFile('''
                            {
                                "name": "hello",
                                "kind": "system", 
                                "disk-size": "1G",
                                "strata": [
                                    "foo",
                                    "bar"
                                ]
                            }'''))
        self.assertEqual(morph.kind, 'system')
        self.assertEqual(morph.disk_size, '1G')
        self.assertEqual(morph.strata, ['foo', 'bar'])


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_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/')