summaryrefslogtreecommitdiff
path: root/morphlib/bins_tests.py
blob: 60361ecea4c49b97ce5f70404cbebba70f0dc0d3 (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
217
# Copyright (C) 2011-2014  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 gzip
import os
import shutil
import stat
import tempfile
import tarfile
import unittest
import StringIO

import morphlib


class BinsTest(unittest.TestCase):

    def recursive_lstat(self, root):
        '''Return a list of (pathname, stat) pairs for everything in root.

        Pathnames are relative to root. Directories are recursed into.
        The stat result is selective, not all fields are used.

        '''

        def remove_root(pathname):
            self.assertTrue(pathname.startswith(root))
            if pathname == root:
                return '.'
            else:
                return pathname[(len(root) + 1):]

        def lstat(filename):
            st = os.lstat(filename)

            # For directories, the size is dependent on the contents, and
            # possibly on things that have been deleted already. An unpacked
            # directory can be identical even if the size field is different.
            # So we ignore it for directories.
            #
            # Similarly, the mtime for a directory will change when we remove
            # files in the directory, and a different mtime is not necessarily
            # a sign of a bad unpack. It's possible for the tests to arrange
            # for everything to be correct as far as directory mtimes are
            # concerned, but it's not worth it, so we fudge the mtime too.
            if stat.S_ISDIR(st.st_mode):
                return (st.st_mode, 0, 0)
            else:
                return (st.st_mode, st.st_size, 0)

        result = []

        for dirname, subdirs, basenames in os.walk(root):
            result.append((remove_root(dirname), lstat(dirname)))
            for basename in sorted(basenames):
                filename = os.path.join(dirname, basename)
                result.append((remove_root(filename), lstat(filename)))
            subdirs.sort()

        return result


class ChunkTests(BinsTest):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.instdir = os.path.join(self.tempdir, 'inst')
        self.chunk_file = os.path.join(self.tempdir, 'chunk')
        self.chunk_f = open(self.chunk_file, 'wb')
        self.unpacked = os.path.join(self.tempdir, 'unpacked')

    def tearDown(self):
        self.chunk_f.close()
        shutil.rmtree(self.tempdir)

    def populate_instdir(self):
        timestamp = 12765

        os.mkdir(self.instdir)

        bindir = os.path.join(self.instdir, 'bin')
        os.mkdir(bindir)
        filename = os.path.join(bindir, 'foo')
        with open(filename, 'w'):
            pass
        os.utime(filename, (timestamp, timestamp))

        libdir = os.path.join(self.instdir, 'lib')
        os.mkdir(libdir)
        filename = os.path.join(libdir, 'libfoo.so')
        with open(filename, 'w'):
            pass
        os.utime(filename, (timestamp, timestamp))

        self.instdir_orig_files = self.recursive_lstat(self.instdir)

    def create_chunk(self, includes):
        self.populate_instdir()
        morphlib.bins.create_chunk(self.instdir, self.chunk_f, includes)
        self.chunk_f.flush()

    def unpack_chunk(self):
        os.mkdir(self.unpacked)
        morphlib.bins.unpack_binary(self.chunk_file, self.unpacked)

    def test_empties_files(self):
        self.create_chunk(['bin/foo', 'lib/libfoo.so'])
        self.assertEqual([x for x, y in self.recursive_lstat(self.instdir)],
                         ['.', 'bin', 'lib'])

    def test_creates_and_unpacks_chunk_exactly(self):
        self.create_chunk(['bin', 'bin/foo', 'lib', 'lib/libfoo.so'])
        self.unpack_chunk()
        self.assertEqual(self.instdir_orig_files,
                         self.recursive_lstat(self.unpacked))

    def test_uses_only_matching_names(self):
        self.create_chunk(['bin/foo'])
        self.unpack_chunk()
        self.assertEqual([x for x, y in self.recursive_lstat(self.unpacked)],
                         ['.', 'bin', 'bin/foo'])
        self.assertEqual([x for x, y in self.recursive_lstat(self.instdir)],
                         ['.', 'bin', 'lib', 'lib/libfoo.so'])

    def test_does_not_compress_artifact(self):
        self.create_chunk(['bin'])
        f = gzip.open(self.chunk_file)
        self.assertRaises(IOError, f.read)
        f.close()


class ExtractTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.instdir = os.path.join(self.tempdir, 'inst')
        self.unpacked = os.path.join(self.tempdir, 'unpacked')

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def create_chunk(self, callback):
        fh = StringIO.StringIO()
        os.mkdir(self.instdir)
        patterns = callback(self.instdir)
        morphlib.bins.create_chunk(self.instdir, fh, patterns)
        shutil.rmtree(self.instdir)
        fh.flush()
        fh.seek(0)
        return fh

    def test_extracted_files_replace_links(self):
        def make_linkfile(basedir):
            with open(os.path.join(basedir, 'babar'), 'w') as f:
                pass
            os.symlink('babar', os.path.join(basedir, 'bar'))
            return ['babar']
        linktar = self.create_chunk(make_linkfile)

        def make_file(basedir):
            with open(os.path.join(basedir, 'bar'), 'w') as f:
                pass
            return ['bar']
        filetar = self.create_chunk(make_file)

        os.mkdir(self.unpacked)
        morphlib.bins.unpack_binary_from_file(linktar, self.unpacked)
        morphlib.bins.unpack_binary_from_file(filetar, self.unpacked)
        mode = os.lstat(os.path.join(self.unpacked, 'bar')).st_mode
        self.assertTrue(stat.S_ISREG(mode))

    def test_extracted_dirs_keep_links(self):
        def make_usrlink(basedir):
            os.symlink('.', os.path.join(basedir, 'usr'))
            return ['usr']
        linktar = self.create_chunk(make_usrlink)

        def make_usrdir(basedir):
            os.mkdir(os.path.join(basedir, 'usr'))
            return ['usr']
        dirtar = self.create_chunk(make_usrdir)

        morphlib.bins.unpack_binary_from_file(linktar, self.unpacked)
        morphlib.bins.unpack_binary_from_file(dirtar, self.unpacked)
        mode = os.lstat(os.path.join(self.unpacked, 'usr')).st_mode
        self.assertTrue(stat.S_ISLNK(mode))

    def test_extracted_files_follow_links(self):
        def make_usrlink(basedir):
            os.symlink('.', os.path.join(basedir, 'usr'))
            return ['usr']
        linktar = self.create_chunk(make_usrlink)

        def make_usrdir(basedir):
            os.mkdir(os.path.join(basedir, 'usr'))
            with open(os.path.join(basedir, 'usr', 'foo'), 'w') as f:
                pass
            return ['usr', 'usr/foo']
        dirtar = self.create_chunk(make_usrdir)

        morphlib.bins.unpack_binary_from_file(linktar, self.unpacked)
        morphlib.bins.unpack_binary_from_file(dirtar, self.unpacked)
        mode = os.lstat(os.path.join(self.unpacked, 'foo')).st_mode
        self.assertTrue(stat.S_ISREG(mode))