github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_aks_example_test.go (about) 1 //go:build azure 2 // +build azure 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 "crypto/tls" 11 "fmt" 12 "path/filepath" 13 "strings" 14 "testing" 15 "time" 16 17 "github.com/gruntwork-io/terratest/modules/azure" 18 http_helper "github.com/gruntwork-io/terratest/modules/http-helper" 19 "github.com/gruntwork-io/terratest/modules/k8s" 20 "github.com/gruntwork-io/terratest/modules/random" 21 "github.com/gruntwork-io/terratest/modules/terraform" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestTerraformAzureAKSExample(t *testing.T) { 27 t.Parallel() 28 // MC_+ResourceGroupName_ClusterName_AzureRegion must be no greater than 80 chars. 29 // https://docs.microsoft.com/en-us/azure/aks/troubleshooting#what-naming-restrictions-are-enforced-for-aks-resources-and-parameters 30 expectedClusterName := fmt.Sprintf("terratest-aks-cluster-%s", random.UniqueId()) 31 expectedResourceGroupName := fmt.Sprintf("terratest-aks-rg-%s", random.UniqueId()) 32 expectedAagentCount := 3 33 34 terraformOptions := &terraform.Options{ 35 TerraformDir: "../../examples/azure/terraform-azure-aks-example", 36 Vars: map[string]interface{}{ 37 "cluster_name": expectedClusterName, 38 "resource_group_name": expectedResourceGroupName, 39 "agent_count": expectedAagentCount, 40 }, 41 } 42 // At the end of the test, run `terraform destroy` to clean up any resources that were created 43 defer terraform.Destroy(t, terraformOptions) 44 45 // This will run `terraform init` and `terraform apply` and fail the test if there are any errors 46 terraform.InitAndApply(t, terraformOptions) 47 48 // Look up the cluster node count 49 cluster, err := azure.GetManagedClusterE(t, expectedResourceGroupName, expectedClusterName, "") 50 require.NoError(t, err) 51 actualCount := *(*cluster.ManagedClusterProperties.AgentPoolProfiles)[0].Count 52 53 // Test that the Node count matches the Terraform specification 54 assert.Equal(t, int32(expectedAagentCount), actualCount) 55 56 // Path to the Kubernetes resource config we will test 57 kubeResourcePath, err := filepath.Abs("../../examples/azure/terraform-azure-aks-example/nginx-deployment.yml") 58 require.NoError(t, err) 59 60 // To ensure we can reuse the resource config on the same cluster to test different scenarios, we setup a unique 61 // namespace for the resources for this test. 62 // Note that namespaces must be lowercase. 63 namespaceName := strings.ToLower(random.UniqueId()) 64 65 // Setup the kubectl config and context. Here we choose to use the defaults, which is: 66 // - HOME/.kube/config for the kubectl config file 67 // - Current context of the kubectl config file 68 options := k8s.NewKubectlOptions("", "../../examples/azure/terraform-azure-aks-example/kubeconfig", namespaceName) 69 70 k8s.CreateNamespace(t, options, namespaceName) 71 // ... and make sure to delete the namespace at the end of the test 72 defer k8s.DeleteNamespace(t, options, namespaceName) 73 74 // At the end of the test, run `kubectl delete -f RESOURCE_CONFIG` to clean up any resources that were created. 75 defer k8s.KubectlDelete(t, options, kubeResourcePath) 76 77 // This will run `kubectl apply -f RESOURCE_CONFIG` and fail the test if there are any errors 78 k8s.KubectlApply(t, options, kubeResourcePath) 79 80 // This will wait up to 10 seconds for the service to become available, to ensure that we can access it. 81 k8s.WaitUntilServiceAvailable(t, options, "nginx-service", 10, 20*time.Second) 82 // Now we verify that the service will successfully boot and start serving requests 83 service := k8s.GetService(t, options, "nginx-service") 84 endpoint := k8s.GetServiceEndpoint(t, options, service, 80) 85 86 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 87 tlsConfig := tls.Config{} 88 89 // Test the endpoint for up to 5 minutes. This will only fail if we timeout waiting for the service to return a 200 90 // response. 91 http_helper.HttpGetWithRetryWithCustomValidation( 92 t, 93 fmt.Sprintf("http://%s", endpoint), 94 &tlsConfig, 95 30, 96 10*time.Second, 97 func(statusCode int, body string) bool { 98 return statusCode == 200 99 }, 100 ) 101 }