diff --git a/.ci/prepare.sh b/.ci/prepare.sh new file mode 100755 index 00000000..92b62ee9 --- /dev/null +++ b/.ci/prepare.sh @@ -0,0 +1,36 @@ +#!/bin/sh -e +# Install pmbootstrap depends, set up pmos user with sudo +if [ "$(id -u)" != 0 ]; then + echo "ERROR: this script is meant to be executed in the gitlab-ci" + echo "environment only." + exit 1 +fi + +topdir="$(realpath "$(dirname "$0")/..")" +cd "$topdir" + +ln -sf "$PWD"/pmbootstrap.py /usr/local/bin/pmbootstrap + +apk add -q \ + git \ + openssl \ + py3-pytest \ + py3-pytest-cov \ + sudo + +adduser -D pmos +chown -R pmos:pmos . +echo 'pmos ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers + +su pmos -c "git config --global user.email postmarketos-ci@localhost" +su pmos -c "git config --global user.name postmarketOS_CI" + +echo "Initializing pmbootstrap" +if ! su pmos -c "yes '' | pmbootstrap \ + --details-to-stdout \ + init \ + >/tmp/pmb_init 2>&1"; then + cat /tmp/pmb_init + exit 1 +fi +echo "" diff --git a/.ci/pytest.sh b/.ci/pytest.sh new file mode 100755 index 00000000..54e48fbb --- /dev/null +++ b/.ci/pytest.sh @@ -0,0 +1,24 @@ +#!/bin/sh -e +topdir="$(realpath "$(dirname "$0")/..")" +cd "$topdir" + +# Make sure that the work folder format is up to date, and that there are no +# mounts from aborted test cases (#1595) +./pmbootstrap.py work_migrate +./pmbootstrap.py -q shutdown + +# Make sure we have a valid device (#1128) +device="$(./pmbootstrap.py config device)" +pmaports="$(./pmbootstrap.py config aports)" +deviceinfo="$(ls -1 "$pmaports"/device/*/device-"$device"/deviceinfo)" +if ! [ -e "$deviceinfo" ]; then + echo "ERROR: Could not find deviceinfo file for selected device:" \ + "$device" + echo "Expected path: $deviceinfo" + echo "Maybe you have switched to a branch where your device does not" + echo "exist? Use 'pmbootstrap config device qemu-amd64' to switch to" + echo "a valid device." + exit 1 +fi + +pytest -vv -x --cov=pmb test -m "not skip_ci" "$@" diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 25bbebde..98919308 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,20 +3,6 @@ image: alpine:latest -cache: - paths: - - venv - -before_script: - - ./.gitlab/setup-pmos-environment.sh - # venv created in CI_PROJECT_DIR for caching - - "[[ ! -d venv ]] && virtualenv venv -p $(which python3)" - - "source venv/bin/activate" - - "apk -q add py3-pytest-cov py3-coveralls py3-pytest" - - "python3 --version" - - "su pmos -c 'git config --global user.email postmarketos-ci@localhost' || true" - - "su pmos -c 'git config --global user.name postmarketOS_CI' || true" - stages: - checks - tests @@ -38,9 +24,10 @@ stages: static-code-analysis: stage: checks <<: *only-default + before_script: + - .ci/prepare.sh script: - # Note: This script uses CI_PROJECT_DIR - - su pmos -c "CI_PROJECT_DIR=$CI_PROJECT_DIR .gitlab/shared-runner_test-pmbootstrap.sh --static-code-analysis" + - su pmos -c "test/static_code_analysis.sh" vermin: image: alpine:latest @@ -61,18 +48,19 @@ mr-settings: script: - .gitlab/check_mr_settings.py -test-pmbootstrap: +pytest: stage: tests <<: *only-default + before_script: + - .ci/prepare.sh script: - # Note: This script uses CI_PROJECT_DIR - - su pmos -c "CI_PROJECT_DIR=$CI_PROJECT_DIR .gitlab/shared-runner_test-pmbootstrap.sh --testcases-fast" + - su pmos -c .ci/pytest.sh after_script: # Move logs so it can be saved as artifacts - "[[ -f /home/pmos/.local/var/pmbootstrap/log.txt ]] && mv /home/pmos/.local/var/pmbootstrap/log.txt $CI_PROJECT_DIR/log.txt" - "[[ -f /home/pmos/.local/var/pmbootstrap/log_testsuite.txt ]] && mv /home/pmos/.local/var/pmbootstrap/log_testsuite.txt $CI_PROJECT_DIR/log_testsuite.txt" - "[[ -f /home/pmos/.config/pmbootstrap.cfg ]] && cp /home/pmos/.config/pmbootstrap.cfg $CI_PROJECT_DIR/pmbootstrap.cfg" - - "sudo dmesg > $CI_PROJECT_DIR/dmesg.txt" + - "dmesg > $CI_PROJECT_DIR/dmesg.txt" artifacts: when: always paths: diff --git a/.gitlab/config.toml b/.gitlab/config.toml deleted file mode 100644 index b4419ed2..00000000 --- a/.gitlab/config.toml +++ /dev/null @@ -1,22 +0,0 @@ -# 'qemu' gitlab runner configuration file -# Author: Clayton Craft -concurrent = 4 -check_interval = 0 -log_level = "debug" - -[[runners]] - name = "corredor" - url = "https://gitlab.com/" - token = - executor = "virtualbox" - builds_dir = "/home/pmos/builds" - [runners.ssh] - user = "pmos" - password = - identity_file = "/home/pmos/.ssh/id_ecdsa" - [runners.virtualbox] - base_name = "pmbootstrap-vm" - base_snapshot = "ci-snapshot-python-3.6" - #disable_snapshots = false - disable_snapshots = true - [runners.cache] diff --git a/.gitlab/setup-pmos-environment.sh b/.gitlab/setup-pmos-environment.sh deleted file mode 100755 index 0908d772..00000000 --- a/.gitlab/setup-pmos-environment.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -# -# This script is meant for the gitlab CI shared runners, not for -# any specific runners. Specific runners are expected to provide -# all of these configurations to save time, at least for now. -# Author: Clayton Craft - -# skip non-shared runner -[ -d "/home/pmos" ] && echo "pmos user already exists, assume running on pre-configured runner" && exit - -# mount binfmt_misc -mount -t binfmt_misc none /proc/sys/fs/binfmt_misc - -# install dependencies -apk update -apk -q add git sudo bash openssl py3-virtualenv - -# create pmos user -echo "Creating pmos user" -adduser -s /bin/sh -h /home/pmos pmos -chown -R pmos:pmos . -echo 'pmos ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers diff --git a/.gitlab/shared-runner_test-pmbootstrap.sh b/.gitlab/shared-runner_test-pmbootstrap.sh deleted file mode 100755 index aceb9ba4..00000000 --- a/.gitlab/shared-runner_test-pmbootstrap.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# -# This script is meant to be executed by a non-root user, since pmbootstrap -# commands will fail otherwise. This is primarily used by the gitlab CI shared -# runners. -# This script also assumes, if run outside a gitlab CI runner, that cwd is -# the root of the pmbootstrap project. For gitlab CI runners, $CI_PROJECT_DIR -# is used. -# Author: Clayton Craft - -# Return failure on any failure of commands below -set -e - -# Fail quickly if run as root, since other commands here will fail -[[ "$(id -u)" != "0" ]] - -# These are specific to the gitlab CI -[[ $CI_PROJECT_DIR ]] && cd "$CI_PROJECT_DIR" -# shellcheck disable=SC1091 -[[ -d venv ]] && source ./venv/bin/activate - -# Init test (pipefail disabled so 'yes' doesn't fail test) -set +o pipefail -yes ''| ./pmbootstrap.py init -set -o pipefail - -# this seems to be needed for some tests to pass -set +o pipefail -yes | ./pmbootstrap.py zap -m -p -set -o pipefail - -case $1 in - --static-code-analysis ) - ./test/static_code_analysis.sh - ;; - --testcases-fast ) - # testcases_fast (qemu is omitted by not passing --all) - ./test/testcases_fast.sh - ;; -esac diff --git a/README.md b/README.md index 74fc7234..2c75a2b0 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ $ pmbootstrap log_distccd You also need to install the following python packages (pip can be useful if you distribution hasn't got them packaged): * `pytest` -* `pytest-cov` (only needed for `test/testcases_fast.sh`, which is what CI runs) +* `pytest-cov` * `flake8` On Alpine Linux it can be done with: @@ -220,11 +220,11 @@ You can now run `pytest -vv` inside the pmbootstrap folder to run all available CI runs slightly reduces set of tests (it skips tests that require running qemu) by this: ```shell -$ ./test/testcases_fast.sh +$ .ci/pytest.sh ``` This is the easiest way to do the same as CI. -Alternatively you can run a single test if you wish: +Alternatively you can run a single test file if you wish: ```shell $ pytest -vv ./test/test_keys.py ``` diff --git a/test/pytest.ini b/test/pytest.ini new file mode 100644 index 00000000..1b96e6cb --- /dev/null +++ b/test/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +addopts = --strict-markers +markers = + skip_ci diff --git a/test/test_qemu_running_processes.py b/test/test_qemu_running_processes.py index 1e5fc2ff..4993ac3e 100644 --- a/test/test_qemu_running_processes.py +++ b/test/test_qemu_running_processes.py @@ -164,6 +164,7 @@ def is_running(args, programs, timeout=300, sleep_before_retry=1): return False +@pytest.mark.skip_ci def test_none(args, tmpdir, qemu): qemu.run(args, tmpdir) @@ -175,12 +176,14 @@ def test_none(args, tmpdir, qemu): assert is_running(args, ["invalid-process"], 1) is False +@pytest.mark.skip_ci def test_xfce4(args, tmpdir, qemu): qemu.run(args, tmpdir, "xfce4") assert is_running(args, ["xfce4-session", "xfdesktop", "xfce4-panel", "Thunar", "dbus-daemon", "xfwm4"]) +@pytest.mark.skip_ci def test_plasma_mobile(args, tmpdir, qemu): # NOTE: Once we have plasma mobile running properly without GL, we can # check for more processes diff --git a/test/testcases_fast.sh b/test/testcases_fast.sh deleted file mode 100755 index a2682b63..00000000 --- a/test/testcases_fast.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/sh -e -# usage: testcases_fast.sh [--all] - -# Disable QEMU test, it runs on a special CI runner in parallel (see #1610). -disabled="qemu_running_processes" - -# Optionally enable all test cases -if [ "$1" = "--all" ]; then - disabled="" -else - echo "Disabled test case(s): $disabled" - echo "Use '$(basename "$0") --all' to enable all test cases." -fi - -# Make sure that the work folder format is up to date, and that there are no -# mounts from aborted test cases (#1595) -cd "$(dirname "$0")/.." -./pmbootstrap.py work_migrate -./pmbootstrap.py -q shutdown - -# Make sure we have a valid device (#1128) -device="$(./pmbootstrap.py config device)" -pmaports="$(./pmbootstrap.py config aports)" -deviceinfo="$pmaports/device/*/device-$device/deviceinfo" -# shellcheck disable=SC2086 -if ! [ -e $deviceinfo ]; then - echo "ERROR: Could not find deviceinfo file for selected device '$device'." - echo "Expected path: $deviceinfo" - echo "Maybe you have switched to a branch where your device does not exist?" - echo "Use 'pmbootstrap config device qemu-amd64' to switch to a valid device." - exit 1 -fi - -# Filter out disabled testcases -enabled="" -for file in test/test_*.py; do - for test in $disabled; do - [ "test/test_${test}.py" = "$file" ] && continue 2 - done - enabled="$enabled $file" -done - -# Run enabled testcases with coverage enabled -# Note: Pytest is called through python so that this is compatible with -# running from a venv (e.g. in gitlab CI) -# shellcheck disable=SC2086 -python3 -m pytest -vv -x --cov=pmb $enabled --tb=native