summaryrefslogtreecommitdiff
path: root/morphlib/bins_tests.py
blob: 73daff50874ffb87d83b5ed95d510a6737a65c5c (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
# Copyright (C) 2011  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 tempfile
import unittest

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)
            return (st.st_mode, st.st_size, int(st.st_mtime))
    
        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.ex = morphlib.execute.Execute('.', lambda s: None)
        self.tempdir = tempfile.mkdtemp()
        self.instdir = os.path.join(self.tempdir, 'inst')
        self.chunk_file = os.path.join(self.tempdir, 'chunk')
        self.unpacked = os.path.join(self.tempdir, 'unpacked')
        
    def tearDown(self):
        shutil.rmtree(self.tempdir)

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

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

    def test_empties_everything(self):
        self.populate_instdir()
        morphlib.bins.create_chunk(self.instdir, self.chunk_file, ['.'],
                                   self.ex)
        empty = os.path.join(self.tempdir, 'empty')
        os.mkdir(empty)
        self.assertEqual([x for x,y in self.recursive_lstat(self.instdir)],
                         ['.'])

    def test_creates_and_unpacks_chunk_exactly(self):
        self.populate_instdir()
        orig_files = self.recursive_lstat(self.instdir)
        morphlib.bins.create_chunk(self.instdir, self.chunk_file, ['.'],
                                   self.ex)
        os.mkdir(self.unpacked)
        morphlib.bins.unpack_binary(self.chunk_file, self.unpacked, self.ex)
        self.assertEqual(orig_files, self.recursive_lstat(self.unpacked))

    def test_uses_only_matching_names(self):
        self.populate_instdir()
        morphlib.bins.create_chunk(self.instdir, self.chunk_file, ['bin'],
                                   self.ex)
        os.mkdir(self.unpacked)
        morphlib.bins.unpack_binary(self.chunk_file, self.unpacked, self.ex)
        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)],
                         ['.', 'lib', 'lib/libfoo.so'])

class StratumTests(BinsTest):

    def setUp(self):
        self.ex = morphlib.execute.Execute('.', lambda s: None)
        self.tempdir = tempfile.mkdtemp()
        self.instdir = os.path.join(self.tempdir, 'inst')
        self.stratum_file = os.path.join(self.tempdir, 'stratum')
        self.unpacked = os.path.join(self.tempdir, 'unpacked')
        
    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def populate_instdir(self):
        os.mkdir(self.instdir)
        os.mkdir(os.path.join(self.instdir, 'bin'))

    def test_creates_and_unpacks_stratum_exactly(self):
        self.populate_instdir()
        morphlib.bins.create_stratum(self.instdir, self.stratum_file, self.ex)
        os.mkdir(self.unpacked)
        morphlib.bins.unpack_binary(self.stratum_file, self.unpacked, self.ex)
        self.assertEqual(self.recursive_lstat(self.instdir),
                         self.recursive_lstat(self.unpacked))