github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_loganalytics_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  	"strconv"
    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 TestTerraformAzureLogAnalyticsExample(t *testing.T) {
    21  	t.Parallel()
    22  
    23  	// subscriptionID is overridden by the environment variable "ARM_SUBSCRIPTION_ID"
    24  	subscriptionID := ""
    25  	uniquePostfix := random.UniqueId()
    26  
    27  	// website::tag::1:: Configure Terraform setting up a path to Terraform code.
    28  	terraformOptions := &terraform.Options{
    29  		// The path to where our Terraform code is located
    30  		TerraformDir: "../../examples/azure/terraform-azure-loganalytics-example",
    31  		// Variables to pass to our Terraform code using -var options
    32  		Vars: map[string]interface{}{
    33  			"postfix": uniquePostfix,
    34  		},
    35  	}
    36  
    37  	// website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created
    38  	defer terraform.Destroy(t, terraformOptions)
    39  
    40  	// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
    41  	terraform.InitAndApply(t, terraformOptions)
    42  
    43  	// website::tag::3:: Run `terraform output` to get the values of output variables
    44  	resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name")
    45  	workspaceName := terraform.Output(t, terraformOptions, "loganalytics_workspace_name")
    46  	sku := terraform.Output(t, terraformOptions, "loganalytics_workspace_sku")
    47  	retentionPeriodString := terraform.Output(t, terraformOptions, "loganalytics_workspace_retention")
    48  
    49  	// website::tag::4:: Verify the Log Analytics properties and ensure it matches the output.
    50  	workspaceExists := azure.LogAnalyticsWorkspaceExists(t, workspaceName, resourceGroupName, subscriptionID)
    51  	assert.True(t, workspaceExists, "log analytics workspace not found.")
    52  
    53  	actualWorkspace := azure.GetLogAnalyticsWorkspace(t, workspaceName, resourceGroupName, subscriptionID)
    54  
    55  	actualSku := string(actualWorkspace.Sku.Name)
    56  	assert.Equal(t, strings.ToLower(sku), strings.ToLower(actualSku), "log analytics sku mismatch")
    57  
    58  	actualRetentionPeriod := *actualWorkspace.RetentionInDays
    59  	expectedPeriod, _ := strconv.ParseInt(retentionPeriodString, 10, 32)
    60  	assert.Equal(t, int32(expectedPeriod), actualRetentionPeriod, "log analytics retention period mismatch")
    61  }