Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: initial implementation of top-level pytest tests #433

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ jobs:
exit 1
fi

pytest-tests:
runs-on: ubuntu-latest
env:
poetry_version: '1.8.3'
steps:
- uses: actions/checkout@v4

- name: Cache poetry in ~/.local
uses: actions/cache/restore@v4
id: cache-poetry-restore
with:
path: ~/.local
key: "${{ runner.os }}-local-${{ env.poetry_version }}"

- name: Install poetry
if: steps.cache-poetry-restore.outputs.cache-hit != 'true'
run: pip install poetry==${{ env.poetry_version }}

- name: Save cache
if: steps.cache-poetry-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.local
key: ${{ steps.cache-poetry-restore.outputs.cache-primary-key }}

- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.12'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether such new python version won't collide with possible future basic functional tests to test actual python packages? Shouldn't we rather stick to what we have in 2024a branch, that means Python 3.11? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.12 is not a problem. I think that that would be a valid comment for what @paulovmr is doing in

(and he has that handled, in his PR there's a separate env for every image)

Here, if we wanted to run something inside the images, which is the only meaningful way to do functional tests of packages installed in images, then we would naturally not use the python, package version, or lock file maintained in the top level by poetry, but we'd run pip install the same way as the current makefile tests install papermill, etc.

cache: 'poetry'

- name: Configure poetry
run: poetry env use "${{ steps.setup-python.outputs.python-path }}"

- name: Install deps
run: poetry install --sync

- run: poetry run pytest

code-static-analysis:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ podman run -it -p 8888:8888 quay.io/opendatahub/workbench-images:jupyter-mini

### Deploy & Test

#### Running Python selftests in Pytest

```shell
pip install poetry
poetry env use /usr/bin/python3.12
poetry config virtualenvs.in-project true
poetry install --sync

poetry run pytest
```

#### Notebooks

Deploy the notebook images in your Kubernetes environment using:
Expand Down
108 changes: 108 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "notebooks"
version = "2024.1"
authors = []
description = "Open Data Hub / OpenShift AI Notebook / Workbench images, and tests for the same in Python."
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "~3.12"


[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"
pytest-subtests = "^0.12.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
17 changes: 17 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://docs.pytest.org/en/7.1.x/reference/reference.html#configuration-options

[pytest]
addopts = --strict-markers --capture=no --tb=short

required_plugins = pytest-subtests

junit_logging = all
junit_log_passing_tests = False

log_cli = True
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = INFO

log_file = logs/pytest-logs.txt
log_file_level = DEBUG
Empty file added tests/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import pathlib
import tomllib
from typing import TYPE_CHECKING

if TYPE_CHECKING:
import pytest_subtests

PROJECT_ROOT = pathlib.Path(__file__).parent.parent


def test_image_pipfiles(subtests: pytest_subtests.plugin.SubTests):
for file in PROJECT_ROOT.glob("**/Pipfile"):
with subtests.test(msg="checking Pipfile", pipfile=file):
directory = file.parent # "ubi9-python-3.9"
ubi, lang, python = directory.name.split("-")

with open(file, "rb") as fp:
pipfile = tomllib.load(fp)
assert "requires" in pipfile, "Pipfile is missing a [[requires]] section"
assert pipfile["requires"]["python_version"] == python, "Pipfile does not declare the expected Python version"