github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/azure/terraform_azure_network_example_test.go (about) 1 // +build azure azureslim,network 2 3 // NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for 4 // CircleCI. 5 6 package test 7 8 import ( 9 "fmt" 10 "strings" 11 "testing" 12 13 "github.com/gruntwork-io/terratest/modules/azure" 14 "github.com/gruntwork-io/terratest/modules/random" 15 "github.com/gruntwork-io/terratest/modules/terraform" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestTerraformAzureNetworkExample(t *testing.T) { 20 t.Parallel() 21 22 // Create values for Terraform 23 subscriptionID := "" // subscriptionID is overridden by the environment variable "ARM_SUBSCRIPTION_ID" 24 uniquePostfix := random.UniqueId() // "resource" - switch for terratest or manual terraform deployment 25 expectedLocation := "eastus2" 26 expectedSubnetRange := "10.0.20.0/24" 27 expectedPrivateIP := "10.0.20.5" 28 expectedDnsIp01 := "10.0.0.5" 29 expectedDnsIp02 := "10.0.0.6" 30 exectedDNSLabel := fmt.Sprintf("dns-terratest-%s", strings.ToLower(uniquePostfix)) // only lowercase, numeric and hyphens chars allowed for DNS 31 32 // Configure Terraform setting up a path to Terraform code. 33 terraformOptions := &terraform.Options{ 34 // Relative path to the Terraform dir 35 TerraformDir: "../../examples/azure/terraform-azure-network-example", 36 37 // Variables to pass to our Terraform code using -var options. 38 Vars: map[string]interface{}{ 39 "postfix": uniquePostfix, 40 "subnet_prefix": expectedSubnetRange, 41 "private_ip": expectedPrivateIP, 42 "dns_ip_01": expectedDnsIp01, 43 "dns_ip_02": expectedDnsIp02, 44 "location": expectedLocation, 45 "domain_name_label": exectedDNSLabel, 46 }, 47 } 48 49 // At the end of the test, run `terraform destroy` to clean up any resources that were created 50 defer terraform.Destroy(t, terraformOptions) 51 52 // Run `terraform init` and `terraform apply`. Fail the test if there are any errors 53 terraform.InitAndApply(t, terraformOptions) 54 55 // Run `terraform output` to get the values of output variables 56 expectedRgName := terraform.Output(t, terraformOptions, "resource_group_name") 57 expectedVNetName := terraform.Output(t, terraformOptions, "virtual_network_name") 58 expectedSubnetName := terraform.Output(t, terraformOptions, "subnet_name") 59 expectedPublicAddressName := terraform.Output(t, terraformOptions, "public_address_name") 60 expectedPrivateNicName := terraform.Output(t, terraformOptions, "network_interface_internal") 61 expectedPublicNicName := terraform.Output(t, terraformOptions, "network_interface_external") 62 63 // Tests are separated into subtests to differentiate integrated tests and pure resource tests 64 65 // Integrated network resource tests 66 t.Run("VirtualNetwork_Subnet", func(t *testing.T) { 67 // Check the Subnet exists in the Virtual Network Subnets with the expected Address Prefix 68 actualVnetSubnets := azure.GetVirtualNetworkSubnets(t, expectedVNetName, expectedRgName, subscriptionID) 69 assert.NotNil(t, actualVnetSubnets[expectedSubnetName]) 70 assert.Equal(t, expectedSubnetRange, actualVnetSubnets[expectedSubnetName]) 71 }) 72 73 t.Run("NIC_PublicAddress", func(t *testing.T) { 74 // Check the internal network interface does NOT have a public IP 75 actualPrivateIPOnly := azure.GetNetworkInterfacePublicIPs(t, expectedPrivateNicName, expectedRgName, subscriptionID) 76 assert.Equal(t, 0, len(actualPrivateIPOnly)) 77 78 // Check the external network interface has a public IP 79 actualPublicIPs := azure.GetNetworkInterfacePublicIPs(t, expectedPublicNicName, expectedRgName, subscriptionID) 80 assert.Equal(t, 1, len(actualPublicIPs)) 81 }) 82 83 t.Run("Subnet_NIC", func(t *testing.T) { 84 // Check the private IP is in the subnet range 85 checkPrivateIpInSubnet := azure.CheckSubnetContainsIP(t, expectedPrivateIP, expectedSubnetName, expectedVNetName, expectedRgName, subscriptionID) 86 assert.True(t, checkPrivateIpInSubnet) 87 }) 88 89 // Test for resource presence 90 t.Run("Exists", func(t *testing.T) { 91 // Check the Virtual Network exists 92 assert.True(t, azure.VirtualNetworkExists(t, expectedVNetName, expectedRgName, subscriptionID)) 93 94 // Check the Subnet exists 95 assert.True(t, azure.SubnetExists(t, expectedSubnetName, expectedVNetName, expectedRgName, subscriptionID)) 96 97 // Check the Network Interfaces exist 98 assert.True(t, azure.NetworkInterfaceExists(t, expectedPrivateNicName, expectedRgName, subscriptionID)) 99 assert.True(t, azure.NetworkInterfaceExists(t, expectedPublicNicName, expectedRgName, subscriptionID)) 100 101 // Check Network Interface that does not exist in the Resource Group 102 assert.False(t, azure.NetworkInterfaceExists(t, "negative-test", expectedRgName, subscriptionID)) 103 104 // Check Public Address exists 105 assert.True(t, azure.PublicAddressExists(t, expectedPublicAddressName, expectedRgName, subscriptionID)) 106 }) 107 108 // Tests for useful network properties 109 t.Run("Network", func(t *testing.T) { 110 // Check the Virtual Network DNS server IPs 111 actualDNSIPs := azure.GetVirtualNetworkDNSServerIPs(t, expectedVNetName, expectedRgName, subscriptionID) 112 assert.Contains(t, actualDNSIPs, expectedDnsIp01) 113 assert.Contains(t, actualDNSIPs, expectedDnsIp02) 114 115 // Check the Network Interface private IP 116 actualPrivateIPs := azure.GetNetworkInterfacePrivateIPs(t, expectedPrivateNicName, expectedRgName, subscriptionID) 117 assert.Contains(t, actualPrivateIPs, expectedPrivateIP) 118 119 // Check the Public Address's Public IP is allocated 120 actualPublicIP := azure.GetIPOfPublicIPAddressByName(t, expectedPublicAddressName, expectedRgName, subscriptionID) 121 assert.NotEmpty(t, actualPublicIP) 122 123 // Check DNS created for this example is reserved 124 actualDnsNotAvailable := azure.CheckPublicDNSNameAvailability(t, expectedLocation, exectedDNSLabel, subscriptionID) 125 assert.False(t, actualDnsNotAvailable) 126 127 // Check new randomized DNS is available 128 newDNSLabel := fmt.Sprintf("dns-terratest-%s", strings.ToLower(random.UniqueId())) 129 actualDnsAvailable := azure.CheckPublicDNSNameAvailability(t, expectedLocation, newDNSLabel, subscriptionID) 130 assert.True(t, actualDnsAvailable) 131 }) 132 133 }