|
| 1 | +# Characterization tests for the new-vs-existing VPC input validation. |
| 2 | +# |
| 3 | +# These run `terraform plan` with the AWS provider mocked, so they need no AWS |
| 4 | +# credentials and create no infrastructure. They pin the behavior of the |
| 5 | +# `configuration_error` precondition (see outputs.tf): valid input combinations |
| 6 | +# must plan cleanly, and contradictory combinations must fail fast. |
| 7 | +# |
| 8 | +# On a valid config the precondition passes and the `configuration_error` |
| 9 | +# output renders the requirement checklist with every line marked ✅; an |
| 10 | +# unmet requirement shows ❌ and a contradictory config trips the precondition |
| 11 | +# during plan. |
| 12 | + |
| 13 | +mock_provider "aws" { |
| 14 | + # slice(..., 0, 2) and the cidrsubnet math need at least two AZ names. |
| 15 | + mock_data "aws_availability_zones" { |
| 16 | + defaults = { |
| 17 | + names = ["us-east-1a", "us-east-1b", "us-east-1c"] |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +variables { |
| 23 | + name = "quilt-test" |
| 24 | + cidr = "10.0.0.0/16" |
| 25 | +} |
| 26 | + |
| 27 | +# --- Valid: create a new VPC ------------------------------------------------- |
| 28 | + |
| 29 | +run "new_vpc_external_alb" { |
| 30 | + command = plan |
| 31 | + |
| 32 | + variables { |
| 33 | + create_new_vpc = true |
| 34 | + internal = false |
| 35 | + existing_vpc_id = null |
| 36 | + existing_api_endpoint = null |
| 37 | + existing_intra_subnets = null |
| 38 | + existing_private_subnets = null |
| 39 | + existing_public_subnets = null |
| 40 | + existing_user_security_group = null |
| 41 | + existing_user_subnets = null |
| 42 | + } |
| 43 | + |
| 44 | + assert { |
| 45 | + condition = strcontains(output.configuration_error, "create a new VPC") |
| 46 | + error_message = "New-VPC config must be checked against the new-network requirements" |
| 47 | + } |
| 48 | + |
| 49 | + assert { |
| 50 | + condition = !strcontains(output.configuration_error, "❌") |
| 51 | + error_message = "A valid new-VPC config (internal = false) must satisfy every requirement" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +run "new_vpc_internal_alb" { |
| 56 | + command = plan |
| 57 | + |
| 58 | + variables { |
| 59 | + create_new_vpc = true |
| 60 | + internal = true |
| 61 | + existing_vpc_id = null |
| 62 | + existing_api_endpoint = null |
| 63 | + existing_intra_subnets = null |
| 64 | + existing_private_subnets = null |
| 65 | + existing_public_subnets = null |
| 66 | + existing_user_security_group = null |
| 67 | + existing_user_subnets = null |
| 68 | + } |
| 69 | + |
| 70 | + assert { |
| 71 | + condition = !strcontains(output.configuration_error, "❌") |
| 72 | + error_message = "A valid new-VPC config (internal = true) must satisfy every requirement" |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +# --- Valid: use an existing VPC ---------------------------------------------- |
| 77 | + |
| 78 | +run "existing_vpc_external_alb" { |
| 79 | + command = plan |
| 80 | + |
| 81 | + variables { |
| 82 | + create_new_vpc = false |
| 83 | + internal = false |
| 84 | + existing_vpc_id = "vpc-00000000000000000" |
| 85 | + existing_api_endpoint = null |
| 86 | + existing_intra_subnets = ["subnet-intra-a", "subnet-intra-b"] |
| 87 | + existing_private_subnets = ["subnet-priv-a", "subnet-priv-b"] |
| 88 | + existing_public_subnets = ["subnet-pub-a", "subnet-pub-b"] |
| 89 | + existing_user_security_group = "sg-00000000000000000" |
| 90 | + existing_user_subnets = null |
| 91 | + } |
| 92 | + |
| 93 | + assert { |
| 94 | + condition = strcontains(output.configuration_error, "use an existing VPC") |
| 95 | + error_message = "Existing-VPC config must be checked against the existing-network requirements" |
| 96 | + } |
| 97 | + |
| 98 | + assert { |
| 99 | + condition = !strcontains(output.configuration_error, "❌") |
| 100 | + error_message = "A valid existing-VPC config (internal = false) must satisfy every requirement" |
| 101 | + } |
| 102 | + |
| 103 | + assert { |
| 104 | + condition = output.vpc_id == "vpc-00000000000000000" |
| 105 | + error_message = "Existing-VPC mode must surface the supplied existing_vpc_id" |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +run "existing_vpc_internal_alb" { |
| 110 | + command = plan |
| 111 | + |
| 112 | + variables { |
| 113 | + create_new_vpc = false |
| 114 | + internal = true |
| 115 | + existing_vpc_id = "vpc-00000000000000000" |
| 116 | + existing_api_endpoint = "vpce-00000000000000000" |
| 117 | + existing_intra_subnets = ["subnet-intra-a", "subnet-intra-b"] |
| 118 | + existing_private_subnets = ["subnet-priv-a", "subnet-priv-b"] |
| 119 | + existing_public_subnets = null |
| 120 | + existing_user_security_group = "sg-00000000000000000" |
| 121 | + existing_user_subnets = ["subnet-user-a", "subnet-user-b"] |
| 122 | + } |
| 123 | + |
| 124 | + assert { |
| 125 | + condition = !strcontains(output.configuration_error, "❌") |
| 126 | + error_message = "A valid existing-VPC config (internal = true) must satisfy every requirement" |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +# --- Invalid: contradictory input must fail fast ----------------------------- |
| 131 | + |
| 132 | +run "new_vpc_with_existing_id_is_rejected" { |
| 133 | + command = plan |
| 134 | + |
| 135 | + variables { |
| 136 | + create_new_vpc = true |
| 137 | + internal = false |
| 138 | + existing_vpc_id = "vpc-00000000000000000" |
| 139 | + existing_api_endpoint = null |
| 140 | + existing_intra_subnets = null |
| 141 | + existing_private_subnets = null |
| 142 | + existing_public_subnets = null |
| 143 | + existing_user_security_group = null |
| 144 | + existing_user_subnets = null |
| 145 | + } |
| 146 | + |
| 147 | + # create_new_vpc = true while supplying an existing VPC id satisfies neither |
| 148 | + # the new-network nor the existing-network requirement set. |
| 149 | + expect_failures = [output.configuration_error] |
| 150 | +} |
| 151 | + |
| 152 | +run "existing_vpc_missing_inputs_is_rejected" { |
| 153 | + command = plan |
| 154 | + |
| 155 | + variables { |
| 156 | + create_new_vpc = false |
| 157 | + internal = false |
| 158 | + existing_vpc_id = "vpc-00000000000000000" |
| 159 | + existing_api_endpoint = null |
| 160 | + existing_intra_subnets = null |
| 161 | + existing_private_subnets = null |
| 162 | + existing_public_subnets = null |
| 163 | + existing_user_security_group = null |
| 164 | + existing_user_subnets = null |
| 165 | + } |
| 166 | + |
| 167 | + # create_new_vpc = false without the required existing_* inputs is incomplete. |
| 168 | + expect_failures = [output.configuration_error] |
| 169 | +} |
0 commit comments