github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/azure/terraform_azure_sqldb_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 "strings" 10 "testing" 11 12 "github.com/Azure/azure-sdk-for-go/services/sql/mgmt/2014-04-01/sql" 13 "github.com/gruntwork-io/terratest/modules/azure" 14 "github.com/gruntwork-io/terratest/modules/random" 15 "github.com/gruntwork-io/terratest/modules/terraform" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestTerraformAzureSQLDBExample(t *testing.T) { 20 t.Parallel() 21 22 uniquePostfix := strings.ToLower(random.UniqueId()) 23 24 // website::tag::1:: Configure Terraform setting up a path to Terraform code. 25 terraformOptions := &terraform.Options{ 26 // The path to where our Terraform code is located 27 TerraformDir: "../../examples/azure/terraform-azure-sqldb-example", 28 Vars: map[string]interface{}{ 29 "postfix": uniquePostfix, 30 }, 31 } 32 33 // website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created 34 defer terraform.Destroy(t, terraformOptions) 35 36 // website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors. 37 terraform.InitAndApply(t, terraformOptions) 38 39 // website::tag::3:: Run `terraform output` to get the values of output variables 40 expectedSQLServerID := terraform.Output(t, terraformOptions, "sql_server_id") 41 expectedSQLServerName := terraform.Output(t, terraformOptions, "sql_server_name") 42 43 expectedSQLServerFullDomainName := terraform.Output(t, terraformOptions, "sql_server_full_domain_name") 44 expectedSQLDBName := terraform.Output(t, terraformOptions, "sql_database_name") 45 46 expectedSQLDBID := terraform.Output(t, terraformOptions, "sql_database_id") 47 expectedResourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name") 48 expectedSQLDBStatus := "Online" 49 50 // website::tag::4:: Get the SQL server details and assert them against the terraform output 51 actualSQLServer := azure.GetSQLServer(t, expectedResourceGroupName, expectedSQLServerName, "") 52 53 assert.Equal(t, expectedSQLServerID, *actualSQLServer.ID) 54 assert.Equal(t, expectedSQLServerFullDomainName, *actualSQLServer.FullyQualifiedDomainName) 55 assert.Equal(t, sql.ServerStateReady, actualSQLServer.State) 56 57 // website::tag::5:: Get the SQL server DB details and assert them against the terraform output 58 actualSQLDatabase := azure.GetSQLDatabase(t, expectedResourceGroupName, expectedSQLServerName, expectedSQLDBName, "") 59 60 assert.Equal(t, expectedSQLDBID, *actualSQLDatabase.ID) 61 assert.Equal(t, expectedSQLDBStatus, *actualSQLDatabase.Status) 62 }