summaryrefslogtreecommitdiff
path: root/morphlib/stagingarea_tests.py
blob: 3d378573a3ad81c8a7be7e1e493525ced3531403 (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
# 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, see <http://www.gnu.org/licenses/>.


import cliapp
import os
import shutil
import tarfile
import tempfile
import unittest

import morphlib


class FakeBuildEnvironment(object):

    def __init__(self):
        self.env = {
        }
        self.extra_path = ['/extra-path']


class FakeSource(object):

    def __init__(self):
        self.morphology = {
            'name': 'le-name',
        }
        self.name = 'le-name'


class FakeArtifact(object):

    def __init__(self):
        self.source = FakeSource()


class FakeArtifactCache(object):

    def __init__(self, tempdir):
        self.tempdir = tempdir

    def create_chunk(self, chunkdir):
        if not chunkdir:
            chunkdir = os.path.join(self.tempdir, 'chunk')
        if not os.path.exists(chunkdir):
            os.mkdir(chunkdir)
        with open(os.path.join(chunkdir, 'file.txt'), 'w'):
            pass

        return chunkdir

    def get(self, artifact, directory=None):
        return self.create_chunk(directory)


class FakeApplication(object):

    def __init__(self, cachedir, tempdir):
        self.settings = {
            'cachedir': cachedir,
            'tempdir': tempdir,
        }
        for leaf in ('chunks',):
            d = os.path.join(tempdir, leaf)
            if not os.path.exists(d):
                os.makedirs(d)

    def runcmd(self, *args, **kwargs):
        return cliapp.runcmd(*args, **kwargs)

    def runcmd_unchecked(self, *args, **kwargs):
        return cliapp.runcmd_unchecked(*args, **kwargs)

    def status(self, **kwargs):
        pass


class StagingAreaTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.cachedir = os.path.join(self.tempdir, 'cachedir')
        os.mkdir(self.cachedir)
        os.mkdir(os.path.join(self.cachedir, 'artifacts'))
        self.staging = os.path.join(self.tempdir, 'staging')
        self.created_dirs = []
        self.build_env = FakeBuildEnvironment()
        self.sa = morphlib.stagingarea.StagingArea(
            FakeApplication(self.cachedir, self.tempdir), self.staging,
            self.build_env)

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

    def create_chunk(self):
        chunkdir = os.path.join(self.tempdir, 'chunk')
        os.mkdir(chunkdir)
        with open(os.path.join(chunkdir, 'file.txt'), 'w'):
            pass

        return chunkdir

    def list_tree(self, root):
        files = []
        for dirname, subdirs, basenames in os.walk(root):
            paths = [os.path.join(dirname, x) for x in basenames]
            for x in [dirname] + sorted(paths):
                files.append(x[len(root):] or '/')
        return files

    def fake_mkdir(self, dirname):
        self.created_dirs.append(dirname)

    def test_remembers_specified_directory(self):
        self.assertEqual(self.sa.dirname, self.staging)

    def test_creates_build_directory(self):
        source = FakeSource()
        self.sa._mkdir = self.fake_mkdir
        dirname = self.sa.builddir(source)
        self.assertEqual(self.created_dirs, [dirname])
        self.assertTrue(dirname.startswith(self.staging))

    def test_creates_install_directory(self):
        source = FakeSource()
        self.sa._mkdir = self.fake_mkdir
        dirname = self.sa.destdir(source)
        self.assertEqual(self.created_dirs, [dirname])
        self.assertTrue(dirname.startswith(self.staging))

    def test_creates_overlay_upper_directory(self):
        source = FakeSource()
        self.sa._mkdir = self.fake_mkdir
        dirname = self.sa.overlay_upperdir(source)
        self.assertEqual(self.created_dirs, [dirname])
        self.assertTrue(dirname.startswith(self.staging))

    def test_creates_overlay_directory(self):
        source = FakeSource()
        self.sa._mkdir = self.fake_mkdir
        dirname = self.sa.overlaydir(source)
        self.assertEqual(self.created_dirs, [dirname])
        self.assertTrue(dirname.startswith(self.staging))

    def test_makes_relative_name(self):
        filename = os.path.join(self.staging, 'foobar')
        self.assertEqual(self.sa.relative(filename), '/foobar')

    def test_installs_artifact(self):
        artifact = FakeArtifact()
        artifact_cache = FakeArtifactCache(self.tempdir)
        self.sa.install_artifact(artifact_cache, artifact)
        self.assertEqual(self.list_tree(self.staging), ['/', '/file.txt'])

    def test_removes_everything(self):
        artifact = FakeArtifact()
        artifact_cache = FakeArtifactCache(self.tempdir)
        self.sa.install_artifact(artifact_cache, artifact)
        self.sa.remove()
        self.assertFalse(os.path.exists(self.staging))

    def test_supports_non_isolated_mode(self):
        sa = morphlib.stagingarea.StagingArea(
            object(), self.staging, self.build_env, use_chroot=False)
        filename = os.path.join(self.staging, 'foobar')
        self.assertEqual(sa.relative(filename), filename)