vpc.tftest.hcl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. mock_provider "aws" {}
  2. variables {
  3. layer = "foundation"
  4. region = "us-east-1"
  5. ip_range = "10.88.0.0/24"
  6. }
  7. run "names_follow_layer_region_role" {
  8. command = plan
  9. assert {
  10. condition = aws_vpc.this.tags["Name"] == "foundation-us-east-1-vpc" && output.name == "foundation-us-east-1-vpc"
  11. error_message = "VPC name must be <layer>-<region>-vpc."
  12. }
  13. assert {
  14. condition = aws_vpc.this.cidr_block == "10.88.0.0/24" && aws_subnet.public.cidr_block == "10.88.0.0/24"
  15. error_message = "The VPC and its one subnet use the given range."
  16. }
  17. }
  18. run "internet_path_is_explicit_and_complete" {
  19. command = apply
  20. assert {
  21. condition = aws_internet_gateway.this.vpc_id == aws_vpc.this.id
  22. error_message = "The internet gateway must be attached to this VPC."
  23. }
  24. assert {
  25. condition = aws_route.internet.destination_cidr_block == "0.0.0.0/0" && aws_route.internet.gateway_id == aws_internet_gateway.this.id && aws_route.internet.route_table_id == aws_route_table.public.id
  26. error_message = "The explicit route table must send 0.0.0.0/0 to the internet gateway."
  27. }
  28. assert {
  29. condition = aws_route_table_association.public.subnet_id == aws_subnet.public.id && aws_route_table_association.public.route_table_id == aws_route_table.public.id
  30. error_message = "The route table must be associated with the public subnet."
  31. }
  32. assert {
  33. condition = aws_route_table.public.vpc_id == aws_vpc.this.id && aws_subnet.public.vpc_id == aws_vpc.this.id
  34. error_message = "Subnet and route table belong to this VPC."
  35. }
  36. }
  37. run "no_implicit_public_addresses_and_no_ipv6" {
  38. command = plan
  39. assert {
  40. condition = aws_subnet.public.map_public_ip_on_launch == false
  41. error_message = "The subnet must not hand out public addresses on its own."
  42. }
  43. assert {
  44. condition = aws_vpc.this.assign_generated_ipv6_cidr_block == false
  45. error_message = "IPv6 must stay off."
  46. }
  47. }
  48. run "availability_zone_is_optional_and_passed_through" {
  49. command = plan
  50. variables {
  51. availability_zone = "us-east-1b"
  52. }
  53. assert {
  54. condition = aws_subnet.public.availability_zone == "us-east-1b"
  55. error_message = "availability_zone must reach the subnet."
  56. }
  57. }
  58. run "no_nat_gateway_or_elastic_address" {
  59. command = plan
  60. assert {
  61. condition = !can(regex("(?m)^resource\\s+\"aws_(nat_gateway|eip)\"", file("${path.module}/main.tf")))
  62. error_message = "A NAT gateway or elastic address is a recurring charge the cost gate rejects or prices; the VPC module must not create one."
  63. }
  64. }
  65. run "contract_outputs_are_their_sources" {
  66. command = apply
  67. assert {
  68. condition = output.id == aws_vpc.this.id && output.ip_range == aws_vpc.this.cidr_block && output.subnet_id == aws_subnet.public.id
  69. error_message = "Outputs must be exactly their resource attributes."
  70. }
  71. assert {
  72. condition = jsonencode(sort(flatten(regexall("output \"([A-Za-z0-9_-]+)\"", file("${path.module}/outputs.tf"))))) == jsonencode(["id", "ip_range", "name", "subnet_id"])
  73. error_message = "The output set is pinned: the contract three plus subnet_id."
  74. }
  75. }
  76. run "rejects_public_range" {
  77. command = plan
  78. variables {
  79. ip_range = "8.8.8.0/24"
  80. }
  81. expect_failures = [var.ip_range]
  82. }
  83. run "rejects_malformed_range" {
  84. command = plan
  85. variables {
  86. ip_range = "not-a-cidr"
  87. }
  88. expect_failures = [var.ip_range]
  89. }
  90. run "rejects_range_aws_cannot_accept" {
  91. command = plan
  92. variables {
  93. ip_range = "10.0.0.0/8"
  94. }
  95. expect_failures = [var.ip_range]
  96. }
  97. run "rejects_a_host_address_instead_of_the_network_address" {
  98. command = plan
  99. variables {
  100. ip_range = "10.0.0.1/24"
  101. }
  102. expect_failures = [var.ip_range]
  103. }
  104. run "accepts_the_network_address" {
  105. command = plan
  106. variables {
  107. ip_range = "172.16.0.0/16"
  108. }
  109. assert {
  110. condition = aws_vpc.this.cidr_block == "172.16.0.0/16"
  111. error_message = "A canonical range must be accepted."
  112. }
  113. }
  114. run "default_security_group_is_managed_with_no_rules" {
  115. command = apply
  116. assert {
  117. condition = aws_default_security_group.this.vpc_id == aws_vpc.this.id
  118. error_message = "The VPC's default security group must be managed by this module."
  119. }
  120. assert {
  121. condition = !can(regex("(?m)^\\s*(ingress|egress)\\s*(=|\\{)", file("${path.module}/main.tf")))
  122. error_message = "The default security group must declare no ingress or egress rule."
  123. }
  124. }
  125. run "subnet_id_waits_for_the_route_and_association" {
  126. command = plan
  127. assert {
  128. condition = can(regex("output \"subnet_id\" \\{[^}]*depends_on = \\[[^\\]]*aws_route\\.internet[^\\]]*aws_route_table_association\\.public", file("${path.module}/outputs.tf")))
  129. error_message = "subnet_id must depend on the internet route and the route table association."
  130. }
  131. }