github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_network_example_test.go (about)

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