forked from Mirror/pmbootstrap
WIP: test: start fixing mypy errors (MR 2252)
Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
parent
1232c35e48
commit
2a0ca33770
5 changed files with 33 additions and 31 deletions
|
@ -3,4 +3,4 @@
|
||||||
import pmb.config
|
import pmb.config
|
||||||
|
|
||||||
|
|
||||||
testdata = pmb.config.pmb_src + "/test/testdata"
|
testdata = pmb.config.pmb_src / "test/testdata"
|
||||||
|
|
|
@ -64,4 +64,4 @@ def prepare_tmpdir(args: PmbArgs, monkeypatch, tmpdir):
|
||||||
return path_local, run_git
|
return path_local, run_git
|
||||||
|
|
||||||
def copy_dotgit(args: PmbArgs, tmpdir):
|
def copy_dotgit(args: PmbArgs, tmpdir):
|
||||||
shutil.copytree(args.aports + "/.git", tmpdir + "/.git", ignore_dangling_symlinks=True)
|
shutil.copytree(args.aports / ".git", tmpdir + "/.git", ignore_dangling_symlinks=True)
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
# Copyright 2023 Oliver Smith
|
# Copyright 2023 Oliver Smith
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
from typing import List
|
||||||
import pytest
|
import pytest
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pmb.core.types import PathString
|
||||||
import pmb_test # noqa
|
import pmb_test # noqa
|
||||||
import pmb.build
|
import pmb.build
|
||||||
import pmb.chroot.apk
|
import pmb.chroot.apk
|
||||||
from pmb.core import Chroot
|
from pmb.core import Chroot
|
||||||
|
|
||||||
|
cmds_progress: List[PathString] = []
|
||||||
|
cmds: List[PathString] = []
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def args(tmpdir, request):
|
def args(tmpdir, request):
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
import os
|
import os
|
||||||
import copy
|
import copy
|
||||||
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
import tarfile
|
import tarfile
|
||||||
import glob
|
import glob
|
||||||
|
@ -28,42 +29,42 @@ def args(request):
|
||||||
|
|
||||||
def test_read_signature_info(args: PmbArgs):
|
def test_read_signature_info(args: PmbArgs):
|
||||||
# Tempfolder inside chroot for fake apk files
|
# Tempfolder inside chroot for fake apk files
|
||||||
tmp_path = "/tmp/test_read_signature_info"
|
tmp_path = Path("/tmp/test_read_signature_info")
|
||||||
tmp_path_outside = pmb.config.work / "chroot_native" + tmp_path
|
tmp_path_outside = pmb.config.work / "chroot_native" / tmp_path
|
||||||
if os.path.exists(tmp_path_outside):
|
if os.path.exists(tmp_path_outside):
|
||||||
pmb.chroot.root(args, ["rm", "-r", tmp_path])
|
pmb.chroot.root(args, ["rm", "-r", tmp_path])
|
||||||
pmb.chroot.user(args, ["mkdir", "-p", tmp_path])
|
pmb.chroot.user(args, ["mkdir", "-p", tmp_path])
|
||||||
|
|
||||||
# No signature found
|
# No signature found
|
||||||
pmb.chroot.user(args, ["tar", "-czf", tmp_path + "/no_sig.apk",
|
pmb.chroot.user(args, ["tar", "-czf", tmp_path / "no_sig.apk",
|
||||||
"/etc/issue"])
|
"/etc/issue"])
|
||||||
with tarfile.open(tmp_path_outside + "/no_sig.apk", "r:gz") as tar:
|
with tarfile.open(tmp_path_outside / "no_sig.apk", "r:gz") as tar:
|
||||||
with pytest.raises(RuntimeError) as e:
|
with pytest.raises(RuntimeError) as e:
|
||||||
pmb.chroot.apk_static.read_signature_info(tar)
|
pmb.chroot.apk_static.read_signature_info(tar)
|
||||||
assert "Could not find signature" in str(e.value)
|
assert "Could not find signature" in str(e.value)
|
||||||
|
|
||||||
# Signature file with invalid name
|
# Signature file with invalid name
|
||||||
pmb.chroot.user(args, ["mkdir", "-p", tmp_path + "/sbin"])
|
pmb.chroot.user(args, ["mkdir", "-p", tmp_path / "sbin"])
|
||||||
pmb.chroot.user(args, ["cp", "/etc/issue", tmp_path +
|
pmb.chroot.user(args, ["cp", "/etc/issue", tmp_path /
|
||||||
"/sbin/apk.static.SIGN.RSA.invalid.pub"])
|
"sbin/apk.static.SIGN.RSA.invalid.pub"])
|
||||||
pmb.chroot.user(args, ["tar", "-czf", tmp_path + "/invalid_sig.apk",
|
pmb.chroot.user(args, ["tar", "-czf", tmp_path / "invalid_sig.apk",
|
||||||
"sbin/apk.static.SIGN.RSA.invalid.pub"],
|
"sbin/apk.static.SIGN.RSA.invalid.pub"],
|
||||||
working_dir=tmp_path)
|
working_dir=tmp_path)
|
||||||
with tarfile.open(tmp_path_outside + "/invalid_sig.apk", "r:gz") as tar:
|
with tarfile.open(tmp_path_outside / "invalid_sig.apk", "r:gz") as tar:
|
||||||
with pytest.raises(RuntimeError) as e:
|
with pytest.raises(RuntimeError) as e:
|
||||||
pmb.chroot.apk_static.read_signature_info(tar)
|
pmb.chroot.apk_static.read_signature_info(tar)
|
||||||
assert "Invalid signature key" in str(e.value)
|
assert "Invalid signature key" in str(e.value)
|
||||||
|
|
||||||
# Signature file with realistic name
|
# Signature file with realistic name
|
||||||
path = glob.glob(pmb.config.apk_keys_path + "/*.pub")[0]
|
path = list(pmb.config.apk_keys_path.glob("/*.pub"))[0]
|
||||||
name = os.path.basename(path)
|
name = os.path.basename(path)
|
||||||
path_archive = "sbin/apk.static.SIGN.RSA." + name
|
path_archive = "sbin/apk.static.SIGN.RSA." + name
|
||||||
pmb.chroot.user(args, ["mv",
|
pmb.chroot.user(args, ["mv",
|
||||||
f"{tmp_path}/sbin/apk.static.SIGN.RSA.invalid.pub",
|
tmp_path / "sbin/apk.static.SIGN.RSA.invalid.pub",
|
||||||
f"{tmp_path}/{path_archive}"])
|
tmp_path / path_archive])
|
||||||
pmb.chroot.user(args, ["tar", "-czf", tmp_path + "/realistic_name_sig.apk",
|
pmb.chroot.user(args, ["tar", "-czf", tmp_path / "realistic_name_sig.apk",
|
||||||
path_archive], working_dir=tmp_path)
|
path_archive], working_dir=tmp_path)
|
||||||
with tarfile.open(f"{tmp_path_outside}/realistic_name_sig.apk", "r:gz")\
|
with tarfile.open(tmp_path_outside / "realistic_name_sig.apk", "r:gz")\
|
||||||
as tar:
|
as tar:
|
||||||
sigfilename, sigkey_path = pmb.chroot.apk_static.read_signature_info(
|
sigfilename, sigkey_path = pmb.chroot.apk_static.read_signature_info(
|
||||||
tar)
|
tar)
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
# Copyright 2023 Oliver Smith
|
# Copyright 2023 Oliver Smith
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
import sys
|
import sys
|
||||||
from pmb.core.types import PmbArgs
|
from typing import Any
|
||||||
|
from pmb.core.types import Env, PmbArgs
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import pmb_test # noqa
|
import pmb_test # noqa
|
||||||
|
@ -58,12 +59,12 @@ def test_shell_escape(args: PmbArgs):
|
||||||
def test_shell_escape_env(args: PmbArgs):
|
def test_shell_escape_env(args: PmbArgs):
|
||||||
key = "PMBOOTSTRAP_TEST_ENVIRONMENT_VARIABLE"
|
key = "PMBOOTSTRAP_TEST_ENVIRONMENT_VARIABLE"
|
||||||
value = "long value with spaces and special characters: '\"\\!$test"
|
value = "long value with spaces and special characters: '\"\\!$test"
|
||||||
env = {key: value}
|
env: Env = {key: value}
|
||||||
cmd = ["sh", "-c", "env | grep " + key + " | grep -v SUDO_COMMAND"]
|
cmd = ["sh", "-c", "env | grep " + key + " | grep -v SUDO_COMMAND"]
|
||||||
ret = key + "=" + value + "\n"
|
ret = key + "=" + value + "\n"
|
||||||
|
|
||||||
copy = list(cmd)
|
copy = list(cmd)
|
||||||
func = pmb.helpers.run.user
|
func: Any = pmb.helpers.run.user
|
||||||
assert func(args, cmd, output_return=True, env=env) == ret
|
assert func(args, cmd, output_return=True, env=env) == ret
|
||||||
assert cmd == copy
|
assert cmd == copy
|
||||||
|
|
||||||
|
@ -83,34 +84,30 @@ def test_shell_escape_env(args: PmbArgs):
|
||||||
def test_flat_cmd_simple():
|
def test_flat_cmd_simple():
|
||||||
func = pmb.helpers.run_core.flat_cmd
|
func = pmb.helpers.run_core.flat_cmd
|
||||||
cmd = ["echo", "test"]
|
cmd = ["echo", "test"]
|
||||||
working_dir = None
|
|
||||||
ret = "echo test"
|
ret = "echo test"
|
||||||
env = {}
|
env: Env = {}
|
||||||
assert func(cmd, working_dir, env) == ret
|
assert func(cmd, env=env) == ret
|
||||||
|
|
||||||
|
|
||||||
def test_flat_cmd_wrap_shell_string_with_spaces():
|
def test_flat_cmd_wrap_shell_string_with_spaces():
|
||||||
func = pmb.helpers.run_core.flat_cmd
|
func = pmb.helpers.run_core.flat_cmd
|
||||||
cmd = ["echo", "string with spaces"]
|
cmd = ["echo", "string with spaces"]
|
||||||
working_dir = None
|
|
||||||
ret = "echo 'string with spaces'"
|
ret = "echo 'string with spaces'"
|
||||||
env = {}
|
env: Env = {}
|
||||||
assert func(cmd, working_dir, env) == ret
|
assert func(cmd, env=env) == ret
|
||||||
|
|
||||||
|
|
||||||
def test_flat_cmd_wrap_env_simple():
|
def test_flat_cmd_wrap_env_simple():
|
||||||
func = pmb.helpers.run_core.flat_cmd
|
func = pmb.helpers.run_core.flat_cmd
|
||||||
cmd = ["echo", "test"]
|
cmd = ["echo", "test"]
|
||||||
working_dir = None
|
|
||||||
ret = "JOBS=5 echo test"
|
ret = "JOBS=5 echo test"
|
||||||
env = {"JOBS": "5"}
|
env: Env = {"JOBS": "5"}
|
||||||
assert func(cmd, working_dir, env) == ret
|
assert func(cmd, env=env) == ret
|
||||||
|
|
||||||
|
|
||||||
def test_flat_cmd_wrap_env_spaces():
|
def test_flat_cmd_wrap_env_spaces():
|
||||||
func = pmb.helpers.run_core.flat_cmd
|
func = pmb.helpers.run_core.flat_cmd
|
||||||
cmd = ["echo", "test"]
|
cmd = ["echo", "test"]
|
||||||
working_dir = None
|
|
||||||
ret = "JOBS=5 TEST='spaces string' echo test"
|
ret = "JOBS=5 TEST='spaces string' echo test"
|
||||||
env = {"JOBS": "5", "TEST": "spaces string"}
|
env: Env = {"JOBS": "5", "TEST": "spaces string"}
|
||||||
assert func(cmd, working_dir, env) == ret
|
assert func(cmd, env=env) == ret
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue