|
| 1 | +# CTS Integration Tests for Konflux CI |
| 2 | + |
| 3 | +This directory contains integration test scenarios for the Compose Tracking Service (CTS) that run automatically in Konflux CI after each build. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The integration test scenario validates that the built CTS container image works correctly by: |
| 8 | + |
| 9 | +1. **Deploying a test environment** - Creates a PostgreSQL database and deploys the CTS service |
| 10 | +2. **Running integration tests** - Validates API endpoints and service functionality |
| 11 | +3. **Cleaning up** - Removes all test resources |
| 12 | + |
| 13 | +## Files |
| 14 | + |
| 15 | +### Integration Test Scenario |
| 16 | + |
| 17 | +- **`cts-integration-test.yaml`** - IntegrationTestScenario CRD that defines when and how tests run |
| 18 | + - Runs automatically after successful builds |
| 19 | + - References the test pipeline |
| 20 | + - Marks failed builds if tests fail |
| 21 | + |
| 22 | +### Test Pipelines |
| 23 | + |
| 24 | +- **`integration-test-k8s.yaml`** - Main integration test pipeline (recommended) |
| 25 | + - Deploys PostgreSQL database |
| 26 | + - Deploys CTS service with the built image |
| 27 | + - Runs comprehensive API tests |
| 28 | + - Automatically cleans up resources |
| 29 | + |
| 30 | +- **`integration-test-simple.yaml`** - Simplified validation pipeline |
| 31 | + - Only validates image structure and metadata |
| 32 | + - Faster but less comprehensive |
| 33 | + - Useful for quick smoke tests |
| 34 | + |
| 35 | +- **`integration-test-pipeline.yaml`** - Alternative podman-based approach |
| 36 | + - Uses podman-in-podman for testing |
| 37 | + - More complex, kept for reference |
| 38 | + |
| 39 | +### Test Scripts |
| 40 | + |
| 41 | +- **`../tests/integration_test.py`** - Standalone integration test script |
| 42 | + - Can be run locally or in CI |
| 43 | + - Tests all major API endpoints |
| 44 | + - Usage: `CTS_URL=http://localhost:5005 python3 tests/integration_test.py` |
| 45 | + |
| 46 | +## What Gets Tested |
| 47 | + |
| 48 | +The integration tests validate: |
| 49 | + |
| 50 | +- ✅ **Service Startup** - Container starts without errors |
| 51 | +- ✅ **Database Connectivity** - Migrations run successfully |
| 52 | +- ✅ **API Endpoints** - All major endpoints respond correctly: |
| 53 | + - `GET /api/1/` - API root |
| 54 | + - `GET /api/1/about/` - Version information |
| 55 | + - `GET /api/1/composes/` - Compose listing |
| 56 | + - `GET /api/1/openapi.json` - OpenAPI specification |
| 57 | +- ✅ **Error Handling** - 404 responses for invalid endpoints |
| 58 | +- ✅ **Pagination** - Query parameters work correctly |
| 59 | + |
| 60 | +## How It Works |
| 61 | + |
| 62 | +### In Konflux CI |
| 63 | + |
| 64 | +1. Code is pushed to the repository or PR is created |
| 65 | +2. Tekton build pipeline builds the container image |
| 66 | +3. **IntegrationTestScenario** automatically triggers |
| 67 | +4. Test pipeline deploys and validates the image |
| 68 | +5. Results are reported back to the PR/commit |
| 69 | + |
| 70 | +### Pipeline Execution Flow |
| 71 | + |
| 72 | +``` |
| 73 | +┌─────────────────────────────┐ |
| 74 | +│ 1. Parse Snapshot │ Extract image URL from Konflux snapshot |
| 75 | +└─────────────┬───────────────┘ |
| 76 | + │ |
| 77 | +┌─────────────▼───────────────┐ |
| 78 | +│ 2. Deploy PostgreSQL DB │ Create database for testing |
| 79 | +└─────────────┬───────────────┘ |
| 80 | + │ |
| 81 | +┌─────────────▼───────────────┐ |
| 82 | +│ 3. Deploy CTS Service │ Deploy CTS with test image |
| 83 | +└─────────────┬───────────────┘ |
| 84 | + │ |
| 85 | +┌─────────────▼───────────────┐ |
| 86 | +│ 4. Run Integration Tests │ Execute API validation tests |
| 87 | +└─────────────┬───────────────┘ |
| 88 | + │ |
| 89 | +┌─────────────▼───────────────┐ |
| 90 | +│ 5. Cleanup (always runs) │ Remove test resources |
| 91 | +└─────────────────────────────┘ |
| 92 | +``` |
| 93 | + |
| 94 | +## Running Tests Locally |
| 95 | + |
| 96 | +### Option 1: Using docker-compose (recommended) |
| 97 | + |
| 98 | +```bash |
| 99 | +# Start the services |
| 100 | +docker-compose up -d db api |
| 101 | + |
| 102 | +# Wait for service to be ready |
| 103 | +sleep 10 |
| 104 | + |
| 105 | +# Run integration tests |
| 106 | +CTS_URL=http://localhost:5005 python3 tests/integration_test.py |
| 107 | +``` |
| 108 | + |
| 109 | +### Option 2: Using existing deployment |
| 110 | + |
| 111 | +```bash |
| 112 | +# Point to your deployment |
| 113 | +CTS_URL=https://cts.example.com python3 tests/integration_test.py |
| 114 | + |
| 115 | +# With custom timeout |
| 116 | +python3 tests/integration_test.py --url http://localhost:5005 --timeout 120 |
| 117 | +``` |
| 118 | + |
| 119 | +### Option 3: Test the Tekton pipeline locally |
| 120 | + |
| 121 | +If you have a Kubernetes cluster with Tekton installed: |
| 122 | + |
| 123 | +```bash |
| 124 | +# Create a test snapshot (adjust IMAGE_URL) |
| 125 | +cat <<EOF | kubectl apply -f - |
| 126 | +apiVersion: tekton.dev/v1 |
| 127 | +kind: PipelineRun |
| 128 | +metadata: |
| 129 | + name: cts-integration-test-manual |
| 130 | +spec: |
| 131 | + pipelineRef: |
| 132 | + name: cts-integration-test-k8s |
| 133 | + params: |
| 134 | + - name: SNAPSHOT |
| 135 | + value: | |
| 136 | + { |
| 137 | + "components": [ |
| 138 | + { |
| 139 | + "name": "cts", |
| 140 | + "containerImage": "quay.io/redhat-user-workloads/team-sp-rhelcmp-tenant/cts/cts:latest" |
| 141 | + } |
| 142 | + ] |
| 143 | + } |
| 144 | +EOF |
| 145 | + |
| 146 | +# Watch the pipeline run |
| 147 | +tkn pipelinerun logs -f cts-integration-test-manual |
| 148 | +``` |
| 149 | + |
| 150 | +## Customizing Tests |
| 151 | + |
| 152 | +### Adding New Test Cases |
| 153 | + |
| 154 | +Edit `tests/integration_test.py` and add new test methods: |
| 155 | + |
| 156 | +```python |
| 157 | +def test_my_new_feature(self): |
| 158 | + """Test my new API endpoint""" |
| 159 | + status, data = self._http_get('/api/1/my-endpoint/') |
| 160 | + assert status == 200, f"Expected 200, got {status}" |
| 161 | + assert 'expected_field' in data, "Missing expected field" |
| 162 | +``` |
| 163 | + |
| 164 | +Then add it to the test suite in `run_all_tests()`: |
| 165 | + |
| 166 | +```python |
| 167 | +self._run_test("My new feature", self.test_my_new_feature) |
| 168 | +``` |
| 169 | + |
| 170 | +### Modifying the Test Environment |
| 171 | + |
| 172 | +Edit `.tekton/integration-test-k8s.yaml` to change: |
| 173 | + |
| 174 | +- Database configuration (in `deploy-test-database` task) |
| 175 | +- CTS environment variables (in `deploy-cts-service` task) |
| 176 | +- Test execution logic (in `run-integration-tests` task) |
| 177 | + |
| 178 | +## Troubleshooting |
| 179 | + |
| 180 | +### Tests Failing in Konflux |
| 181 | + |
| 182 | +1. Check PipelineRun logs: |
| 183 | + ```bash |
| 184 | + kubectl logs -n team-sp-rhelcmp-tenant -l tekton.dev/pipeline=cts-integration-test-k8s |
| 185 | + ``` |
| 186 | + |
| 187 | +2. Check if image is accessible: |
| 188 | + ```bash |
| 189 | + skopeo inspect docker://YOUR_IMAGE_URL |
| 190 | + ``` |
| 191 | + |
| 192 | +3. Check pod logs: |
| 193 | + ```bash |
| 194 | + kubectl logs -n team-sp-rhelcmp-tenant -l app=cts-test |
| 195 | + ``` |
| 196 | + |
| 197 | +### Tests Timing Out |
| 198 | + |
| 199 | +- Increase timeout in IntegrationTestScenario |
| 200 | +- Check pod events: `kubectl describe pod -l app=cts-test` |
| 201 | +- Verify image pull is not failing |
| 202 | +- Check database readiness probe |
| 203 | + |
| 204 | +### Database Connection Issues |
| 205 | + |
| 206 | +- Verify PostgreSQL deployment is ready |
| 207 | +- Check service DNS resolution |
| 208 | +- Review CTS logs for connection errors |
| 209 | + |
| 210 | +## Best Practices |
| 211 | + |
| 212 | +1. **Keep tests fast** - Integration tests should complete within 5 minutes |
| 213 | +2. **Test critical paths** - Focus on essential API functionality |
| 214 | +3. **Make tests idempotent** - Tests should work with empty or existing data |
| 215 | +4. **Clean up resources** - Always clean up in `finally` section |
| 216 | +5. **Use meaningful assertions** - Clear error messages help debugging |
| 217 | + |
| 218 | +## Related Documentation |
| 219 | + |
| 220 | +- [Konflux Integration Tests](https://konflux-ci.dev/docs/how-tos/testing/integration/) |
| 221 | +- [Tekton Pipelines](https://tekton.dev/docs/pipelines/) |
| 222 | +- [CTS API Documentation](../cts/static/openapispec.json) |
0 commit comments