github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_servicebus_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  	"os"
    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 TestTerraformAzureServiceBusExample(t *testing.T) {
    21  	t.Parallel()
    22  
    23  	uniquePostfix := strings.ToLower(random.UniqueId())
    24  
    25  	// website::tag::1:: Configure Terraform setting up a path to Terraform code.
    26  	terraformOptions := &terraform.Options{
    27  		// The path to where our Terraform code is located
    28  		TerraformDir: "../../examples/azure/terraform-azure-servicebus-example",
    29  		Vars: map[string]interface{}{
    30  			"postfix": uniquePostfix,
    31  		},
    32  	}
    33  
    34  	// website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created
    35  	defer terraform.Destroy(t, terraformOptions)
    36  
    37  	// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
    38  	terraform.InitAndApply(t, terraformOptions)
    39  
    40  	// website::tag::3:: Run `terraform output` to get the values of output variables
    41  	expectedTopicSubscriptionsMap := terraform.OutputMapOfObjects(t, terraformOptions, "topics")
    42  	expectedNamespaceName := terraform.Output(t, terraformOptions, "namespace_name")
    43  	expectedResourceGroup := terraform.Output(t, terraformOptions, "resource_group")
    44  
    45  	for topicName, topicsMap := range expectedTopicSubscriptionsMap {
    46  		actualsubscriptionNames := azure.ListTopicSubscriptionsName(t,
    47  			os.Getenv("ARM_SUBSCRIPTION_ID"),
    48  			expectedNamespaceName,
    49  			expectedResourceGroup,
    50  			topicName)
    51  
    52  		subscriptionsMap := topicsMap.(map[string]interface{})["subscriptions"].(map[string]interface{})
    53  		subscriptionNamesFromOutput := getMapKeylist(subscriptionsMap)
    54  		// each subscription from the output should also exist in Azure
    55  		assert.Equal(t, len(subscriptionNamesFromOutput), len(actualsubscriptionNames))
    56  		for _, subscrptionName := range subscriptionNamesFromOutput {
    57  			assert.Contains(t, actualsubscriptionNames, subscrptionName)
    58  		}
    59  	}
    60  }
    61  
    62  func getMapKeylist(mapList map[string]interface{}) []string {
    63  	names := make([]string, 0)
    64  	for key := range mapList {
    65  		names = append(names, key)
    66  	}
    67  	return names
    68  }