Skip to content

Commit

Permalink
tests: add testcase for immutable storagezone fields
Browse files Browse the repository at this point in the history
Add a testcase that ensures changing an immutable storagezone field fails
  • Loading branch information
fho committed Jun 15, 2022
1 parent 8dc5f89 commit f3188cc
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions internal/provider/resource_storagezone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"regexp"

"errors"
"fmt"
Expand Down Expand Up @@ -172,6 +173,94 @@ resource "bunny_storagezone" "%s" {
})
}

func TestChangingImmutableFieldsFails(t *testing.T) {
const resourceName = "mytest1"
const fullResourceName = "bunny_storagezone." + resourceName
storageZoneName := randStorageZoneName()

attrs := bunny.StorageZone{
Name: ptr.ToString(storageZoneName),
Region: ptr.ToString("NY"),
ReplicationRegions: []string{"DE"},
}

resource.Test(t, resource.TestCase{
Providers: testProviders,
Steps: []resource.TestStep{
// create storagezone
{
Config: fmt.Sprintf(`
resource "bunny_storagezone" "mytest1" {
name = "%s"
region = "%s"
replication_regions = %s
}
`,
storageZoneName,
*attrs.Region,
tfStrList(attrs.ReplicationRegions),
),
Check: checkSzState(t, fullResourceName, &attrs),
},
// change region
{
Config: fmt.Sprintf(`
resource "bunny_storagezone" "mytest1" {
name = "%s"
region = "LA"
replication_regions = ["DE"]
}
`,
storageZoneName,
),
ExpectError: regexp.MustCompile(".*'region' is immutable.*"),
},
// change name
{
Config: fmt.Sprintf(`
resource "bunny_storagezone" "mytest1" {
name = "%s"
region = "LA"
replication_regions = ["DE"]
}
`,
storageZoneName+resource.UniqueId(),
),
Check: checkSzState(t, fullResourceName, &attrs),
ExpectError: regexp.MustCompile(".*'name' is immutable.*"),
},
// replace a replication_region
{
Config: fmt.Sprintf(`
resource "bunny_storagezone" "mytest1" {
name = "%s"
region = "NY"
replication_regions = ["LA"]
}
`,
storageZoneName,
),
Check: checkSzState(t, fullResourceName, &attrs),
ExpectError: regexp.MustCompile(".*'replication_regions' can be added but not removed.*"),
},
// remove replication_region
{
Config: fmt.Sprintf(`
resource "bunny_storagezone" "mytest1" {
name = "%s"
region = "NY"
}
`,
storageZoneName,
),
Check: checkSzState(t, fullResourceName, &attrs),
ExpectError: regexp.MustCompile(".*'replication_regions' can be added but not removed.*"),
},
},
CheckDestroy: checkStorageZoneNotExists(fullResourceName),
})
}

func checkSzState(t *testing.T, resourceName string, wanted *bunny.StorageZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
clt := newAPIClient()
Expand Down

0 comments on commit f3188cc

Please sign in to comment.