|
| 1 | +"""Exercise the real wrapper with fake clients to verify its evidence gates.""" |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +RUNNER = Path(__file__).resolve().parent / "run.sh" |
| 11 | +LISTS = ("server list", "network list", "security group list", |
| 12 | + "volume list", "keypair list", "subnet list", "router list") |
| 13 | + |
| 14 | +# Only the external clients are replaced. The wrapper still runs its full |
| 15 | +# preflight, workspace handling, apply/destroy, and clean-state assertions. |
| 16 | +CLIENT = r''' |
| 17 | +import json |
| 18 | +import os |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | +
|
| 22 | +tool = Path(sys.argv[0]).name |
| 23 | +args = sys.argv[1:] |
| 24 | +if tool == "curl": |
| 25 | + sys.exit(0) |
| 26 | +if tool == "openstack": |
| 27 | + command = " ".join(args[:3] if args[0] in ("security", "floating") else args[:2]) |
| 28 | + if command == os.environ.get("FAIL_LIST"): |
| 29 | + print("simulated API failure", file=sys.stderr) |
| 30 | + sys.exit(1) |
| 31 | + if command == "catalog list": |
| 32 | + print("http://localhost:35357/v3") |
| 33 | + elif command == "floating ip list": |
| 34 | + print("fip-owned" if os.environ.get("LEAK_LIST") == command else "fip-unrelated") |
| 35 | + elif command.endswith(" list"): |
| 36 | + print("tf-compat-leftover" if os.environ.get("LEAK_LIST") == command else "unrelated") |
| 37 | + else: |
| 38 | + print("fixture-id") |
| 39 | + sys.exit(0) |
| 40 | +
|
| 41 | +workspace = None |
| 42 | +if args[0].startswith("-chdir="): |
| 43 | + workspace = Path(args.pop(0).split("=", 1)[1]) |
| 44 | +if args[0] == "version": |
| 45 | + print("Terraform v1.13.4") |
| 46 | +elif args[:2] == ["state", "pull"]: |
| 47 | + if os.environ.get("STATE_MODE") == "error": |
| 48 | + sys.exit(1) |
| 49 | + if os.environ.get("STATE_MODE") == "malformed": |
| 50 | + print("{") |
| 51 | + elif os.environ.get("STATE_MODE") == "no-fips": |
| 52 | + print(json.dumps({"version": 4, "resources": []})) |
| 53 | + else: |
| 54 | + print(json.dumps({"version": 4, "resources": [{ |
| 55 | + "type": "openstack_networking_floatingip_v2", |
| 56 | + "instances": [{"attributes": {"id": "fip-owned"}}], |
| 57 | + }]})) |
| 58 | +elif args[0] == "output": |
| 59 | + print("{}") |
| 60 | +elif args[0] == "destroy": |
| 61 | + (workspace / "destroyed").touch() |
| 62 | +elif args[0] == "plan" and (workspace / "destroyed").exists(): |
| 63 | + print("Plan: 9 to add, 0 to change, 0 to destroy.") |
| 64 | + sys.exit(2) |
| 65 | +''' |
| 66 | + |
| 67 | + |
| 68 | +class TerraformRunnerTests(unittest.TestCase): |
| 69 | + def setUp(self): |
| 70 | + self.directory = tempfile.TemporaryDirectory() |
| 71 | + self.addCleanup(self.directory.cleanup) |
| 72 | + self.root = Path(self.directory.name) |
| 73 | + for tool in ("terraform", "openstack", "curl"): |
| 74 | + script = self.root / tool |
| 75 | + script.write_text(f"#!{sys.executable}\n" + CLIENT) |
| 76 | + script.chmod(0o755) |
| 77 | + |
| 78 | + def run_wrapper(self, **settings): |
| 79 | + env = dict(os.environ, PATH=f"{self.root}{os.pathsep}{os.environ['PATH']}", |
| 80 | + OPENSTACK_CLI=str(self.root / "openstack"), |
| 81 | + TF_PLUGIN_CACHE_DIR=str(self.root / "cache"), |
| 82 | + TF_RESULTS_DIR=str(self.root / "results"), |
| 83 | + TMPDIR=str(self.root), FAIL_LIST="", LEAK_LIST="", STATE_MODE="") |
| 84 | + env.update(settings) |
| 85 | + return subprocess.run(["bash", str(RUNNER)], env=env, text=True, |
| 86 | + capture_output=True, timeout=30, check=False) |
| 87 | + |
| 88 | + def assert_failed(self, result): |
| 89 | + self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) |
| 90 | + self.assertNotIn("SUMMARY: PASS", result.stdout) |
| 91 | + |
| 92 | + def test_success_requires_clean_state_and_unrelated_objects_are_ignored(self): |
| 93 | + for mode in ("", "no-fips"): |
| 94 | + with self.subTest(mode=mode): |
| 95 | + result = self.run_wrapper(STATE_MODE=mode) |
| 96 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 97 | + self.assertIn("SUMMARY: PASS", result.stdout) |
| 98 | + self.assertFalse(list(self.root.glob("tf-compat-workspace.*"))) |
| 99 | + |
| 100 | + def test_failed_resource_listing_cannot_prove_no_residue(self): |
| 101 | + for command in (*LISTS, "floating ip list"): |
| 102 | + with self.subTest(command=command): |
| 103 | + self.assert_failed(self.run_wrapper(FAIL_LIST=command)) |
| 104 | + |
| 105 | + def test_leaked_resources_fail_including_nameless_floating_ips(self): |
| 106 | + for command in (*LISTS, "floating ip list"): |
| 107 | + with self.subTest(command=command): |
| 108 | + self.assert_failed(self.run_wrapper(LEAK_LIST=command)) |
| 109 | + |
| 110 | + def test_unreadable_state_cannot_hide_floating_ips(self): |
| 111 | + for mode in ("error", "malformed"): |
| 112 | + with self.subTest(mode=mode): |
| 113 | + self.assert_failed(self.run_wrapper(STATE_MODE=mode)) |
| 114 | + |
| 115 | + def test_catalog_failure_cannot_pass_preflight(self): |
| 116 | + self.assert_failed(self.run_wrapper(FAIL_LIST="catalog list")) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + unittest.main() |
0 commit comments