From 2470c2892612abc8e1eff1a9862279d2346b3dc9 Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Mon, 23 Nov 2026 05:12:11 +0000 Subject: [PATCH] fix(infra): onboard, plan and apply read one requirements file (#6698) --- .../.openspec.yaml | 2 + .../design.md | 23 ++++++++++ .../proposal.md | 27 ++++++++++++ .../specs/infra-onboard/spec.md | 12 ++++++ .../specs/infra-plan/spec.md | 20 +++++++++ .../tasks.md | 10 +++++ openspec/specs/infra-onboard/spec.md | 6 ++- openspec/specs/infra-plan/spec.md | 6 ++- src/devops_cli/infra/ansible_facts.py | 9 ++-- src/devops_cli/infra/onboard.py | 17 +++++--- src/devops_cli/infra/plan.py | 10 ++--- src/devops_cli/infra/repo_content.py | 8 +++- tests/commands/test_infra_onboard.py | 19 +++++++++ tests/infra/test_onboard.py | 42 +++++++++++++++++++ tests/infra/test_plan.py | 31 +++++++++++++- 15 files changed, 224 insertions(+), 18 deletions(-) create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/.openspec.yaml create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/design.md create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/proposal.md create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-onboard/spec.md create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-plan/spec.md create mode 100644 openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/tasks.md diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/.openspec.yaml b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/.openspec.yaml new file mode 100644 index 0000000000..c10a97dee2 --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-11-23 diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/design.md b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/design.md new file mode 100644 index 0000000000..1336da7c77 --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/design.md @@ -0,0 +1,23 @@ +# Design + +## Decisions + +### 1. Shared helper in `repo_content.py` +Use the existing `_REQUIREMENTS_FILES` order and one helper `find_collection_requirements(root: Path) -> Path | None` in `repo_content.py`. The helper returns the first existing path. The collections row, `install_collections`, and the onboard roles row all use this helper. +- **Evidence:** `src/devops_cli/infra/repo_content.py:37` defines `_REQUIREMENTS_FILES = ("collections/requirements.yml", "collections/requirements.yaml")`. +- **Reason:** Reusing the existing two-name discovery removes the drift that caused the defect. + +### 2. Pass discovered path to `install_requirements` +Update `install_requirements(tool: AnsibleTool, project: Path, target: Path, requirements: Path | None = None) -> None` in `ansible_facts.py` to accept the resolved requirements file. +- **Evidence:** `src/devops_cli/infra/ansible_facts.py:178` passes `project / REQUIREMENTS_FILE` to `galaxy_argv`. +- **Reason:** Passing the resolved path allows callers to supply the discovered file while preserving backwards compatibility. + +## Rejected alternatives + +### Accept only `collections/requirements.yml` +- **Evidence:** `src/devops_cli/infra/repo_content.py:37` has supported `.yaml` in onboard since inception. +- **Reason:** Rejecting `.yaml` breaks existing repositories that use `collections/requirements.yaml` and alters the existing `present` check in onboard. + +### Repeat the two-name check in each caller +- **Evidence:** `src/devops_cli/infra/plan.py:473` and `src/devops_cli/infra/onboard.py:254` previously drifted from `repo_content.py:200`. +- **Reason:** Duplicated discovery logic causes future drift. diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/proposal.md b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/proposal.md new file mode 100644 index 0000000000..977c8501bd --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/proposal.md @@ -0,0 +1,27 @@ +# Proposal: #6698 + +## Why +The collections row of `devops infra onboard` reads `collections/requirements.yml` or `collections/requirements.yaml`, but `devops infra plan` and `devops infra apply` install only `collections/requirements.yml`. The roles row of onboard also installs only `collections/requirements.yml`. A repo that holds only `collections/requirements.yaml` gets a `present` collections row, but plan and apply run with no pinned collections installed. Closes #6698. + +## What Changes +Use one shared discovery rule for the collection requirements file across onboard, plan and apply: +- Export a helper `find_collection_requirements(root: Path) -> Path | None` in `repo_content.py` that checks `_REQUIREMENTS_FILES` (`collections/requirements.yml`, `collections/requirements.yaml`) in order. +- In `plan.py`, `install_collections` uses the discovered requirements file rather than hardcoding `collections/requirements.yml`. +- In `ansible_facts.py`, `install_requirements` accepts an optional `requirements: Path | None = None` parameter, defaulting to `project / REQUIREMENTS_FILE` when omitted. +- In `onboard.py`, `_read_collection_roles` uses the discovered requirements file. +- Add unit tests verifying discovery and installation parity for repositories holding only `collections/requirements.yaml`. + +## Capabilities +- `infra-onboard`: The roles row installs collections from `collections/requirements.yaml` when `.yml` is absent. +- `infra-plan`: The plan and apply install step installs collections from `collections/requirements.yaml` when `.yml` is absent. + +## Impact +No breaking changes. Repositories with `collections/requirements.yaml` now have their pinned collections correctly installed during onboard, plan, and apply. + +## Feasibility +Verified in `src/devops_cli/infra/repo_content.py:37` that `_REQUIREMENTS_FILES` already defines `collections/requirements.yml` and `collections/requirements.yaml`. `read_collection_requirements` uses this tuple, while `plan.py` and `onboard.py` hardcode `REQUIREMENTS_FILE` (`collections/requirements.yml`). Sharing the discovery rule makes behavior consistent across all infra commands. + +The change fits one pull request. + +## Existing Tool Considered +No open, maintained tool covers repository collection file discovery. The alternatives check in #6698 records `none`. The internal change uses standard Python `pathlib.Path` checks and adds no dependencies, deleting the duplicated hardcoded checks. Tokens to build: ~2,000, keep: 500, elapsed time: < 1 hour. diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-onboard/spec.md b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-onboard/spec.md new file mode 100644 index 0000000000..eeafb75f52 --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-onboard/spec.md @@ -0,0 +1,12 @@ +## MODIFIED Requirements + +### Requirement: The roles row lists the roles of the pinned collections +When the export holds a collection requirements file (`collections/requirements.yml` or `collections/requirements.yaml`), the command SHALL install collections into a temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with that folder as collections path, and remove the folder. The roles row SHALL list role folders and each collection role by full name. The command MUST NOT read collection files directly. + +#### Scenario: A role of a pinned collection +- **WHEN** `collections/requirements.yml` pins the collection `example.infra`, whose role `nvidia` has an argument spec +- **THEN** the roles row is `present` and lists the 5 roles of `ansible/roles`, then `example.infra.nvidia`, `ansible-doc` ran with `ANSIBLE_COLLECTIONS_PATH` set to the `-p` folder of the install and `ANSIBLE_COLLECTIONS_SCAN_SYS_PATH` set to `False`, and the folder no longer exists + +#### Scenario: Onboard on a YAML-only repository +- **WHEN** `collections/requirements.yaml` pins the collection `example.infra` and no `collections/requirements.yml` exists +- **THEN** the roles row installs from `collections/requirements.yaml` and lists the roles of the pinned collection diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-plan/spec.md b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-plan/spec.md new file mode 100644 index 0000000000..2bc74d18a4 --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/specs/infra-plan/spec.md @@ -0,0 +1,20 @@ +## MODIFIED Requirements + +### Requirement: The plan runs the playbook in check mode +The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of the discovered requirements file (`collections/requirements.yml` or `collections/requirements.yaml`) once for each version of that file. + +#### Scenario: The run options +- **WHEN** a person plans `ansible/playbooks/03_k3s.yaml` with `--limit node-5.example.com --tags k3s` +- **THEN** `ansible-galaxy collection install` runs before `ansible-runner`, and `ansible-runner` gets `--playbook ansible/playbooks/03_k3s.yaml`, `--limit node-5.example.com:!offline`, the command line `--check --diff -v -i ansible/inventory/hosts.yaml --tags k3s -e ansible_ssh_private_key_file=` and `ANSIBLE_HOST_KEY_CHECKING=True` + +#### Scenario: The collections install once +- **WHEN** a person plans twice at the same commit +- **THEN** `ansible-galaxy` runs once and `ansible-runner` runs twice + +#### Scenario: A playbook outside the repo +- **WHEN** a person plans `../outside.yaml` +- **THEN** the command exits 1, says that it is not a playbook file of the infra repo, and starts no `ansible-runner` + +#### Scenario: Plan on a YAML-only repository +- **WHEN** the project has only `collections/requirements.yaml` and no `collections/requirements.yml` +- **THEN** the install step installs the pinned collections from `collections/requirements.yaml` diff --git a/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/tasks.md b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/tasks.md new file mode 100644 index 0000000000..929c75649c --- /dev/null +++ b/openspec/changes/archive/2026-11-23-6698-onboard-plan-and-apply-read-one-requirements-file/tasks.md @@ -0,0 +1,10 @@ +# Tasks + +## 1. Behavior: discover and install collections from requirements.yaml or requirements.yml + +- [x] 1.1 Write failing tests: a repository with only `collections/requirements.yaml` gets the same answer from the collections row and from the install of plan; `find_collection_requirements` returns `collections/requirements.yaml` when `.yml` is absent; the onboard roles row installs from `collections/requirements.yaml` + - Verification: `$ uv run devops ci test tests/commands/test_infra_onboard.py tests/infra/test_plan.py` +- [x] 1.2 Implement `find_collection_requirements` in `repo_content.py`, pass the resolved requirements path to `install_requirements` in `ansible_facts.py`, and update callers in `plan.py` and `onboard.py` + - Verification: `$ uv run devops ci test tests/commands/test_infra_onboard.py tests/infra/test_plan.py` + +- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD). diff --git a/openspec/specs/infra-onboard/spec.md b/openspec/specs/infra-onboard/spec.md index 0d2a910082..d275097d6a 100644 --- a/openspec/specs/infra-onboard/spec.md +++ b/openspec/specs/infra-onboard/spec.md @@ -152,12 +152,16 @@ Only `ansible-runner` SHALL go on PATH. The command SHALL start the Ansible comm - **THEN** the command finds no tool environment, and a dry run shows each Ansible command with no folder ### Requirement: The roles row lists the roles of the pinned collections -When the export holds `collections/requirements.yml`, the command SHALL install its collections into a new temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with only that folder as the collections path, and remove the folder. The roles row SHALL list the roles of the role folders, then each role of the listing that names a collection, by its full name. The command MUST NOT read the files of a collection itself. +When the export holds a collection requirements file (`collections/requirements.yml` or `collections/requirements.yaml`), the command SHALL install collections into a temporary folder with `ansible-galaxy collection install`, read `ansible-doc -t role -l -j` with that folder as collections path, and remove the folder. The roles row SHALL list role folders and each collection role by full name. The command MUST NOT read collection files directly. #### Scenario: A role of a pinned collection - **WHEN** `collections/requirements.yml` pins the collection `example.infra`, whose role `nvidia` has an argument spec - **THEN** the roles row is `present` and lists the 5 roles of `ansible/roles`, then `example.infra.nvidia`, `ansible-doc` ran with `ANSIBLE_COLLECTIONS_PATH` set to the `-p` folder of the install and `ANSIBLE_COLLECTIONS_SCAN_SYS_PATH` set to `False`, and the folder no longer exists +#### Scenario: Onboard on a YAML-only repository +- **WHEN** `collections/requirements.yaml` pins the collection `example.infra` and no `collections/requirements.yml` exists +- **THEN** the roles row installs from `collections/requirements.yaml` and lists the roles of the pinned collection + ### Requirement: The roles row keeps the role folders when the collections do not read When no Ansible tool environment is installed, or its folder holds no `ansible-doc`, the command SHALL start no install, and the roles row SHALL list the roles of the role folders and say in its reason that it does not list the roles of the pinned collections. When the install or the listing fails, the row SHALL list the roles of the role folders and name the command, its exit code and its last line in its reason. diff --git a/openspec/specs/infra-plan/spec.md b/openspec/specs/infra-plan/spec.md index 5e445f9177..61ecaa76f2 100644 --- a/openspec/specs/infra-plan/spec.md +++ b/openspec/specs/infra-plan/spec.md @@ -17,7 +17,7 @@ How `devops infra plan` runs a playbook of an infra repo in check mode at one co - **THEN** the command exits 1, names the ref, and exports nothing ### Requirement: The plan runs the playbook in check mode -The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of `collections/requirements.yml` once for each version of that file. +The command SHALL run `ansible-runner run` with `--check --diff -v`, the inventory of the profile, `--tags` when given, the key of `devops ssh` as `ansible_ssh_private_key_file`, the repo's `ansible.cfg` and host key checks on. Before the run it SHALL install the pinned collections of the discovered requirements file (`collections/requirements.yml` or `collections/requirements.yaml`) once for each version of that file. #### Scenario: The run options - **WHEN** a person plans `ansible/playbooks/03_k3s.yaml` with `--limit node-5.example.com --tags k3s` @@ -31,6 +31,10 @@ The command SHALL run `ansible-runner run` with `--check --diff -v`, the invento - **WHEN** a person plans `../outside.yaml` - **THEN** the command exits 1, says that it is not a playbook file of the infra repo, and starts no `ansible-runner` +#### Scenario: Plan on a YAML-only repository +- **WHEN** the project has only `collections/requirements.yaml` and no `collections/requirements.yml` +- **THEN** the install step installs the pinned collections from `collections/requirements.yaml` + ### Requirement: No host of the offline group is in the limit The host pattern of the run SHALL be the `--limit`, else `all`, followed by `!` as a term of its own when the profile names a group of hosts out of service: the term is joined with a comma when the limit holds a comma, and with a colon when it holds none. diff --git a/src/devops_cli/infra/ansible_facts.py b/src/devops_cli/infra/ansible_facts.py index 50ded71d0c..2c48b80e4e 100644 --- a/src/devops_cli/infra/ansible_facts.py +++ b/src/devops_cli/infra/ansible_facts.py @@ -167,15 +167,18 @@ def galaxy_argv(tool: AnsibleTool, requirements: Path, target: Path) -> list[str ) -def install_requirements(tool: AnsibleTool, project: Path, target: Path) -> None: - """Install the collections of `collections/requirements.yml` of the repo in `project` into the +def install_requirements( + tool: AnsibleTool, project: Path, target: Path, requirements: Path | None = None +) -> None: + """Install the collections of the requirements file of the repo in `project` into the folder `target`, with the repo's `ansible.cfg`. Raises: InfraReadError: the install fails. """ + req_file = requirements if requirements is not None else project / REQUIREMENTS_FILE result = run_subprocess( - galaxy_argv(tool, project / REQUIREMENTS_FILE, target), + galaxy_argv(tool, req_file, target), cwd=project, env=config_env(project), quiet=True, diff --git a/src/devops_cli/infra/onboard.py b/src/devops_cli/infra/onboard.py index 5358a0ec20..c152f143d1 100644 --- a/src/devops_cli/infra/onboard.py +++ b/src/devops_cli/infra/onboard.py @@ -40,7 +40,6 @@ from devops_cli.infra.access import AgentAccess from devops_cli.infra.ansible_facts import ( DOC_COMMAND, - REQUIREMENTS_FILE, AnsibleConfig, AnsibleInventory, AnsibleTool, @@ -55,6 +54,7 @@ from devops_cli.infra.repo_content import ( YamlContent, find_ci_workflows, + find_collection_requirements, find_node_targets, find_roles, read_collection_requirements, @@ -249,19 +249,24 @@ def _read_facts(root: Path) -> _Facts: def _read_collection_roles(tool: AnsibleTool, root: Path) -> tuple[str, ...] | str: """The roles of the pinned collections of the repo at `root`, which an install puts in a temporary folder for their listing; or the reason that the roles row does not list them. A repo - with no `collections/requirements.yml` pins no collection.""" + with no collection requirements file pins no collection.""" texts = MESSAGES.infra - if not (root / REQUIREMENTS_FILE).is_file(): + requirements = find_collection_requirements(root) + if requirements is None: return () if not (tool.installed and check_binary(tool.argv(DOC_COMMAND)[0])): return texts.roles_no_doc with tempfile.TemporaryDirectory(prefix=_COLLECTIONS_PREFIX) as folder: - roles = _attempt(lambda: _install_and_list(tool, root, Path(folder))) + roles = _attempt( + lambda: _install_and_list(tool, root, Path(folder), requirements=requirements) + ) return roles if isinstance(roles, tuple) else texts.roles_unread.format(reason=roles) -def _install_and_list(tool: AnsibleTool, root: Path, folder: Path) -> tuple[str, ...]: - install_requirements(tool, root, folder) +def _install_and_list( + tool: AnsibleTool, root: Path, folder: Path, requirements: Path | None = None +) -> tuple[str, ...]: + install_requirements(tool, root, folder, requirements=requirements) return read_collection_roles(tool, root, folder) diff --git a/src/devops_cli/infra/plan.py b/src/devops_cli/infra/plan.py index e1d7abc917..594f4fbe93 100644 --- a/src/devops_cli/infra/plan.py +++ b/src/devops_cli/infra/plan.py @@ -73,7 +73,6 @@ from devops_cli.infra.access import AgentAccess, require_agent_level from devops_cli.infra.ad_hoc import read_job_events, tool_env from devops_cli.infra.ansible_facts import ( - REQUIREMENTS_FILE, RUNNER_COMMAND, AnsibleInventory, AnsibleTool, @@ -84,6 +83,7 @@ read_inventory, ) from devops_cli.infra.onboard import OnboardTarget +from devops_cli.infra.repo_content import find_collection_requirements from devops_cli.infra.repo_tree import export_commit, render_read from devops_cli.lang import ERRORS, MESSAGES from devops_cli.models.infra import ( @@ -465,13 +465,13 @@ def collections_folder(requirements: Path) -> Path: def install_collections(tool: AnsibleTool, project: Path) -> Path | None: """The folder of the pinned collections of the repo in `project`, installed once for each - version of `collections/requirements.yml`; none when the repo has no such file. + version of its collection requirements file; none when the repo has no such file. Raises: InfraReadError: the install fails. """ - requirements = project / REQUIREMENTS_FILE - if not requirements.is_file(): + requirements = find_collection_requirements(project) + if requirements is None: return None target = collections_folder(requirements) if target.is_dir(): @@ -479,7 +479,7 @@ def install_collections(tool: AnsibleTool, project: Path) -> Path | None: target.parent.mkdir(parents=True, exist_ok=True, mode=_PRIVATE) staging = Path(tempfile.mkdtemp(prefix=_INSTALL_PREFIX, dir=target.parent)) try: - install_requirements(tool, project, staging) + install_requirements(tool, project, staging, requirements=requirements) staging.rename(target) finally: shutil.rmtree(staging, ignore_errors=True) diff --git a/src/devops_cli/infra/repo_content.py b/src/devops_cli/infra/repo_content.py index bc8e69513a..3f92195ef8 100644 --- a/src/devops_cli/infra/repo_content.py +++ b/src/devops_cli/infra/repo_content.py @@ -194,10 +194,15 @@ def find_roles(root: Path, roles_path: tuple[Path, ...]) -> tuple[str, ...]: return tuple(sorted(roles)) +def find_collection_requirements(root: Path) -> Path | None: + """The collection requirements file of the repository (requirements.yml or requirements.yaml).""" + return next((root / name for name in _REQUIREMENTS_FILES if (root / name).is_file()), None) + + def read_collection_requirements(root: Path) -> CollectionRequirements: """The collections file of the repository, and each collection it names with no exact version.""" - path = next((root / name for name in _REQUIREMENTS_FILES if (root / name).is_file()), None) + path = find_collection_requirements(root) if path is None: return CollectionRequirements(path=None) relative = path.relative_to(root).as_posix() @@ -275,6 +280,7 @@ def _is_node_target(name: str) -> bool: "CollectionRequirements", "YamlContent", "find_ci_workflows", + "find_collection_requirements", "find_node_targets", "find_roles", "read_collection_requirements", diff --git a/tests/commands/test_infra_onboard.py b/tests/commands/test_infra_onboard.py index c21b02286a..6b0f7ebc6a 100644 --- a/tests/commands/test_infra_onboard.py +++ b/tests/commands/test_infra_onboard.py @@ -255,3 +255,22 @@ def test_a_relative_base_dir_resolves_under_the_main_worktree( assert onboard.default_checkout(RepoRef("owner/name"), settings) == ( tmp_path / "repos" / "owner" / "name" ) + + +def test_collections_row_and_install_agree_on_requirements_yaml(tmp_path: Path) -> None: + """The collections row and the requirements helper agree on `collections/requirements.yaml`.""" + from devops_cli.infra.repo_content import ( + find_collection_requirements, + read_collection_requirements, + ) + + colls = tmp_path / "collections" + colls.mkdir() + yaml_file = colls / "requirements.yaml" + yaml_file.write_text("collections:\n - name: community.general\n", encoding="utf-8") + + reqs = read_collection_requirements(tmp_path) + found = find_collection_requirements(tmp_path) + + assert reqs.path == "collections/requirements.yaml" + assert found == yaml_file diff --git a/tests/infra/test_onboard.py b/tests/infra/test_onboard.py index 52bb07e778..7889a13812 100644 --- a/tests/infra/test_onboard.py +++ b/tests/infra/test_onboard.py @@ -22,6 +22,10 @@ from devops_cli.config.settings import load_settings from devops_cli.exceptions.infra import InfraReadError from devops_cli.infra.onboard import check_onboarding, resolve_target +from devops_cli.infra.repo_content import ( + find_collection_requirements, + read_collection_requirements, +) from devops_cli.models.infra import OnboardReport, OnboardState from tests.support.fakes.process import ProcessAnswer, ProcessCall, ProcessFake from tests.support.infra import ( @@ -532,6 +536,44 @@ def test_the_roles_row_lists_the_roles_of_the_pinned_collections(onboarding: Onb assert not Path(folder).exists() +def test_the_roles_row_installs_from_requirements_yaml(onboarding: Onboarding) -> None: + """A repo with only `collections/requirements.yaml` installs from that file.""" + yml = onboarding.repo / "collections" / "requirements.yml" + yaml_path = onboarding.repo / "collections" / "requirements.yaml" + pin = " - name: example.infra\n version: 1.0.0\n" + content = yml.read_text(encoding="utf-8") + pin + yml.unlink() + yaml_path.write_text(content, encoding="utf-8") + + row = onboarding.report().rows[6] + + assert (row.state, row.reason, row.items) == ( + PRESENT, + "6 roles", + (*FOLDER_ROLES, "example.infra.nvidia"), + ) + install = _call_of(onboarding.process, "ansible-galaxy") + req_arg = install.argv[install.argv.index("-r") + 1] + assert req_arg.endswith("collections/requirements.yaml") + + +def test_collections_row_and_find_requirements_agree_on_yaml(tmp_path: Path) -> None: + """A repo with only `collections/requirements.yaml` gets a present collections row and + find_collection_requirements returns that file.""" + colls = tmp_path / "collections" + colls.mkdir() + yaml_file = colls / "requirements.yaml" + yaml_file.write_text( + "collections:\n - name: community.general\n version: 1.0.0\n", encoding="utf-8" + ) + + found = find_collection_requirements(tmp_path) + assert found == yaml_file + + reqs = read_collection_requirements(tmp_path) + assert reqs.path == "collections/requirements.yaml" + + def test_without_ansible_doc_the_roles_row_keeps_the_role_folders( onboarding: Onboarding, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/tests/infra/test_plan.py b/tests/infra/test_plan.py index fc897c5c94..a2139f520c 100644 --- a/tests/infra/test_plan.py +++ b/tests/infra/test_plan.py @@ -16,10 +16,15 @@ import pytest from devops_cli.infra.ad_hoc import read_job_events -from devops_cli.infra.ansible_facts import parse_inventory +from devops_cli.infra.ansible_facts import ( + REQUIREMENTS_FILE, + AnsibleTool, + parse_inventory, +) from devops_cli.infra.plan import ( host_pattern, host_plans, + install_collections, nondisruptive_units, plan_id, plan_outcome, @@ -481,3 +486,27 @@ def test_the_events_read_in_counter_order_without_a_partial_file(tmp_path: Path) (tmp_path / f"{events[0]['uuid']}-partial.json").write_text("{}", encoding="utf-8") assert read_job_events(tmp_path) == events + + +def test_install_collections_uses_requirements_yaml( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A project with only `collections/requirements.yaml` installs from that file.""" + colls = tmp_path / "collections" + colls.mkdir() + yaml_file = colls / "requirements.yaml" + yaml_file.write_text("collections:\n - name: example.infra\n", encoding="utf-8") + + tool = AnsibleTool(bin_dir=tmp_path / "bin") + installed: list[Path] = [] + + def fake_install( + tool: AnsibleTool, project: Path, target: Path, requirements: Path | None = None + ) -> None: + installed.append(requirements if requirements is not None else project / REQUIREMENTS_FILE) + + monkeypatch.setattr("devops_cli.infra.plan.install_requirements", fake_install) + target = install_collections(tool, tmp_path) + + assert target is not None + assert installed == [yaml_file]