diff options
author | Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com> | 2022-11-22 15:17:08 +0100 |
---|---|---|
committer | Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com> | 2023-01-10 14:19:02 +0100 |
commit | dbba1dd594d2b7afeddb06b58c336c4a01a2a6e8 (patch) | |
tree | 5a880e1392d94afbf37b65b5ece5596e2a7343b7 | |
parent | 3e2ac3769eb3207f2b6a680c32057658cc0bfe38 (diff) | |
download | libvirt-ci-dbba1dd594d2b7afeddb06b58c336c4a01a2a6e8.zip libvirt-ci-dbba1dd594d2b7afeddb06b58c336c4a01a2a6e8.tar.gz libvirt-ci-dbba1dd594d2b7afeddb06b58c336c4a01a2a6e8.tar.bz2 |
tests: unit test for Container._build_args method
Test that {Podman,Docker}._build_args method
returns the correct list.
Signed-off-by: Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com>
-rw-r--r-- | tests/test_containers.py | 139 |
1 files changed, 138 insertions, 1 deletions
diff --git a/tests/test_containers.py b/tests/test_containers.py index 59ba27f..0321861 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -1,11 +1,12 @@ import pwd import pytest +from pathlib import Path from io import TextIOBase from _pytest.monkeypatch import MonkeyPatch from test_utils.utils import assert_equal_list -from lcitool.containers import ContainerError, Podman +from lcitool.containers import ContainerError, Docker, Podman id_mapping = [ @@ -53,6 +54,11 @@ def podman(): return Podman() +@pytest.fixture(scope="module") +def docker(): + return Docker() + + class TestPodmanExtraArgs: """Unit test for Podman()._extra_args""" @@ -90,3 +96,134 @@ class TestPodmanExtraArgs: def test_extra_args_invalid_input(self, user, exception, mock_pwd, podman): with pytest.raises(exception): podman._extra_args(user) + + +class TestEngineOptions: + """Unit test class for Container._build_args function""" + + def podman_extra_args(self, user): + _, _, uid, _, _, _, _ = get_pwuid(user) + if uid == 0: + return [] + return id_mapping + + @pytest.fixture(scope="class", autouse=True) + def patch_extra_args(self): + monkeypatch = MonkeyPatch() + monkeypatch.setattr("lcitool.containers.Podman._extra_args", self.podman_extra_args) + yield monkeypatch + monkeypatch.undo() + + @pytest.mark.parametrize( + "args, options", + [ + pytest.param({"user": 0}, [], id="numeric-root"), + pytest.param({"user": "root"}, [], id="string-root"), + pytest.param({"user": 1}, [], id="numeric-testuser"), + pytest.param({"user": "user"}, [], id="string-testuser"), + pytest.param( + dict(user=0, env=["FOO=bar", "BAR=baz"]), + ["--env=FOO=bar", "--env=BAR=baz"], + id="environmental-variable-root" + ), + pytest.param( + dict(user="user", env=["FOO=baz"]), + ["--env=FOO=baz"], + id="environmental-variable-testuser" + ), + pytest.param( + dict(user="root", datadir="/abc"), + ["--volume", "/abc:/root/datadir:z", "--workdir", "/root"], + id="scratch-directory-root" + ), + pytest.param( + dict(user=1, datadir="/tmp/src"), + [ + "--volume", "/tmp/src:/home/user/datadir:z", + "--workdir", "/home/user" + ], + id="scratch-directory-testuser" + ), + pytest.param( + dict(user=0, script="build"), + ["--workdir", "/root"], + id="script-file-root" + ), + pytest.param( + dict(user=1, script="build"), + ["--workdir", "/home/user"], + id="script-file-testuser" + ), + pytest.param( + dict(user=0, datadir="/abc", script="bundle.sh"), + [ + "--volume", "/abc:/root/datadir:z", + "--workdir", "/root" + ], + id="scratch-directory-script-file-for-root" + ), + pytest.param( + dict(user=1, datadir="/tmp/random", script="bundle"), + [ + "--volume", "/tmp/random:/home/user/datadir:z", + "--workdir", "/home/user" + ], + id="scratch-directory-script-file-for-testuser" + ), + pytest.param( + dict(user=0, env=["FOO=baz"], datadir="/abc", script="bundle.sh"), + [ + "--volume", "/abc:/root/datadir:z", + "--env=FOO=baz", + "--workdir", "/root" + ], + id="env-scratch-directory-script-file-for-root" + ), + pytest.param( + dict(user=1, env=["BAR=baz"], datadir="/tmp/random", script="bundle"), + [ + "--volume", "/tmp/random:/home/user/datadir:z", + "--env=BAR=baz", + "--workdir", "/home/user" + ], + id="env-scratch-directory-script-file-for-testuser" + ) + ] + ) + def test_options(self, args, options, docker, podman, mock_pwd, tmp_path): + args["tempdir"] = tmp_path + uid, gid, _, workdir = get_pwuid(args.get("user"))[2:6] + template = [ + "--user", f"{uid}:{gid}", + "--volume", f"{tmp_path}/passwd.copy:/etc/passwd:ro,z", + "--volume", f"{tmp_path}/group.copy:/etc/group:ro,z", + "--ulimit", "nofile=1024:1024", + "--cap-add", "SYS_PTRACE" + ] + + extra_option = [] + if args.get("script"): + script_path = Path(tmp_path, args.get("script")) + script_path.write_text("") # to create the file + args["script"] = script_path + + extra_option = [ + "--volume", f"{Path(tmp_path, 'script')}:{workdir}/script:z" + ] + + # test docker options + assert_equal_list( + docker._build_args(**args), + template + extra_option + options, + [], "item" + ) + + if args.get("user") == 1 or args.get("user") == "user": + options += id_mapping + + # test podman options + assert_equal_list( + podman._build_args(**args), + template + extra_option + options, + [], "item" + ) |