github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/azure/terraform_azure_keyvault_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 "testing" 10 11 "github.com/gruntwork-io/terratest/modules/azure" 12 "github.com/gruntwork-io/terratest/modules/random" 13 "github.com/gruntwork-io/terratest/modules/terraform" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestTerraformAzureKeyVaultExample(t *testing.T) { 18 t.Parallel() 19 20 uniquePostfix := random.UniqueId() 21 22 // website::tag::1:: Configure Terraform setting up a path to Terraform code. 23 terraformOptions := &terraform.Options{ 24 // The path to where our Terraform code is located 25 TerraformDir: "../../examples/azure/terraform-azure-keyvault-example", 26 Vars: map[string]interface{}{ 27 "postfix": uniquePostfix, 28 }, 29 } 30 31 // website::tag::6:: At the end of the test, run `terraform destroy` to clean up any resources that were created 32 defer terraform.Destroy(t, terraformOptions) 33 34 // website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors. 35 terraform.InitAndApply(t, terraformOptions) 36 37 // website::tag::3:: Run `terraform output` to get the values of output variables 38 resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name") 39 keyVaultName := terraform.Output(t, terraformOptions, "key_vault_name") 40 expectedSecretName := terraform.Output(t, terraformOptions, "secret_name") 41 expectedKeyName := terraform.Output(t, terraformOptions, "key_name") 42 expectedCertificateName := terraform.Output(t, terraformOptions, "certificate_name") 43 44 // website::tag::4:: Determine whether the keyvault exists 45 keyVault := azure.GetKeyVault(t, resourceGroupName, keyVaultName, "") 46 assert.Equal(t, keyVaultName, *keyVault.Name) 47 48 // website::tag::5:: Determine whether the secret, key, and certificate exists 49 secretExists := azure.KeyVaultSecretExists(t, keyVaultName, expectedSecretName) 50 assert.True(t, secretExists, "kv-secret does not exist") 51 52 keyExists := azure.KeyVaultKeyExists(t, keyVaultName, expectedKeyName) 53 assert.True(t, keyExists, "kv-key does not exist") 54 55 certificateExists := azure.KeyVaultCertificateExists(t, keyVaultName, expectedCertificateName) 56 assert.True(t, certificateExists, "kv-cert does not exist") 57 }