Add missing tests for `cncf.kubernetes` script and delete utilities
- Dominant language
- Python
- Stars
- 46.9k
- Forks
- 17.8k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 472
Description
## Description
Two modules in the `cncf.kubernetes` provider have no dedicated test module, and (unlike most entries on the `OVERLOOKED_TESTS` list) they are not covered indirectly. Nothing under any `providers/*/tests/` directory imports them at all:
| Module | Expected test file |
| --- | --- |
| `providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/python_kubernetes_script.py` | `providers/cncf/kubernetes/tests/unit/cncf/kubernetes/test_python_kubernetes_script.py` |
| `providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/utils/delete_from.py` | `providers/cncf/kubernetes/tests/unit/cncf/kubernetes/utils/test_delete_from.py` |
Both are currently suppressed in the `OVERLOOKED_TESTS` allowlist in [`airflow-core/tests/unit/always/test_project_structure.py`](https://github.com/apache/airflow/blob/main/airflow-core/tests/unit/always/test_project_structure.py).
This is a scoped subset of the meta issue #35442, limited to the `cncf.kubernetes` provider.
### What should these tests cover?
`python_kubernetes_script.py` backs the `@task.kubernetes` decorator. `remove_task_decorator` does string surgery on user source, stripping `@setup`, `@teardown` and the task decorator itself, with `_balance_parens` walking nested parentheses to find where a parameterised decorator ends. That is easy to break and the failure mode is silently corrupted user code, so it is worth covering directly:
- a decorator with no arguments, with arguments, and with nested parentheses inside the arguments
- source carrying `@setup` and `@teardown` alongside the task decorator
- source where the decorator name never appears, which should return the input unchanged
- `write_python_script` rendering the Jinja template to the target file, including `render_template_as_native_obj=True` selecting `NativeEnvironment`, and `StrictUndefined` raising on a missing context variable
While writing these it is worth pinning the current behaviour of the inner `_remove_task_decorator`, which reads `python_source` from the enclosing scope rather than its own `py_source` argument. The two happen to be the same object on every iteration of the loop that calls it, so the result is correct today, but nothing stops that from silently changing.
`delete_from.py` is vendored from `kubernetes_asyncio` and mirrors the upstream `create_from_yaml` helpers. Worth covering:
- the API class name derivation in `_delete_from_yaml_single_item` from `apiVersion`, covering the core group, a named group, `.k8s.io` stripping, and DNS subdomain to CamelCase conversion
- the kind conversion from CamelCase to snake_case
- namespaced versus non namespaced dispatch, meaning `delete_namespaced_` when the API exposes it and `delete_` otherwise
- a `namespace` in the document metadata taking precedence over the `namespace` argument, which the code calls out explicitly
- `List` kinds fanning out over `items` and inheriting `apiVersion` from the parent document
- `ApiException`s being collected rather than raised eagerly, then surfaced together as `FailToDeleteError`, plus the `FailToDeleteError.__str__` formatting
These can all be driven with mocked `kubernetes.client` API classes using `autospec`, so no cluster is required.
## Definition of Done
1. Add the two test modules at the paths in the table above.
2. Remove the two corresponding entries from `OVERLOOKED_TESTS` in `airflow-core/tests/unit/always/test_project_structure.py`
3. Both of these tests should pass:
```bash
breeze testing providers-tests providers/cncf/kubernetes/tests/unit/cncf/kubernetes/test_python_kubernetes_script.py
breeze testing providers-tests providers/cncf/kubernetes/tests/unit/cncf/kubernetes/utils/test_delete_from.py
```
---
Drafted-by: Claude Code (Opus 5); reviewed and edited by @jroachgolf84 before posting.
Contributor guide
Research direction
Start by reading python_kubernetes_script.py and utils/delete_from.py, then inspect their neighboring provider tests and the two OVERLOOKED_TESTS entries in airflow-core/tests/unit/always/test_project_structure.py. Add the two specified test modules using mocked kubernetes.client APIs, covering the listed decorator, rendering, deletion, exception, and formatting cases. Done means both breeze testing commands pass and the two allowlist entries are removed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kubernetes, python
- Domain
- devops, testing
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100