summaryrefslogtreecommitdiff
path: root/ick2/actionenvs_tests.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-03-17 19:35:30 +0200
committerLars Wirzenius <liw@liw.fi>2018-03-30 11:52:19 +0300
commitc4841e5b3af2ebaa448a1a0b43a65941cf5c6c3e (patch)
tree5044362b227e3e92254847c40c00f02671f13106 /ick2/actionenvs_tests.py
parent114a276e06aeb19cd9150175efa53d7267b6a6ff (diff)
downloadick2-c4841e5b3af2ebaa448a1a0b43a65941cf5c6c3e.tar.gz
Add: ActionEnvironment class for executing in various contexts
Diffstat (limited to 'ick2/actionenvs_tests.py')
-rw-r--r--ick2/actionenvs_tests.py138
1 files changed, 138 insertions, 0 deletions
diff --git a/ick2/actionenvs_tests.py b/ick2/actionenvs_tests.py
new file mode 100644
index 0000000..4643527
--- /dev/null
+++ b/ick2/actionenvs_tests.py
@@ -0,0 +1,138 @@
+# 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 unittest
+
+
+import ick2
+
+
+class FakeReporter:
+
+ def __init__(self):
+ self.stdout = b''
+ self.stderr = b''
+
+ def report(self, exit_code, stdout, stderr):
+ self.stdout += stdout
+ self.stderr += stderr
+
+
+class RunnerTests(unittest.TestCase):
+
+ def test_runs_echo(self):
+ reporter = FakeReporter()
+ r = ick2.Runner(reporter)
+ exit_code = r.runcmd(['echo', 'hello', 'world'])
+ self.assertEqual(exit_code, 0)
+ self.assertEqual(reporter.stdout, b'hello world\n')
+
+ def test_captures_lots_of_output(self):
+ reporter = FakeReporter()
+ r = ick2.Runner(reporter, maxbuf=1)
+ exit_code = r.runcmd(['echo', 'hello', 'world'])
+ self.assertEqual(exit_code, 0)
+ self.assertEqual(reporter.stdout, b'hello world\n')
+
+ def test_captures_stderr(self):
+ reporter = FakeReporter()
+ r = ick2.Runner(reporter)
+ exit_code = r.runcmd(['sh', '-c', 'echo eek 1>&2 ; exit 2'])
+ self.assertEqual(exit_code, 2)
+ self.assertEqual(reporter.stdout, b'eek\n')
+
+
+class EnvironmentTestsBase(unittest.TestCase):
+
+ def setUp(self):
+ self.workspace = '/workspace'
+ self.fake = FakeRuncmd()
+ self.env = self.create_env()
+ self.env.host_runcmd = self.fake
+
+ def create_env(self):
+ return DummyEnvironment(None, self.workspace, None)
+
+ def expected_argv(self, argv):
+ return argv
+
+ def test_runs_argv(self):
+ argv = ['echo', 'hello', 'world']
+ self.fake.exit_code = 42
+ exit_code = self.env.runcmd(argv)
+ self.assertEqual(self.fake.argv, self.expected_argv(argv))
+ self.assertEqual(self.fake.exit_code, exit_code)
+
+
+class DummyEnvironment(ick2.ActionEnvironment):
+
+ def runcmd(self, argv):
+ return self.host_runcmd(argv)
+
+
+class HostEnvironmentTests(EnvironmentTestsBase):
+
+ def create_env(self):
+ return ick2.HostEnvironment(None, self.workspace, None)
+
+ def expected_argv(self, argv):
+ return argv
+
+ def test_pipeline(self):
+ reporter = FakeReporter()
+ env = ick2.HostEnvironment(None, None, reporter)
+
+ echo = ['echo', 'hello', 'world']
+ cat = ['cat']
+ exit_code = env.host_runcmd(echo, cat)
+ self.assertEqual(exit_code, 0)
+ self.assertEqual(reporter.stdout, b'hello world\n')
+ self.assertEqual(reporter.stderr, b'')
+
+
+class ChrootEnvironmentTests(EnvironmentTestsBase):
+
+ def create_env(self):
+ return ick2.ChrootEnvironment(None, self.workspace, None)
+
+ def expected_argv(self, argv):
+ return ['sudo', 'chroot', self.workspace] + argv
+
+
+class ContainerEnvironmentTests(EnvironmentTestsBase):
+
+ def create_env(self):
+ self.systree = 'systree'
+ self.workspace = 'workspace'
+ return ick2.ContainerEnvironment(self.systree, self.workspace, None)
+
+ def expected_argv(self, argv):
+ prefix = [
+ 'sudo', 'systemd-nspawn', '-D', self.systree,
+ '--bind', '{}:/workspace'.format(self.workspace),
+ '--chdir', '/workspace',
+ ]
+ return prefix + argv
+
+
+class FakeRuncmd:
+
+ def __init__(self):
+ self.argv = None
+ self.exit_code = None
+
+ def __call__(self, argv, cwd=None):
+ self.argv = argv
+ return self.exit_code