|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Copyright 2018 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# #################################### # |
| 18 | +# Terraform tests # |
| 19 | +# #################################### # |
| 20 | + |
| 21 | +@test "Ensure that Terraform configures the dirs and download the plugins" { |
| 22 | + |
| 23 | + run terraform init |
| 24 | + [ "$status" -eq 0 ] |
| 25 | +} |
| 26 | + |
| 27 | +@test "Ensure that Terraform updates the plugins" { |
| 28 | + run terraform get |
| 29 | + [ "$status" -eq 0 ] |
| 30 | +} |
| 31 | + |
| 32 | +@test "Terraform plan, ensure connection and creation of resources" { |
| 33 | + |
| 34 | + run terraform plan |
| 35 | + [ "$status" -eq 0 ] |
| 36 | + [[ "$output" =~ 7\ to\ add ]] |
| 37 | + [[ "$output" =~ 0\ to\ change ]] |
| 38 | + [[ "$output" =~ 0\ to\ destroy ]] |
| 39 | +} |
| 40 | + |
| 41 | +@test "Terraform apply" { |
| 42 | + |
| 43 | + run terraform apply -auto-approve |
| 44 | + [ "$status" -eq 0 ] |
| 45 | + [[ "$output" =~ 7\ added ]] |
| 46 | + [[ "$output" =~ 0\ changed ]] |
| 47 | + [[ "$output" =~ 0\ destroyed ]] |
| 48 | +} |
| 49 | + |
| 50 | +# #################################### # |
| 51 | +# gcloud tests # |
| 52 | +# #################################### # |
| 53 | + |
| 54 | +@test "Test that the network was created with the correct settings" { |
| 55 | + |
| 56 | + NETWORK_NAME="$(terraform output network_name)" |
| 57 | + |
| 58 | + run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(autoCreateSubnetworks)'[no-pad] |
| 59 | + [ "$status" -eq 0 ] |
| 60 | + [[ "${lines[0]}" = "False" ]] |
| 61 | + |
| 62 | + run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(routingConfig.routingMode)'[no-pad] |
| 63 | + [ "$status" -eq 0 ] |
| 64 | + [[ "${lines[0]}" = "REGIONAL" ]] |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +@test "Test that the subnets were created with the correct settings" { |
| 69 | + |
| 70 | + NETWORK_NAME="$(terraform output network_name)" |
| 71 | + |
| 72 | + # test-network-01-subnet-01 |
| 73 | + SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[0]' -r)" |
| 74 | + SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[0]' -r)" |
| 75 | + SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[0]' -r)" |
| 76 | + SUBNET_SECONDARY_RANGE_IP="$(terraform output -json subnets_secondary_ranges | jq '.value[0] | .[0].ip_cidr_range' -r)" |
| 77 | + SUBNET_SECONDARY_RANGE_NAME="$(terraform output -json subnets_secondary_ranges | jq '.value[0] | .[0].range_name' -r)" |
| 78 | + |
| 79 | + run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[0])'[no-pad] |
| 80 | + [ "$status" -eq 0 ] |
| 81 | + [[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]] |
| 82 | + |
| 83 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad] |
| 84 | + [ "$status" -eq 0 ] |
| 85 | + [[ "${lines[0]}" = "${SUBNET_IP}" ]] |
| 86 | + |
| 87 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad] |
| 88 | + [ "$status" -eq 0 ] |
| 89 | + [[ "${lines[0]}" = "False" ]] |
| 90 | + |
| 91 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].ipCidrRange)'[no-pad] |
| 92 | + [ "$status" -eq 0 ] |
| 93 | + [[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_IP}" ]] |
| 94 | + |
| 95 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].rangeName)'[no-pad] |
| 96 | + [ "$status" -eq 0 ] |
| 97 | + [[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_NAME}" ]] |
| 98 | + |
| 99 | + # test-network-01-subnet-02 |
| 100 | + SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[1]' -r)" |
| 101 | + SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[1]' -r)" |
| 102 | + SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[1]' -r)" |
| 103 | + SUBNET_SECONDARY_RANGE_IP="$(terraform output -json subnets_secondary_ranges | jq '.value[1] | .[0].ip_cidr_range' -r)" |
| 104 | + SUBNET_SECONDARY_RANGE_NAME="$(terraform output -json subnets_secondary_ranges | jq '.value[1] | .[0].range_name' -r)" |
| 105 | + |
| 106 | + run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[1])'[no-pad] |
| 107 | + [ "$status" -eq 0 ] |
| 108 | + [[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]] |
| 109 | + |
| 110 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad] |
| 111 | + [ "$status" -eq 0 ] |
| 112 | + [[ "${lines[0]}" = "${SUBNET_IP}" ]] |
| 113 | + |
| 114 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad] |
| 115 | + [ "$status" -eq 0 ] |
| 116 | + [[ "${lines[0]}" = "False" ]] |
| 117 | + |
| 118 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].ipCidrRange)'[no-pad] |
| 119 | + [ "$status" -eq 0 ] |
| 120 | + [[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_IP}" ]] |
| 121 | + |
| 122 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(secondaryIpRanges[0].rangeName)'[no-pad] |
| 123 | + [ "$status" -eq 0 ] |
| 124 | + [[ "${lines[0]}" = "${SUBNET_SECONDARY_RANGE_NAME}" ]] |
| 125 | + |
| 126 | + # test-network-01-subnet-03 |
| 127 | + SUBNET_NAME="$(terraform output -json subnets_names | jq '.value[2]' -r)" |
| 128 | + SUBNET_REGION="$(terraform output -json subnets_regions | jq '.value[2]' -r)" |
| 129 | + SUBNET_IP="$(terraform output -json subnets_ips | jq '.value[2]' -r)" |
| 130 | + |
| 131 | + run gcloud --project=${PROJECT_ID} compute networks describe ${NETWORK_NAME} --format='get(subnetworks[2])'[no-pad] |
| 132 | + [ "$status" -eq 0 ] |
| 133 | + [[ "${lines[0]}" = "https://www.googleapis.com/compute/v1/projects/${PROJECT_ID}/regions/${SUBNET_REGION}/subnetworks/${SUBNET_NAME}" ]] |
| 134 | + |
| 135 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(ipCidrRange)'[no-pad] |
| 136 | + [ "$status" -eq 0 ] |
| 137 | + [[ "${lines[0]}" = "${SUBNET_IP}" ]] |
| 138 | + |
| 139 | + run gcloud --project=${PROJECT_ID} compute networks subnets describe ${SUBNET_NAME} --region=${SUBNET_REGION} --format='get(privateIpGoogleAccess)'[no-pad] |
| 140 | + [ "$status" -eq 0 ] |
| 141 | + [[ "${lines[0]}" = "False" ]] |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +# #################################### # |
| 146 | +# Terraform destroy test # |
| 147 | +# #################################### # |
| 148 | + |
| 149 | +@test "Terraform destroy" { |
| 150 | + run terraform destroy -force |
| 151 | + [ "$status" -eq 0 ] |
| 152 | + [[ "$output" =~ 7\ destroyed ]] |
| 153 | +} |
0 commit comments