|
| 1 | +# PKO Migration Script Tests |
| 2 | + |
| 3 | +This directory contains comprehensive tests for the `olm_pko_migration.py` script. |
| 4 | + |
| 5 | +## Test Files |
| 6 | + |
| 7 | +### 1. `test_olm_pko_migration.py` - Python Unit Tests |
| 8 | + |
| 9 | +Comprehensive unit tests for the migration script using Python's `unittest` framework. |
| 10 | + |
| 11 | +**Test Coverage:** |
| 12 | + |
| 13 | +- **Git Operations** (`TestGitOperations`) |
| 14 | + - Parsing git remotes from various formats (SSH, HTTPS) |
| 15 | + - Extracting GitHub URLs |
| 16 | + - Deriving operator names from repository URLs |
| 17 | + - Error handling for non-git repositories |
| 18 | + |
| 19 | +- **Manifest Annotation** (`TestManifestAnnotation`) |
| 20 | + - Adding PKO phase annotations to manifests |
| 21 | + - Preserving existing annotations |
| 22 | + - Replacing container images with template variables |
| 23 | + - Handling edge cases (missing fields, etc.) |
| 24 | + |
| 25 | +- **Manifest Processing** (`TestManifestProcessing`) |
| 26 | + - Phase assignment based on resource kind: |
| 27 | + - CRDs → `crds` phase |
| 28 | + - RBAC resources → `rbac` phase |
| 29 | + - Deployments → `deploy` phase with image templating |
| 30 | + - Services → `deploy` phase |
| 31 | + - Handling invalid YAML gracefully |
| 32 | + |
| 33 | +- **File Discovery** (`TestFileDiscovery`) |
| 34 | + - Recursive and non-recursive file scanning |
| 35 | + - YAML file filtering (.yaml and .yml) |
| 36 | + - Handling nonexistent paths |
| 37 | + |
| 38 | +- **PKO Manifest Generation** (`TestPKOManifestGeneration`) |
| 39 | + - PackageManifest structure validation |
| 40 | + - Phase definitions |
| 41 | + - Availability probes |
| 42 | + - Config schema |
| 43 | + |
| 44 | +- **File Writing** (`TestFileWriting`) |
| 45 | + - Creating manifest files with correct names |
| 46 | + - Using `.gotmpl` extension for Deployments |
| 47 | + - Custom filename support |
| 48 | + - Skipping/forcing package kinds |
| 49 | + |
| 50 | +- **Template Generation** (`TestTemplateGeneration`) |
| 51 | + - Dockerfile.pko generation |
| 52 | + - Tekton pipeline manifests (push and PR) |
| 53 | + - ClusterPackage template |
| 54 | + - Error handling (missing directories) |
| 55 | + |
| 56 | +- **Integration Tests** (`TestIntegration`) |
| 57 | + - End-to-end conversion process |
| 58 | + - Verifying complete output structure |
| 59 | + - Checking all generated files |
| 60 | + |
| 61 | +**Running the Python tests:** |
| 62 | + |
| 63 | +```bash |
| 64 | +# Install dependencies (if not already installed) |
| 65 | +pip install pyyaml pytest |
| 66 | + |
| 67 | +# Run all tests with verbose output |
| 68 | +python3 -m pytest boilerplate/openshift/golang-osd-operator/test_olm_pko_migration.py -v |
| 69 | + |
| 70 | +# Run specific test class |
| 71 | +python3 -m pytest boilerplate/openshift/golang-osd-operator/test_olm_pko_migration.py::TestGitOperations -v |
| 72 | + |
| 73 | +# Run specific test method |
| 74 | +python3 -m pytest boilerplate/openshift/golang-osd-operator/test_olm_pko_migration.py::TestManifestAnnotation::test_annotate_adds_phase_annotation -v |
| 75 | + |
| 76 | +# Run with coverage report |
| 77 | +python3 -m pytest boilerplate/openshift/golang-osd-operator/test_olm_pko_migration.py --cov=olm_pko_migration --cov-report=html |
| 78 | +``` |
| 79 | + |
| 80 | +### 2. `test/case/convention/openshift/golang-osd-operator/08-pko-migration` - Bash Integration Test |
| 81 | + |
| 82 | +Integration test following the boilerplate repository's test framework conventions. |
| 83 | + |
| 84 | +**Test Coverage:** |
| 85 | + |
| 86 | +- **Complete Migration Workflow** |
| 87 | + - Creating test operator structure |
| 88 | + - Running migration script |
| 89 | + - Verifying all generated files |
| 90 | + |
| 91 | +- **PKO Manifest Validation** |
| 92 | + - PackageManifest structure and phases |
| 93 | + - Correct phase annotations on all resources |
| 94 | + - Collision protection annotations |
| 95 | + |
| 96 | +- **Resource Type Handling** |
| 97 | + - CRDs annotated with `crds` phase |
| 98 | + - RBAC resources (ServiceAccount, ClusterRole) with `rbac` phase |
| 99 | + - Deployments with `deploy` phase and `.gotmpl` extension |
| 100 | + - Image templating in Deployments |
| 101 | + - Services with `deploy` phase |
| 102 | + |
| 103 | +- **Cleanup Job Template** |
| 104 | + - Generated cleanup Job with proper structure |
| 105 | + - ServiceAccount, Role, RoleBinding, and Job resources |
| 106 | + - Correct phase annotations (`cleanup-rbac`, `cleanup-deploy`) |
| 107 | + |
| 108 | +- **Recursive vs Non-Recursive Mode** |
| 109 | + - `--no-recursive` flag skips subdirectories |
| 110 | + - Default recursive mode includes all nested YAML files |
| 111 | + |
| 112 | +- **Optional Component Generation** |
| 113 | + - Tekton pipeline generation (push and PR variants) |
| 114 | + - Dockerfile.pko generation |
| 115 | + - ClusterPackage template generation |
| 116 | + - Flag handling (`--no-tekton`, `--no-dockerfile`) |
| 117 | + |
| 118 | +**Running the bash test:** |
| 119 | + |
| 120 | +```bash |
| 121 | +# Run the specific PKO migration test |
| 122 | +./test/case/convention/openshift/golang-osd-operator/08-pko-migration |
| 123 | + |
| 124 | +# Run all tests in the repository |
| 125 | +make test |
| 126 | + |
| 127 | +# Run with preserved temp directories for debugging |
| 128 | +PRESERVE_TEMP_DIRS=1 ./test/case/convention/openshift/golang-osd-operator/08-pko-migration |
| 129 | + |
| 130 | +# Run tests matching a pattern |
| 131 | +make test CASE_GLOB="*pko*" |
| 132 | +``` |
| 133 | + |
| 134 | +## Test Strategy |
| 135 | + |
| 136 | +The test suite uses a two-tiered approach: |
| 137 | + |
| 138 | +1. **Unit Tests (Python)**: Fast, isolated tests for individual functions and edge cases |
| 139 | +2. **Integration Tests (Bash)**: End-to-end validation of the complete migration workflow |
| 140 | + |
| 141 | +This combination ensures: |
| 142 | +- Individual components work correctly in isolation |
| 143 | +- The complete system works as expected in realistic scenarios |
| 144 | +- Edge cases and error conditions are handled properly |
| 145 | +- The script integrates well with the boilerplate repository conventions |
| 146 | + |
| 147 | +## Adding New Tests |
| 148 | + |
| 149 | +### Adding Python Unit Tests |
| 150 | + |
| 151 | +Add new test methods to the appropriate test class in `test_olm_pko_migration.py`: |
| 152 | + |
| 153 | +```python |
| 154 | +class TestManifestProcessing(unittest.TestCase): |
| 155 | + def test_new_feature(self): |
| 156 | + """Test description.""" |
| 157 | + # Arrange |
| 158 | + manifest_str = "..." |
| 159 | + |
| 160 | + # Act |
| 161 | + result = migration.some_function(manifest_str) |
| 162 | + |
| 163 | + # Assert |
| 164 | + self.assertEqual(result, expected_value) |
| 165 | +``` |
| 166 | + |
| 167 | +### Adding Bash Integration Tests |
| 168 | + |
| 169 | +Add new test sections to the `08-pko-migration` script: |
| 170 | + |
| 171 | +```bash |
| 172 | +echo "Testing new feature" |
| 173 | + |
| 174 | +# Setup |
| 175 | +# ... preparation ... |
| 176 | + |
| 177 | +# Test |
| 178 | +python3 "${REPO_ROOT}/boilerplate/openshift/golang-osd-operator/olm_pko_migration.py" \ |
| 179 | + ... args ... |
| 180 | + |
| 181 | +# Verify |
| 182 | +if [[ ! -f expected_file ]]; then |
| 183 | + err "Expected file was not created" |
| 184 | +fi |
| 185 | +``` |
| 186 | + |
| 187 | +## CI/CD Integration |
| 188 | + |
| 189 | +The bash test is automatically run as part of the boilerplate repository's CI pipeline: |
| 190 | +- Triggered on pull requests |
| 191 | +- Runs in containerized environment |
| 192 | +- Must pass for PR approval |
| 193 | + |
| 194 | +To run the same checks locally: |
| 195 | +```bash |
| 196 | +make container-pr-check |
| 197 | +``` |
| 198 | + |
| 199 | +## Troubleshooting |
| 200 | + |
| 201 | +### Tests Failing Locally |
| 202 | + |
| 203 | +1. **Check Python dependencies**: Ensure `pyyaml` is installed |
| 204 | +2. **Git configuration**: Tests require a git repository with configured user |
| 205 | +3. **File permissions**: Ensure test files are executable (`chmod +x`) |
| 206 | + |
| 207 | +### Debugging Test Failures |
| 208 | + |
| 209 | +```bash |
| 210 | +# For bash tests, preserve temp directories |
| 211 | +PRESERVE_TEMP_DIRS=1 ./test/case/convention/openshift/golang-osd-operator/08-pko-migration |
| 212 | + |
| 213 | +# For Python tests, use verbose mode |
| 214 | +python3 -m pytest test_olm_pko_migration.py -vv -s |
| 215 | +``` |
| 216 | + |
| 217 | +### Common Issues |
| 218 | + |
| 219 | +**ImportError: No module named 'yaml'** |
| 220 | +- Solution: `pip install pyyaml` |
| 221 | + |
| 222 | +**Git errors in tests** |
| 223 | +- Solution: Ensure you're in a git repository or the test creates one properly |
| 224 | + |
| 225 | +**Permission denied** |
| 226 | +- Solution: `chmod +x test/case/convention/openshift/golang-osd-operator/08-pko-migration` |
0 commit comments