github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/azure/terraform_azure_aks_example_test.go (about)

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