Skip to content

Commit 6fa1b88

Browse files
committed
Refactor integration tests to include random names
This commit updates the integration test fixtures to allow for randomized subnetwork names as well as randomized network names. Without this commit the integration test fixtures will create VPC networks with randomized names, but the subnetwork names will remain static. Because subnetworks are regional resources, you can't have two subnetworks with the same name tied to the same project and region. This means that without adding randomness to subnetwork names, only 1 run of the integration test pipeline can occur at any given time. Normally a `random_string` resource is used to impart randomness into resource names, but because of the way lookups are performed on the `subnets` and `secondary_ranges` input variables, those variables cannot contain computed values. To remedy this the test fixtures in `test/fixtures/<scenario name>` now include an input variable named `random_string_for_testing` that accepts a string to be appended on both the network and subnetwork names. The `test/ci_integration.sh` script has been modified to look for an environment variable named `RANDOM_STRING_FOR_TESTING` and pass that to `var.random_string_for_testing`. If that environment variable has not been declared then a 5-character string is generated on the fly and used. This commit also contains an update to `Makefile` to ensure that `test/ci_integration.sh` is sourced and the `setup_environment()` function is called for any of the `docker_*` make targets. The `Makefile` has also been modified to ensure that the environment variables that are referenced by `test/ci_integration.sh` are being passed down to the Docker container.
1 parent 1d7ff1e commit 6fa1b88

File tree

27 files changed

+199
-143
lines changed

27 files changed

+199
-143
lines changed

Makefile

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,50 +84,60 @@ docker_run:
8484
docker run --rm -it \
8585
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
8686
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
87-
-e TF_VAR_project_id \
87+
-e PROJECT_ID \
88+
-e SERVICE_ACCOUNT_JSON \
89+
-e RANDOM_STRING_FOR_TESTING \
8890
-v $(CURDIR):/cft/workdir \
8991
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
90-
/bin/bash
92+
/bin/bash -c 'source test/ci_integration.sh && setup_environment && exec /bin/bash'
9193

9294
.PHONY: docker_create
9395
docker_create:
9496
docker run --rm -it \
9597
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
9698
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
97-
-e TF_VAR_project_id \
99+
-e PROJECT_ID \
100+
-e SERVICE_ACCOUNT_JSON \
101+
-e RANDOM_STRING_FOR_TESTING \
98102
-v $(CURDIR):/cft/workdir \
99103
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
100-
/bin/bash -c "kitchen create"
104+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen create"
101105

102106
.PHONY: docker_converge
103107
docker_converge:
104108
docker run --rm -it \
105109
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
106110
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
107-
-e TF_VAR_project_id \
111+
-e PROJECT_ID \
112+
-e SERVICE_ACCOUNT_JSON \
113+
-e RANDOM_STRING_FOR_TESTING \
108114
-v $(CURDIR):/cft/workdir \
109115
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
110-
/bin/bash -c "kitchen converge && kitchen converge"
116+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen converge && kitchen converge"
111117

112118
.PHONY: docker_verify
113119
docker_verify:
114120
docker run --rm -it \
115121
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
116122
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
117-
-e TF_VAR_project_id \
123+
-e PROJECT_ID \
124+
-e SERVICE_ACCOUNT_JSON \
125+
-e RANDOM_STRING_FOR_TESTING \
118126
-v $(CURDIR):/cft/workdir \
119127
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
120-
/bin/bash -c "kitchen verify"
128+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen verify"
121129

122130
.PHONY: docker_destroy
123131
docker_destroy:
124132
docker run --rm -it \
125133
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
126134
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
127-
-e TF_VAR_project_id \
135+
-e PROJECT_ID \
136+
-e SERVICE_ACCOUNT_JSON \
137+
-e RANDOM_STRING_FOR_TESTING \
128138
-v $(CURDIR):/cft/workdir \
129139
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
130-
/bin/bash -c "kitchen destroy"
140+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen destroy"
131141

132142
.PHONY: test_integration_docker
133143
test_integration_docker: docker_create docker_converge docker_verify docker_destroy

examples/delete_default_gateway_routes/main.tf

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,24 @@
1515
*/
1616

1717
locals {
18-
network_name = "test-network-${random_string.random_suffix.result}"
19-
}
20-
21-
resource "random_string" "random_suffix" {
22-
length = 4
23-
upper = "false"
24-
special = "false"
18+
subnet_01 = "${var.network_name}-subnet-01"
2519
}
2620

2721
module "test-vpc-module" {
2822
source = "../../"
2923
project_id = "${var.project_id}"
30-
network_name = "${local.network_name}"
24+
network_name = "${var.network_name}"
3125
delete_default_internet_gateway_routes = "true"
3226

3327
subnets = [
3428
{
35-
subnet_name = "subnet-41"
29+
subnet_name = "${local.subnet_01}"
3630
subnet_ip = "10.20.30.0/24"
3731
subnet_region = "us-west1"
3832
},
3933
]
4034

4135
secondary_ranges = {
42-
subnet-41 = []
36+
"${local.subnet_01}" = []
4337
}
4438
}

examples/delete_default_gateway_routes/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
variable "project_id" {
1818
description = "The project ID to host the network in"
1919
}
20+
21+
variable "network_name" {
22+
description = "The name of the VPC network being created"
23+
}

examples/multi_vpc/main.tf

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616

1717
locals {
18-
network_01_name = "test-network-301"
19-
network_02_name = "test-network-302"
18+
network_01_subnet_01 = "${var.network_01_name}-subnet-01"
19+
network_01_subnet_02 = "${var.network_01_name}-subnet-02"
20+
network_01_subnet_03 = "${var.network_01_name}-subnet-03"
21+
network_02_subnet_01 = "${var.network_02_name}-subnet-01"
22+
network_02_subnet_02 = "${var.network_02_name}-subnet-02"
2023

2124
network_01_routes = [
2225
{
23-
name = "${local.network_01_name}-egress-inet"
26+
name = "${var.network_01_name}-egress-inet"
2427
description = "route through IGW to access internet"
2528
destination_range = "0.0.0.0/0"
2629
tags = "egress-inet"
@@ -30,14 +33,14 @@ locals {
3033

3134
network_02_routes = [
3235
{
33-
name = "${local.network_02_name}-egress-inet"
36+
name = "${var.network_02_name}-egress-inet"
3437
description = "route through IGW to access internet"
3538
destination_range = "0.0.0.0/0"
3639
tags = "egress-inet"
3740
next_hop_internet = "true"
3841
},
3942
{
40-
name = "${local.network_02_name}-testapp-proxy"
43+
name = "${var.network_02_name}-testapp-proxy"
4144
description = "route through proxy to reach app"
4245
destination_range = "10.50.10.0/24"
4346
tags = "app-proxy"
@@ -46,40 +49,28 @@ locals {
4649
]
4750
}
4851

49-
resource "random_string" "random_suffix_01" {
50-
length = 4
51-
upper = "false"
52-
special = "false"
53-
}
54-
55-
resource "random_string" "random_suffix_02" {
56-
length = 4
57-
upper = "false"
58-
special = "false"
59-
}
60-
6152
module "test-vpc-module-01" {
6253
source = "../../"
6354
project_id = "${var.project_id}"
64-
network_name = "test-network-${random_string.random_suffix_01.result}"
55+
network_name = "${var.network_01_name}"
6556

6657
subnets = [
6758
{
68-
subnet_name = "${local.network_01_name}-subnet-01"
59+
subnet_name = "${local.network_01_subnet_01}"
6960
subnet_ip = "10.10.10.0/24"
7061
subnet_region = "us-west1"
7162
subnet_private_access = "false"
7263
subnet_flow_logs = "true"
7364
},
7465
{
75-
subnet_name = "${local.network_01_name}-subnet-02"
66+
subnet_name = "${local.network_01_subnet_02}"
7667
subnet_ip = "10.10.20.0/24"
7768
subnet_region = "us-west1"
7869
subnet_private_access = "false"
7970
subnet_flow_logs = "true"
8071
},
8172
{
82-
subnet_name = "${local.network_01_name}-subnet-03"
73+
subnet_name = "${local.network_01_subnet_03}"
8374
subnet_ip = "10.10.30.0/24"
8475
subnet_region = "us-west1"
8576
subnet_private_access = "false"
@@ -88,25 +79,25 @@ module "test-vpc-module-01" {
8879
]
8980

9081
secondary_ranges = {
91-
"${local.network_01_name}-subnet-01" = [
82+
"${local.network_01_subnet_01}" = [
9283
{
93-
range_name = "${local.network_01_name}-subnet-01-01"
84+
range_name = "${local.network_01_subnet_01}-01"
9485
ip_cidr_range = "192.168.64.0/24"
9586
},
9687
{
97-
range_name = "${local.network_01_name}-subnet-01-02"
88+
range_name = "${local.network_01_subnet_01}-02"
9889
ip_cidr_range = "192.168.65.0/24"
9990
},
10091
]
10192

102-
"${local.network_01_name}-subnet-02" = [
93+
"${local.network_01_subnet_02}" = [
10394
{
104-
range_name = "${local.network_01_name}-subnet-02-01"
95+
range_name = "${local.network_02_subnet_01}-01"
10596
ip_cidr_range = "192.168.74.0/24"
10697
},
10798
]
10899

109-
"${local.network_01_name}-subnet-03" = []
100+
"${local.network_01_subnet_03}" = []
110101
}
111102

112103
routes = "${local.network_01_routes}"
@@ -115,18 +106,18 @@ module "test-vpc-module-01" {
115106
module "test-vpc-module-02" {
116107
source = "../../"
117108
project_id = "${var.project_id}"
118-
network_name = "test-network-${random_string.random_suffix_02.result}"
109+
network_name = "${var.network_02_name}"
119110

120111
subnets = [
121112
{
122-
subnet_name = "${local.network_02_name}-subnet-01"
113+
subnet_name = "${local.network_02_subnet_01}"
123114
subnet_ip = "10.10.40.0/24"
124115
subnet_region = "us-west1"
125116
subnet_private_access = "false"
126117
subnet_flow_logs = "true"
127118
},
128119
{
129-
subnet_name = "${local.network_02_name}-subnet-02"
120+
subnet_name = "${local.network_02_subnet_02}"
130121
subnet_ip = "10.10.50.0/24"
131122
subnet_region = "us-west1"
132123
subnet_private_access = "false"
@@ -135,14 +126,14 @@ module "test-vpc-module-02" {
135126
]
136127

137128
secondary_ranges = {
138-
"${local.network_02_name}-subnet-01" = [
129+
"${local.network_02_subnet_01}" = [
139130
{
140-
range_name = "${local.network_02_name}-subnet-02-01"
131+
range_name = "${local.network_02_subnet_02}-01"
141132
ip_cidr_range = "192.168.75.0/24"
142133
},
143134
]
144135

145-
"${local.network_02_name}-subnet-02" = []
136+
"${local.network_02_subnet_02}" = []
146137
}
147138

148139
routes = "${local.network_02_routes}"

examples/multi_vpc/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@
1717
variable "project_id" {
1818
description = "The project ID to host the network in"
1919
}
20+
21+
variable "network_01_name" {
22+
description = "The name of the first VPC network being created"
23+
}
24+
25+
variable "network_02_name" {
26+
description = "The name of the second VPC network being created"
27+
}

examples/secondary_ranges/main.tf

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,53 @@
1515
*/
1616

1717
locals {
18-
network_name = "test-network-${random_string.random_suffix.result}"
19-
}
20-
21-
resource "random_string" "random_suffix" {
22-
length = 4
23-
upper = "false"
24-
special = "false"
18+
subnet_01 = "${var.network_name}-subnet-01"
19+
subnet_02 = "${var.network_name}-subnet-02"
20+
subnet_03 = "${var.network_name}-subnet-03"
2521
}
2622

2723
module "vpc-secondary-ranges" {
2824
source = "../../"
2925
project_id = "${var.project_id}"
30-
network_name = "${local.network_name}"
26+
network_name = "${var.network_name}"
3127

3228
subnets = [
3329
{
34-
subnet_name = "secondary-ranges-subnet-01"
30+
subnet_name = "${local.subnet_01}"
3531
subnet_ip = "10.10.10.0/24"
3632
subnet_region = "us-west1"
3733
},
3834
{
39-
subnet_name = "secondary-ranges-subnet-02"
35+
subnet_name = "${local.subnet_02}"
4036
subnet_ip = "10.10.20.0/24"
4137
subnet_region = "us-west1"
4238
subnet_private_access = "true"
4339
subnet_flow_logs = "true"
4440
},
4541
{
46-
subnet_name = "secondary-ranges-subnet-03"
42+
subnet_name = "${local.subnet_03}"
4743
subnet_ip = "10.10.30.0/24"
4844
subnet_region = "us-west1"
4945
},
5046
]
5147

5248
secondary_ranges = {
53-
secondary-ranges-subnet-01 = [
49+
"${local.subnet_01}" = [
5450
{
55-
range_name = "subnet-01-01"
51+
range_name = "${local.subnet_01}-01"
5652
ip_cidr_range = "192.168.64.0/24"
5753
},
5854
{
59-
range_name = "subnet-01-02"
55+
range_name = "${local.subnet_01}-02"
6056
ip_cidr_range = "192.168.65.0/24"
6157
},
6258
]
6359

64-
secondary-ranges-subnet-02 = []
60+
"${local.subnet_02}" = []
6561

66-
secondary-ranges-subnet-03 = [
62+
"${local.subnet_03}" = [
6763
{
68-
range_name = "subnet-03-01"
64+
range_name = "${local.subnet_03}-01"
6965
ip_cidr_range = "192.168.66.0/24"
7066
},
7167
]

examples/secondary_ranges/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
variable "project_id" {
1818
description = "The project ID to host the network in"
1919
}
20+
21+
variable "network_name" {
22+
description = "The name of the VPC network being created"
23+
}

0 commit comments

Comments
 (0)