# Adding a New Scenario Test (CI/tests_v2) This guide explains how to add a new chaos scenario test to the v2 pytest framework. The layout is **folder-per-scenario**: each scenario has its own directory under `scenarios//` containing the test file, Kubernetes resources, and the Krkn scenario base YAML. ## Option 1: Scaffold script (recommended) From the **repository root**: ```bash python CI/tests_v2/scaffold.py --scenario service_hijacking ``` This creates: - `CI/tests_v2/scenarios/service_hijacking/test_service_hijacking.py` — A test class extending `BaseScenarioTest` with a stub `test_happy_path` and `WORKLOAD_MANIFEST` pointing to the folder’s `resource.yaml`. - `CI/tests_v2/scenarios/service_hijacking/resource.yaml` — A placeholder Deployment (namespace is patched at deploy time). - `CI/tests_v2/scenarios/service_hijacking/scenario_base.yaml` — A placeholder Krkn scenario; edit this with the structure expected by your scenario type. The script automatically registers the marker in `CI/tests_v2/pytest.ini`. For example, it adds: ``` service_hijacking: marks a test as a service_hijacking scenario test ``` **Next steps after scaffolding:** 1. Verify the marker was added to `pytest.ini` (the scaffold does this automatically). 2. Edit `scenario_base.yaml` with the structure your Krkn scenario type expects (see `scenarios/application_outage/scenario_base.yaml` and `scenarios/pod_disruption/scenario_base.yaml` for examples). The top-level key should match `SCENARIO_NAME`. 3. If your scenario uses a **list** structure (like pod_disruption) instead of a **dict** with a top-level key, set `NAMESPACE_KEY_PATH` (e.g. `[0, "config", "namespace_pattern"]`) and `NAMESPACE_IS_REGEX = True` if the namespace is a regex pattern. 4. The generated `test_happy_path` already uses `self.run_scenario(self.tmp_path, ns)` and assertions. Add more test methods (e.g. negative tests with `@pytest.mark.no_workload`) as needed. 5. Adjust `resource.yaml` if your scenario needs a different workload (e.g. specific image or labels). If your Kraken scenario type string is not `_scenarios`, pass it explicitly: ```bash python CI/tests_v2/scaffold.py --scenario node_disruption --scenario-type node_scenarios ``` ## Option 2: Manual setup 1. **Create the scenario folder** `CI/tests_v2/scenarios//`. 2. **Add resource.yaml** Kubernetes manifest(s) for the workload (Deployment or Pod). Use a distinct label (e.g. `app: -target`). Omit or leave `metadata.namespace`; the framework patches it at deploy time. 3. **Add scenario_base.yaml** The canonical Krkn scenario structure. Tests will load this, patch namespace (and any overrides), write to `tmp_path`, and pass to `build_config`. See existing scenarios for the format your scenario type expects. 4. **Add test_.py** - Import `BaseScenarioTest` from `lib.base` and helpers from `lib.utils` (e.g. `assert_kraken_success`, `get_pods_list`, `scenario_dir` if needed). - Define a class extending `BaseScenarioTest` with: - `WORKLOAD_MANIFEST = "CI/tests_v2/scenarios//resource.yaml"` - `WORKLOAD_IS_PATH = True` - `LABEL_SELECTOR = "app=