Skip to content

Commit

Permalink
Adding acceptance tests to verify upgrading to framework does not tri…
Browse files Browse the repository at this point in the history
…gger unexpected replace (#177)
  • Loading branch information
bendbennett committed Jun 27, 2022
1 parent f888917 commit bf97f8f
Show file tree
Hide file tree
Showing 12 changed files with 615 additions and 167 deletions.
47 changes: 44 additions & 3 deletions internal/provider/provider_resource_id_test.go
Expand Up @@ -8,7 +8,7 @@ import (

func TestAccResourceID(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_id" "foo" {
Expand All @@ -30,9 +30,9 @@ func TestAccResourceID(t *testing.T) {
})
}

func TestAccResourceID_importWithPrefix(t *testing.T) {
func TestAccResourceID_ImportWithPrefix(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_id" "bar" {
Expand All @@ -55,3 +55,44 @@ func TestAccResourceID_importWithPrefix(t *testing.T) {
},
})
}

func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
ExternalProviders: providerVersion332(),
Config: `resource "random_id" "bar" {
byte_length = 4
prefix = "cloud-"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
),
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_id" "bar" {
byte_length = 4
prefix = "cloud-"
}`,
PlanOnly: true,
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_id" "bar" {
byte_length = 4
prefix = "cloud-"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
),
},
},
})
}
60 changes: 49 additions & 11 deletions internal/provider/provider_resource_integer_test.go
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourceIntegerBasic(t *testing.T) {
func TestAccResourceInteger(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
Expand All @@ -32,10 +32,10 @@ func TestAccResourceIntegerBasic(t *testing.T) {
})
}

func TestAccResourceIntegerUpdate(t *testing.T) {
func TestAccResourceInteger_ChangeSeed(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
Expand All @@ -61,10 +61,10 @@ func TestAccResourceIntegerUpdate(t *testing.T) {
})
}

func TestAccResourceIntegerSeedless_to_seeded(t *testing.T) {
func TestAccResourceInteger_SeedlessToSeeded(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
Expand All @@ -89,10 +89,10 @@ func TestAccResourceIntegerSeedless_to_seeded(t *testing.T) {
})
}

func TestAccResourceIntegerSeeded_to_seedless(t *testing.T) {
func TestAccResourceInteger_SeededToSeedless(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
Expand All @@ -117,10 +117,10 @@ func TestAccResourceIntegerSeeded_to_seedless(t *testing.T) {
})
}

func TestAccResourceIntegerBig(t *testing.T) {
func TestAccResourceInteger_Big(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
Expand All @@ -139,10 +139,48 @@ func TestAccResourceIntegerBig(t *testing.T) {
})
}

func TestAccResourceInteger_UpgradeFromVersion3_3_2(t *testing.T) {
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
ExternalProviders: providerVersion332(),
Config: `resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"),
),
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}`,
PlanOnly: true,
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"),
),
},
},
})
}

func testCheckNotEmptyString(field string) func(input string) error {
return func(input string) error {
if input == "" {
return fmt.Errorf("%s is empty string", field)
return fmt.Errorf("%s is empty stringresource", field)
}

return nil
Expand Down
95 changes: 84 additions & 11 deletions internal/provider/provider_resource_password_test.go
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourcePasswordBasic(t *testing.T) {
func TestAccResourcePassword(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_password" "basic" {
Expand Down Expand Up @@ -46,9 +46,9 @@ func TestAccResourcePasswordBasic(t *testing.T) {
})
}

func TestAccResourcePasswordOverride(t *testing.T) {
func TestAccResourcePassword_Override(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_password" "override" {
Expand All @@ -67,11 +67,11 @@ func TestAccResourcePasswordOverride(t *testing.T) {
})
}

// TestAccResourcePassword_StateUpgrade_V0toV2 covers the state upgrades from V0 to V2.
// TestAccResourcePassword_StateUpgradeV0toV2 covers the state upgrades from V0 to V2.
// This includes the deprecation and removal of `number` and the addition of `numeric`
// and `bcrypt_hash` attributes.
// v3.1.3 is used as this is last version before `bcrypt_hash` attributed was added.
func TestAccResourcePassword_StateUpgrade_V0toV2(t *testing.T) {
func TestAccResourcePassword_StateUpgradeV0toV2(t *testing.T) {
t.Parallel()

cases := []struct {
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestAccResourcePassword_StateUpgrade_V0toV2(t *testing.T) {
Check: resource.ComposeTestCheckFunc(c.beforeStateUpgrade...),
},
{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: c.configDuringUpgrade,
Check: resource.ComposeTestCheckFunc(c.afterStateUpgrade...),
},
Expand All @@ -289,7 +289,7 @@ func TestAccResourcePassword_StateUpgrade_V0toV2(t *testing.T) {
// This includes the deprecation and removal of `number` and the addition of `numeric` attributes.
// v3.2.0 was used as this is the last version before `number` was deprecated and `numeric` attribute
// was added.
func TestAccResourcePassword_StateUpgrade_V1toV2(t *testing.T) {
func TestAccResourcePassword_StateUpgradeV1toV2(t *testing.T) {
t.Parallel()

cases := []struct {
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestAccResourcePassword_StateUpgrade_V1toV2(t *testing.T) {
Check: resource.ComposeTestCheckFunc(c.beforeStateUpgrade...),
},
{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: c.configDuringUpgrade,
Check: resource.ComposeTestCheckFunc(c.afterStateUpgrade...),
},
Expand All @@ -494,9 +494,9 @@ func TestAccResourcePassword_StateUpgrade_V1toV2(t *testing.T) {
}
}

func TestAccResourcePasswordMin(t *testing.T) {
func TestAccResourcePassword_Min(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_password" "min" {
Expand All @@ -518,3 +518,76 @@ func TestAccResourcePasswordMin(t *testing.T) {
},
})
}

func TestAccResourcePassword_UpgradeFromVersion3_3_2(t *testing.T) {
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
ExternalProviders: providerVersion332(),
Config: `resource "random_password" "min" {
length = 12
override_special = "!#@"
min_lower = 2
min_upper = 3
min_special = 1
min_numeric = 4
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_password.min", "result", testCheckLen(12)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([a-z].*){2,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([0-9].*){4,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([!#@])`)),
resource.TestCheckResourceAttr("random_password.min", "special", "true"),
resource.TestCheckResourceAttr("random_password.min", "upper", "true"),
resource.TestCheckResourceAttr("random_password.min", "lower", "true"),
resource.TestCheckResourceAttr("random_password.min", "numeric", "true"),
resource.TestCheckResourceAttr("random_password.min", "min_special", "1"),
resource.TestCheckResourceAttr("random_password.min", "min_upper", "3"),
resource.TestCheckResourceAttr("random_password.min", "min_lower", "2"),
resource.TestCheckResourceAttr("random_password.min", "min_numeric", "4"),
resource.TestCheckResourceAttrSet("random_password.min", "bcrypt_hash"),
),
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_password" "min" {
length = 12
override_special = "!#@"
min_lower = 2
min_upper = 3
min_special = 1
min_numeric = 4
}`,
PlanOnly: true,
},
{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Config: `resource "random_password" "min" {
length = 12
override_special = "!#@"
min_lower = 2
min_upper = 3
min_special = 1
min_numeric = 4
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_password.min", "result", testCheckLen(12)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([a-z].*){2,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([0-9].*){4,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([!#@])`)),
resource.TestCheckResourceAttr("random_password.min", "special", "true"),
resource.TestCheckResourceAttr("random_password.min", "upper", "true"),
resource.TestCheckResourceAttr("random_password.min", "lower", "true"),
resource.TestCheckResourceAttr("random_password.min", "numeric", "true"),
resource.TestCheckResourceAttr("random_password.min", "min_special", "1"),
resource.TestCheckResourceAttr("random_password.min", "min_upper", "3"),
resource.TestCheckResourceAttr("random_password.min", "min_lower", "2"),
resource.TestCheckResourceAttr("random_password.min", "min_numeric", "4"),
resource.TestCheckResourceAttrSet("random_password.min", "bcrypt_hash"),
),
},
},
})
}

0 comments on commit bf97f8f

Please sign in to comment.