github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_recoveryservices_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  	"testing"
    11  
    12  	"github.com/gruntwork-io/terratest/modules/azure"
    13  	"github.com/gruntwork-io/terratest/modules/random"
    14  	"github.com/gruntwork-io/terratest/modules/terraform"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestTerraformAzureRecoveryServicesExample(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	// subscriptionID is overridden by the environment variable "ARM_SUBSCRIPTION_ID"
    22  	subscriptionID := ""
    23  	uniquePostfix := 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-recoveryservices-example",
    29  		// Variables to pass to our Terraform code using -var options
    30  		Vars: map[string]interface{}{
    31  			"postfix": uniquePostfix,
    32  		},
    33  	}
    34  
    35  	// website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created
    36  	defer terraform.Destroy(t, terraformOptions)
    37  
    38  	// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
    39  	terraform.InitAndApply(t, terraformOptions)
    40  
    41  	// website::tag::3:: Run `terraform output` to get the values of output variables
    42  	resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name")
    43  	vaultName := terraform.Output(t, terraformOptions, "recovery_service_vault_name")
    44  	policyVmName := terraform.Output(t, terraformOptions, "backup_policy_vm_name")
    45  
    46  	// website::tag::4:: Verify the recovery services resources
    47  	exists := azure.RecoveryServicesVaultExists(t, vaultName, resourceGroupName, subscriptionID)
    48  	assert.True(t, exists, "vault does not exist")
    49  
    50  	policyList := azure.GetRecoveryServicesVaultBackupPolicyList(t, vaultName, resourceGroupName, subscriptionID)
    51  	assert.NotNil(t, policyList, "vault backup policy list is nil")
    52  
    53  	vmPolicyList := azure.GetRecoveryServicesVaultBackupProtectedVMList(t, policyVmName, vaultName, resourceGroupName, subscriptionID)
    54  	assert.NotNil(t, vmPolicyList, "vault backup policy list for protected vm is nil")
    55  }