Test py-allspice in CI #1122
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test py-allspice in CI | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
schedule: | |
- cron: "0 5 * * *" | |
jobs: | |
test: | |
name: Test py-allspice | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
python-version: ["3.10", "3.11", "3.12"] | |
if: github.repository == 'AllSpiceIO/py-allspice' | |
services: | |
postgres: | |
image: postgres:12 | |
env: | |
POSTGRES_USER: allspice_hub | |
POSTGRES_PASSWORD: allspice | |
POSTGRES_DB: allspice_hub | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
allspice_hub: | |
image: allspice/allspice-hub:latest | |
credentials: | |
username: ${{ secrets.DOCKER_PULL_USER }} | |
password: ${{ secrets.DOCKER_PULL_TOKEN }} | |
env: | |
USER_UID: 1000 | |
USER_GID: 1000 | |
RUN_USER: git | |
# This is required to bypass allspice_hub's "setup" page for a new install. | |
# We'll create an account and token in the "Set up allspice_hub" step. | |
ALLSPICE__security__INSTALL_LOCK: true | |
# This isn't noted in the allspice hub docs for docker-compose, but | |
# without it the container will fail to start. | |
ALLSPICE__database__DB_TYPE: postgres | |
ALLSPICE__database__HOST: postgres:5432 | |
ALLSPICE__database__NAME: allspice_hub | |
ALLSPICE__database__USER: allspice_hub | |
ALLSPICE__database__PASSWD: allspice | |
ports: | |
- 3000:3000 | |
options: >- | |
--health-start-period=10s | |
--health-cmd="curl --fail http://localhost:3000/ || exit 1" | |
--health-interval=10s | |
--health-timeout=10s | |
--health-retries=5 | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
pip install -r requirements-test.txt | |
- name: Check formatting | |
run: | | |
ruff format --diff . | |
- name: Lint with ruff | |
run: | | |
ruff check --target-version=py310 . | |
- name: Set up allspice_hub | |
run: | | |
# The only way to create a user is to either use the CLI or the web | |
# interface, so we'll use curl to create a user and then use the API | |
# to create a token. | |
curl 'http://localhost:3000/user/sign_up' \ | |
-H 'Content-Type: application/x-www-form-urlencoded' \ | |
--data-raw 'user_name=test&email=secondarytest%40test.org&password=password&retype=password' | |
curl 'http://localhost:3000/api/v1/users/test/tokens' \ | |
-H "Content-Type: application/json" \ | |
-d '{"name":"test", "scopes": ["all"]}' \ | |
-u test:password > token.json | |
python -c "import json; print(json.load(open('token.json'))['sha1'])" > .token | |
# This token is only used with the job's container, so it is safe to | |
# echo it here. | |
echo "AllSpice Hub token = $(cat .token)" | |
# If the test is running on a schedule, the purpose of the test is to | |
# ensure py-allspice isn't out of date with the current version of hub, so | |
# we'll ignore the casettes. | |
# | |
# If this test run fails, we should update the cassettes manually and | |
# check the changes to see what needs to be changed. | |
- name: Test with pytest without using casettes | |
if: ${{ github.event_name == 'schedule' }} | |
run: | | |
python -m pytest --disable-recording | |
# Otherwise, we'll use the casettes to make the tests fast. | |
- name: Test with pytest using casettes | |
if: ${{ github.event_name != 'schedule' }} | |
# We'll also generate code coverage, which we will later turn into a | |
# coverage report if this pipeline is running on a pull request. | |
run: | | |
python -m coverage run --branch -m pytest --record-mode=none | |
- name: Generate coverage report | |
if: ${{ github.event_name == 'pull_request' }} | |
run: | | |
python -m coverage xml | |
git fetch origin ${{ github.base_ref }} --depth=2 | |
python ./scripts/report_coverage.py coverage.xml origin/${{ github.base_ref }} --output coverage.md | |
- name: Upload coverage report | |
# Only upload one of the coverage reports, since they should be the same. | |
if: ${{ github.event_name == 'pull_request' && matrix.python-version == '3.12' && github.actor != 'dependabot[bot]' }} | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message-path: coverage.md | |
- name: Check attribute types | |
# This script will re-generate the attribute types and check if they | |
# are different from the committed version. The formatter and linter | |
# runs ensure that the differences aren't due to either, and we've | |
# already verified that the committed code passes both. | |
run: | | |
python -m pip install -e . | |
python ./scripts/generate_attribute_types.py | |
ruff format . | |
ruff check --fix . | |
git diff --exit-code ./allspice/apiobject.py | |
# We might want to consider moving this to a new workflow if we add addition test jobs | |
notifications: | |
name: Notifications | |
runs-on: ubuntu-22.04 | |
needs: [test] | |
if: always() | |
steps: | |
- name: Slack Notification | |
uses: slackapi/slack-github-action@v2 | |
with: | |
webhook: ${{ secrets.SLACK_WEBHOOK_DEV_CI }} | |
webhook-type: incoming-webhook | |
payload: | | |
attachments: | |
- color: "${{ needs.test.result == 'success' && 'good' || needs.test.result == 'failure' && 'danger' || 'warning' }}" | |
fields: | |
- title: "py-allspice CI: ${{ needs.test.result }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" | |
value: "GitHub Action build result: ${{ needs.test.result }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" |