206 lines
6.2 KiB
YAML
206 lines
6.2 KiB
YAML
|
|
# Tekton Test Task for Bakery-IA CI/CD
|
||
|
|
# This task runs unit tests and linting for changed services
|
||
|
|
|
||
|
|
apiVersion: tekton.dev/v1beta1
|
||
|
|
kind: Task
|
||
|
|
metadata:
|
||
|
|
name: run-tests
|
||
|
|
namespace: tekton-pipelines
|
||
|
|
labels:
|
||
|
|
app.kubernetes.io/name: bakery-ia-cicd
|
||
|
|
app.kubernetes.io/component: test
|
||
|
|
spec:
|
||
|
|
workspaces:
|
||
|
|
- name: source
|
||
|
|
description: Source code workspace
|
||
|
|
params:
|
||
|
|
- name: services
|
||
|
|
type: string
|
||
|
|
description: Comma-separated list of services to test
|
||
|
|
- name: skip-lint
|
||
|
|
type: string
|
||
|
|
description: Skip linting if "true"
|
||
|
|
default: "false"
|
||
|
|
- name: skip-tests
|
||
|
|
type: string
|
||
|
|
description: Skip tests if "true"
|
||
|
|
default: "false"
|
||
|
|
results:
|
||
|
|
- name: test-status
|
||
|
|
description: Overall test status (passed/failed/skipped)
|
||
|
|
- name: tested-services
|
||
|
|
description: List of services that were tested
|
||
|
|
- name: failed-services
|
||
|
|
description: List of services that failed tests
|
||
|
|
steps:
|
||
|
|
- name: run-tests
|
||
|
|
image: python:3.11-slim
|
||
|
|
script: |
|
||
|
|
#!/bin/bash
|
||
|
|
set -e
|
||
|
|
|
||
|
|
SOURCE_PATH="$(workspaces.source.path)"
|
||
|
|
SERVICES="$(params.services)"
|
||
|
|
SKIP_LINT="$(params.skip-lint)"
|
||
|
|
SKIP_TESTS="$(params.skip-tests)"
|
||
|
|
|
||
|
|
TESTED_SERVICES=""
|
||
|
|
FAILED_SERVICES=""
|
||
|
|
OVERALL_STATUS="passed"
|
||
|
|
|
||
|
|
cd "$SOURCE_PATH"
|
||
|
|
|
||
|
|
echo "============================================"
|
||
|
|
echo "Running Tests"
|
||
|
|
echo "============================================"
|
||
|
|
echo "Services: $SERVICES"
|
||
|
|
echo "Skip Lint: $SKIP_LINT"
|
||
|
|
echo "Skip Tests: $SKIP_TESTS"
|
||
|
|
echo "============================================"
|
||
|
|
|
||
|
|
# Skip if no services to test
|
||
|
|
if [ "$SERVICES" = "none" ] || [ -z "$SERVICES" ]; then
|
||
|
|
echo "No services to test, skipping..."
|
||
|
|
echo "skipped" > $(results.test-status.path)
|
||
|
|
echo "none" > $(results.tested-services.path)
|
||
|
|
echo "none" > $(results.failed-services.path)
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Install common test dependencies
|
||
|
|
echo ""
|
||
|
|
echo "Installing test dependencies..."
|
||
|
|
pip install --quiet pytest pytest-cov pytest-asyncio ruff mypy 2>/dev/null || true
|
||
|
|
|
||
|
|
# Convert comma-separated list to space-separated
|
||
|
|
SERVICES_LIST=$(echo "$SERVICES" | tr ',' ' ')
|
||
|
|
|
||
|
|
for SERVICE in $SERVICES_LIST; do
|
||
|
|
# Trim whitespace
|
||
|
|
SERVICE=$(echo "$SERVICE" | tr -d ' ')
|
||
|
|
|
||
|
|
# Skip infrastructure changes
|
||
|
|
if [ "$SERVICE" = "infrastructure" ]; then
|
||
|
|
echo "Skipping infrastructure (not testable)"
|
||
|
|
continue
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "--------------------------------------------"
|
||
|
|
echo "Testing service: $SERVICE"
|
||
|
|
echo "--------------------------------------------"
|
||
|
|
|
||
|
|
# Determine service path
|
||
|
|
if [ "$SERVICE" = "frontend" ]; then
|
||
|
|
SERVICE_PATH="$SOURCE_PATH/frontend"
|
||
|
|
elif [ "$SERVICE" = "gateway" ]; then
|
||
|
|
SERVICE_PATH="$SOURCE_PATH/gateway"
|
||
|
|
else
|
||
|
|
SERVICE_PATH="$SOURCE_PATH/services/$SERVICE"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Check if service exists
|
||
|
|
if [ ! -d "$SERVICE_PATH" ]; then
|
||
|
|
echo "Warning: Service directory not found: $SERVICE_PATH"
|
||
|
|
continue
|
||
|
|
fi
|
||
|
|
|
||
|
|
cd "$SERVICE_PATH"
|
||
|
|
SERVICE_FAILED=false
|
||
|
|
|
||
|
|
# Install service-specific dependencies if requirements.txt exists
|
||
|
|
if [ -f "requirements.txt" ]; then
|
||
|
|
echo "Installing service dependencies..."
|
||
|
|
pip install --quiet -r requirements.txt 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Run linting (ruff)
|
||
|
|
if [ "$SKIP_LINT" != "true" ]; then
|
||
|
|
echo ""
|
||
|
|
echo "Running linter (ruff)..."
|
||
|
|
if [ -d "app" ]; then
|
||
|
|
ruff check app/ --output-format=text 2>&1 || {
|
||
|
|
echo "Linting failed for $SERVICE"
|
||
|
|
SERVICE_FAILED=true
|
||
|
|
}
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Run tests
|
||
|
|
if [ "$SKIP_TESTS" != "true" ]; then
|
||
|
|
echo ""
|
||
|
|
echo "Running tests (pytest)..."
|
||
|
|
if [ -d "tests" ]; then
|
||
|
|
pytest tests/ -v --tb=short 2>&1 || {
|
||
|
|
echo "Tests failed for $SERVICE"
|
||
|
|
SERVICE_FAILED=true
|
||
|
|
}
|
||
|
|
elif [ -d "app/tests" ]; then
|
||
|
|
pytest app/tests/ -v --tb=short 2>&1 || {
|
||
|
|
echo "Tests failed for $SERVICE"
|
||
|
|
SERVICE_FAILED=true
|
||
|
|
}
|
||
|
|
else
|
||
|
|
echo "No tests directory found, skipping tests"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Track results
|
||
|
|
if [ -z "$TESTED_SERVICES" ]; then
|
||
|
|
TESTED_SERVICES="$SERVICE"
|
||
|
|
else
|
||
|
|
TESTED_SERVICES="$TESTED_SERVICES,$SERVICE"
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ "$SERVICE_FAILED" = true ]; then
|
||
|
|
OVERALL_STATUS="failed"
|
||
|
|
if [ -z "$FAILED_SERVICES" ]; then
|
||
|
|
FAILED_SERVICES="$SERVICE"
|
||
|
|
else
|
||
|
|
FAILED_SERVICES="$FAILED_SERVICES,$SERVICE"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
cd "$SOURCE_PATH"
|
||
|
|
done
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "============================================"
|
||
|
|
echo "Test Summary"
|
||
|
|
echo "============================================"
|
||
|
|
echo "Tested services: $TESTED_SERVICES"
|
||
|
|
echo "Failed services: $FAILED_SERVICES"
|
||
|
|
echo "Overall status: $OVERALL_STATUS"
|
||
|
|
|
||
|
|
# Write results
|
||
|
|
echo "$OVERALL_STATUS" > $(results.test-status.path)
|
||
|
|
|
||
|
|
if [ -z "$TESTED_SERVICES" ]; then
|
||
|
|
echo "none" > $(results.tested-services.path)
|
||
|
|
else
|
||
|
|
echo "$TESTED_SERVICES" > $(results.tested-services.path)
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ -z "$FAILED_SERVICES" ]; then
|
||
|
|
echo "none" > $(results.failed-services.path)
|
||
|
|
else
|
||
|
|
echo "$FAILED_SERVICES" > $(results.failed-services.path)
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Exit with error if tests failed
|
||
|
|
if [ "$OVERALL_STATUS" = "failed" ]; then
|
||
|
|
echo ""
|
||
|
|
echo "ERROR: Some tests failed!"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "All tests passed!"
|
||
|
|
resources:
|
||
|
|
limits:
|
||
|
|
cpu: 1000m
|
||
|
|
memory: 2Gi
|
||
|
|
requests:
|
||
|
|
cpu: 500m
|
||
|
|
memory: 1Gi
|