github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_sqldb_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 "strings" 11 "testing" 12 13 "github.com/Azure/azure-sdk-for-go/services/sql/mgmt/2014-04-01/sql" 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 TestTerraformAzureSQLDBExample(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-sqldb-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 expectedSQLServerID := terraform.Output(t, terraformOptions, "sql_server_id") 42 expectedSQLServerName := terraform.Output(t, terraformOptions, "sql_server_name") 43 44 expectedSQLServerFullDomainName := terraform.Output(t, terraformOptions, "sql_server_full_domain_name") 45 expectedSQLDBName := terraform.Output(t, terraformOptions, "sql_database_name") 46 47 expectedSQLDBID := terraform.Output(t, terraformOptions, "sql_database_id") 48 expectedResourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name") 49 expectedSQLDBStatus := "Online" 50 51 // website::tag::4:: Get the SQL server details and assert them against the terraform output 52 actualSQLServer := azure.GetSQLServer(t, expectedResourceGroupName, expectedSQLServerName, "") 53 54 assert.Equal(t, expectedSQLServerID, *actualSQLServer.ID) 55 assert.Equal(t, expectedSQLServerFullDomainName, *actualSQLServer.FullyQualifiedDomainName) 56 assert.Equal(t, sql.ServerStateReady, actualSQLServer.State) 57 58 // website::tag::5:: Get the SQL server DB details and assert them against the terraform output 59 actualSQLDatabase := azure.GetSQLDatabase(t, expectedResourceGroupName, expectedSQLServerName, expectedSQLDBName, "") 60 61 assert.Equal(t, expectedSQLDBID, *actualSQLDatabase.ID) 62 assert.Equal(t, expectedSQLDBStatus, *actualSQLDatabase.Status) 63 }