summaryrefslogtreecommitdiff
path: root/morphlib/builder_tests.py
blob: 54bc4a8f9f25acb088f7c90f815ccf0f5c18b7eb (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
# Copyright (C) 2012-2016  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 json
import os
import StringIO
import unittest

import morphlib
import morphlib.gitdir_tests


class FakeBuildSystem(object):

    def __init__(self):
        self.build_commands = ['buildsys-it']


class FakeApp(object):
    def __init__(self, runcmd=None):
        self.runcmd = runcmd


class FakeStagingArea(object):

    def __init__(self, runcmd, build_env):
        self.runcmd = runcmd
        self.env = build_env.env


class FakeSource(object):

    def __init__(self):
        self.morphology = {
            'name': 'a',
            'kind': 'b',
            'description': 'c',
        }
        self.name = 'a'

        with morphlib.gitdir_tests.allow_nonexistant_git_repos():
            self.repo = morphlib.repocache.CachedRepo(
                'path', 'repo', 'url')
        self.repo_name = 'url'
        self.original_ref = 'e'
        self.sha1 = 'f'
        self.filename = 'g'


class FakeArtifact(object):

    def __init__(self, name):
        self.name = name
        self.source = FakeSource()
        self.cache_key = 'blahblah'
        self.cache_id = {}


class FakeBuildEnv(object):

    def __init__(self):
        self.arch = 'le-arch'
        self.env = {
            'PATH': '/le-bin:/le-bon:/le-bin-bon',
        }


class FakeFileHandle(object):

    def __init__(self, cache, key):
        self._string = ""
        self._cache = cache
        self._key = key

    def __enter__(self):
        return self

    def _writeback(self):
        self._cache._cached[self._key] = self._string

    def __exit__(self, type, value, traceback):
        self._writeback()

    def close(self):
        self._writeback()

    def write(self, string):
        self._string += string


class FakeArtifactCache(object):

    def __init__(self):
        self._cached = {}

    def put(self, artifact):
        return FakeFileHandle(self, (artifact.cache_key, artifact.name))

    def put_artifact_metadata(self, artifact, name):
        return FakeFileHandle(self, (artifact.cache_key, artifact.name, name))

    def put_source_metadata(self, source, cachekey, name):
        return FakeFileHandle(self, (cachekey, name))

    def get(self, artifact):
        return StringIO.StringIO(
            self._cached[(artifact.cache_key, artifact.name)])

    def get_artifact_metadata(self, artifact, name):
        return StringIO.StringIO(
            self._cached[(artifact.cache_key, artifact.name, name)])

    def get_source_metadata(self, source, cachekey, name):
        return StringIO.StringIO(self._cached[(cachekey, name)])

    def has(self, artifact):
        return (artifact.cache_key, artifact.name) in self._cached

    def has_artifact_metadata(self, artifact, name):
        return (artifact.cache_key, artifact.name, name) in self._cached

    def has_source_metadata(self, source, cachekey, name):
        return (cachekey, name) in self._cached


class BuilderBaseTests(unittest.TestCase):

    def fake_runcmd(self, argv, *args, **kwargs):
        self.commands_run.append(argv)

    def setUp(self):
        self.commands_run = []
        self.app = FakeApp(self.fake_runcmd)
        self.staging_area = FakeStagingArea(self.fake_runcmd, FakeBuildEnv())
        self.artifact_cache = FakeArtifactCache()
        self.artifact = FakeArtifact('le-artifact')
        self.repo_cache = None
        self.build_env = FakeBuildEnv()
        self.max_jobs = 1
        self.builder = morphlib.builder.BuilderBase(self.app,
                                                     self.staging_area,
                                                     self.artifact_cache,
                                                     None,
                                                     self.artifact,
                                                     self.repo_cache,
                                                     self.max_jobs,
                                                     False)

    def test_runs_desired_command(self):
        self.builder.runcmd(['foo', 'bar'])
        self.assertEqual(self.commands_run, [['foo', 'bar']])

    def test_writes_build_times(self):
        with self.builder.build_watch('nothing'):
            pass
        self.builder.save_build_times()
        self.assertTrue(self.artifact_cache.has_source_metadata(
            self.artifact.source, self.artifact.cache_key, 'meta'))

    def test_watched_events_in_cache(self):
        events = ["configure", "build", "install"]
        for event in events:
            with self.builder.build_watch(event):
                pass
        self.builder.save_build_times()
        meta = json.load(self.artifact_cache.get_source_metadata(
            self.artifact.source, self.artifact.cache_key, 'meta'))
        self.assertEqual(sorted(events),
                         sorted(meta['build-times'].keys()))

    def test_downloads_depends(self):
        lac = FakeArtifactCache()
        rac = FakeArtifactCache()
        afacts = [FakeArtifact(name) for name in ('a', 'b', 'c')]
        for a in afacts:
            fh = rac.put(a)
            fh.write(a.name)
            fh.close()
        morphlib.builder.download_depends(afacts, lac, rac)
        self.assertTrue(all(lac.has(a) for a in afacts))

    def test_downloads_depends_metadata(self):
        lac = FakeArtifactCache()
        rac = FakeArtifactCache()
        afacts = [FakeArtifact(name) for name in ('a', 'b', 'c')]
        for a in afacts:
            fh = rac.put(a)
            fh.write(a.name)
            fh.close()
            fh = rac.put_artifact_metadata(a, 'meta')
            fh.write('metadata')
            fh.close()
        morphlib.builder.download_depends(afacts, lac, rac, ('meta',))
        self.assertTrue(all(lac.has(a) for a in afacts))
        self.assertTrue(all(lac.has_artifact_metadata(a, 'meta')
                            for a in afacts))


class ChunkBuilderTests(unittest.TestCase):

    def setUp(self):
        self.app = FakeApp()
        self.build = morphlib.builder.ChunkBuilder(self.app, None, None,
                                                    None, None, None, 1, False)