summaryrefslogtreecommitdiff
path: root/ick2/actions_tests.py
blob: 775428fa81da9cfbe02783aacdbfb9b93a3406f2 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright (C) 2018  Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import shutil
import tempfile
import unittest


import ick2


class ActionFactoryTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.workspace_area = self.tempdir
        self.project = 'magic'
        self.build_id = 'moomin/42'
        self.af = ick2.ActionFactory(
            self.build_id, 'systree', self.workspace_area, None)

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

    def test_sets_token(self):
        token = 'this is a token'
        self.af.set_token(token)
        self.assertEqual(self.af.get_token(), token)

    def test_lists_allows_environments(self):
        allowed = self.af.get_allowed_environments()
        self.assertTrue(isinstance(allowed, list))
        self.assertNotEqual(allowed, [])

    def test_creates_host_environment_by_default(self):
        spec = {
            'shell': 'echo hello world',
        }
        env = self.af.create_environment(spec, self.project)
        self.assertTrue(isinstance(env, ick2.HostEnvironment))

        self.assertEqual(
            env.get_workspace_directory(),
            os.path.join(self.workspace_area, self.project))

    def test_creates_host_environment_when_asked_explicitly(self):
        spec = {
            'where': 'host',
        }
        env = self.af.create_environment(spec, self.project)
        self.assertTrue(isinstance(env, ick2.HostEnvironment))

    def test_creates_chroot_environment_by_default(self):
        spec = {
            'where': 'chroot',
        }
        env = self.af.create_environment(spec, self.project)
        self.assertTrue(isinstance(env, ick2.ChrootEnvironment))

    def test_creates_container_environment_by_default(self):
        spec = {
            'where': 'container',
        }
        env = self.af.create_environment(spec, self.project)
        self.assertTrue(isinstance(env, ick2.ContainerEnvironment))

    def test_creates_shell_action_on_host(self):
        def url(blob_name):
            return blob_name

        spec = {
            'shell': 'echo hello, world',
        }
        token = 'secret token'
        self.af.set_token(token)
        self.af.set_blob_url_func(url)
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.ShellAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))
        self.assertEqual(action.get_build_id(), self.build_id)
        self.assertEqual(action.get_token(), token)
        self.assertEqual(action.get_blob_upload_url('foo'), 'foo')

    def test_creates_python_action_on_host(self):
        spec = {
            'python': 'pass',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.PythonAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_creates_debootstrap_action_on_host(self):
        spec = {
            'debootstrap': 'auto',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.DebootstrapAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_creates_create_workspace_action_on_host(self):
        spec = {
            'action': 'create_workspace',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.CreateWorkspaceAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_creates_archive_workspace_action_on_host(self):
        spec = {
            'archive': 'workspace',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.ArchiveWorkspaceAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_creates_populate_systree_action_on_host(self):
        spec = {
            'action': 'populate_systree',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.PopulateSystreeAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_creates_git_action_on_host(self):
        spec = {
            'action': 'git',
        }
        action = self.af.create_action(spec, self.project)
        self.assertTrue(isinstance(action, ick2.GitAction))
        self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment))

    def test_raises_exception_for_unknown_step(self):
        with self.assertRaises(ick2.UnknownStepError):
            self.af.create_action({}, self.project)


class ShellActionTests(unittest.TestCase):

    def test_encodes_parameters(self):
        params = {
            'foo': 'bar',
        }
        action = ick2.ShellAction(None)
        encoded = action.encode64(params)
        decoded = action.decode64(encoded)
        self.assertEqual(params, decoded)

    def test_runs_argv(self):
        params = {}
        step = {
            'shell': 'echo hello, world',
        }
        env = DummyEnvironment()
        env.exit_code = 42
        action = ick2.ShellAction(env)
        exit_code = action.execute(params, step)
        self.assertEqual(exit_code, env.exit_code)


class PythonActionTests(unittest.TestCase):

    def test_runs_argv(self):
        params = {}
        step = {
            'python': 'pass',
        }
        env = DummyEnvironment()
        env.exit_code = 42
        action = ick2.PythonAction(env)
        exit_code = action.execute(params, step)
        self.assertEqual(exit_code, env.exit_code)


class DebootstrapActionTests(unittest.TestCase):

    def test_runs_argv(self):
        params = {
            'debian_codename': 'stretch',
        }
        step = {
            'debootstrap': 'auto',
            'mirror': 'http://deb.debian.org/debian',
        }
        env = DummyEnvironment()
        env.exit_code = 42
        action = ick2.DebootstrapAction(env)
        exit_code = action.execute(params, step)
        self.assertEqual(exit_code, env.exit_code)


class CreateWorkspaceActionTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()

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

    def test_runs_argv(self):
        params = {}
        step = {
            'action': 'create_workspace',
        }
        env = DummyEnvironment(tempdir=self.tempdir)
        action = ick2.CreateWorkspaceAction(env)
        exit_code = action.execute(params, step)
        self.assertEqual(exit_code, 0)


class DummyEnvironment:

    def __init__(self, tempdir=None):
        self.exit_code = None
        self.argv = None
        self.tempdir = tempdir or tempfile.mkdtemp()

    def report(self, exit_code, msg):
        pass

    def host_runcmd(self, argv, **kwargs):
        self.argv = argv
        return self.exit_code

    def runcmd(self, argv, **kwargs):
        return self.host_runcmd(argv)

    def get_workspace_directory(self):
        return self.tempdir