This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Local tools: Mermaid check and Terraform fmt/validate/plan in a scratch directory."""
|
||||
|
||||
import json
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
@@ -140,6 +141,110 @@ async def test_plan_never_runs_apply_and_refuses_unsafe_variables(monkeypatch):
|
||||
assert "-var=region=europe-west2" in plan
|
||||
|
||||
|
||||
# --- schema lookup -----------------------------------------------------------------------
|
||||
|
||||
SCHEMA = {
|
||||
"provider_schemas": {
|
||||
"registry.terraform.io/example/cloud": {
|
||||
"resource_schemas": {
|
||||
"cloud_service": {
|
||||
"block": {
|
||||
"attributes": {
|
||||
"name": {"type": "string", "required": True},
|
||||
"labels": {"type": ["map", "string"], "optional": True},
|
||||
"uri": {"type": "string", "computed": True},
|
||||
},
|
||||
"block_types": {
|
||||
"template": {
|
||||
"nesting_mode": "list",
|
||||
"max_items": 1,
|
||||
"block": {
|
||||
"attributes": {
|
||||
"cpu_boost": {
|
||||
"type": "bool",
|
||||
"optional": True,
|
||||
"description": "Boost CPU at start.",
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
"cloud_job": {"block": {"attributes": {"name": {"type": "string"}}}},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PIN = {"cloud": {"source": "example/cloud", "version": "1.2.3"}}
|
||||
|
||||
|
||||
def schema_runner(monkeypatch) -> tuple[TerraformRunner, list[list[str]]]:
|
||||
commands: list[list[str]] = []
|
||||
|
||||
async def fake_run(self, args, cwd, env=None):
|
||||
commands.append(list(args))
|
||||
if args[1] == "init":
|
||||
assert (
|
||||
'cloud = { source = "example/cloud", version = "1.2.3" }'
|
||||
in (Path(cwd) / "versions.tf").read_text()
|
||||
)
|
||||
return 0, "ok"
|
||||
return 0, json.dumps(SCHEMA)
|
||||
|
||||
monkeypatch.setattr(TerraformRunner, "_run", fake_run)
|
||||
return TerraformRunner(providers=PIN), commands
|
||||
|
||||
|
||||
async def test_schema_lists_what_code_may_set_and_outlines_the_blocks(monkeypatch):
|
||||
runner, _ = schema_runner(monkeypatch)
|
||||
|
||||
result = await runner.schema("cloud_service")
|
||||
|
||||
assert result.message.startswith("cloud_service (example/cloud 1.2.3)")
|
||||
assert " name (string, required)" in result.message
|
||||
assert " labels (map of string, optional)" in result.message
|
||||
assert "uri" not in result.message # read-only: code cannot set it
|
||||
assert " template { } (list, at most 1): cpu_boost" in result.message
|
||||
|
||||
|
||||
async def test_schema_gives_one_block_in_full(monkeypatch):
|
||||
runner, _ = schema_runner(monkeypatch)
|
||||
|
||||
result = await runner.schema("cloud_service", block="template")
|
||||
missing = await runner.schema("cloud_service", block="template.containers")
|
||||
|
||||
assert " cpu_boost (bool, optional): Boost CPU at start." in result.message
|
||||
assert "'containers' is not a block there" in missing.message
|
||||
|
||||
|
||||
async def test_schema_says_where_a_setting_sits_or_that_it_cannot_be_set(monkeypatch):
|
||||
runner, _ = schema_runner(monkeypatch)
|
||||
|
||||
found = await runner.schema("cloud_service", find="cpu_boost")
|
||||
absent = await runner.schema("cloud_job", find="cpu_boost")
|
||||
|
||||
assert "template.cpu_boost (bool, optional)" in found.message
|
||||
assert "It cannot be set on this resource type." in absent.message
|
||||
|
||||
|
||||
async def test_schema_suggests_close_resource_types_and_is_loaded_once(monkeypatch):
|
||||
runner, commands = schema_runner(monkeypatch)
|
||||
|
||||
unknown = await runner.schema("cloud_servce")
|
||||
await runner.schema("cloud_job")
|
||||
|
||||
assert "Close matches: cloud_service" in unknown.message
|
||||
assert [command[1] for command in commands] == ["init", "providers"]
|
||||
assert "apply" not in [arg for command in commands for arg in command]
|
||||
|
||||
|
||||
async def test_schema_is_unavailable_without_a_pinned_provider():
|
||||
result = await TerraformRunner().schema("cloud_service")
|
||||
|
||||
assert "pins no Terraform provider" in result.message
|
||||
|
||||
|
||||
# --- registry ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -155,6 +260,7 @@ def test_local_tools_are_built_by_name_with_their_tier():
|
||||
"terraform_fmt",
|
||||
"terraform_validate",
|
||||
"terraform_plan",
|
||||
"terraform_schema",
|
||||
}
|
||||
with pytest.raises(KeyError, match="terraform_apply"):
|
||||
local_tools(["terraform_apply"], TerraformRunner())
|
||||
|
||||
Reference in New Issue
Block a user