Files
orchestration_engine/tests/tools/test_modules.py

209 lines
6.3 KiB
Python

"""Terraform modules: which may be used, and what one accepts (iteration 8)."""
import httpx
import pytest
from tests.reference_workflow import modules
from tests.reference_workflow.local import TerraformRunner
from tests.reference_workflow.modules import (
ModuleLookup,
admits,
check_module_sources,
module_calls,
)
ALLOWED = ("example-modules",)
GOOD = """
module "network" {
source = "example-modules/network/cloud"
version = "1.2.3"
subnets = [
{
source = "not the module's source"
version = "nor its version"
}
]
}
"""
def refused(code: str, allowed=ALLOWED) -> str:
result = check_module_sources({"terraform/main.tf": code}, allowed)
return result.message if result else ""
def test_a_module_call_is_read_with_its_own_source_and_version():
(call,) = module_calls({"main.tf": GOOD})
assert (call.name, call.source, call.version) == (
"network",
"example-modules/network/cloud",
"1.2.3",
)
def test_a_module_from_an_allowed_namespace_at_an_exact_version_is_accepted():
assert refused(GOOD) == ""
assert refused(GOOD.replace("network/cloud", "network/cloud//modules/nat")) == ""
assert refused('resource "thing" "x" {}') == ""
@pytest.mark.parametrize(
"source",
[
"someone-else/network/cloud",
"git::https://example.com/network.git",
"github.com/example-modules/network",
"./modules/network",
"../shared",
"https://example.com/network.zip",
"",
],
)
def test_any_other_source_is_refused(source):
message = refused(GOOD.replace("example-modules/network/cloud", source))
assert message.startswith("Refused") and "example-modules" in message
@pytest.mark.parametrize("version", ["~> 1.2", ">= 1.0", "1.2", "latest", ""])
def test_a_version_that_is_not_exact_is_refused(version):
assert "one exact version" in refused(GOOD.replace("1.2.3", version))
def test_with_no_namespace_configured_every_module_is_refused():
assert "none are configured" in refused(GOOD, allowed=())
def test_a_module_in_json_syntax_is_refused():
result = check_module_sources(
{"terraform/main.tf.json": '{"module": {"x": {"source": "a/b/c"}}}'}, ALLOWED
)
assert result and result.message.startswith("Refused")
async def test_validation_refuses_a_disallowed_module_before_terraform_runs():
runner = TerraformRunner(binary="no-such-binary", module_namespaces=ALLOWED)
result = await runner.validate(
{"terraform/main.tf": GOOD.replace("example-modules", "someone-else")}
)
assert result.message.startswith("Refused: module")
def test_a_version_constraint_is_read():
assert admits(">= 6.37.0, < 9", "8.6.0")
assert not admits(">= 6.2, < 8", "8.6.0")
assert admits("~> 3.0", "3.7.2") and not admits("~> 3.0", "4.0.0")
assert admits("~> 3.1.2", "3.1.9") and not admits("~> 3.1.2", "3.2.0")
assert admits("", "8.6.0")
REGISTRY_ANSWER = {
"version": "1.2.3",
"root": {
"inputs": [
{"name": "labels", "type": "map(string)", "description": "Labels."},
{
"name": "name",
"type": "string",
"description": "Name.",
"required": True,
},
],
"outputs": [{"name": "id"}],
"provider_dependencies": [{"name": "cloud", "version": ">= 6, < 8"}],
},
"submodules": [
{
"path": "modules/nat",
"inputs": [{"name": "router", "type": "string", "required": True}],
"provider_dependencies": [{"name": "random", "version": "~> 3.0"}],
}
],
}
@pytest.fixture
def registry(monkeypatch):
asked: list[str] = []
class Client:
def __init__(self, **_):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *_):
return False
async def get(self, url):
asked.append(url)
if "missing" in url:
return httpx.Response(404, json={"errors": ["Not Found"]})
return httpx.Response(200, json=REGISTRY_ANSWER)
monkeypatch.setattr(modules.httpx, "AsyncClient", Client)
return asked
async def test_a_modules_inputs_come_back_required_first_with_its_outputs(registry):
lookup = ModuleLookup(namespaces=ALLOWED, pinned={"cloud": "7.1.0"})
reply = (await lookup.describe("example-modules/network/cloud", "1.2.3")).message
assert reply.index("- name (required") < reply.index("- labels (optional")
assert "Outputs: id" in reply and "Submodules: modules/nat" in reply
assert "NOT USABLE" not in reply
assert registry == [f"{modules.REGISTRY}/example-modules/network/cloud/1.2.3"]
# The same module is not fetched twice.
await lookup.describe("example-modules/network/cloud", "1.2.3", find="name")
assert len(registry) == 1
async def test_a_module_the_pinned_provider_does_not_satisfy_is_called_out(registry):
lookup = ModuleLookup(namespaces=ALLOWED, pinned={"cloud": "8.6.0"})
reply = (await lookup.describe("example-modules/network/cloud")).message
assert "NOT USABLE: it requires cloud >= 6, < 8" in reply
assert "pinned to cloud 8.6.0" in reply
async def test_a_submodule_is_described_by_its_own_inputs(registry):
lookup = ModuleLookup(namespaces=ALLOWED, pinned={"cloud": "8.6.0"})
reply = (
await lookup.describe("example-modules/network/cloud//modules/nat")
).message
assert "- router (required" in reply and "- name" not in reply
assert "It also uses the random provider" in reply
missing = await lookup.describe("example-modules/network/cloud//modules/vpn")
assert "has no submodule" in missing.message and "modules/nat" in missing.message
async def test_find_narrows_the_inputs(registry):
lookup = ModuleLookup(namespaces=ALLOWED)
reply = (
await lookup.describe("example-modules/network/cloud", find="label")
).message
assert "- labels" in reply and "- name (" not in reply
async def test_a_module_outside_the_allowed_namespaces_is_not_looked_up(registry):
lookup = ModuleLookup(namespaces=ALLOWED)
reply = (await lookup.describe("someone-else/network/cloud")).message
assert reply.startswith("Refused") and registry == []
assert (
"no module" in (await lookup.describe("example-modules/missing/cloud")).message
)