summaryrefslogtreecommitdiff
path: root/morphlib/builder_tests.py
blob: a8f064623938aeb63c56a5fe92a5ddeda61303c6 (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
# 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 os
import shutil
import unittest

import morphlib


class FakeSubmodule(object):

    def __init__(self, **kwargs):
        for name in kwargs:
            setattr(self, name, kwargs[name])


class FakeTreeish(object):

    def __init__(self, tempdir, repo, subtreeish=None):
        self.repo = tempdir.join(repo)
        self.ref = 'master'
        self.submodules = []

        temp_repo = tempdir.join('temp_repo')

        os.mkdir(temp_repo)
        ex = morphlib.execute.Execute(temp_repo, lambda s: None)
        ex.runv(['git', 'init', '--quiet'])
        with open(os.path.join(temp_repo, 'file.txt'), 'w') as f:
            f.write('foobar\n')
        ex.runv(['git', 'add', 'file.txt'])
        ex.runv(['git', 'commit', '--quiet', '-m', 'foo'])
        
        if subtreeish is not None:
            ex.runv(['git', 'submodule', 'add', subtreeish.repo])
            path = os.path.basename(subtreeish.repo)
            self.submodules = [FakeSubmodule(repo=subtreeish.repo,
                                             ref='master',
                                             path=path,
                                             treeish=subtreeish)]
        
        ex = morphlib.execute.Execute(tempdir.dirname, lambda s: None)
        ex.runv(['git', 'clone', '-n', temp_repo, self.repo])
        
        shutil.rmtree(temp_repo)
        

class FactoryTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = morphlib.tempdir.Tempdir()
        self.factory = morphlib.builder.Factory(self.tempdir)
        
    def tearDown(self):
        self.tempdir.remove()

    def create_chunk(self):
        '''Create a simple binary chunk.'''
        
        inst = self.tempdir.join('dummy-inst')
        os.mkdir(inst)
        for x in ['bin', 'etc', 'lib']:
            os.mkdir(os.path.join(inst, x))
        
        binary = self.tempdir.join('dummy-chunk')
        ex = None # this is not actually used by the function!
        with open(binary, 'wb') as f:
            morphlib.bins.create_chunk(inst, f, ['.'], ex)
        return binary

    def test_has_no_staging_area_initially(self):
        self.assertEqual(self.factory.staging, None)
        
    def test_creates_staging_area(self):
        self.factory.create_staging()
        self.assertEqual(os.listdir(self.factory.staging), [])

    def test_removes_staging_area(self):
        self.factory.create_staging()
        staging = self.factory.staging
        self.factory.remove_staging()
        self.assertEqual(self.factory.staging, None)
        self.assertFalse(os.path.exists(staging))

    def test_unpacks_binary_from_file(self):
        binary = self.create_chunk()
        self.factory.create_staging()
        self.factory.unpack_binary_from_file(binary)
        self.assertEqual(sorted(os.listdir(self.factory.staging)),
                         sorted(['bin', 'etc', 'lib']))

    def test_removes_staging_area_with_contents(self):
        binary = self.create_chunk()
        self.factory.create_staging()
        self.factory.unpack_binary_from_file(binary)
        staging = self.factory.staging
        self.factory.remove_staging()
        self.assertEqual(self.factory.staging, None)
        self.assertFalse(os.path.exists(staging))

    def test_unpacks_onto_system(self):
    
        # We can't test this by really unpacking onto the system.
        # Instead, we rely on the fact that if the normal unpacking
        # works, the actual worker function for unpacking works, and
        # we can just verify that it gets called with the right
        # parameters.
    
        def fake_unpack(binary, dirname):
            self.dirname = dirname
    
        binary = self.create_chunk()
        self.factory._unpack_binary = fake_unpack
        self.factory.unpack_binary_from_file_onto_system(binary)
        self.assertEqual(self.dirname, '/')

    def test_unpacks_simple_sources(self):
        self.factory.create_staging()
        srcdir = self.tempdir.join('src')
        treeish = FakeTreeish(self.tempdir, 'repo')
        self.factory.unpack_sources(treeish, srcdir)
        self.assertTrue(os.path.exists(os.path.join(srcdir, 'file.txt')))

    def test_unpacks_submodules(self):
        self.factory.create_staging()
        srcdir = self.tempdir.join('src')
        subtreeish = FakeTreeish(self.tempdir, 'subrepo')
        supertreeish = FakeTreeish(self.tempdir, 'repo', subtreeish=subtreeish)
        self.factory.unpack_sources(supertreeish, srcdir)
        self.assertEqual(sorted(os.listdir(srcdir)),
                         sorted(['.git', 'file.txt', 'subrepo']))
        self.assertEqual(sorted(os.listdir(os.path.join(srcdir, 'subrepo'))),
                         sorted(['.git', 'file.txt']))

    def test_sets_timestamp_for_unpacked_files(self):
        self.factory.create_staging()
        srcdir = self.tempdir.join('src')
        treeish = FakeTreeish(self.tempdir, 'repo')
        self.factory.unpack_sources(treeish, srcdir)
        
        mtime = None
        for dirname, subdirs, basenames in os.walk(srcdir):
            pathnames = [os.path.join(dirname, x) for x in basenames]
            for pathname in pathnames + [dirname]:
                st = os.lstat(pathname)
                if mtime is None:
                    mtime = st.st_mtime
                else:
                    self.assertEqual((pathname, mtime), 
                                     (pathname, st.st_mtime))