|
| 1 | +name: Tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + pull_request: |
| 7 | + branches: [ main, develop ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + # Unit tests - fast, no external dependencies |
| 11 | + unit-tests: |
| 12 | + name: Unit Tests (Python ${{ matrix.python-version }}) |
| 13 | + runs-on: ubuntu-latest |
| 14 | + strategy: |
| 15 | + matrix: |
| 16 | + python-version: ["3.10", "3.11", "3.12"] |
| 17 | + |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up Python ${{ matrix.python-version }} |
| 22 | + uses: actions/setup-python@v5 |
| 23 | + with: |
| 24 | + python-version: ${{ matrix.python-version }} |
| 25 | + |
| 26 | + - name: Cache pip packages |
| 27 | + uses: actions/cache@v3 |
| 28 | + with: |
| 29 | + path: ~/.cache/pip |
| 30 | + key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }} |
| 31 | + restore-keys: | |
| 32 | + ${{ runner.os }}-pip- |
| 33 | +
|
| 34 | + - name: Install dependencies |
| 35 | + run: | |
| 36 | + python -m pip install --upgrade pip |
| 37 | + pip install -e . |
| 38 | + pip install pytest pytest-cov pytest-mock |
| 39 | +
|
| 40 | + - name: Run unit tests |
| 41 | + run: | |
| 42 | + pytest tests/ -v -m "not integration and not e2e" \ |
| 43 | + --cov=src/openshift_ai_auth \ |
| 44 | + --cov-report=xml \ |
| 45 | + --cov-report=term-missing |
| 46 | +
|
| 47 | + - name: Upload coverage to Codecov |
| 48 | + uses: codecov/codecov-action@v3 |
| 49 | + with: |
| 50 | + files: ./coverage.xml |
| 51 | + flags: unittests |
| 52 | + name: codecov-${{ matrix.python-version }} |
| 53 | + |
| 54 | + # Integration tests - use mock OAuth server |
| 55 | + integration-tests: |
| 56 | + name: Integration Tests |
| 57 | + runs-on: ubuntu-latest |
| 58 | + needs: unit-tests |
| 59 | + |
| 60 | + steps: |
| 61 | + - uses: actions/checkout@v4 |
| 62 | + |
| 63 | + - name: Set up Python 3.11 |
| 64 | + uses: actions/setup-python@v5 |
| 65 | + with: |
| 66 | + python-version: "3.11" |
| 67 | + |
| 68 | + - name: Install dependencies |
| 69 | + run: | |
| 70 | + python -m pip install --upgrade pip |
| 71 | + pip install -e . |
| 72 | + pip install pytest pytest-cov pytest-mock requests |
| 73 | +
|
| 74 | + - name: Run integration tests with mock server |
| 75 | + run: | |
| 76 | + pytest tests/integration/ -v -m integration \ |
| 77 | + --cov=src/openshift_ai_auth \ |
| 78 | + --cov-report=xml \ |
| 79 | + --cov-report=term-missing |
| 80 | +
|
| 81 | + - name: Upload integration coverage |
| 82 | + uses: codecov/codecov-action@v3 |
| 83 | + with: |
| 84 | + files: ./coverage.xml |
| 85 | + flags: integration |
| 86 | + name: codecov-integration |
| 87 | + |
| 88 | + # E2E tests with real Keycloak (optional, slower) |
| 89 | + e2e-tests: |
| 90 | + name: End-to-End Tests |
| 91 | + runs-on: ubuntu-latest |
| 92 | + needs: integration-tests |
| 93 | + if: github.event_name == 'push' || github.event.pull_request.draft == false |
| 94 | + |
| 95 | + services: |
| 96 | + keycloak: |
| 97 | + image: quay.io/keycloak/keycloak:23.0 |
| 98 | + env: |
| 99 | + KEYCLOAK_ADMIN: admin |
| 100 | + KEYCLOAK_ADMIN_PASSWORD: admin |
| 101 | + KC_HTTP_ENABLED: "true" |
| 102 | + KC_HOSTNAME_STRICT: "false" |
| 103 | + KC_HOSTNAME_STRICT_HTTPS: "false" |
| 104 | + ports: |
| 105 | + - 8080:8080 |
| 106 | + options: >- |
| 107 | + --health-cmd "curl -f http://localhost:8080/health/ready || exit 1" |
| 108 | + --health-interval 10s |
| 109 | + --health-timeout 5s |
| 110 | + --health-retries 30 |
| 111 | +
|
| 112 | + steps: |
| 113 | + - uses: actions/checkout@v4 |
| 114 | + |
| 115 | + - name: Set up Python 3.11 |
| 116 | + uses: actions/setup-python@v5 |
| 117 | + with: |
| 118 | + python-version: "3.11" |
| 119 | + |
| 120 | + - name: Install dependencies |
| 121 | + run: | |
| 122 | + python -m pip install --upgrade pip |
| 123 | + pip install -e . |
| 124 | + pip install pytest pytest-mock requests |
| 125 | +
|
| 126 | + - name: Wait for Keycloak |
| 127 | + run: | |
| 128 | + echo "Waiting for Keycloak to be ready..." |
| 129 | + timeout 120 bash -c 'until curl -f http://localhost:8080/health/ready; do sleep 2; done' |
| 130 | +
|
| 131 | + - name: Configure Keycloak test realm |
| 132 | + run: | |
| 133 | + # Get admin token |
| 134 | + TOKEN=$(curl -X POST 'http://localhost:8080/realms/master/protocol/openid-connect/token' \ |
| 135 | + -H 'Content-Type: application/x-www-form-urlencoded' \ |
| 136 | + -d 'username=admin' \ |
| 137 | + -d 'password=admin' \ |
| 138 | + -d 'grant_type=password' \ |
| 139 | + -d 'client_id=admin-cli' \ |
| 140 | + | jq -r '.access_token') |
| 141 | +
|
| 142 | + # Create test realm |
| 143 | + curl -X POST 'http://localhost:8080/admin/realms' \ |
| 144 | + -H "Authorization: Bearer $TOKEN" \ |
| 145 | + -H 'Content-Type: application/json' \ |
| 146 | + -d '{ |
| 147 | + "realm": "test", |
| 148 | + "enabled": true |
| 149 | + }' |
| 150 | +
|
| 151 | + # Create test client |
| 152 | + curl -X POST 'http://localhost:8080/admin/realms/test/clients' \ |
| 153 | + -H "Authorization: Bearer $TOKEN" \ |
| 154 | + -H 'Content-Type: application/json' \ |
| 155 | + -d '{ |
| 156 | + "clientId": "test-client", |
| 157 | + "enabled": true, |
| 158 | + "publicClient": false, |
| 159 | + "secret": "test-secret", |
| 160 | + "redirectUris": ["http://localhost:8080/*"], |
| 161 | + "standardFlowEnabled": true, |
| 162 | + "directAccessGrantsEnabled": true, |
| 163 | + "serviceAccountsEnabled": true |
| 164 | + }' |
| 165 | +
|
| 166 | + - name: Run E2E tests |
| 167 | + env: |
| 168 | + OIDC_ISSUER: http://localhost:8080/realms/test |
| 169 | + OIDC_CLIENT_ID: test-client |
| 170 | + OIDC_CLIENT_SECRET: test-secret |
| 171 | + run: | |
| 172 | + pytest tests/ -v -m e2e --tb=short |
| 173 | +
|
| 174 | + # Lint and format checks |
| 175 | + lint: |
| 176 | + name: Lint & Format |
| 177 | + runs-on: ubuntu-latest |
| 178 | + |
| 179 | + steps: |
| 180 | + - uses: actions/checkout@v4 |
| 181 | + |
| 182 | + - name: Set up Python 3.11 |
| 183 | + uses: actions/setup-python@v5 |
| 184 | + with: |
| 185 | + python-version: "3.11" |
| 186 | + |
| 187 | + - name: Install linting tools |
| 188 | + run: | |
| 189 | + python -m pip install --upgrade pip |
| 190 | + pip install ruff mypy |
| 191 | +
|
| 192 | + - name: Run ruff |
| 193 | + run: | |
| 194 | + ruff check src/ tests/ |
| 195 | +
|
| 196 | + - name: Run mypy |
| 197 | + run: | |
| 198 | + mypy src/openshift_ai_auth --ignore-missing-imports |
| 199 | + continue-on-error: true # Don't fail build on type errors yet |
| 200 | + |
| 201 | + # Security scanning |
| 202 | + security: |
| 203 | + name: Security Scan |
| 204 | + runs-on: ubuntu-latest |
| 205 | + |
| 206 | + steps: |
| 207 | + - uses: actions/checkout@v4 |
| 208 | + |
| 209 | + - name: Run Trivy vulnerability scanner |
| 210 | + uses: aquasecurity/trivy-action@master |
| 211 | + with: |
| 212 | + scan-type: 'fs' |
| 213 | + scan-ref: '.' |
| 214 | + format: 'sarif' |
| 215 | + output: 'trivy-results.sarif' |
| 216 | + |
| 217 | + - name: Upload Trivy results to GitHub Security |
| 218 | + uses: github/codeql-action/upload-sarif@v2 |
| 219 | + with: |
| 220 | + sarif_file: 'trivy-results.sarif' |
0 commit comments