150 lines
4.9 KiB
YAML
150 lines
4.9 KiB
YAML
name: Run Tests
|
|
|
|
on:
|
|
push:
|
|
branches: [main, dev]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
jobs:
|
|
backend-tests:
|
|
name: Backend Tests
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
SECRET_KEY: test-secret-key-for-ci-environment-32chars-minimum
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
run: |
|
|
git clone --depth 1 http://gitea:3000/jimmy/nas-tools.git .
|
|
git fetch --depth 1 origin ${{ github.sha }}
|
|
git checkout ${{ github.sha }}
|
|
|
|
- name: Setup Python
|
|
run: |
|
|
python3 --version
|
|
pip3 --version
|
|
|
|
- name: Cache Python dependencies
|
|
id: cache-python
|
|
run: |
|
|
CACHE_KEY="python-$(cat dashboard/backend/requirements.txt dashboard/backend/requirements-dev.txt | md5sum | cut -d' ' -f1)"
|
|
CACHE_DIR="/tmp/pytest-cache/$CACHE_KEY"
|
|
PIP_CACHE_DIR="/tmp/pip-cache"
|
|
echo "CACHE_DIR=$CACHE_DIR" >> $GITHUB_ENV
|
|
echo "CACHE_KEY=$CACHE_KEY" >> $GITHUB_ENV
|
|
echo "PIP_CACHE_DIR=$PIP_CACHE_DIR" >> $GITHUB_ENV
|
|
mkdir -p "$PIP_CACHE_DIR"
|
|
if [ -d "$CACHE_DIR" ]; then
|
|
echo "Cache hit for $CACHE_KEY"
|
|
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Cache miss for $CACHE_KEY"
|
|
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
|
mkdir -p "$CACHE_DIR"
|
|
fi
|
|
|
|
- name: Install dependencies
|
|
run: |
|
|
cd dashboard/backend
|
|
if [ "${{ steps.cache-python.outputs.cache-hit }}" = "true" ]; then
|
|
echo "Restoring venv from cache $CACHE_KEY..."
|
|
cp -a $CACHE_DIR/venv .
|
|
else
|
|
echo "Installing fresh dependencies..."
|
|
python3 -m venv venv
|
|
. venv/bin/activate
|
|
# Use Tsinghua PyPI mirror for faster downloads in China
|
|
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
|
|
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements-dev.txt
|
|
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple pytest-timeout
|
|
# Save to cache
|
|
echo "Saving venv to cache $CACHE_KEY..."
|
|
cp -a venv $CACHE_DIR/
|
|
fi
|
|
|
|
- name: Run unit tests
|
|
run: |
|
|
cd dashboard/backend
|
|
. venv/bin/activate
|
|
pytest tests/unit/ -v --timeout=30 -x
|
|
|
|
- name: Run integration tests
|
|
run: |
|
|
cd dashboard/backend
|
|
. venv/bin/activate
|
|
pytest tests/integration/ -v --timeout=30 -x
|
|
|
|
frontend-tests:
|
|
name: Frontend Tests
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
run: |
|
|
git clone --depth 1 http://gitea:3000/jimmy/nas-tools.git .
|
|
git fetch --depth 1 origin ${{ github.sha }}
|
|
git checkout ${{ github.sha }}
|
|
|
|
- name: Setup Node.js
|
|
run: |
|
|
node --version
|
|
npm --version
|
|
|
|
- name: Cache Node dependencies
|
|
id: cache-node
|
|
run: |
|
|
CACHE_KEY="node-$(md5sum dashboard/frontend/package-lock.json | cut -d' ' -f1)"
|
|
CACHE_DIR="/tmp/npm-cache/$CACHE_KEY"
|
|
echo "CACHE_DIR=$CACHE_DIR" >> $GITHUB_ENV
|
|
echo "CACHE_KEY=$CACHE_KEY" >> $GITHUB_ENV
|
|
if [ -d "$CACHE_DIR" ]; then
|
|
echo "Cache hit for $CACHE_KEY"
|
|
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Cache miss for $CACHE_KEY"
|
|
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
|
mkdir -p "$CACHE_DIR"
|
|
fi
|
|
|
|
- name: Install dependencies
|
|
env:
|
|
NODE_OPTIONS: "--max-old-space-size=2048"
|
|
run: |
|
|
cd dashboard/frontend
|
|
if [ "${{ steps.cache-node.outputs.cache-hit }}" = "true" ]; then
|
|
echo "Restoring from cache $CACHE_KEY..."
|
|
cp -a $CACHE_DIR/node_modules .
|
|
else
|
|
echo "Installing fresh dependencies..."
|
|
npm ci
|
|
# Save to cache
|
|
echo "Saving to cache $CACHE_KEY..."
|
|
cp -a node_modules $CACHE_DIR/
|
|
fi
|
|
|
|
- name: Run tests
|
|
env:
|
|
NODE_OPTIONS: "--max-old-space-size=2048"
|
|
run: |
|
|
cd dashboard/frontend
|
|
npm run test:coverage -- --reporter=verbose --run --pool=forks --poolOptions.forks.maxForks=2
|
|
|
|
test-summary:
|
|
name: Test Summary
|
|
runs-on: ubuntu-latest
|
|
needs: [backend-tests, frontend-tests]
|
|
if: always()
|
|
|
|
steps:
|
|
- name: Check job results
|
|
run: |
|
|
echo "Backend tests: ${{ needs.backend-tests.result }}"
|
|
echo "Frontend tests: ${{ needs.frontend-tests.result }}"
|
|
|
|
if [ "${{ needs.backend-tests.result }}" != "success" ] || [ "${{ needs.frontend-tests.result }}" != "success" ]; then
|
|
echo "❌ Some tests failed"
|
|
exit 1
|
|
fi
|
|
echo "✅ All tests passed"
|