Skip to content

Commit

Permalink
Adds tox.ini and basic rock test
Browse files Browse the repository at this point in the history
Adds the following tox targets: fmt, lint, pack, export-to-docker,
sanity, integration.

Adds basic rock test, checking that the expected files exist in the
rock, and checking the added executables outputs.
  • Loading branch information
claudiubelu committed Jul 16, 2024
1 parent 0699c7f commit 61e7f85
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/test_rock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

import pathlib
import subprocess

import pytest
import yaml

ROCK_EXPECTED_FILES = [
"/install-cni.sh",
"/ip-control-loop",
"/whereabouts",
]

METADATA = yaml.safe_load(pathlib.Path("./rockcraft.yaml").read_text())
# Assuming rock name is the same as application name.
ROCK_NAME = METADATA["name"]
VERSION = METADATA["version"]
LOCAL_ROCK_IMAGE = f"{ROCK_NAME}:{VERSION}"


def _run_in_docker(check_exit_code, *command):
return subprocess.run(
[
"docker",
"run",
"--rm",
"-i",
LOCAL_ROCK_IMAGE,
"exec",
*command,
],
check=check_exit_code,
capture_output=True,
)


@pytest.mark.abort_on_fail
def test_rock():
"""Test rock."""
# check rock filesystem
_run_in_docker(True, "ls", "-la", *ROCK_EXPECTED_FILES)

# check binary name and version.
process = _run_in_docker(True, f"/{ROCK_NAME}", "version")
output = process.stderr.decode("utf-8")
assert ROCK_NAME in output and VERSION in output

# check other binary. It expects KUBERNETES_SERVICE_HOST to be defined.
process = _run_in_docker(False, "/ip-control-loop")
output = process.stderr.decode("utf-8")
assert "KUBERNETES_SERVICE_HOST" in output

# check script. It expects serviceaccount token to exist.
process = _run_in_docker(False, "/install-cni.sh")
output = process.stderr.decode("utf-8")
expected_error = (
"cat: /var/run/secrets/kubernetes.io/serviceaccount/token: "
"No such file or directory"
)
assert expected_error == output.strip()
92 changes: 92 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

[tox]
skipsdist = True
skip_missing_interpreters = True
envlist = fmt, lint, pack, export-to-docker, sanity, integration

[testenv]
setenv =
PYTHONPATH={toxinidir}
PYTHONBREAKPOINT=ipdb.set_trace

[testenv:fmt]
description = Apply coding style standards to code
deps =
black
isort
commands =
isort {toxinidir}/tests
black {toxinidir}/tests

[testenv:lint]
description = Check code against coding style standards
deps =
black
flake8
pep8-naming
isort
commands =
flake8 tests
isort --check-only --diff {toxinidir}/tests
black --check --diff {toxinidir}/tests

[testenv:pack]
passenv = *
allowlist_externals =
rockcraft
commands =
rockcraft pack

[testenv:export-to-docker]
passenv = *
allowlist_externals =
bash
skopeo
yq
commands =
# export already packed rock to docker
bash -c 'NAME=$(yq -r .name rockcraft.yaml) && \
VERSION=$(yq -r .version rockcraft.yaml) && \
ARCH=$(yq -r ".platforms | keys | .[0]" rockcraft.yaml) && \
ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}.rock" && \
DOCKER_IMAGE=$NAME:$VERSION && \\
echo "Exporting $ROCK to docker as $DOCKER_IMAGE" && \
rockcraft.skopeo --insecure-policy copy oci-archive:$ROCK docker-daemon:$DOCKER_IMAGE'

[testenv:sanity]
passenv = *
deps =
pytest
pyyaml
allowlist_externals =
echo
commands =
# run rock tests
pytest -v --tb native --show-capture=all --log-cli-level=INFO {posargs} {toxinidir}/tests

[testenv:integration]
passenv = *
allowlist_externals =
echo
bash
git
rm
tox
deps =
juju<4.0
pytest
pytest-operator
ops
commands =
echo "WARNING: This is a placeholder test - no test is implemented here."

[flake8]
max-line-length = 120
select = E,W,F,C,N
# E231: missing whitespace after ':'
# E231 excluded for false positives in strings and fstrings.
ignore = W503,E231
exclude = venv,.git,.tox,.tox_env,.venv,build,dist,*.egg_info
show-source = true

0 comments on commit 61e7f85

Please sign in to comment.