Skip to content

Commit

Permalink
mssql test fixes (#15949)
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer authored Mar 23, 2022
1 parent 3dc6142 commit a8895c8
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,18 @@ resource "azurerm_storage_account" "test" {
}
}
resource "azurerm_sql_virtual_network_rule" "sqlvnetrule" {
name = "sql-vnet-rule"
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_mssql_server.test.name
subnet_id = azurerm_subnet.test.id
resource "azurerm_mssql_virtual_network_rule" "sqlvnetrule" {
name = "sql-vnet-rule"
server_id = azurerm_mssql_server.test.id
subnet_id = azurerm_subnet.test.id
}
resource "azurerm_sql_firewall_rule" "test" {
name = "FirewallRule1"
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_mssql_server.test.name
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
resource "azurerm_mssql_firewall_rule" "test" {
name = "FirewallRule1"
server_id = azurerm_mssql_server.test.id
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
}
Expand Down
45 changes: 43 additions & 2 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ func resourceMsSqlDatabase() *pluginsdk.Resource {
if !features.ThreePointOhBeta() {
return nil
}
transparentDataEncryption := d.Get("transparent_data_encryption_enabled").(bool)
sku := d.Get("sku_name").(string)
if !strings.HasPrefix(sku, "DW") && !d.Get("transparent_data_encryption_enabled").(bool) {
if !strings.HasPrefix(sku, "DW") && !transparentDataEncryption {
return fmt.Errorf("transparent data encryption can only be disabled on Data Warehouse SKUs")
}

return nil
}),
}
Expand Down Expand Up @@ -322,7 +324,46 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
return fmt.Errorf("waiting for create/update of %s: %+v", id, err)
}

if features.ThreePointOhBeta() {
// Wait for the ProvisioningState to become "Succeeded"
log.Printf("[DEBUG] Waiting for %s to become ready", id)
pendingStatuses := make([]string, 0)
for _, s := range sql.PossibleDatabaseStatusValues() {
if s != sql.DatabaseStatusOnline {
pendingStatuses = append(pendingStatuses, string(s))
}
}
stateConf := &pluginsdk.StateChangeConf{
Pending: pendingStatuses,
Target: []string{string(sql.DatabaseStatusOnline)},
Refresh: func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking to see if %s is online...", id)

resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
return nil, "", fmt.Errorf("polling for the status of %s: %+v", id, err)
}

if props := resp.DatabaseProperties; props != nil {
return resp, string(props.Status), nil
}

return resp, "", nil
},
MinTimeout: 1 * time.Minute,
ContinuousTargetOccurence: 2,
}
if d.IsNewResource() {
stateConf.Timeout = d.Timeout(pluginsdk.TimeoutCreate)
} else {
stateConf.Timeout = d.Timeout(pluginsdk.TimeoutUpdate)
}

if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to become ready: %+v", id, err)
}

// Cannot set transparent data encryption for secondary databases
if createMode != string(sql.CreateModeOnlineSecondary) && createMode != string(sql.CreateModeSecondary) {
statusProperty := sql.TransparentDataEncryptionStatusDisabled
encryptionStatus := d.Get("transparent_data_encryption_enabled").(bool)
if encryptionStatus {
Expand Down
9 changes: 6 additions & 3 deletions internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func TestAccMsSqlDatabase_BC(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test")
r := MsSqlDatabaseResource{}

// Limited regional availability for BC
data.Locations.Primary = "westeurope"

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.bc(data),
Expand Down Expand Up @@ -244,7 +247,7 @@ func TestAccMsSqlDatabase_createPITRMode(t *testing.T) {

{
PreConfig: func() { time.Sleep(11 * time.Minute) },
Config: r.createPITRMode(data),
Config: r.createPITRMode(data, time.Now().Add(time.Duration(9)*time.Minute).UTC().Format(time.RFC3339)),
Check: acceptance.ComposeTestCheckFunc(
check.That("azurerm_mssql_database.pitr").ExistsInAzure(r),
),
Expand Down Expand Up @@ -1014,7 +1017,7 @@ resource "azurerm_mssql_database" "copy" {
`, r.complete(data), data.RandomInteger)
}

func (r MsSqlDatabaseResource) createPITRMode(data acceptance.TestData) string {
func (r MsSqlDatabaseResource) createPITRMode(data acceptance.TestData, restorePointInTime string) string {
return fmt.Sprintf(`
%[1]s
Expand All @@ -1026,7 +1029,7 @@ resource "azurerm_mssql_database" "pitr" {
creation_source_database_id = azurerm_mssql_database.test.id
}
`, r.basic(data), data.RandomInteger, time.Now().Add(time.Duration(7)*time.Minute).UTC().Format(time.RFC3339))
`, r.basic(data), data.RandomInteger, restorePointInTime)
}

func (r MsSqlDatabaseResource) createSecondaryMode(data acceptance.TestData, tag string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r MsSqlDatabaseVulnerabilityAssessmentRuleBaselineResource) basic(data acc
resource "azurerm_mssql_database_vulnerability_assessment_rule_baseline" "test" {
server_vulnerability_assessment_id = azurerm_mssql_server_vulnerability_assessment.test.id
database_name = azurerm_sql_database.test.name
database_name = azurerm_mssql_database.test.name
rule_id = "VA2111"
baseline_name = "default"
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r MsSqlDatabaseVulnerabilityAssessmentRuleBaselineResource) primary(data a
resource "azurerm_mssql_database_vulnerability_assessment_rule_baseline" "test" {
server_vulnerability_assessment_id = azurerm_mssql_server_vulnerability_assessment.test.id
database_name = azurerm_sql_database.test.name
database_name = azurerm_mssql_database.test.name
rule_id = "VA2065"
baseline_name = "master"
Expand All @@ -136,7 +136,7 @@ func (r MsSqlDatabaseVulnerabilityAssessmentRuleBaselineResource) update(data ac
resource "azurerm_mssql_database_vulnerability_assessment_rule_baseline" "test" {
server_vulnerability_assessment_id = azurerm_mssql_server_vulnerability_assessment.test.id
database_name = azurerm_sql_database.test.name
database_name = azurerm_mssql_database.test.name
rule_id = "VA2065"
baseline_name = "master"
Expand Down Expand Up @@ -170,7 +170,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctestsqlserver%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -195,7 +195,7 @@ resource "azurerm_storage_container" "test" {
resource "azurerm_mssql_server_security_alert_policy" "test" {
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
state = "Enabled"
}
Expand All @@ -205,12 +205,10 @@ resource "azurerm_mssql_server_vulnerability_assessment" "test" {
storage_account_access_key = azurerm_storage_account.test.primary_access_key
}
resource "azurerm_sql_database" "test" {
name = "acctestdb%[1]d"
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_sql_server.test.name
location = azurerm_resource_group.test.location
edition = "Standard"
resource "azurerm_mssql_database" "test" {
name = "acctestdb%[1]d"
server_id = azurerm_mssql_server.test.id
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary)
}
6 changes: 3 additions & 3 deletions internal/services/mssql/mssql_elasticpool_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -61,7 +61,7 @@ resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-dtu-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
max_size_gb = 50
zone_redundant = false
Expand All @@ -81,7 +81,7 @@ resource "azurerm_mssql_elasticpool" "test" {
data "azurerm_mssql_elasticpool" "test" {
name = azurerm_mssql_elasticpool.test.name
resource_group_name = azurerm_resource_group.test.name
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
}
`, data.RandomInteger, data.Locations.Primary)
}
28 changes: 20 additions & 8 deletions internal/services/mssql/mssql_elasticpool_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func TestAccMsSqlElasticPool_premiumDTUZoneRedundant(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test")
r := MsSqlElasticPoolResource{}

// Limited regional availability for ZRS
data.Locations.Primary = "westeurope"

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.premiumDTUZoneRedundant(data),
Expand Down Expand Up @@ -155,6 +158,9 @@ func TestAccMsSqlElasticPool_fsv2FamilyVCore(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test")
r := MsSqlElasticPoolResource{}

// Limited regional availability for Fsv2 family
data.Locations.Primary = "westeurope"

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.fsv2VCore(data),
Expand All @@ -170,6 +176,9 @@ func TestAccMsSqlElasticPool_dcFamilyVCore(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test")
r := MsSqlElasticPoolResource{}

// Limited regional availability for DC-series
data.Locations.Primary = "westeurope"

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.dcVCore(data),
Expand All @@ -185,6 +194,9 @@ func TestAccMsSqlElasticPool_dcFamilyBcTierVCore(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test")
r := MsSqlElasticPoolResource{}

// Limited regional availability for DC-series
data.Locations.Primary = "westeurope"

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.dcVCoreBc(data),
Expand Down Expand Up @@ -313,7 +325,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -326,7 +338,7 @@ resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-dtu-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
max_size_gb = %.7[6]f
zone_redundant = %[9]t
Expand Down Expand Up @@ -355,7 +367,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -368,7 +380,7 @@ resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-vcore-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
max_size_gb = 5
sku {
Expand Down Expand Up @@ -397,7 +409,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -410,7 +422,7 @@ resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-vcore-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
max_size_bytes = 214748364800
sku {
Expand Down Expand Up @@ -439,7 +451,7 @@ resource "azurerm_resource_group" "test" {
location = "%[2]s"
}
resource "azurerm_sql_server" "test" {
resource "azurerm_mssql_server" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -452,7 +464,7 @@ resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-dtu-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_sql_server.test.name
server_name = azurerm_mssql_server.test.name
max_size_gb = 50
zone_redundant = false
license_type = "%[3]s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ func (r MsSqlManagedInstanceActiveDirectoryAdministratorResource) Exists(ctx con

func (r MsSqlManagedInstanceActiveDirectoryAdministratorResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
data "azuread_client_config" "test" {}
resource "azuread_application" "test" {
display_name = "acctest-ManagedInstance-%[1]d"
display_name = "acctest-ManagedInstance-%[2]d"
sign_in_audience = "AzureADMyOrg"
}
Expand All @@ -77,16 +79,9 @@ resource "azuread_directory_role" "reader" {
resource "azuread_directory_role_member" "test" {
role_object_id = azuread_directory_role.reader.object_id
member_object_id = data.azurerm_mssql_managed_instance.test.identity.0.principal_id
member_object_id = azurerm_mssql_managed_instance.test.identity.0.principal_id
}
data "azurerm_mssql_managed_instance" "test" {
name = "aaa-tbamford-testing"
resource_group_name = "aaa-tbamford-sqlmanagedinstance-testing"
}
%[2]s
`, data.RandomInteger, MsSqlManagedInstanceResource{}.identity(data))
`, MsSqlManagedInstanceResource{}.identity(data), data.RandomInteger)
}

func (r MsSqlManagedInstanceActiveDirectoryAdministratorResource) basic(data acceptance.TestData, aadOnly bool) string {
Expand Down
8 changes: 4 additions & 4 deletions internal/services/mssql/mssql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func resourceMsSqlServerCreate(d *pluginsdk.ResourceData, meta interface{}) erro
props.ServerProperties.Administrators = expandMsSqlServerAdministrators(azureADAdministrator.([]interface{}))
}

if _, ok := d.GetOk("identity"); ok {
expandedIdentity, err := expandSqlServerIdentity(d.Get("identity").([]interface{}))
if v, ok := d.GetOk("identity"); ok {
expandedIdentity, err := expandSqlServerIdentity(v.([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
Expand Down Expand Up @@ -334,8 +334,8 @@ func resourceMsSqlServerUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
},
}

if ok := d.HasChange("identity"); ok {
expandedIdentity, err := expandSqlServerIdentity(d.Get("identity").([]interface{}))
if v, ok := d.GetOk("identity"); ok {
expandedIdentity, err := expandSqlServerIdentity(v.([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
Expand Down
Loading

0 comments on commit a8895c8

Please sign in to comment.