github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/azure/terraform_azure_mysqldb_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  	"fmt"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/Azure/azure-sdk-for-go/profiles/latest/mysql/mgmt/mysql"
    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 TestTerraformAzureMySQLDBExample(t *testing.T) {
    21  	t.Parallel()
    22  
    23  	uniquePostfix := strings.ToLower(random.UniqueId())
    24  	expectedServerSkuName := "GP_Gen5_2"
    25  	expectedServerStoragemMb := "5120"
    26  	expectedDatabaseCharSet := "utf8"
    27  	expectedDatabaseCollation := "utf8_unicode_ci"
    28  
    29  	// website::tag::1:: Configure Terraform setting up a path to Terraform code.
    30  	terraformOptions := &terraform.Options{
    31  		// The path to where our Terraform code is located
    32  		TerraformDir: "../../examples/azure/terraform-azure-mysqldb-example",
    33  		Vars: map[string]interface{}{
    34  			"postfix":                uniquePostfix,
    35  			"mysqlserver_sku_name":   expectedServerSkuName,
    36  			"mysqlserver_storage_mb": expectedServerStoragemMb,
    37  			"mysqldb_charset":        expectedDatabaseCharSet,
    38  		},
    39  	}
    40  
    41  	// website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created
    42  	defer terraform.Destroy(t, terraformOptions)
    43  
    44  	// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
    45  	terraform.InitAndApply(t, terraformOptions)
    46  
    47  	// website::tag::3:: Run `terraform output` to get the values of output variables
    48  	expectedResourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name")
    49  	expectedMYSQLServerName := terraform.Output(t, terraformOptions, "mysql_server_name")
    50  
    51  	expectedMYSQLDBName := terraform.Output(t, terraformOptions, "mysql_database_name")
    52  
    53  	// website::tag::4:: Get mySQL server details and assert them against the terraform output
    54  	actualMYSQLServer := azure.GetMYSQLServer(t, expectedResourceGroupName, expectedMYSQLServerName, "")
    55  
    56  	assert.Equal(t, expectedServerSkuName, *actualMYSQLServer.Sku.Name)
    57  	assert.Equal(t, expectedServerStoragemMb, fmt.Sprint(*actualMYSQLServer.ServerProperties.StorageProfile.StorageMB))
    58  
    59  	assert.Equal(t, mysql.ServerStateReady, actualMYSQLServer.ServerProperties.UserVisibleState)
    60  
    61  	// website::tag::5:: Get  mySQL server DB details and assert them against the terraform output
    62  	actualDatabase := azure.GetMYSQLDB(t, expectedResourceGroupName, expectedMYSQLServerName, expectedMYSQLDBName, "")
    63  
    64  	assert.Equal(t, expectedDatabaseCharSet, *actualDatabase.DatabaseProperties.Charset)
    65  	assert.Equal(t, expectedDatabaseCollation, *actualDatabase.DatabaseProperties.Collation)
    66  }