github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/azure/terraform_azure_postgresql_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  	"os"
    11  	"strings"
    12  	"testing"
    13  
    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 TestPostgreSQLDatabase(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.WithDefaultRetryableErrors(t, &terraform.Options{
    27  		TerraformDir: "../../examples/azure/terraform-azure-postgresql-example",
    28  		Vars: map[string]interface{}{
    29  			"postfix": uniquePostfix,
    30  		},
    31  		NoColor: true,
    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  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    40  
    41  	// website::tag::3:: Run `terraform output` to get the values of output variables
    42  	expectedServername := "postgresqlserver-" + uniquePostfix // see fixture
    43  	actualServername := terraform.Output(t, terraformOptions, "servername")
    44  	rgName := terraform.Output(t, terraformOptions, "rgname")
    45  	expectedSkuName := terraform.Output(t, terraformOptions, "sku_name")
    46  
    47  	// website::tag::4:: Get the Server details and assert them against the terraform output
    48  	actualServer := azure.GetPostgreSQLServer(t, rgName, actualServername, subscriptionID)
    49  	// Verify
    50  	assert.NotNil(t, actualServer)
    51  	assert.Equal(t, expectedServername, actualServername)
    52  	assert.Equal(t, expectedSkuName, *actualServer.Sku.Name)
    53  
    54  }