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