summaryrefslogtreecommitdiff
path: root/morphlib/builder_tests.py
blob: 0067022f62ee88b3a12baf3175ce4a906d30f9e5 (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
# 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, 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
import morphlib.gitdir_tests
import morphlib.testutils


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.cachedrepo.CachedRepo(
                FakeApp(), 'repo', 'url', 'path')
        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 BuilderBaseTests(unittest.TestCase):

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

    def fake_open(self, filename, mode):
        self.open_filename = filename
        self.open_handle = StringIO.StringIO()
        self.open_handle.close = lambda: None
        return self.open_handle

    def setUp(self):
        self.commands_run = []
        self.app = FakeApp(self.fake_runcmd)
        self.staging_area = FakeStagingArea(self.fake_runcmd, FakeBuildEnv())
        self.artifact_cache = morphlib.testutils.FakeLocalArtifactCache()
        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()))


class ChunkBuilderTests(unittest.TestCase):

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