test_cost_check_instances_codex.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """Codex review of PR 48: instances tied to the shared code, every costed
  2. resource reconciled, --plan on instance paths, config.yml check in the gate."""
  3. import json
  4. from test_cost_check_instances import (
  5. add_site,
  6. aws_instances,
  7. build,
  8. cost_check,
  9. droplets,
  10. fails_with,
  11. run,
  12. write_config,
  13. write_json,
  14. )
  15. from test_cost_check_multicloud import inst, plan
  16. EAST = "stacks/sites/east/aws/10-access"
  17. def aws_site(root, resources=None):
  18. add_site(
  19. root,
  20. key="east",
  21. stack="aws/10-access",
  22. instance_of="stacks/aws/10-access",
  23. resources=aws_instances(2) if resources is None else resources,
  24. )
  25. def test_instance_cannot_claim_cheaper_resources_than_shared_code(tmp_path, capsys):
  26. root = build(tmp_path, do_cap=212.0)
  27. approved = json.loads((root / "docs/stages/approved.json").read_text())
  28. approved["ceiling"] = 212.0
  29. write_json(root / "docs/stages/approved.json", approved)
  30. prices = json.loads((root / "docs/cost-prices.json").read_text())
  31. prices["droplets"]["expensive"] = 100.0
  32. write_json(root / "docs/cost-prices.json", prices)
  33. write_json(
  34. root / "stacks/10-access/cost.json",
  35. {
  36. "resources": [
  37. {
  38. "name": f"d{i}",
  39. "kind": "droplet",
  40. "size": "expensive",
  41. "backups": False,
  42. }
  43. for i in range(2)
  44. ]
  45. },
  46. )
  47. add_site(root, resources=droplets(2))
  48. fails_with(root, capsys, "must equal the base stack's entry")
  49. def test_instance_must_include_all_paid_resources_of_shared_stack(tmp_path, capsys):
  50. root = build(tmp_path, aws=True)
  51. base = json.loads((root / "stacks/aws/10-access/cost.json").read_text())
  52. base["resources"].append({"name": "data", "kind": "ebs_volume", "gb": 100})
  53. write_json(root / "stacks/aws/10-access/cost.json", base)
  54. aws_site(root)
  55. fails_with(root, capsys, "resource names must equal the base stack's")
  56. def test_instance_volume_size_must_equal_the_base_volume(tmp_path, capsys):
  57. root = build(tmp_path, aws=True)
  58. vol = {"name": "data", "kind": "ebs_volume", "gb": 100}
  59. base = json.loads((root / "stacks/aws/10-access/cost.json").read_text())
  60. base["resources"].append(vol)
  61. write_json(root / "stacks/aws/10-access/cost.json", base)
  62. aws_site(root, aws_instances(2) + [{**vol, "gb": 1}])
  63. fails_with(root, capsys, "must equal the base stack's entry")
  64. def test_matching_aws_instance_with_volume_passes_reconciliation(tmp_path):
  65. root = build(tmp_path, aws=True)
  66. vol = {"name": "data", "kind": "ebs_volume", "gb": 100}
  67. base = json.loads((root / "stacks/aws/10-access/cost.json").read_text())
  68. base["resources"].append(vol)
  69. write_json(root / "stacks/aws/10-access/cost.json", base)
  70. aws_site(root, aws_instances(2) + [vol])
  71. assert cost_check.structure_problems(root) == []
  72. def write_plan(root, name, *changes):
  73. path = root / name
  74. path.write_text(json.dumps(plan(*changes)))
  75. return str(path)
  76. def plan_run(root, capsys, plan_path, stack):
  77. code = cost_check.main(["--root", str(root), "--plan", plan_path, "--stack", stack])
  78. return code, capsys.readouterr()
  79. def test_plan_check_accepts_an_instance_path(tmp_path, capsys):
  80. root = build(tmp_path, aws=True)
  81. aws_site(root)
  82. good = write_plan(root, "plan.json", inst("a"), inst("b"))
  83. _code, out = plan_run(root, capsys, good, EAST)
  84. # The fixture's AWS approval is over the ceiling (a different failure); only
  85. # the plan result is under test here.
  86. assert "implemented for aws only" not in out.err
  87. assert "the plan costs" not in out.err
  88. assert "no cost.json to reconcile" not in out.err
  89. def test_plan_check_compares_with_the_instance_manifest(tmp_path, capsys):
  90. root = build(tmp_path, aws=True)
  91. aws_site(root)
  92. bad = write_plan(root, "plan.json", inst("a"), inst("b"), inst("c"))
  93. code, out = plan_run(root, capsys, bad, EAST)
  94. assert code == 1
  95. assert f"{EAST}: the plan costs" in out.err
  96. def test_config_stack_id_without_a_manifest_fails_the_gate_itself(tmp_path, capsys):
  97. root = build(tmp_path, do_cap=24.0)
  98. write_config(root, "cah-10-access", "cah-10-access-tor2")
  99. fails_with(
  100. root,
  101. capsys,
  102. ".spacelift/config.yml stack id cah-10-access-tor2 has 0 manifest(s)",
  103. )
  104. def test_config_with_every_id_matched_passes_the_gate(tmp_path, capsys):
  105. root = build(tmp_path, do_cap=24.0)
  106. add_site(root)
  107. write_config(root, "cah-10-access", "cah-10-access-tor2")
  108. code, _, err = run(root, capsys)
  109. assert code == 0, err
  110. def test_unreadable_config_fails_closed(tmp_path, capsys):
  111. root = build(tmp_path)
  112. (root / ".spacelift").mkdir()
  113. (root / ".spacelift/config.yml").write_text("stacks: [not, a, map]\n")
  114. fails_with(root, capsys, ".spacelift/config.yml is unreadable")