Skip to content

Commit

Permalink
test: refactor postgres tests into test table (PSKD-1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
joboon committed Feb 27, 2025
1 parent ec18299 commit 872465c
Showing 1 changed file with 84 additions and 71 deletions.
155 changes: 84 additions & 71 deletions test/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,98 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
DefaultPostgresServerName = "default"
)

// TestPostgresServers verifies all PostgreSQL Flexible Server configurations
func TestPostgresServers(t *testing.T) {
t.Parallel()

variables := getDefaultPlanVars(t)
variables["postgres_servers"] = map[string]interface{}{
"default": map[string]interface{}{},
postgresResourceMapName := "module.flex_postgresql[\"" + DefaultPostgresServerName + "\"].azurerm_postgresql_flexible_server.flexpsql"
tests := map[string]testCase{
"postgresFlexServerExists": {
expected: `nil`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$}",
assertFunction: assert.NotEqual,
},
"postgresFlexServerSKUName": {
expected: `GP_Standard_D4s_v3`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.sku_name}",
},
"postgresFlexServerStorageSize": {
expected: `131072`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.storage_mb}",
},
"postgresFlexServerBackupRetentionDays": {
expected: `7`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.backup_retention_days}",
},
"postgresFlexServerGeoRedundantBackup": {
expected: `false`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.geo_redundant_backup_enabled}",
},
"postgresFlexServerAdminLogin": {
expected: `pgadmin`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.administrator_login}",
},
"postgresFlexServerAdminPassword": {
expected: `my$up3rS3cretPassw0rd`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.administrator_password}",
},
"postgresFlexServerVersion": {
expected: `15`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.version}",
},
"postgresFlexServerSSLEnforcement": {
expected: `OFF`,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.postgresql_configurations[*].require_secure_transport}",
assertFunction: assert.NotEqual,
},
"postgresFlexServerVnetId": {
expected: ``,
resourceMapName: postgresResourceMapName,
attributeJsonPath: "{$.virtual_network_id}",
},
"postgresFlexServerConfigurationMaxPreparedTransactionsName": {
expected: `max_prepared_transactions`,
resourceMapName: "module.flex_postgresql[\"" + DefaultPostgresServerName + "\"].azurerm_postgresql_flexible_server_configuration.flexpsql[\"max_prepared_transactions\"]",
attributeJsonPath: "{$.name}",
},
"postgresFlexServerConfigurationMaxPreparedTransactionsValue": {
expected: `1024`,
resourceMapName: "module.flex_postgresql[\"" + DefaultPostgresServerName + "\"].azurerm_postgresql_flexible_server_configuration.flexpsql[\"max_prepared_transactions\"]",
attributeJsonPath: "{$.value}",
},
}
plan, err := initPlanWithVariables(t, variables)
assert.NoError(t, err)

// Validate PostgreSQL servers in Terraform plan
postgresDefault := plan.ResourcePlannedValuesMap["module.flex_postgresql[\"default\"].azurerm_postgresql_flexible_server.flexpsql"]
assert.NotNil(t, postgresDefault, "Default PostgreSQL Flexible Server should be created")

// Validate SKU Name
expectedSku := "GP_Standard_D4s_v3"
assert.Equal(t, expectedSku, postgresDefault.AttributeValues["sku_name"], "Mismatch in SKU Name")

// Validate Storage Size (MB)
expectedStorage := 131072
actualStorage := int(postgresDefault.AttributeValues["storage_mb"].(float64))
assert.Equal(t, expectedStorage, actualStorage, "Mismatch in Storage Size")

// Validate Backup Retention Days
expectedBackupDays := 7
actualBackupDays := int(postgresDefault.AttributeValues["backup_retention_days"].(float64))
assert.Equal(t, expectedBackupDays, actualBackupDays, "Mismatch in Backup Retention Days")

// Validate Geo-Redundant Backup
expectedGeoRedundantBackup := false
assert.Equal(t, expectedGeoRedundantBackup, postgresDefault.AttributeValues["geo_redundant_backup_enabled"], "Mismatch in Geo-Redundant Backup")

// Validate Administrator Login
expectedAdminLogin := "pgadmin"
assert.Equal(t, expectedAdminLogin, postgresDefault.AttributeValues["administrator_login"], "Mismatch in Administrator Login")

// Validate Administrator Password
expectedAdminPassword := "my$up3rS3cretPassw0rd"
assert.Equal(t, expectedAdminPassword, postgresDefault.AttributeValues["administrator_password"], "Mismatch in Administrator Password")

// Validate Server Version
expectedServerVersion := "15"
assert.Equal(t, expectedServerVersion, postgresDefault.AttributeValues["version"], "Mismatch in PostgreSQL Server Version")

// Validate SSL Enforcement
expectedSSLEnforcement := false
requireSecureTransport, err := getJsonPathFromStateResource(t, postgresDefault, "{$.postgresql_configurations[*].require_secure_transport}")
assert.NoError(t, err)
sslEnforcementDisabled := requireSecureTransport == "OFF"
assert.Equal(t, expectedSSLEnforcement, sslEnforcementDisabled, "Mismatch in SSL Enforcement: Expected False")

//validate connectivity_method
vnetAttr, vnetExists := postgresDefault.AttributeValues["virtual_network_id"]

//The expected connectivity method
expectedConnectivity := "public"

// Infer connectivity type: if VNet is set, it's private; otherwise, it's public
actualConnectivity := "public"
if vnetExists && vnetAttr != nil {
actualConnectivity = "private"
// Prepare to generate the plan
variables := getDefaultPlanVars(t)
variables["postgres_servers"] = map[string]any{
DefaultPostgresServerName: map[string]any{},
}
// Validate the inferred connectivity method
assert.Equal(t, expectedConnectivity, actualConnectivity, "Mismatch in inferred Connectivity Method")

//validate postgresql_configurations
postgresConfig := plan.ResourcePlannedValuesMap["module.flex_postgresql[\"default\"].azurerm_postgresql_flexible_server_configuration.flexpsql[\"max_prepared_transactions\"]"]
assert.NotNil(t, postgresConfig, "PostgreSQL Configuration for max_prepared_transactions should exist")

// Validate name attribute
expectedConfigName := "max_prepared_transactions"
actualConfigName := postgresConfig.AttributeValues["name"].(string)
assert.Equal(t, expectedConfigName, actualConfigName, "Mismatch in PostgreSQL Configuration Name")

// Validate value attribute
expectedConfigValue := "1024"
actualConfigValue := postgresConfig.AttributeValues["value"].(string)
assert.Equal(t, expectedConfigValue, actualConfigValue, "Mismatch in max_prepared_transactions configuration value")
// Generate the plan
plan, err := initPlanWithVariables(t, variables)
require.NotNil(t, plan)
require.NoError(t, err)

// Run the tests
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
runTest(t, tc, plan)
})
}
}

0 comments on commit 872465c

Please sign in to comment.