kupferbootstrap/integration_tests.py

88 lines
3 KiB
Python
Raw Normal View History

2022-09-22 06:31:57 +02:00
import click
import os
import pytest
from glob import glob
from subprocess import CompletedProcess
2022-09-22 06:31:57 +02:00
from config.state import config, CONFIG_DEFAULTS
2022-09-22 06:31:57 +02:00
from constants import SRCINFO_METADATA_FILE
from exec.cmd import run_cmd
2022-09-22 06:31:57 +02:00
from exec.file import get_temp_dir
from logger import setup_logging
from packages.cli import cmd_build, cmd_clean, cmd_update
2022-09-22 06:31:57 +02:00
from utils import git_get_branch
tempdir = None
config.try_load_file()
setup_logging(True)
@pytest.fixture()
def ctx() -> click.Context:
global tempdir
if not tempdir:
tempdir = get_temp_dir()
2022-09-22 06:31:57 +02:00
if not os.environ.get('INTEGRATION_TESTS_USE_GLOBAL_CONFIG', 'false').lower() == 'true':
config.file.paths.update(CONFIG_DEFAULTS.paths | {'cache_dir': tempdir})
config_path = os.path.join(tempdir, 'kupferbootstrap.toml')
config.runtime.config_file = config_path
if not os.path.exists(config_path):
config.write()
config.try_load_file(config_path)
print(f'cache_dir: {config.file.paths.cache_dir}')
2022-09-22 06:31:57 +02:00
return click.Context(click.Command('integration_tests'))
def test_config_load(ctx: click.Context):
path = config.runtime.config_file
assert path
assert path.startswith('/tmp/')
assert os.path.exists(path)
config.enforce_config_loaded()
2022-09-22 06:31:57 +02:00
def test_packages_update(ctx: click.Context):
pkgbuilds_path = config.get_path('pkgbuilds')
kbs_branch = git_get_branch(config.runtime.script_source_dir)
# Gitlab CI integration: the CI checks out a detached commit, branch comes back empty.
if not kbs_branch and os.environ.get('CI', 'false') == 'true':
kbs_branch = os.environ.get('CI_COMMIT_BRANCH', '')
branches: dict[str, bool] = {'main': False, 'dev': False}
if kbs_branch:
branches[kbs_branch] = True
for branch, may_fail in branches.items():
2022-09-22 06:31:57 +02:00
config.file.pkgbuilds.git_branch = branch
try:
ctx.invoke(cmd_update, non_interactive=True, switch_branch=True, discard_changes=True)
2022-09-22 06:31:57 +02:00
except Exception as ex:
print(f'may_fail: {may_fail}; Exception: {ex}')
2022-09-22 06:31:57 +02:00
if not may_fail:
raise ex
# check branch really doesn't exist
res = run_cmd(f"git ls-remote {CONFIG_DEFAULTS.pkgbuilds.git_repo} 'refs/heads/*' | grep 'refs/heads/{branch}'")
assert isinstance(res, CompletedProcess)
assert res.returncode != 0
continue
assert git_get_branch(pkgbuilds_path) == branch
2022-09-22 06:31:57 +02:00
def test_packages_clean(ctx: click.Context):
if not glob(os.path.join(config.get_path('pkgbuilds'), '*', '*', SRCINFO_METADATA_FILE)):
ctx.invoke(cmd_update, non_interactive=True)
ctx.invoke(cmd_clean, what=['git'], force=True)
def build_pkgs(_ctx: click.Context, query: list[str], arch: str = 'aarch64'):
_ctx.invoke(cmd_build, paths=query, arch=arch)
def test_packages_build_by_path(ctx: click.Context):
name = 'device/device-sdm845-oneplus-enchilada'
build_pkgs(ctx, [name])
def test_split_package_build_by_name(ctx: click.Context):
name = 'device-sdm845-xiaomi-beryllium-ebbg'
build_pkgs(ctx, [name])