test: add unit tests for build views #3312
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: CI for KernelCI Dashboard | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
- reopened | |
- ready_for_review | |
concurrency: | |
group: ci-check-${{ github.ref }} | |
cancel-in-progress: true | |
env: | |
CI: true | |
jobs: | |
setup-node: | |
if: github.event.pull_request.draft != true | |
runs-on: ubuntu-latest | |
timeout-minutes: 10 | |
outputs: | |
paths-node_modules: ${{ steps.paths-node_modules.outputs.paths }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Cache node_modules | |
uses: actions/cache@v4 | |
id: cache-node_modules | |
with: | |
key: node_modules-${{ runner.os }}-${{ hashFiles('./dashboard/pnpm-lock.yaml') }} | |
path: ./dashboard/node_modules | |
- name: Install pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9.15.2 | |
- name: Installing dependencies with pnpm | |
if: steps.cache-node_modules.outputs.cache-hit != 'true' | |
run: pnpm install --frozen-lockfile | |
working-directory: ./dashboard | |
lint-js: | |
if: github.event.pull_request.draft != true | |
needs: setup-node | |
runs-on: ubuntu-latest | |
timeout-minutes: 5 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Fetch node_modules | |
uses: actions/cache@v4 | |
id: fetch-node_modules | |
with: | |
key: node_modules-${{ runner.os }}-${{ hashFiles('./dashboard/pnpm-lock.yaml') }} | |
path: ./dashboard/node_modules | |
- name: Install pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9.15.2 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version-file: './dashboard/.nvmrc' | |
- name: Run eslint | |
run: pnpm lint-staged | |
working-directory: ./dashboard | |
build-front: | |
if: github.event.pull_request.draft != true | |
needs: setup-node | |
runs-on: ubuntu-latest | |
timeout-minutes: 10 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Fetch node_modules | |
uses: actions/cache@v4 | |
id: fetch-node_modules | |
with: | |
key: node_modules-${{ runner.os }}-${{ hashFiles('./dashboard/pnpm-lock.yaml') }} | |
path: ./dashboard/node_modules | |
- name: Install pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9.15.2 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version-file: './dashboard/.nvmrc' | |
- name: Run tests | |
run: pnpm test | |
working-directory: ./dashboard | |
- name: Run build | |
run: pnpm build | |
working-directory: ./dashboard | |
lint-and-unit-test-django: | |
if: github.event.pull_request.draft != true | |
runs-on: ubuntu-latest | |
timeout-minutes: 3 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Backend | |
uses: ./.github/actions/setup-backend | |
- name: Lint | |
run: poetry run flake8 | |
working-directory: ./backend | |
- name: Check Format | |
run: poetry run black --check . | |
working-directory: ./backend | |
- name: Run unit tests with coverage | |
run: | | |
poetry run pytest -m unit --cov=kernelCI_app --cov=kernelCI_cache --cov-report=term-missing | |
cp .coverage coverage-unit.sqlite | |
working-directory: ./backend | |
- name: Upload coverage data (unit) | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-data-unit | |
path: backend/coverage-unit.sqlite | |
integration-test-django: | |
if: github.event.pull_request.draft != true | |
runs-on: ubuntu-latest | |
timeout-minutes: 10 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Backend | |
uses: ./.github/actions/setup-backend | |
- name: Configure credentials variables | |
run: | | |
echo -n "${{ secrets.PG_JSON }}" | base64 --decode > application_default_credentials.json | |
chmod 777 application_default_credentials.json | |
mkdir -p backend/runtime/secrets | |
echo "${{ secrets.PG_PASSWORD }}" > backend/runtime/secrets/postgres_password_secret | |
echo "DB_DEFAULT_USER=${{ secrets.PG_USERNAME }}" >> $GITHUB_ENV | |
echo "DJANGO_SECRET_KEY=$(openssl rand -base64 22)" >> $GITHUB_ENV | |
echo "DB_DEFAULT_NAME=kcidb" >> $GITHUB_ENV | |
cp .env.backend.example .env.backend | |
cp .env.db.example .env.db | |
cp .env.proxy.example .env.proxy | |
- name: Run database proxy and backend docker services | |
run: docker compose up backend -d --build | |
- name: Wait for backend to be ready | |
run: | | |
for i in {1..20}; do | |
BACKEND_STATUS=$(docker ps -q -f "name=dashboard_backend_service" -f "status=running") | |
PROXY_STATUS=$(docker ps -q -f "name=cloudsql-proxy" -f "status=running") | |
if [[ -n "$BACKEND_STATUS" && -n "$PROXY_STATUS" ]] then | |
echo "Backend is ready!" | |
docker ps | |
sleep 5 | |
break | |
fi | |
echo "Waiting for backend to be ready... $i" | |
sleep 5 | |
done | |
- name: Run integration tests with coverage | |
run: | | |
poetry run pytest -m "integration" --run-all --cov=kernelCI_app --cov=kernelCI_cache --cov-report=term-missing | |
cp .coverage coverage-integration.sqlite | |
working-directory: ./backend | |
- name: Upload coverage data (integration) | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-data-integration | |
path: backend/coverage-integration.sqlite | |
- name: Clean containers | |
run: docker compose down --volumes --remove-orphans && docker system prune -af | |
update-coverage-badge: | |
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) | |
runs-on: ubuntu-latest | |
needs: | |
- lint-and-unit-test-django | |
- integration-test-django | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Backend | |
uses: ./.github/actions/setup-backend | |
- name: Download coverage data (unit) | |
uses: actions/download-artifact@v4 | |
with: | |
name: coverage-data-unit | |
- name: Download coverage data (integration) | |
uses: actions/download-artifact@v4 | |
with: | |
name: coverage-data-integration | |
- name: Rename coverage files for combine | |
run: | | |
cp coverage-unit.sqlite backend/.coverage.unit | |
cp coverage-integration.sqlite backend/.coverage.integration | |
ls -la backend/.coverage* | |
- name: Combine coverage and generate json | |
run: | | |
poetry run coverage combine | |
poetry run coverage json -o ${{ github.workspace }}/coverage.json | |
working-directory: ./backend | |
- name: Update Coverage Badge | |
uses: we-cli/coverage-badge-action@main | |
deploy-staging: | |
if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) | |
runs-on: ubuntu-latest | |
needs: | |
- lint-js | |
- build-front | |
- lint-and-unit-test-django | |
- integration-test-django | |
steps: | |
- name: Configure staging host authenticity | |
run: | | |
mkdir -p ~/.ssh/ && chmod 700 ~/.ssh/ | |
touch ~/.ssh/known_hosts && chmod 600 ~/.ssh/known_hosts | |
echo "$SSH_HOSTKEY" > ~/.ssh/known_hosts | |
env: | |
SSH_HOSTKEY: ${{ secrets.STAGING_HOSTKEY }} | |
- name: Deploy to staging | |
run: | | |
eval $(ssh-agent -s) | |
echo "$SSH_KEY" | ssh-add - >/dev/null | |
ssh "${SSH_USER}@${SSH_HOST}" "deploy ${GITHUB_SHA}" | |
env: | |
SSH_USER: ${{ secrets.STAGING_USER }} | |
SSH_HOST: ${{ secrets.STAGING_HOST }} | |
SSH_KEY: ${{ secrets.STAGING_KEY }} |