summaryrefslogtreecommitdiff
path: root/morphlib/blobs_tests.py
blob: b1062459535db05b37fa5f1e41941a9dec5278ed (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
# Copyright (C) 2012  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 unittest

import morphlib


class FakeChunkMorph(object):
    @property
    def kind(self):
        return 'chunk'


class FakeStratumMorph(object):
    @property
    def kind(self):
        return 'stratum'


class BlobsTests(unittest.TestCase):

    def test_create_a_chunk_blob(self):
        morph = FakeChunkMorph()
        chunk = morphlib.blobs.Blob.create_blob(morph)
        self.assertTrue(isinstance(chunk, morphlib.blobs.Chunk))
        self.assertEqual(morph, chunk.morph)

    def test_create_a_stratum_blob(self):
        morph = FakeStratumMorph()
        stratum = morphlib.blobs.Blob.create_blob(morph)
        self.assertTrue(isinstance(stratum, morphlib.blobs.Stratum))
        self.assertEqual(morph, stratum.morph)

    def test_create_a_system_blob(self):
        class FakeSystemMorph(object):
            @property
            def kind(self):
                return 'system'

        morph = FakeSystemMorph()
        system = morphlib.blobs.Blob.create_blob(morph)
        self.assertTrue(isinstance(system, morphlib.blobs.System))
        self.assertEqual(morph, system.morph)

    def test_create_an_invalid_blob(self):
        class FakeInvalidMorph(object):
            @property
            def kind(self):
                return 'invalid'

            @property
            def filename(self):
                return '/foo/bar/baz.morph'

        morph = FakeInvalidMorph()
        self.assertRaises(TypeError, morphlib.blobs.Blob.create_blob, morph)

    def test_blob_with_parents(self):
        blob1 = morphlib.blobs.Blob(FakeChunkMorph())
        blob2 = morphlib.blobs.Blob(FakeStratumMorph())
        blob3 = morphlib.blobs.Blob(FakeStratumMorph())

        self.assertEqual(len(blob1.parents), 0)

        blob1.add_parent(blob2)
        self.assertTrue(blob2 in blob1.parents)
        self.assertTrue(blob3 not in blob1.parents)
        self.assertEqual(len(blob1.parents), 1)

        blob1.add_parent(blob3)
        self.assertTrue(blob2 in blob1.parents)
        self.assertTrue(blob3 in blob1.parents)
        self.assertEqual(len(blob1.parents), 2)

        blob1.remove_parent(blob2)
        self.assertTrue(blob2 not in blob1.parents)
        self.assertTrue(blob3 in blob1.parents)
        self.assertEqual(len(blob1.parents), 1)

        blob1.remove_parent(blob3)
        self.assertTrue(blob2 not in blob1.parents)
        self.assertTrue(blob3 not in blob1.parents)
        self.assertEqual(len(blob1.parents), 0)

    def test_blob_add_remove_dependency(self):
        blob1 = morphlib.blobs.Blob(None)
        blob2 = morphlib.blobs.Blob(None)

        self.assertEqual(len(blob1.dependencies), 0)
        self.assertEqual(len(blob2.dependencies), 0)

        blob1.add_dependency(blob2)

        self.assertTrue(blob2 in blob1.dependencies)
        self.assertTrue(blob1 in blob2.dependents)

        self.assertTrue(blob1.depends_on(blob2))

        blob2.add_dependency(blob1)

        self.assertTrue(blob2 in blob1.dependencies)
        self.assertTrue(blob1 in blob2.dependents)
        self.assertTrue(blob1 in blob2.dependencies)
        self.assertTrue(blob2 in blob1.dependents)

        self.assertTrue(blob1.depends_on(blob2))
        self.assertTrue(blob2.depends_on(blob1))

        blob1.remove_dependency(blob2)

        self.assertTrue(blob2 not in blob1.dependencies)
        self.assertTrue(blob1 not in blob2.dependents)
        self.assertTrue(blob1 in blob2.dependencies)
        self.assertTrue(blob2 in blob1.dependents)

        self.assertFalse(blob1.depends_on(blob2))
        self.assertTrue(blob2.depends_on(blob1))

        blob2.remove_dependency(blob1)

        self.assertTrue(blob2 not in blob1.dependencies)
        self.assertTrue(blob1 not in blob2.dependents)
        self.assertTrue(blob1 not in blob2.dependencies)
        self.assertTrue(blob2 not in blob1.dependents)

        self.assertFalse(blob1.depends_on(blob2))
        self.assertFalse(blob2.depends_on(blob1))

    def test_hashing_and_equality_checks(self):
        morph = FakeChunkMorph()
        blob1 = morphlib.blobs.Blob.create_blob(morph)
        blob2 = morphlib.blobs.Blob.create_blob(morph)
        blob3 = morphlib.blobs.Blob.create_blob(FakeChunkMorph())

        self.assertEqual(hash(blob1), hash(blob2))
        self.assertEqual(blob1, blob2)

        self.assertNotEqual(hash(blob1), hash(blob3))
        self.assertNotEqual(blob1, blob3)

    def test_chunks(self):
        settings = { 'git-base-url': [] }
        loader = morphlib.morphologyloader.MorphologyLoader(settings)
        loader._get_morph_text = self.get_morph_text

        class FakeTreeish(object):
            def __init__(self):
                self.original_repo = 'test-repo'
        faketreeish = FakeTreeish()

        faketreeish.original_repo = 'hello'
        stratum_morph = loader.load(faketreeish, 'foo.morph')
        stratum = morphlib.blobs.Stratum(stratum_morph)
        self.assertEquals(len(stratum.chunks), 1)
        self.assertTrue('foo' in stratum.chunks)
        self.assertEqual(['.'], stratum.chunks['foo'])

        chunk_morph = loader.load(faketreeish, 'bar.morph')
        chunk = morphlib.blobs.Chunk(chunk_morph)
        self.assertEqual(len(chunk.chunks), 2)
        self.assertTrue('include' in chunk.chunks)
        self.assertEqual(chunk.chunks['include'], ['include/'])
        self.assertTrue('src' in chunk.chunks)
        self.assertEqual(chunk.chunks['src'], ['src/'])

    def get_morph_text(self, treeish, filename):
        if filename == 'foo.morph':
            return ('''
                    {
                        "name": "foo",
                        "kind": "stratum",
                        "sources": [
                            {
                                "name": "bar",
                                "repo": "bar",
                                "ref": "master"
                            }
                        ]
                    }''')
        else:
            return ('''
                    {
                        "name": "bar",
                        "kind": "chunk",
                        "chunks": {
                            "include": [ "include/" ],
                            "src": [ "src/" ]
                        }
                    }''')