|
| 1 | +# Fake Skopeo Client for Testing |
| 2 | + |
| 3 | +Fake skopeo allows you to mock skopeo operations (`inspect`, `copy`) by defining expected responses in a YAML configuration file. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +### 1. Create a mock configuration file |
| 8 | + |
| 9 | +```yaml |
| 10 | +inspect: |
| 11 | + - match: |
| 12 | + image: "docker://quay.io/source/image@sha256:abc123" |
| 13 | + format: "{{.Digest}}" |
| 14 | + return: "sha256:abc123" |
| 15 | + |
| 16 | +copy: |
| 17 | + - match: |
| 18 | + source: "docker://quay.io/source/image@sha256:abc123" |
| 19 | + destination: "docker://quay.io/dest/image:tag" |
| 20 | + # No return = success |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Use the fake client in tests |
| 24 | + |
| 25 | +#### Option A: Using bash wrapper (for Tekton tests) |
| 26 | + |
| 27 | +```bash |
| 28 | +# In your test setup, create a bash function that intercepts the script call |
| 29 | +publish_index_image() { |
| 30 | + python3 -c " |
| 31 | +import sys |
| 32 | +sys.argv[0] = 'publish_index_image' |
| 33 | +sys.path.insert(0, '/path/to/scripts/python/helpers') |
| 34 | +sys.path.insert(0, '/path/to/scripts/python/tasks/internal') |
| 35 | +
|
| 36 | +# Patch BEFORE importing publish_index_image |
| 37 | +from fake import patch_skopeo_client |
| 38 | +patch_skopeo_client() |
| 39 | +
|
| 40 | +# Now import and run |
| 41 | +from publish_index_image import main |
| 42 | +sys.exit(main()) |
| 43 | +" "\$@" |
| 44 | +} |
| 45 | + |
| 46 | +# Set the config file location |
| 47 | +export RELEASE_SERVICE_UTILS_FAKE_SKOPEO_SETUP=/path/to/mock-config.yaml |
| 48 | + |
| 49 | +# Run your test |
| 50 | +publish_index_image \ |
| 51 | + --source-index "quay.io/source/image@sha256:abc123" \ |
| 52 | + --target-index "quay.io/dest/image:tag" \ |
| 53 | + --retries 3 \ |
| 54 | + --source-credential-path /path/to/src-cred \ |
| 55 | + --target-credential-path /path/to/dest-cred |
| 56 | +``` |
| 57 | + |
| 58 | +#### Option B: Direct Python usage |
| 59 | + |
| 60 | +```python |
| 61 | +import os |
| 62 | +os.environ["RELEASE_SERVICE_UTILS_FAKE_SKOPEO_SETUP"] = "/path/to/config.yaml" |
| 63 | + |
| 64 | +from fake import patch_skopeo_client |
| 65 | +patch_skopeo_client() |
| 66 | + |
| 67 | +# Now any code that imports SkopeoClient gets the fake version |
| 68 | +from publish_index_image import main |
| 69 | +main() |
| 70 | +``` |
| 71 | + |
| 72 | +## YAML Configuration Format |
| 73 | + |
| 74 | +### Top-level structure |
| 75 | + |
| 76 | +```yaml |
| 77 | +inspect: |
| 78 | + - match: {...} |
| 79 | + return: ... |
| 80 | + - match: {...} |
| 81 | + return: ... |
| 82 | + |
| 83 | +copy: |
| 84 | + - match: {...} |
| 85 | + return: ... |
| 86 | +``` |
| 87 | +
|
| 88 | +### Inspect rules |
| 89 | +
|
| 90 | +#### With format parameter (returns string) |
| 91 | +
|
| 92 | +```yaml |
| 93 | +inspect: |
| 94 | + - match: |
| 95 | + image: "docker://quay.io/image:tag" |
| 96 | + format: "{{.Digest}}" |
| 97 | + return: "sha256:abc123" |
| 98 | +``` |
| 99 | +
|
| 100 | +#### Without format parameter (returns dict) |
| 101 | +
|
| 102 | +```yaml |
| 103 | +inspect: |
| 104 | + - match: |
| 105 | + image: "docker://quay.io/image:tag" |
| 106 | + return: |
| 107 | + Digest: "sha256:abc123" |
| 108 | + Name: "quay.io/image" |
| 109 | + RepoTags: ["v1.0", "latest"] |
| 110 | +``` |
| 111 | +
|
| 112 | +#### Using regex |
| 113 | +
|
| 114 | +```yaml |
| 115 | +inspect: |
| 116 | + - match: |
| 117 | + image: |
| 118 | + regex: "docker://quay.io/.*@sha256:[a-f0-9]{64}" |
| 119 | + format: "{{.Digest}}" |
| 120 | + return: "sha256:0123456789abcdef..." |
| 121 | +``` |
| 122 | +
|
| 123 | +### Copy rules |
| 124 | +
|
| 125 | +#### Success (no return section) |
| 126 | +
|
| 127 | +```yaml |
| 128 | +copy: |
| 129 | + - match: |
| 130 | + source: "docker://quay.io/src:tag" |
| 131 | + destination: "docker://quay.io/dest:tag" |
| 132 | + # Omit return for success |
| 133 | +``` |
| 134 | + |
| 135 | +#### Explicit success |
| 136 | + |
| 137 | +```yaml |
| 138 | +copy: |
| 139 | + - match: |
| 140 | + source: "docker://quay.io/src:tag" |
| 141 | + destination: "docker://quay.io/dest:tag" |
| 142 | + return: |
| 143 | + success: true |
| 144 | +``` |
| 145 | +
|
| 146 | +#### Failure |
| 147 | +
|
| 148 | +```yaml |
| 149 | +copy: |
| 150 | + - match: |
| 151 | + source: "docker://quay.io/bad:tag" |
| 152 | + destination: "docker://quay.io/dest:tag" |
| 153 | + return: |
| 154 | + success: false |
| 155 | + stderr: "Error: manifest unknown" |
| 156 | + returncode: 1 # optional, defaults to 1 |
| 157 | +``` |
| 158 | +
|
| 159 | +## Matching behavior |
| 160 | +
|
| 161 | +### Field matching |
| 162 | +
|
| 163 | +- Only fields specified in the `match` section need to match |
| 164 | +- Extra parameters in the actual call are ignored |
| 165 | +- `None` values don't match specified patterns |
| 166 | +- Credentials (`Secret` objects) are always ignored in matching |
| 167 | + |
| 168 | +### Match order |
| 169 | + |
| 170 | +- Rules are evaluated top-to-bottom |
| 171 | +- **First matching rule wins** |
| 172 | +- Put specific rules before generic catch-all rules |
| 173 | + |
| 174 | +### Regex matching |
| 175 | + |
| 176 | +- Use `{regex: "pattern"}` syntax for regex fields |
| 177 | +- Regex must match the **entire string** (`re.fullmatch`) |
| 178 | +- Invalid regex patterns cause load-time errors |
| 179 | + |
| 180 | +## Validation |
| 181 | + |
| 182 | +The fake client validates configuration at load time: |
| 183 | + |
| 184 | +- YAML syntax must be valid |
| 185 | +- Each rule must have a `match` section |
| 186 | +- `inspect` rules with `format` must return strings |
| 187 | +- `inspect` rules without `format` must return dicts |
| 188 | +- `copy` rules must return dicts (if return section exists) |
| 189 | +- Operation names must be valid (`inspect`, `copy`) |
| 190 | + |
| 191 | +## Error handling |
| 192 | + |
| 193 | +When no rule matches, the fake client raises `SkopeoClientError` with: |
| 194 | +- Error message: "No mock match found" |
| 195 | +- Attempted parameters shown in stderr |
| 196 | +- Reference to the config file path |
| 197 | + |
| 198 | +Example error: |
| 199 | +``` |
| 200 | +MOCK ERROR: No matching rule found for inspect() |
| 201 | +Attempted with: |
| 202 | + image: "docker://quay.io/actual:tag" |
| 203 | + format: "{{.Digest}}" |
| 204 | +Config file: /path/to/mock-config.yaml |
| 205 | +``` |
| 206 | + |
| 207 | +## Environment Variables |
| 208 | + |
| 209 | +- **`RELEASE_SERVICE_UTILS_FAKE_SKOPEO_SETUP`** (required): Path to YAML config file |
| 210 | + |
| 211 | +## Running Tests |
| 212 | + |
| 213 | +```bash |
| 214 | +# Install test dependencies |
| 215 | +pip install pytest pyyaml |
| 216 | +
|
| 217 | +# Run the tests |
| 218 | +pytest fake/test_fake_skopeo.py -v |
| 219 | +``` |
| 220 | + |
| 221 | +## Example Use Cases |
| 222 | + |
| 223 | +### Testing successful publish |
| 224 | + |
| 225 | +```yaml |
| 226 | +inspect: |
| 227 | + - match: |
| 228 | + image: "docker://quay.io/target/image:tag" |
| 229 | + format: "{{.Digest}}" |
| 230 | + return: "sha256:different" # Different from source |
| 231 | +
|
| 232 | +copy: |
| 233 | + - match: |
| 234 | + source: "docker://quay.io/source/image@sha256:abc123" |
| 235 | + destination: "docker://quay.io/target/image:tag" |
| 236 | +``` |
| 237 | + |
| 238 | +### Testing idempotent publish (same digest) |
| 239 | + |
| 240 | +```yaml |
| 241 | +inspect: |
| 242 | + - match: |
| 243 | + image: "docker://quay.io/target/image:tag" |
| 244 | + format: "{{.Digest}}" |
| 245 | + return: "sha256:abc123" # Same as source - should skip copy |
| 246 | +``` |
| 247 | + |
| 248 | +### Testing copy failure |
| 249 | + |
| 250 | +```yaml |
| 251 | +inspect: |
| 252 | + - match: |
| 253 | + image: "docker://quay.io/target/image:tag" |
| 254 | + format: "{{.Digest}}" |
| 255 | + return: "sha256:different" |
| 256 | +
|
| 257 | +copy: |
| 258 | + - match: |
| 259 | + source: "docker://quay.io/source/image@sha256:abc123" |
| 260 | + destination: "docker://quay.io/target/image:tag" |
| 261 | + return: |
| 262 | + success: false |
| 263 | + stderr: "Error: authentication required" |
| 264 | +``` |
| 265 | + |
| 266 | +## See Also |
| 267 | + |
| 268 | +- `example_config.yaml` - Comprehensive example configuration |
| 269 | +- `test_fake_skopeo.py` - Test suite demonstrating usage |
0 commit comments