import pytest import os import subprocess import json # Path discovery - We are in external/dynamic-infra-tooling/tests # Correct project root is 3 levels up PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")) ANSIBLE_BIN = "ansible-playbook" PLAYBOOK_PATH = os.path.join(PROJECT_ROOT, "ansible", "playbooks", "test-infra-plugin.yml") def test_ansible_lookup_plugin_integration(): """Verifies the Ansible plugin can be loaded and executed by Ansible""" if not os.path.exists(PLAYBOOK_PATH): pytest.skip(f"Playbook not found at {PLAYBOOK_PATH}") env = os.environ.copy() env["LC_ALL"] = "en_US.UTF-8" env["LANG"] = "en_US.UTF-8" # Ensure Ansible finds the plugin env["ANSIBLE_LOOKUP_PLUGINS"] = os.path.join(PROJECT_ROOT, "ansible", "plugins", "lookup") # Run the test playbook cmd = [ANSIBLE_BIN, PLAYBOOK_PATH] result = subprocess.run(cmd, capture_output=True, text=True, env=env, cwd=os.path.join(PROJECT_ROOT, "ansible")) assert result.returncode == 0 assert "The next available IP is" in result.stdout assert "The certificate for loopaware.com is loopaware.com.pem" in result.stdout def test_opentofu_external_wrapper(): """Verifies the OpenTofu wrapper returns valid JSON for the external data source""" wrapper_script = os.path.join(PROJECT_ROOT, "scripts", "tofu-infra-query.sh") if not os.path.exists(wrapper_script): pytest.skip(f"Wrapper script not found at {wrapper_script}") # Simulate Tofu sending JSON query to stdin query = json.dumps({"command": "ip next-free"}) # Use shell execution to ensure the wrapper can find its relative paths correctly if needed result = subprocess.run( [wrapper_script], input=query, capture_output=True, text=True ) assert result.returncode == 0 data = json.loads(result.stdout) assert "result" in data assert "10.32." in data["result"]