Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-24
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Design

## Decisions
- Update `EnvironmentTree.cluster_secrets` to fall back to `self.repo / "tests" / "fixtures" / "cluster-secrets.toml"` when the environment root lacks `cluster-secrets.toml`.
- Update `cluster_secrets.py` to check repository candidates via `find_repository(Path.cwd())` and `Path.cwd()` if `__file__.parents[3]` is not a repository root.

## Rejected alternatives
- Bundle `cluster-secrets.toml` inside Python package: Environment manifests belong outside product package distributions.
- Hardcode `/workspace/project` in code: Fails outside containerized test runners.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Proposal: #6948

## Why
When `devops_cli` is installed as a package in a container image, such as `/app/.venv` in `devops-cli-service:local`, `Path(__file__).resolve().parents[3]` points to the Python installation directory instead of the repository root. `load_cluster_secrets_table()` fails to find `tests/fixtures/cluster-secrets.toml` and returns an incomplete cluster secrets table, causing Service Image Tests to fail.

## What Changes
- Add repository fixture fallback to `EnvironmentTree.cluster_secrets` in `src/devops_cli/k8s/environment.py`.
- Add dynamic fixture discovery in `src/devops_cli/k8s/cluster_secrets.py` that inspects the repository root of `Path.cwd()` when `__file__` is in `site-packages`.
- Add test verifying `load_cluster_secrets_table()` resolves the fixture file when `__file__` resides in site-packages.

## Capabilities
- Resolve cluster secrets fixture when running from installed package environments.

## Impact
- Service Image Tests will find the cluster secrets fixture and pass across all shards.
- No breaking changes to CLI commands or external consumers.

## Feasibility
- Verified locally that monkeypatching `__file__` to `/app/.venv/...` reproduces the 10-row failure, and dynamic discovery restores all 23 rows.

## Existing Tool Considered
- Standard library `pathlib.Path` and existing `find_repository` utility.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# cluster-secrets-loader Specification

## MODIFIED Requirements

### Requirement: Push-secrets uses the environment file
`devops k8s push-secrets` MUST load the environment rows from `cluster-secrets.toml` and append the product rows. When running in a repository checkout where the environment root lacks `cluster-secrets.toml`, the loader SHALL resolve the fixture file `tests/fixtures/cluster-secrets.toml` from the repository root when running from either source or an installed package.

#### Scenario: A fixture environment tree
- **WHEN** the test `tests/k8s/test_cluster_secret_push.py` runs with `--k8s-dir` pointing at a fixture environment tree
- **THEN** the plan contains each fixture row and each product row, and `uv run pytest tests/k8s -q -k cluster_secret` passes.

#### Scenario: Running in an installed package environment
- **WHEN** `load_cluster_secrets_table()` runs from an installed package where `__file__` is in site-packages and the working directory is inside a repository checkout
- **THEN** the loader locates `tests/fixtures/cluster-secrets.toml` and returns all 23 cluster secret rows.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tasks

## 1. Behavior

- [x] 1.1 Add test for installed package fixture resolution in `tests/k8s/test_cluster_secrets_file.py`
- Verification: `$ uv run pytest tests/k8s/test_cluster_secrets_file.py -k test_load_cluster_secrets_table_resolves_fixture_when_installed_in_site_packages`
- [x] 1.2 Implement repository fixture resolution in `src/devops_cli/k8s/environment.py` and `src/devops_cli/k8s/cluster_secrets.py`
- Verification: `$ uv run pytest tests/k8s/test_cluster_secrets_file.py`

## 2. Verification

- [x] 2.1 Run CI quality gates
- Verification: `$ uv run devops ci --check --only test,lint,typecheck,k8s`
6 changes: 5 additions & 1 deletion openspec/specs/cluster-secrets-loader/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ The module `src/devops_cli/k8s/cluster_secrets.py` MUST contain no environment S
- **THEN** the command finds no match, and `git grep -n 'name="valkey-' src/devops_cli/k8s/cluster_secrets.py` finds only `valkey-devops-auth` and `valkey-runs-auth`.

### Requirement: Push-secrets uses the environment file
`devops k8s push-secrets` MUST load the environment rows from `cluster-secrets.toml` and append the product rows.
`devops k8s push-secrets` MUST load the environment rows from `cluster-secrets.toml` and append the product rows. When running in a repository checkout where the environment root lacks `cluster-secrets.toml`, the loader SHALL resolve the fixture file `tests/fixtures/cluster-secrets.toml` from the repository root when running from either source or an installed package.

#### Scenario: A fixture environment tree
- **WHEN** the test `tests/k8s/test_cluster_secret_push.py` runs with `--k8s-dir` pointing at a fixture environment tree
- **THEN** the plan contains each fixture row and each product row, and `uv run pytest tests/k8s -q -k cluster_secret` passes.

#### Scenario: Running in an installed package environment
- **WHEN** `load_cluster_secrets_table()` runs from an installed package where `__file__` is in site-packages and the working directory is inside a repository checkout
- **THEN** the loader locates `tests/fixtures/cluster-secrets.toml` and returns all 23 cluster secret rows.
13 changes: 11 additions & 2 deletions src/devops_cli/k8s/cluster_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ def load_cluster_secrets(path: Path) -> tuple[ClusterSecret, ...]:
_FIXTURE_SECRETS_FILE = (
Path(__file__).resolve().parents[3] / "tests" / "fixtures" / "cluster-secrets.toml"
)
_FALLBACK_FIXTURE_SECRETS_FILE = Path("tests/fixtures/cluster-secrets.toml")


def _resolve_fixture_secrets_file() -> Path | None:
if _FIXTURE_SECRETS_FILE.is_file():
return _FIXTURE_SECRETS_FILE
if _FALLBACK_FIXTURE_SECRETS_FILE.is_file():
return _FALLBACK_FIXTURE_SECRETS_FILE
return None


def load_cluster_secrets_table(
Expand Down Expand Up @@ -452,8 +461,8 @@ def load_cluster_secrets_table(
direct = k8s_dir / "cluster-secrets.toml"
if direct.is_file():
env_file = direct
if env_file is None and k8s_dir is None and _FIXTURE_SECRETS_FILE.is_file():
env_file = _FIXTURE_SECRETS_FILE
if env_file is None and k8s_dir is None:
env_file = _resolve_fixture_secrets_file()

env_rows = load_cluster_secrets(env_file) if env_file is not None else ()
return (*env_rows, *CLUSTER_SECRETS)
Expand Down
3 changes: 3 additions & 0 deletions src/devops_cli/k8s/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def cluster_secrets(self) -> Path:
nested = self.root / "k8s" / _CLUSTER_SECRETS
if nested.is_file():
return nested
fixture = self.repo / "tests" / "fixtures" / _CLUSTER_SECRETS
if fixture.is_file():
return fixture
return direct


Expand Down
53 changes: 53 additions & 0 deletions tests/k8s/test_cluster_secrets_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,56 @@ def test_invalid_source_configuration_fails(tmp_path: Path) -> None:
)
with pytest.raises(ValidationError, match="secret entry must specify exactly one of"):
load_cluster_secrets(multi_source)


def test_load_cluster_secrets_table_resolves_fixture_when_installed_in_site_packages(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""When devops_cli is imported from site-packages (like in the Service image),
load_cluster_secrets_table still locates the repository fixture."""
import devops_cli.k8s.cluster_secrets as cs

fake_pkg_file = Path(
"/app/.venv/lib/python3.14/site-packages/devops_cli/k8s/cluster_secrets.py"
)
monkeypatch.setattr(cs, "__file__", str(fake_pkg_file))
monkeypatch.setattr(
cs,
"_FIXTURE_SECRETS_FILE",
fake_pkg_file.parents[3] / "tests" / "fixtures" / "cluster-secrets.toml",
)

table = cs.load_cluster_secrets_table()
assert len(table) == 23
assert any(s.ref == "object-store/seaweedfs-admin" for s in table)
assert any(s.ref == "argocd/argocd-notifications-secret" for s in table)


def test_resolve_fixture_secrets_file_variants(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Test resolution of fixture secrets file across primary, fallback, and absent paths."""
import devops_cli.k8s.cluster_secrets as cs

# Primary exists
assert cs._resolve_fixture_secrets_file() == cs._FIXTURE_SECRETS_FILE

# Primary missing, fallback exists
monkeypatch.setattr(cs, "_FIXTURE_SECRETS_FILE", tmp_path / "missing1.toml")
fake_fallback = tmp_path / "fallback.toml"
fake_fallback.write_text("[[secret]]\n", encoding="utf-8")
monkeypatch.setattr(cs, "_FALLBACK_FIXTURE_SECRETS_FILE", fake_fallback)
assert cs._resolve_fixture_secrets_file() == fake_fallback

# Both missing
monkeypatch.setattr(cs, "_FALLBACK_FIXTURE_SECRETS_FILE", tmp_path / "missing2.toml")
assert cs._resolve_fixture_secrets_file() is None


def test_load_cluster_secrets_table_resolves_fixture_when_env_lacks_secrets() -> None:
"""When env has no cluster_secrets, load_cluster_secrets_table falls back to fixture."""
import devops_cli.k8s.cluster_secrets as cs

table = cs.load_cluster_secrets_table(object())
assert len(table) == 23
assert any(s.ref == "object-store/seaweedfs-admin" for s in table)
14 changes: 14 additions & 0 deletions tests/k8s/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,17 @@ def test_the_product_base_is_k8s_devops_of_the_current_repository(tmp_path: Path
resolve_product_root(tmp_path / "base", start=repo),
tree_at(repo / "k8s").repo,
) == (repo / "k8s" / "devops", tmp_path / "base", repo)


def test_environment_tree_cluster_secrets_resolves_fixture_in_repository(
tmp_path: Path,
) -> None:
"""When the environment root has no cluster-secrets.toml, EnvironmentTree falls back to
tests/fixtures/cluster-secrets.toml in the repository."""
repo = _checkout(tmp_path / "repo")
fixture = repo / "tests" / "fixtures" / "cluster-secrets.toml"
fixture.parent.mkdir(parents=True)
fixture.write_text("[[secret]]\n", encoding="utf-8")
tree = EnvironmentTree(root=repo / "k8s", repo=repo)

assert tree.cluster_secrets == fixture