diff --git a/go.mod b/go.mod index 9f26f96f..4b4ff4e8 100644 --- a/go.mod +++ b/go.mod @@ -8,10 +8,11 @@ require ( github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0 github.com/google/go-cmp v0.6.0 github.com/hashicorp/go-uuid v1.0.3 + github.com/hashicorp/terraform-json v0.22.1 github.com/hashicorp/terraform-plugin-framework v1.11.0 github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 github.com/hashicorp/terraform-plugin-go v0.23.0 - github.com/hashicorp/terraform-plugin-testing v1.9.0 + github.com/hashicorp/terraform-plugin-testing v1.10.0 golang.org/x/crypto v0.26.0 ) @@ -29,12 +30,12 @@ require ( github.com/hashicorp/go-hclog v1.6.3 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.0 // indirect + github.com/hashicorp/go-retryablehttp v0.7.7 // indirect github.com/hashicorp/go-version v1.7.0 // indirect - github.com/hashicorp/hc-install v0.7.0 // indirect + github.com/hashicorp/hc-install v0.8.0 // indirect github.com/hashicorp/hcl/v2 v2.21.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.21.0 // indirect - github.com/hashicorp/terraform-json v0.22.1 // indirect github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 // indirect github.com/hashicorp/terraform-registry-address v0.2.3 // indirect @@ -52,8 +53,8 @@ require ( github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect - github.com/zclconf/go-cty v1.14.4 // indirect - golang.org/x/mod v0.17.0 // indirect + github.com/zclconf/go-cty v1.15.0 // indirect + golang.org/x/mod v0.19.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.23.0 // indirect diff --git a/go.sum b/go.sum index 335d8485..eb621dc4 100644 --- a/go.sum +++ b/go.sum @@ -61,13 +61,15 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI= +github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU= +github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/hc-install v0.7.0 h1:Uu9edVqjKQxxuD28mR5TikkKDd/p55S8vzPC1659aBk= -github.com/hashicorp/hc-install v0.7.0/go.mod h1:ELmmzZlGnEcqoUMKUuykHaPCIR1sYLYX+KSggWSKZuA= +github.com/hashicorp/hc-install v0.8.0 h1:LdpZeXkZYMQhoKPCecJHlKvUkQFixN/nvyR1CdfOLjI= +github.com/hashicorp/hc-install v0.8.0/go.mod h1:+MwJYjDfCruSD/udvBmRB22Nlkwwkwf5sAB6uTIhSaU= github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14= github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= @@ -86,8 +88,8 @@ github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9T github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg= -github.com/hashicorp/terraform-plugin-testing v1.9.0 h1:xOsQRqqlHKXpFq6etTxih3ubdK3HVDtfE1IY7Rpd37o= -github.com/hashicorp/terraform-plugin-testing v1.9.0/go.mod h1:fhhVx/8+XNJZTD5o3b4stfZ6+q7z9+lIWigIYdT6/44= +github.com/hashicorp/terraform-plugin-testing v1.10.0 h1:2+tmRNhvnfE4Bs8rB6v58S/VpqzGC6RCh9Y8ujdn+aw= +github.com/hashicorp/terraform-plugin-testing v1.10.0/go.mod h1:iWRW3+loP33WMch2P/TEyCxxct/ZEcCGMquSLSCVsrc= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM= github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ= @@ -151,8 +153,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8= -github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= +github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ= +github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -160,8 +162,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= +golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= diff --git a/internal/provider/resource_bytes_test.go b/internal/provider/resource_bytes_test.go index c9c950bd..82077a1e 100644 --- a/internal/provider/resource_bytes_test.go +++ b/internal/provider/resource_bytes_test.go @@ -8,7 +8,11 @@ import ( "regexp" "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) @@ -21,11 +25,11 @@ func TestAccResourceBytes(t *testing.T) { Config: `resource "random_bytes" "basic" { length = 32 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr("random_bytes.basic", "base64", regexp.MustCompile(`^[A-Za-z/+\d]{43}=$`)), - resource.TestMatchResourceAttr("random_bytes.basic", "hex", regexp.MustCompile(`^[a-f\d]{64}$`)), - resource.TestCheckResourceAttr("random_bytes.basic", "length", "32"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("base64"), knownvalue.StringRegexp(regexp.MustCompile(`^[A-Za-z/+\d]{43}=$`))), + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("hex"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-f\d]{64}$`))), + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("length"), knownvalue.Int64Exact(32)), + }, }, { // Usage of ImportStateIdFunc is required as the value passed to the `terraform import` command needs @@ -63,11 +67,11 @@ func TestAccResourceBytes_ImportWithoutKeepersThenUpdateShouldNotTriggerChange(t Config: `resource "random_bytes" "basic" { length = 32 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_bytes.basic", "base64", "hkvbcU5f8qGysTFhkI4gzf3yRWC1jXW3aRLCNQFOtNw="), - resource.TestCheckResourceAttr("random_bytes.basic", "hex", "864bdb714e5ff2a1b2b13161908e20cdfdf24560b58d75b76912c235014eb4dc"), - resource.TestCheckResourceAttr("random_bytes.basic", "length", "32"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("base64"), knownvalue.StringExact("hkvbcU5f8qGysTFhkI4gzf3yRWC1jXW3aRLCNQFOtNw=")), + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("hex"), knownvalue.StringExact("864bdb714e5ff2a1b2b13161908e20cdfdf24560b58d75b76912c235014eb4dc")), + statecheck.ExpectKnownValue("random_bytes.basic", tfjsonpath.New("length"), knownvalue.Int64Exact(32)), + }, }, { Config: `resource "random_bytes" "basic" { @@ -94,7 +98,8 @@ func TestAccResourceBytes_LengthErrors(t *testing.T) { } func TestAccResourceBytes_Length_ForceReplacement(t *testing.T) { - var bytes1, bytes2 string + // The base64 attribute values should differ between test steps + assertBase64Differ := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -103,30 +108,30 @@ func TestAccResourceBytes_Length_ForceReplacement(t *testing.T) { Config: `resource "random_bytes" "test" { length = 1 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_bytes.test", "length", "1"), - testExtractResourceAttr("random_bytes.test", "base64", &bytes1), - resource.TestCheckResourceAttrWith("random_bytes.test", "hex", testCheckLen(2)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("length"), knownvalue.Int64Exact(1)), + assertBase64Differ.AddStateValue("random_bytes.test", tfjsonpath.New("base64")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("hex"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-f\d]{2}$`))), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_bytes" "test" { length = 2 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_bytes.test", "length", "2"), - testExtractResourceAttr("random_bytes.test", "base64", &bytes2), - resource.TestCheckResourceAttrWith("random_bytes.test", "hex", testCheckLen(4)), - testCheckAttributeValuesDiffer(&bytes1, &bytes2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("length"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("hex"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-f\d]{4}$`))), + assertBase64Differ.AddStateValue("random_bytes.test", tfjsonpath.New("base64")), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_EmptyMap(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -136,10 +141,10 @@ func TestAccResourceBytes_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -147,18 +152,18 @@ func TestAccResourceBytes_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_NullMap(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -167,28 +172,28 @@ func TestAccResourceBytes_Keepers_Keep_NullMap(t *testing.T) { Config: `resource "random_bytes" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_bytes" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_NullValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -200,10 +205,10 @@ func TestAccResourceBytes_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -213,18 +218,18 @@ func TestAccResourceBytes_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_NullValues(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -237,10 +242,10 @@ func TestAccResourceBytes_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -251,18 +256,18 @@ func TestAccResourceBytes_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_Value(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -274,10 +279,10 @@ func TestAccResourceBytes_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -287,18 +292,18 @@ func TestAccResourceBytes_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Keep_Values(t *testing.T) { - var result1, result2 string + // The hex attribute values should be the same between test steps + assertHexSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -311,10 +316,10 @@ func TestAccResourceBytes_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -325,18 +330,18 @@ func TestAccResourceBytes_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexSame.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -346,10 +351,10 @@ func TestAccResourceBytes_Keepers_Replace_EmptyMapToValue(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -359,18 +364,18 @@ func TestAccResourceBytes_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_NullMapToValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -379,10 +384,10 @@ func TestAccResourceBytes_Keepers_Replace_NullMapToValue(t *testing.T) { Config: `resource "random_bytes" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -392,18 +397,18 @@ func TestAccResourceBytes_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_NullValueToValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -415,10 +420,10 @@ func TestAccResourceBytes_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -428,18 +433,18 @@ func TestAccResourceBytes_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -451,10 +456,10 @@ func TestAccResourceBytes_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -462,18 +467,18 @@ func TestAccResourceBytes_Keepers_Replace_ValueToEmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_ValueToNullMap(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -485,28 +490,28 @@ func TestAccResourceBytes_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_bytes" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_ValueToNullValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -518,10 +523,10 @@ func TestAccResourceBytes_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -531,18 +536,18 @@ func TestAccResourceBytes_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceBytes_Keepers_Replace_ValueToNewValue(t *testing.T) { - var result1, result2 string + // The hex attribute values should differ between test steps + assertHexDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -554,10 +559,10 @@ func TestAccResourceBytes_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result1), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -567,11 +572,10 @@ func TestAccResourceBytes_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_bytes.test", "hex", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_bytes.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertHexDiffer.AddStateValue("random_bytes.test", tfjsonpath.New("hex")), + statecheck.ExpectKnownValue("random_bytes.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) diff --git a/internal/provider/resource_id_test.go b/internal/provider/resource_id_test.go index 9c207698..b2a67423 100644 --- a/internal/provider/resource_id_test.go +++ b/internal/provider/resource_id_test.go @@ -6,7 +6,12 @@ package provider import ( "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" + "github.com/terraform-providers/terraform-provider-random/internal/randomtest" ) func TestAccResourceID(t *testing.T) { @@ -17,12 +22,12 @@ func TestAccResourceID(t *testing.T) { Config: `resource "random_id" "foo" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_id.foo", "b64_url", testCheckLen(6)), - resource.TestCheckResourceAttrWith("random_id.foo", "b64_std", testCheckLen(8)), - resource.TestCheckResourceAttrWith("random_id.foo", "hex", testCheckLen(8)), - resource.TestCheckResourceAttrWith("random_id.foo", "dec", testCheckMinLen(1)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_id.foo", tfjsonpath.New("b64_url"), randomtest.StringLengthExact(6)), + statecheck.ExpectKnownValue("random_id.foo", tfjsonpath.New("b64_std"), randomtest.StringLengthExact(8)), + statecheck.ExpectKnownValue("random_id.foo", tfjsonpath.New("hex"), randomtest.StringLengthExact(8)), + statecheck.ExpectKnownValue("random_id.foo", tfjsonpath.New("dec"), randomtest.StringLengthMin(1)), + }, }, { ResourceName: "random_id.foo", @@ -42,12 +47,12 @@ func TestAccResourceID_ImportWithPrefix(t *testing.T) { 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)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_url"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_std"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("hex"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("dec"), randomtest.StringLengthMin(1)), + }, }, { ResourceName: "random_id.bar", @@ -91,12 +96,12 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) { 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)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_url"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_std"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("hex"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("dec"), randomtest.StringLengthMin(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -112,19 +117,20 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) { 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)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_url"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("b64_std"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("hex"), randomtest.StringLengthExact(14)), + statecheck.ExpectKnownValue("random_id.bar", tfjsonpath.New("dec"), randomtest.StringLengthMin(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_EmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -134,10 +140,10 @@ func TestAccResourceID_Keepers_Keep_EmptyMap(t *testing.T) { byte_length = 4 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -145,18 +151,18 @@ func TestAccResourceID_Keepers_Keep_EmptyMap(t *testing.T) { byte_length = 4 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -166,10 +172,10 @@ func TestAccResourceID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { byte_length = 4 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -179,18 +185,18 @@ func TestAccResourceID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_NullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -199,28 +205,28 @@ func TestAccResourceID_Keepers_Keep_NullMap(t *testing.T) { Config: `resource "random_id" "test" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_id" "test" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -229,10 +235,10 @@ func TestAccResourceID_Keepers_Keep_NullMapToNullValue(t *testing.T) { Config: `resource "random_id" "test" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -242,18 +248,18 @@ func TestAccResourceID_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_NullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -265,10 +271,10 @@ func TestAccResourceID_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -278,18 +284,18 @@ func TestAccResourceID_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_NullValues(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -302,10 +308,10 @@ func TestAccResourceID_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -316,18 +322,18 @@ func TestAccResourceID_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_Value(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -339,10 +345,10 @@ func TestAccResourceID_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -352,18 +358,18 @@ func TestAccResourceID_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Keep_Values(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -376,10 +382,10 @@ func TestAccResourceID_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -390,18 +396,18 @@ func TestAccResourceID_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -411,10 +417,10 @@ func TestAccResourceID_Keepers_Replace_EmptyMapToValue(t *testing.T) { byte_length = 4 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -424,18 +430,18 @@ func TestAccResourceID_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -444,10 +450,10 @@ func TestAccResourceID_Keepers_Replace_NullMapToValue(t *testing.T) { Config: `resource "random_id" "test" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -457,18 +463,18 @@ func TestAccResourceID_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_NullValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -480,10 +486,10 @@ func TestAccResourceID_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -493,18 +499,18 @@ func TestAccResourceID_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -516,10 +522,10 @@ func TestAccResourceID_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -527,18 +533,18 @@ func TestAccResourceID_Keepers_Replace_ValueToEmptyMap(t *testing.T) { byte_length = 4 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_ValueToNullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -550,28 +556,28 @@ func TestAccResourceID_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_id" "test" { byte_length = 4 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_ValueToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -583,10 +589,10 @@ func TestAccResourceID_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -596,18 +602,18 @@ func TestAccResourceID_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_Replace_ValueToNewValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -619,10 +625,10 @@ func TestAccResourceID_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -632,18 +638,18 @@ func TestAccResourceID_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -655,10 +661,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToNullValue(t *testing. "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -668,18 +674,18 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToNullValue(t *testing. "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -691,10 +697,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -704,18 +710,18 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -728,10 +734,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t * "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -742,18 +748,18 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t * "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -766,10 +772,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *test "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -780,18 +786,18 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *test "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -804,10 +810,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -818,18 +824,18 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceID_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -842,10 +848,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapValueToValue(t *testing "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id1), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -856,11 +862,10 @@ func TestAccResourceID_Keepers_FrameworkMigration_NullMapValueToValue(t *testing "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_id.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_id.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_id.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_id.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) diff --git a/internal/provider/resource_integer_test.go b/internal/provider/resource_integer_test.go index b224e999..53789776 100644 --- a/internal/provider/resource_integer_test.go +++ b/internal/provider/resource_integer_test.go @@ -7,8 +7,12 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" ) func TestAccResourceInteger(t *testing.T) { @@ -22,9 +26,9 @@ func TestAccResourceInteger(t *testing.T) { max = 3 seed = "12345" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(3)), + }, }, { ResourceName: "random_integer.integer_1", @@ -74,9 +78,9 @@ func TestAccResourceInteger_ChangeSeed(t *testing.T) { max = 3 seed = "12345" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(3)), + }, }, { Config: `resource "random_integer" "integer_1" { @@ -84,9 +88,9 @@ func TestAccResourceInteger_ChangeSeed(t *testing.T) { max = 3 seed = "123456" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(2)), + }, }, }, }) @@ -102,9 +106,9 @@ func TestAccResourceInteger_SeedlessToSeeded(t *testing.T) { min = 1 max = 3 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_integer.integer_1", "result", testCheckNotEmptyString("result")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.NotNull()), + }, }, { Config: `resource "random_integer" "integer_1" { @@ -112,9 +116,9 @@ func TestAccResourceInteger_SeedlessToSeeded(t *testing.T) { max = 3 seed = "123456" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(2)), + }, }, }, }) @@ -131,18 +135,18 @@ func TestAccResourceInteger_SeededToSeedless(t *testing.T) { max = 3 seed = "12345" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(3)), + }, }, { Config: `resource "random_integer" "integer_1" { min = 1 max = 3 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_integer.integer_1", "result", testCheckNotEmptyString("result")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.NotNull()), + }, }, }, }) @@ -180,9 +184,9 @@ func TestAccResourceInteger_UpgradeFromVersion3_3_2(t *testing.T) { max = 3 seed = "12345" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(3)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -200,16 +204,17 @@ func TestAccResourceInteger_UpgradeFromVersion3_3_2(t *testing.T) { max = 3 seed = "12345" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_integer.integer_1", "result", "3"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_integer.integer_1", tfjsonpath.New("result"), knownvalue.Int64Exact(3)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_EmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -220,10 +225,10 @@ func TestAccResourceInteger_Keepers_Keep_EmptyMap(t *testing.T) { max = 100000000 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -232,18 +237,18 @@ func TestAccResourceInteger_Keepers_Keep_EmptyMap(t *testing.T) { max = 100000000 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -254,10 +259,10 @@ func TestAccResourceInteger_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { max = 100000000 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -268,18 +273,18 @@ func TestAccResourceInteger_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_NullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -289,10 +294,10 @@ func TestAccResourceInteger_Keepers_Keep_NullMap(t *testing.T) { min = 1 max = 100000000 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -300,18 +305,18 @@ func TestAccResourceInteger_Keepers_Keep_NullMap(t *testing.T) { min = 1 max = 100000000 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -321,10 +326,10 @@ func TestAccResourceInteger_Keepers_Keep_NullMapToNullValue(t *testing.T) { min = 1 max = 100000000 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -335,18 +340,18 @@ func TestAccResourceInteger_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_NullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -359,10 +364,10 @@ func TestAccResourceInteger_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -373,18 +378,18 @@ func TestAccResourceInteger_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_NullValues(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -398,10 +403,10 @@ func TestAccResourceInteger_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -413,18 +418,18 @@ func TestAccResourceInteger_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_Value(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -437,10 +442,10 @@ func TestAccResourceInteger_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -451,18 +456,18 @@ func TestAccResourceInteger_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Keep_Values(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -476,10 +481,10 @@ func TestAccResourceInteger_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -491,18 +496,18 @@ func TestAccResourceInteger_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -513,10 +518,10 @@ func TestAccResourceInteger_Keepers_Replace_EmptyMapToValue(t *testing.T) { max = 100000000 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -527,18 +532,18 @@ func TestAccResourceInteger_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -548,10 +553,10 @@ func TestAccResourceInteger_Keepers_Replace_NullMapToValue(t *testing.T) { min = 1 max = 100000000 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -562,18 +567,18 @@ func TestAccResourceInteger_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_NullValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -586,10 +591,10 @@ func TestAccResourceInteger_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -600,18 +605,18 @@ func TestAccResourceInteger_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -624,10 +629,10 @@ func TestAccResourceInteger_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -636,18 +641,18 @@ func TestAccResourceInteger_Keepers_Replace_ValueToEmptyMap(t *testing.T) { max = 100000000 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_ValueToNullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -660,10 +665,10 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -671,18 +676,18 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNullMap(t *testing.T) { min = 1 max = 100000000 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_ValueToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -695,10 +700,10 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -709,18 +714,18 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_Replace_ValueToNewValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -733,10 +738,10 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -747,18 +752,18 @@ func TestAccResourceInteger_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -771,10 +776,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToNullValue(t *tes "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -785,18 +790,18 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToNullValue(t *tes "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -809,10 +814,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToValue(t *testing "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -823,18 +828,18 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToValue(t *testing "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -848,10 +853,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleNullValu "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -863,18 +868,18 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleNullValu "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -888,10 +893,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -903,18 +908,18 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -928,10 +933,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValue(t *testing.T "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -943,18 +948,18 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValue(t *testing.T "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -968,10 +973,10 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValueToValue(t *te "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id1), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -983,26 +988,16 @@ func TestAccResourceInteger_Keepers_FrameworkMigration_NullMapValueToValue(t *te "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_integer.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_integer.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_integer.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_integer.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } -func testCheckNotEmptyString(field string) func(input string) error { - return func(input string) error { - if input == "" { - return fmt.Errorf("%s is empty string", field) - } - - return nil - } -} - +//nolint:unparam // This function is only used to support the legacy import state check combinations with the Check function func testExtractResourceAttr(resourceName string, attributeName string, attributeValue *string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] @@ -1023,16 +1018,6 @@ func testExtractResourceAttr(resourceName string, attributeName string, attribut } } -func testCheckAttributeValuesDiffer(i *string, j *string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if testStringValue(i) == testStringValue(j) { - return fmt.Errorf("attribute values are the same") - } - - return nil - } -} - func testCheckAttributeValuesEqual(i *string, j *string) resource.TestCheckFunc { return func(s *terraform.State) error { if testStringValue(i) != testStringValue(j) { diff --git a/internal/provider/resource_password_test.go b/internal/provider/resource_password_test.go index 8934a3c3..ff129352 100644 --- a/internal/provider/resource_password_test.go +++ b/internal/provider/resource_password_test.go @@ -16,12 +16,17 @@ import ( res "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-go/tftypes" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" "github.com/hashicorp/terraform-plugin-testing/plancheck" + "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" "golang.org/x/crypto/bcrypt" "github.com/terraform-providers/terraform-provider-random/internal/random" + "github.com/terraform-providers/terraform-provider-random/internal/randomtest" ) func TestGenerateHash(t *testing.T) { @@ -117,9 +122,9 @@ func TestAccResourcePassword_Import(t *testing.T) { Config: `resource "random_password" "basic" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_password.basic", "result", testCheckLen(12)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.basic", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + }, }, { ResourceName: "random_password.basic", @@ -149,8 +154,6 @@ func TestAccResourcePassword_Import(t *testing.T) { func TestAccResourcePassword_BcryptHash(t *testing.T) { t.Parallel() - var result, bcryptHash string - resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), Steps: []resource.TestStep{ @@ -158,11 +161,13 @@ func TestAccResourcePassword_BcryptHash(t *testing.T) { Config: `resource "random_password" "test" { length = 73 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash), - testExtractResourceAttr("random_password.test", "result", &result), - testBcryptHashValid(&bcryptHash, &result), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.CompareValuePairs( + "random_password.test", tfjsonpath.New("bcrypt_hash"), + "random_password.test", tfjsonpath.New("result"), + randomtest.BcryptHashMatch(), + ), + }, }, }, }) @@ -171,7 +176,8 @@ func TestAccResourcePassword_BcryptHash(t *testing.T) { // TestAccResourcePassword_BcryptHash_FromVersion3_3_2 verifies behaviour when // upgrading state from schema V2 to V3 without a bcrypt_hash update. func TestAccResourcePassword_BcryptHash_FromVersion3_3_2(t *testing.T) { - var bcryptHash1, bcryptHash2, result1, result2 string + // The bcrypt_hash attribute values should be the same between test steps + assertBcryptHashSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -180,23 +186,28 @@ func TestAccResourcePassword_BcryptHash_FromVersion3_3_2(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash1), - testExtractResourceAttr("random_password.test", "result", &result1), - testBcryptHashValid(&bcryptHash1, &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertBcryptHashSame.AddStateValue("random_password.test", tfjsonpath.New("bcrypt_hash")), + statecheck.CompareValuePairs( + "random_password.test", tfjsonpath.New("bcrypt_hash"), + "random_password.test", tfjsonpath.New("result"), + randomtest.BcryptHashMatch(), + ), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash2), - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&bcryptHash1, &bcryptHash2), - testBcryptHashValid(&bcryptHash2, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertBcryptHashSame.AddStateValue("random_password.test", tfjsonpath.New("bcrypt_hash")), + statecheck.CompareValuePairs( + "random_password.test", tfjsonpath.New("bcrypt_hash"), + "random_password.test", tfjsonpath.New("result"), + randomtest.BcryptHashMatch(), + ), + }, }, }, }) @@ -205,7 +216,8 @@ func TestAccResourcePassword_BcryptHash_FromVersion3_3_2(t *testing.T) { // TestAccResourcePassword_BcryptHash_FromVersion3_4_2 verifies behaviour when // upgrading state from schema V2 to V3 with an expected bcrypt_hash update. func TestAccResourcePassword_BcryptHash_FromVersion3_4_2(t *testing.T) { - var bcryptHash1, bcryptHash2, result1, result2 string + // The bcrypt_hash attribute values should differ between test steps + assertBcryptHashDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -214,23 +226,28 @@ func TestAccResourcePassword_BcryptHash_FromVersion3_4_2(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash1), - testExtractResourceAttr("random_password.test", "result", &result1), - testBcryptHashInvalid(&bcryptHash1, &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertBcryptHashDiffer.AddStateValue("random_password.test", tfjsonpath.New("bcrypt_hash")), + statecheck.CompareValuePairs( + "random_password.test", tfjsonpath.New("bcrypt_hash"), + "random_password.test", tfjsonpath.New("result"), + randomtest.BcryptHashMismatch(), + ), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash2), - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&bcryptHash1, &bcryptHash2), - testBcryptHashValid(&bcryptHash2, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertBcryptHashDiffer.AddStateValue("random_password.test", tfjsonpath.New("bcrypt_hash")), + statecheck.CompareValuePairs( + "random_password.test", tfjsonpath.New("bcrypt_hash"), + "random_password.test", tfjsonpath.New("result"), + randomtest.BcryptHashMatch(), + ), + }, }, }, }) @@ -248,10 +265,10 @@ func TestAccResourcePassword_Override(t *testing.T) { upper = false numeric = false }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_password.override", "result", testCheckLen(4)), - resource.TestCheckResourceAttr("random_password.override", "result", "!!!!"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.override", tfjsonpath.New("result"), randomtest.StringLengthExact(4)), + statecheck.ExpectKnownValue("random_password.override", tfjsonpath.New("result"), knownvalue.StringExact("!!!!")), + }, }, }, }) @@ -262,7 +279,8 @@ func TestAccResourcePassword_Override(t *testing.T) { // override_special value to null and should not result in a plan difference. // Reference: https://github.com/hashicorp/terraform-provider-random/issues/306 func TestAccResourcePassword_OverrideSpecial_FromVersion3_3_2(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -271,21 +289,20 @@ func TestAccResourcePassword_OverrideSpecial_FromVersion3_3_2(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_password.test", "override_special"), - testExtractResourceAttr("random_password.test", "result", &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_password.test", "override_special"), - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + }, }, }, }) @@ -296,7 +313,8 @@ func TestAccResourcePassword_OverrideSpecial_FromVersion3_3_2(t *testing.T) { // override_special value to "", while other versions do not. // Reference: https://github.com/hashicorp/terraform-provider-random/issues/306 func TestAccResourcePassword_OverrideSpecial_FromVersion3_4_2(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -305,21 +323,20 @@ func TestAccResourcePassword_OverrideSpecial_FromVersion3_4_2(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_password.test", "override_special", ""), - testExtractResourceAttr("random_password.test", "result", &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("override_special"), knownvalue.StringExact("")), + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_password.test", "override_special"), - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + }, }, }, }) @@ -365,6 +382,8 @@ func TestAccResourcePassword_Import_FromVersion3_1_3(t *testing.T) { ImportState: true, ImportStateId: "Z=:cbrJE?Ltg", ImportStatePersist: true, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 ImportStateCheck: composeImportStateCheck( testCheckNoResourceAttrInstanceState("length"), testCheckNoResourceAttrInstanceState("number"), @@ -388,17 +407,21 @@ func TestAccResourcePassword_Import_FromVersion3_1_3(t *testing.T) { plancheck.ExpectEmptyPlan(), }, }, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_special"), knownvalue.Int64Exact(0)), + }, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_password.test", "result", testCheckLen(12)), - resource.TestCheckResourceAttr("random_password.test", "number", "true"), - resource.TestCheckResourceAttr("random_password.test", "numeric", "true"), - resource.TestCheckResourceAttr("random_password.test", "upper", "true"), - resource.TestCheckResourceAttr("random_password.test", "lower", "true"), - resource.TestCheckResourceAttr("random_password.test", "special", "true"), - resource.TestCheckResourceAttr("random_password.test", "min_numeric", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_upper", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_lower", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_special", "0"), testExtractResourceAttr("random_password.test", "result", &result2), testCheckAttributeValuesEqual(&result1, &result2), ), @@ -449,17 +472,21 @@ func TestAccResourcePassword_Import_FromVersion3_2_0(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_special"), knownvalue.Int64Exact(0)), + }, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_password.test", "result", testCheckLen(12)), - resource.TestCheckResourceAttr("random_password.test", "number", "true"), - resource.TestCheckResourceAttr("random_password.test", "numeric", "true"), - resource.TestCheckResourceAttr("random_password.test", "upper", "true"), - resource.TestCheckResourceAttr("random_password.test", "lower", "true"), - resource.TestCheckResourceAttr("random_password.test", "special", "true"), - resource.TestCheckResourceAttr("random_password.test", "min_numeric", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_upper", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_lower", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_special", "0"), testExtractResourceAttr("random_password.test", "result", &result2), testCheckAttributeValuesEqual(&result1, &result2), ), @@ -504,17 +531,21 @@ func TestAccResourcePassword_Import_FromVersion3_4_2(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("min_special"), knownvalue.Int64Exact(0)), + }, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_password.test", "result", testCheckLen(12)), - resource.TestCheckResourceAttr("random_password.test", "number", "true"), - resource.TestCheckResourceAttr("random_password.test", "numeric", "true"), - resource.TestCheckResourceAttr("random_password.test", "upper", "true"), - resource.TestCheckResourceAttr("random_password.test", "lower", "true"), - resource.TestCheckResourceAttr("random_password.test", "special", "true"), - resource.TestCheckResourceAttr("random_password.test", "min_numeric", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_upper", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_lower", "0"), - resource.TestCheckResourceAttr("random_password.test", "min_special", "0"), testExtractResourceAttr("random_password.test", "result", &result2), testCheckAttributeValuesEqual(&result1, &result2), ), @@ -530,11 +561,11 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { t.Parallel() cases := []struct { - name string - configBeforeUpgrade string - configDuringUpgrade string - beforeStateUpgrade []resource.TestCheckFunc - afterStateUpgrade []resource.TestCheckFunc + name string + configBeforeUpgrade string + configDuringUpgrade string + beforeUpgradeStateChecks []statecheck.StateCheck + afterUpgradeStateChecks []statecheck.StateCheck }{ { name: "bcrypt_hash", @@ -544,11 +575,11 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckNoResourceAttr("random_password.default", "bcrypt_hash"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("bcrypt_hash")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttrSet("random_password.default", "bcrypt_hash"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), }, }, { @@ -559,13 +590,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -577,13 +612,18 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric")}, }, { name: "number is absent before numeric is false during", @@ -594,13 +634,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -612,13 +656,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -631,13 +679,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -650,13 +702,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -668,13 +724,17 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -687,33 +747,37 @@ func TestAccResourcePassword_StateUpgradeV0toV3(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { - resource.UnitTest(t, resource.TestCase{ + resource.Test(t, resource.TestCase{ Steps: []resource.TestStep{ { ExternalProviders: map[string]resource.ExternalProvider{"random": { VersionConstraint: "3.1.3", Source: "hashicorp/random", }}, - Config: c.configBeforeUpgrade, - Check: resource.ComposeTestCheckFunc(c.beforeStateUpgrade...), + Config: c.configBeforeUpgrade, + ConfigStateChecks: c.beforeUpgradeStateChecks, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: c.configDuringUpgrade, - Check: resource.ComposeTestCheckFunc(c.afterStateUpgrade...), + ConfigStateChecks: c.afterUpgradeStateChecks, }, }, }) @@ -729,11 +793,13 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { t.Parallel() cases := []struct { - name string - configBeforeUpgrade string - configDuringUpgrade string - beforeStateUpgrade []resource.TestCheckFunc - afterStateUpgrade []resource.TestCheckFunc + name string + configBeforeUpgrade string + configDuringUpgrade string + beforeUpgradeStateChecks []statecheck.StateCheck + afterUpgradeStateChecks []statecheck.StateCheck + beforeStateUpgrade []resource.TestCheckFunc + afterStateUpgrade []resource.TestCheckFunc }{ { name: "number is absent before number and numeric are absent during", @@ -743,13 +809,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -761,13 +831,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -779,13 +853,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -798,13 +876,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -816,13 +898,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -835,13 +921,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -854,13 +944,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -872,13 +966,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { configDuringUpgrade: `resource "random_password" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -891,13 +989,17 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_password.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_password.default", tfjsonpath.New("numeric")), }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_password.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_password.default", "number", "random_password.default", "numeric"), + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_password.default", tfjsonpath.New("number"), + "random_password.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, } @@ -908,21 +1010,20 @@ func TestAccResourcePassword_StateUpgradeV1toV3(t *testing.T) { c.configDuringUpgrade = c.configBeforeUpgrade } - // TODO: Why is resource.Test not being used here - resource.UnitTest(t, resource.TestCase{ + resource.Test(t, resource.TestCase{ Steps: []resource.TestStep{ { ExternalProviders: map[string]resource.ExternalProvider{"random": { VersionConstraint: "3.2.0", Source: "hashicorp/random", }}, - Config: c.configBeforeUpgrade, - Check: resource.ComposeTestCheckFunc(c.beforeStateUpgrade...), + Config: c.configBeforeUpgrade, + ConfigStateChecks: c.beforeUpgradeStateChecks, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: c.configDuringUpgrade, - Check: resource.ComposeTestCheckFunc(c.afterStateUpgrade...), + ConfigStateChecks: c.afterUpgradeStateChecks, }, }, }) @@ -943,13 +1044,13 @@ func TestAccResourcePassword_Min(t *testing.T) { 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(`([!#@])`)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + }, }, }, }) @@ -974,21 +1075,21 @@ func TestAccResourcePassword_UpgradeFromVersion2_2_1(t *testing.T) { 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -1005,23 +1106,23 @@ func TestAccResourcePassword_UpgradeFromVersion2_2_1(t *testing.T) { plancheck.ExpectEmptyPlan(), }, }, - 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), + }, }, }, }) @@ -1041,22 +1142,22 @@ func TestAccResourcePassword_UpgradeFromVersion3_2_0(t *testing.T) { 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -1080,23 +1181,23 @@ func TestAccResourcePassword_UpgradeFromVersion3_2_0(t *testing.T) { 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), + }, }, }, }) @@ -1116,23 +1217,23 @@ func TestAccResourcePassword_UpgradeFromVersion3_3_2(t *testing.T) { 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -1156,23 +1257,23 @@ func TestAccResourcePassword_UpgradeFromVersion3_3_2(t *testing.T) { 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", "number", "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"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + statecheck.ExpectKnownValue("random_password.min", tfjsonpath.New("bcrypt_hash"), knownvalue.NotNull()), + }, }, }, }) @@ -2032,7 +2133,8 @@ func TestAccResourcePassword_NumberNumericErrors(t *testing.T) { } func TestAccResourcePassword_Keepers_Keep_EmptyMap(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2042,10 +2144,10 @@ func TestAccResourcePassword_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2053,18 +2155,18 @@ func TestAccResourcePassword_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2074,10 +2176,10 @@ func TestAccResourcePassword_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2087,18 +2189,18 @@ func TestAccResourcePassword_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_NullMap(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2107,28 +2209,28 @@ func TestAccResourcePassword_Keepers_Keep_NullMap(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2137,10 +2239,10 @@ func TestAccResourcePassword_Keepers_Keep_NullMapToNullValue(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2150,18 +2252,18 @@ func TestAccResourcePassword_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_NullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2173,10 +2275,10 @@ func TestAccResourcePassword_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2186,18 +2288,18 @@ func TestAccResourcePassword_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_NullValues(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2210,10 +2312,10 @@ func TestAccResourcePassword_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2224,18 +2326,18 @@ func TestAccResourcePassword_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_Value(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2247,10 +2349,10 @@ func TestAccResourcePassword_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2260,18 +2362,18 @@ func TestAccResourcePassword_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Keep_Values(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2284,10 +2386,10 @@ func TestAccResourcePassword_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2298,18 +2400,18 @@ func TestAccResourcePassword_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2319,10 +2421,10 @@ func TestAccResourcePassword_Keepers_Replace_EmptyMapToValue(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2332,18 +2434,18 @@ func TestAccResourcePassword_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_NullMapToValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2352,10 +2454,10 @@ func TestAccResourcePassword_Keepers_Replace_NullMapToValue(t *testing.T) { Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2365,18 +2467,18 @@ func TestAccResourcePassword_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_NullValueToValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2388,10 +2490,10 @@ func TestAccResourcePassword_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2401,18 +2503,18 @@ func TestAccResourcePassword_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2424,10 +2526,10 @@ func TestAccResourcePassword_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2435,18 +2537,18 @@ func TestAccResourcePassword_Keepers_Replace_ValueToEmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_ValueToNullMap(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2458,28 +2560,28 @@ func TestAccResourcePassword_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_password" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_ValueToNullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2491,10 +2593,10 @@ func TestAccResourcePassword_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2504,18 +2606,18 @@ func TestAccResourcePassword_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_Replace_ValueToNewValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2527,10 +2629,10 @@ func TestAccResourcePassword_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2540,18 +2642,18 @@ func TestAccResourcePassword_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2563,10 +2665,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToNullValue(t *te "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2576,18 +2678,18 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToNullValue(t *te "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2599,10 +2701,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToValue(t *testin "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2612,18 +2714,18 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToValue(t *testin "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2636,10 +2738,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleNullVal "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2650,18 +2752,18 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleNullVal "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2674,10 +2776,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2688,18 +2790,18 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2712,10 +2814,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValue(t *testing. "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2726,18 +2828,18 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValue(t *testing. "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var result1, result2 string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -2750,10 +2852,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValueToValue(t *t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result1), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -2764,11 +2866,10 @@ func TestAccResourcePassword_Keepers_FrameworkMigration_NullMapValueToValue(t *t "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_password.test", "result", &result2), - testCheckAttributeValuesDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_password.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_password.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_password.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) @@ -2829,40 +2930,6 @@ func TestAccResourcePassword_NumericNumberFalse(t *testing.T) { }) } -func testBcryptHashInvalid(hash *string, password *string) resource.TestCheckFunc { - return func(_ *terraform.State) error { - if hash == nil || *hash == "" { - return fmt.Errorf("expected hash value") - } - - if password == nil || *password == "" { - return fmt.Errorf("expected password value") - } - - err := bcrypt.CompareHashAndPassword([]byte(*hash), []byte(*password)) - - if !errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { - return fmt.Errorf("unexpected error: %s", err) - } - - return nil - } -} - -func testBcryptHashValid(hash *string, password *string) resource.TestCheckFunc { - return func(_ *terraform.State) error { - if hash == nil || *hash == "" { - return fmt.Errorf("expected hash value") - } - - if password == nil || *password == "" { - return fmt.Errorf("expected password value") - } - - return bcrypt.CompareHashAndPassword([]byte(*hash), []byte(*password)) - } -} - func composeImportStateCheck(fs ...resource.ImportStateCheckFunc) resource.ImportStateCheckFunc { return func(s []*terraform.InstanceState) error { for i, f := range fs { diff --git a/internal/provider/resource_pet_test.go b/internal/provider/resource_pet_test.go index f0ac15fb..87ecd1c0 100644 --- a/internal/provider/resource_pet_test.go +++ b/internal/provider/resource_pet_test.go @@ -4,12 +4,14 @@ package provider import ( - "fmt" "regexp" - "strings" "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" ) func TestAccResourcePet(t *testing.T) { @@ -19,16 +21,17 @@ func TestAccResourcePet(t *testing.T) { { Config: `resource "random_pet" "pet_1" { }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("-", 2)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-z]+-[a-z]+$`))), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_EmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -37,28 +40,28 @@ func TestAccResourcePet_Keepers_Keep_EmptyMap(t *testing.T) { Config: `resource "random_pet" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -67,10 +70,10 @@ func TestAccResourcePet_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { Config: `resource "random_pet" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -79,18 +82,18 @@ func TestAccResourcePet_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_NullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -98,27 +101,27 @@ func TestAccResourcePet_Keepers_Keep_NullMap(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -126,10 +129,10 @@ func TestAccResourcePet_Keepers_Keep_NullMapToNullValue(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -138,18 +141,18 @@ func TestAccResourcePet_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_NullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -160,10 +163,10 @@ func TestAccResourcePet_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -172,18 +175,18 @@ func TestAccResourcePet_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_NullValues(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -195,10 +198,10 @@ func TestAccResourcePet_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -208,18 +211,18 @@ func TestAccResourcePet_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_Value(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -230,10 +233,10 @@ func TestAccResourcePet_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -242,18 +245,18 @@ func TestAccResourcePet_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Keep_Values(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -265,10 +268,10 @@ func TestAccResourcePet_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -278,18 +281,18 @@ func TestAccResourcePet_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -298,10 +301,10 @@ func TestAccResourcePet_Keepers_Replace_EmptyMapToValue(t *testing.T) { Config: `resource "random_pet" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -310,18 +313,18 @@ func TestAccResourcePet_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -329,10 +332,10 @@ func TestAccResourcePet_Keepers_Replace_NullMapToValue(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -341,18 +344,18 @@ func TestAccResourcePet_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_NullValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -363,10 +366,10 @@ func TestAccResourcePet_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -375,18 +378,18 @@ func TestAccResourcePet_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -397,28 +400,28 @@ func TestAccResourcePet_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_ValueToNullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -429,27 +432,27 @@ func TestAccResourcePet_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_pet" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_ValueToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -460,10 +463,10 @@ func TestAccResourcePet_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -472,18 +475,18 @@ func TestAccResourcePet_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_Replace_ValueToNewValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -494,10 +497,10 @@ func TestAccResourcePet_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -506,18 +509,18 @@ func TestAccResourcePet_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -528,10 +531,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToNullValue(t *testing "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -540,18 +543,18 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToNullValue(t *testing "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -562,10 +565,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -574,18 +577,18 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -597,10 +600,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -610,18 +613,18 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -633,10 +636,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleValue(t *tes "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -646,18 +649,18 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapToMultipleValue(t *tes "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -669,10 +672,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -682,18 +685,18 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -705,10 +708,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValueToValue(t *testin "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id1), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -718,11 +721,10 @@ func TestAccResourcePet_Keepers_FrameworkMigration_NullMapValueToValue(t *testin "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_pet.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_pet.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_pet.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_pet.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) @@ -736,9 +738,9 @@ func TestAccResourcePet_Length(t *testing.T) { Config: `resource "random_pet" "pet_1" { length = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("-", 4)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-z]+-[a-z]+-[a-z]+-[a-z]+$`))), + }, }, }, }) @@ -752,10 +754,9 @@ func TestAccResourcePet_Prefix(t *testing.T) { Config: `resource "random_pet" "pet_1" { prefix = "consul" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("-", 3)), - resource.TestMatchResourceAttr("random_pet.pet_1", "id", regexp.MustCompile("^consul-")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^consul-[a-z]+-[a-z]+$`))), + }, }, }, }) @@ -770,9 +771,9 @@ func TestAccResourcePet_Separator(t *testing.T) { length = 3 separator = "_" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("_", 3)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-z]+_[a-z]+_[a-z]+$`))), + }, }, }, }) @@ -786,10 +787,9 @@ func TestAccResourcePet_UpgradeFromVersion3_3_2(t *testing.T) { Config: `resource "random_pet" "pet_1" { prefix = "consul" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("-", 3)), - resource.TestMatchResourceAttr("random_pet.pet_1", "id", regexp.MustCompile("^consul-")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^consul-[a-z]+-[a-z]+$`))), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -803,23 +803,10 @@ func TestAccResourcePet_UpgradeFromVersion3_3_2(t *testing.T) { Config: `resource "random_pet" "pet_1" { prefix = "consul" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_pet.pet_1", "id", testCheckPetLen("-", 3)), - resource.TestMatchResourceAttr("random_pet.pet_1", "id", regexp.MustCompile("^consul-")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_pet.pet_1", tfjsonpath.New("id"), knownvalue.StringRegexp(regexp.MustCompile(`^consul-[a-z]+-[a-z]+$`))), + }, }, }, }) } - -func testCheckPetLen(separator string, expectedLen int) func(input string) error { - return func(input string) error { - petNameParts := strings.Split(input, separator) - - if len(petNameParts) != expectedLen { - return fmt.Errorf("expected length %d, actual length %d", expectedLen, len(petNameParts)) - } - - return nil - } -} diff --git a/internal/provider/resource_shuffle_test.go b/internal/provider/resource_shuffle_test.go index 8af19446..ff253608 100644 --- a/internal/provider/resource_shuffle_test.go +++ b/internal/provider/resource_shuffle_test.go @@ -4,13 +4,13 @@ package provider import ( - "fmt" - "reflect" - "strconv" "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" ) // These results are current as of Go 1.6. The Go @@ -31,21 +31,27 @@ func TestAccResourceShuffle(t *testing.T) { input = ["a", "b", "c", "d", "e"] seed = "-" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.default_length", "result.#", testAccResourceShuffleCheckLength("5")), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.0", "a"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.1", "c"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.2", "b"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.3", "e"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.4", "d"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.default_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + knownvalue.StringExact("e"), + knownvalue.StringExact("d"), + }, + ), + ), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_EmptyMap(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -55,10 +61,10 @@ func TestAccResourceShuffle_Keepers_Keep_EmptyMap(t *testing.T) { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -66,18 +72,18 @@ func TestAccResourceShuffle_Keepers_Keep_EmptyMap(t *testing.T) { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -87,10 +93,10 @@ func TestAccResourceShuffle_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -100,18 +106,18 @@ func TestAccResourceShuffle_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_NullMap(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -120,28 +126,28 @@ func TestAccResourceShuffle_Keepers_Keep_NullMap(t *testing.T) { Config: `resource "random_shuffle" "test" { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_shuffle" "test" { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -150,10 +156,10 @@ func TestAccResourceShuffle_Keepers_Keep_NullMapToNullValue(t *testing.T) { Config: `resource "random_shuffle" "test" { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -163,18 +169,18 @@ func TestAccResourceShuffle_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_NullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -186,10 +192,10 @@ func TestAccResourceShuffle_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -199,18 +205,18 @@ func TestAccResourceShuffle_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_NullValues(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -223,10 +229,10 @@ func TestAccResourceShuffle_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -237,18 +243,18 @@ func TestAccResourceShuffle_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_Value(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -260,10 +266,10 @@ func TestAccResourceShuffle_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -273,18 +279,18 @@ func TestAccResourceShuffle_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Keep_Values(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -297,10 +303,10 @@ func TestAccResourceShuffle_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -311,18 +317,18 @@ func TestAccResourceShuffle_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -332,10 +338,10 @@ func TestAccResourceShuffle_Keepers_Replace_EmptyMapToValue(t *testing.T) { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -345,18 +351,18 @@ func TestAccResourceShuffle_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_NullMapToValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -365,10 +371,10 @@ func TestAccResourceShuffle_Keepers_Replace_NullMapToValue(t *testing.T) { Config: `resource "random_shuffle" "test" { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -378,18 +384,18 @@ func TestAccResourceShuffle_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_NullValueToValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -401,10 +407,10 @@ func TestAccResourceShuffle_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -414,18 +420,18 @@ func TestAccResourceShuffle_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -437,10 +443,10 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -448,18 +454,18 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToEmptyMap(t *testing.T) { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_ValueToNullMap(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -471,28 +477,28 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_shuffle" "test" { input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_ValueToNullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -504,10 +510,10 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -517,18 +523,18 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_Replace_ValueToNewValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -540,10 +546,10 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -553,18 +559,18 @@ func TestAccResourceShuffle_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -576,10 +582,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToNullValue(t *tes "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -589,18 +595,18 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToNullValue(t *tes "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -612,10 +618,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToValue(t *testing "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -625,18 +631,18 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToValue(t *testing "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -649,10 +655,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleNullValu "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -663,18 +669,18 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleNullValu "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -687,10 +693,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -701,18 +707,18 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapToMultipleValue(t "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -725,10 +731,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValue(t *testing.T "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -739,18 +745,18 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValue(t *testing.T "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsEqual(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultSame.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var result1, result2 []string + // The result attribute values should differ between test steps + assertResultDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -763,10 +769,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValueToValue(t *te "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result1), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -777,11 +783,10 @@ func TestAccResourceShuffle_Keepers_FrameworkMigration_NullMapValueToValue(t *te "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttrList("random_shuffle.test", "result", &result2), - testCheckAttributeValueListsDiffer(&result1, &result2), - resource.TestCheckResourceAttr("random_shuffle.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertResultDiffer.AddStateValue("random_shuffle.test", tfjsonpath.New("result")), + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) @@ -799,9 +804,9 @@ func TestAccResourceShuffle_ResultCount_Zero(t *testing.T) { input = ["a", "b", "c", "d", "e"] result_count = 0 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_shuffle.test", "result.#", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.test", tfjsonpath.New("result"), knownvalue.ListSizeExact(0)), + }, }, }, }) @@ -817,12 +822,17 @@ func TestAccResourceShuffle_ResultCount_Shorter(t *testing.T) { seed = "-" result_count = 3 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.shorter_length", "result.#", testAccResourceShuffleCheckLength("3")), - resource.TestCheckResourceAttr("random_shuffle.shorter_length", "result.0", "a"), - resource.TestCheckResourceAttr("random_shuffle.shorter_length", "result.1", "c"), - resource.TestCheckResourceAttr("random_shuffle.shorter_length", "result.2", "b"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.shorter_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + }, + ), + ), + }, }, }, }) @@ -838,21 +848,26 @@ func TestAccResourceShuffle_ResultCount_Longer(t *testing.T) { seed = "-" result_count = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.longer_length", "result.#", testAccResourceShuffleCheckLength("12")), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.0", "a"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.1", "c"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.2", "b"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.3", "e"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.4", "d"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.5", "a"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.6", "e"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.7", "d"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.8", "c"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.9", "b"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.10", "a"), - resource.TestCheckResourceAttr("random_shuffle.longer_length", "result.11", "b"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.longer_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + knownvalue.StringExact("e"), + knownvalue.StringExact("d"), + knownvalue.StringExact("a"), + knownvalue.StringExact("e"), + knownvalue.StringExact("d"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + knownvalue.StringExact("a"), + knownvalue.StringExact("b"), + }, + ), + ), + }, }, }, }) @@ -868,9 +883,9 @@ func TestAccResourceShuffle_Input_Empty(t *testing.T) { seed = "-" result_count = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.empty_length", "result.#", testAccResourceShuffleCheckLength("0")), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.empty_length", tfjsonpath.New("result"), knownvalue.ListSizeExact(0)), + }, }, }, }) @@ -886,10 +901,15 @@ func TestAccResourceShuffle_Input_One(t *testing.T) { seed = "-" result_count = 1 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.one_length", "result.#", testAccResourceShuffleCheckLength("1")), - resource.TestCheckResourceAttr("random_shuffle.one_length", "result.0", "a"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.one_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + }, + ), + ), + }, }, }, }) @@ -904,14 +924,19 @@ func TestAccResourceShuffle_UpgradeFromVersion3_3_2(t *testing.T) { input = ["a", "b", "c", "d", "e"] seed = "-" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.default_length", "result.#", testAccResourceShuffleCheckLength("5")), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.0", "a"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.1", "c"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.2", "b"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.3", "e"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.4", "d"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.default_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + knownvalue.StringExact("e"), + knownvalue.StringExact("d"), + }, + ), + ), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -927,88 +952,20 @@ func TestAccResourceShuffle_UpgradeFromVersion3_3_2(t *testing.T) { input = ["a", "b", "c", "d", "e"] seed = "-" }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_shuffle.default_length", "result.#", testAccResourceShuffleCheckLength("5")), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.0", "a"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.1", "c"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.2", "b"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.3", "e"), - resource.TestCheckResourceAttr("random_shuffle.default_length", "result.4", "d"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_shuffle.default_length", tfjsonpath.New("result"), + knownvalue.ListExact( + []knownvalue.Check{ + knownvalue.StringExact("a"), + knownvalue.StringExact("c"), + knownvalue.StringExact("b"), + knownvalue.StringExact("e"), + knownvalue.StringExact("d"), + }, + ), + ), + }, }, }, }) } - -func testAccResourceShuffleCheckLength(expectedLength string) func(input string) error { - return func(input string) error { - if input != expectedLength { - return fmt.Errorf("got length %s; expected length %s", input, expectedLength) - } - - return nil - } -} - -//nolint:unparam -func testExtractResourceAttrList(resourceName string, attributeName string, attributeValue *[]string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[resourceName] - - if !ok { - return fmt.Errorf("resource name %s not found in state", resourceName) - } - - elementCountAttr := attributeName + ".#" - - elementCountValue, ok := rs.Primary.Attributes[elementCountAttr] - - if !ok { - return fmt.Errorf("attribute %s not found in resource %s state", elementCountAttr, resourceName) - } - - elementCount, err := strconv.Atoi(elementCountValue) - - if err != nil { - return fmt.Errorf("attribute %s not integer: %w", elementCountAttr, err) - } - - listValue := make([]string, elementCount) - - for i := 0; i < elementCount; i++ { - attr := attributeName + "." + strconv.Itoa(i) - - attrValue, ok := rs.Primary.Attributes[attr] - - if !ok { - return fmt.Errorf("attribute %s not found in resource %s state", attr, resourceName) - } - - listValue[i] = attrValue - } - - *attributeValue = listValue - - return nil - } -} - -func testCheckAttributeValueListsDiffer(i *[]string, j *[]string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if reflect.DeepEqual(i, j) { - return fmt.Errorf("attribute values are the same") - } - - return nil - } -} - -func testCheckAttributeValueListsEqual(i *[]string, j *[]string) resource.TestCheckFunc { - return func(s *terraform.State) error { - if !reflect.DeepEqual(i, j) { - return fmt.Errorf("attribute values are different, got %v and %v", i, j) - } - - return nil - } -} diff --git a/internal/provider/resource_string_test.go b/internal/provider/resource_string_test.go index 84af2fc8..e73787a2 100644 --- a/internal/provider/resource_string_test.go +++ b/internal/provider/resource_string_test.go @@ -5,7 +5,6 @@ package provider import ( "context" - "fmt" "regexp" "testing" @@ -13,7 +12,12 @@ import ( res "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-go/tftypes" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" + "github.com/terraform-providers/terraform-provider-random/internal/randomtest" ) func TestAccResourceString_Import(t *testing.T) { @@ -24,9 +28,9 @@ func TestAccResourceString_Import(t *testing.T) { Config: `resource "random_string" "basic" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.basic", "result", testCheckLen(12)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.basic", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + }, }, { ResourceName: "random_string.basic", @@ -61,7 +65,8 @@ func TestAccResourceString_ImportWithoutKeepersProducesNoPlannedChanges(t *testi } func TestAccResourceString_Keepers_Keep_EmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -71,10 +76,10 @@ func TestAccResourceString_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -82,19 +87,18 @@ func TestAccResourceString_Keepers_Keep_EmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var id1, id2 string - + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ { @@ -103,10 +107,10 @@ func TestAccResourceString_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -116,18 +120,18 @@ func TestAccResourceString_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_NullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -136,28 +140,28 @@ func TestAccResourceString_Keepers_Keep_NullMap(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -166,10 +170,10 @@ func TestAccResourceString_Keepers_Keep_NullMapToNullValue(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -179,18 +183,18 @@ func TestAccResourceString_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_NullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -202,10 +206,10 @@ func TestAccResourceString_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -215,18 +219,18 @@ func TestAccResourceString_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_NullValues(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -239,10 +243,10 @@ func TestAccResourceString_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -253,18 +257,18 @@ func TestAccResourceString_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_Value(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -276,10 +280,10 @@ func TestAccResourceString_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -289,18 +293,18 @@ func TestAccResourceString_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Keep_Values(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -313,10 +317,10 @@ func TestAccResourceString_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -327,18 +331,18 @@ func TestAccResourceString_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -348,10 +352,10 @@ func TestAccResourceString_Keepers_Replace_EmptyMapToValue(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -361,18 +365,18 @@ func TestAccResourceString_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -381,10 +385,10 @@ func TestAccResourceString_Keepers_Replace_NullMapToValue(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -394,18 +398,18 @@ func TestAccResourceString_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_NullValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -417,10 +421,10 @@ func TestAccResourceString_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -430,18 +434,18 @@ func TestAccResourceString_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -453,10 +457,10 @@ func TestAccResourceString_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -464,18 +468,18 @@ func TestAccResourceString_Keepers_Replace_ValueToEmptyMap(t *testing.T) { length = 12 keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_ValueToNullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -487,28 +491,28 @@ func TestAccResourceString_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_ValueToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -520,10 +524,10 @@ func TestAccResourceString_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -533,18 +537,18 @@ func TestAccResourceString_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_Replace_ValueToNewValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -556,10 +560,10 @@ func TestAccResourceString_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -569,18 +573,18 @@ func TestAccResourceString_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -592,10 +596,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToNullValue(t *test "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -605,18 +609,18 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToNullValue(t *test "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -628,10 +632,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToValue(t *testing. "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -641,18 +645,18 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToValue(t *testing. "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -665,10 +669,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleNullValue "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -679,18 +683,18 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleNullValue "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -703,10 +707,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleValue(t * "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -717,18 +721,18 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapToMultipleValue(t * "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -741,10 +745,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapValue(t *testing.T) "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -755,18 +759,18 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapValue(t *testing.T) "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceString_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -779,10 +783,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapValueToValue(t *tes "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id1), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -793,11 +797,10 @@ func TestAccResourceString_Keepers_FrameworkMigration_NullMapValueToValue(t *tes "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_string.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_string.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_string.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) @@ -815,10 +818,9 @@ func TestAccResourceString_Override(t *testing.T) { upper = false numeric = false }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.override", "result", testCheckLen(4)), - resource.TestCheckResourceAttr("random_string.override", "result", "!!!!"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.override", tfjsonpath.New("result"), knownvalue.StringExact("!!!!")), + }, }, }, }) @@ -829,7 +831,8 @@ func TestAccResourceString_Override(t *testing.T) { // override_special value to null and should not result in a plan difference. // Reference: https://github.com/hashicorp/terraform-provider-random/issues/306 func TestAccResourceString_OverrideSpecial_FromVersion3_3_2(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -838,21 +841,20 @@ func TestAccResourceString_OverrideSpecial_FromVersion3_3_2(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_string.test", "override_special"), - testExtractResourceAttr("random_string.test", "result", &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_string.test", tfjsonpath.New("result")), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_string.test", "override_special"), - testExtractResourceAttr("random_string.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_string.test", tfjsonpath.New("result")), + }, }, }, }) @@ -863,7 +865,8 @@ func TestAccResourceString_OverrideSpecial_FromVersion3_3_2(t *testing.T) { // override_special value to "", while other versions do not. // Reference: https://github.com/hashicorp/terraform-provider-random/issues/306 func TestAccResourceString_OverrideSpecial_FromVersion3_4_2(t *testing.T) { - var result1, result2 string + // The result attribute values should be the same between test steps + assertResultSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -872,21 +875,20 @@ func TestAccResourceString_OverrideSpecial_FromVersion3_4_2(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("random_string.test", "override_special", ""), - testExtractResourceAttr("random_string.test", "result", &result1), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("override_special"), knownvalue.StringExact("")), + assertResultSame.AddStateValue("random_string.test", tfjsonpath.New("result")), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_string" "test" { length = 12 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr("random_string.test", "override_special"), - testExtractResourceAttr("random_string.test", "result", &result2), - testCheckAttributeValuesEqual(&result1, &result2), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("override_special"), knownvalue.Null()), + assertResultSame.AddStateValue("random_string.test", tfjsonpath.New("result")), + }, }, }, }) @@ -905,13 +907,13 @@ func TestAccResourceString_Min(t *testing.T) { min_special = 1 min_numeric = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.min", "result", testCheckLen(12)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@].*)`)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@].*)`))), + }, }, }, }) @@ -925,11 +927,11 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { t.Parallel() cases := []struct { - name string - configBeforeUpgrade string - configDuringUpgrade string - beforeStateUpgrade []resource.TestCheckFunc - afterStateUpgrade []resource.TestCheckFunc + name string + configBeforeUpgrade string + configDuringUpgrade string + beforeUpgradeStateChecks []statecheck.StateCheck + afterUpgradeStateChecks []statecheck.StateCheck }{ { name: "number is absent before number and numeric are absent during", @@ -939,13 +941,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { configDuringUpgrade: `resource "random_string" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -957,13 +963,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -975,13 +985,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -994,13 +1008,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -1012,13 +1030,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { configDuringUpgrade: `resource "random_string" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -1031,13 +1053,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -1050,13 +1076,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = false }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -1068,13 +1098,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { configDuringUpgrade: `resource "random_string" "default" { length = 12 }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, { @@ -1087,13 +1121,17 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { length = 12 numeric = true }`, - beforeStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "false"), - resource.TestCheckNoResourceAttr("random_string.default", "numeric"), - }, - afterStateUpgrade: []resource.TestCheckFunc{ - resource.TestCheckResourceAttr("random_string.default", "number", "true"), - resource.TestCheckResourceAttrPair("random_string.default", "number", "random_string.default", "numeric"), + beforeUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(false)), + randomtest.ExpectNoAttribute("random_string.default", tfjsonpath.New("numeric")), + }, + afterUpgradeStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.default", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.CompareValuePairs( + "random_string.default", tfjsonpath.New("number"), + "random_string.default", tfjsonpath.New("numeric"), + compare.ValuesSame(), + ), }, }, } @@ -1107,13 +1145,13 @@ func TestAccResourceString_StateUpgradeV1toV2(t *testing.T) { VersionConstraint: "3.2.0", Source: "hashicorp/random", }}, - Config: c.configBeforeUpgrade, - Check: resource.ComposeTestCheckFunc(c.beforeStateUpgrade...), + Config: c.configBeforeUpgrade, + ConfigStateChecks: c.beforeUpgradeStateChecks, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: c.configDuringUpgrade, - Check: resource.ComposeTestCheckFunc(c.afterStateUpgrade...), + ConfigStateChecks: c.afterUpgradeStateChecks, }, }, }) @@ -1156,21 +1194,21 @@ func TestAccResourceString_UpgradeFromVersion3_2_0(t *testing.T) { min_special = 1 min_numeric = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.min", "result", testCheckLen(12)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@])`)), - resource.TestCheckResourceAttr("random_string.min", "special", "true"), - resource.TestCheckResourceAttr("random_string.min", "upper", "true"), - resource.TestCheckResourceAttr("random_string.min", "lower", "true"), - resource.TestCheckResourceAttr("random_string.min", "number", "true"), - resource.TestCheckResourceAttr("random_string.min", "min_special", "1"), - resource.TestCheckResourceAttr("random_string.min", "min_upper", "3"), - resource.TestCheckResourceAttr("random_string.min", "min_lower", "2"), - resource.TestCheckResourceAttr("random_string.min", "min_numeric", "4"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -1194,22 +1232,22 @@ func TestAccResourceString_UpgradeFromVersion3_2_0(t *testing.T) { min_special = 1 min_numeric = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.min", "result", testCheckLen(12)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@])`)), - resource.TestCheckResourceAttr("random_string.min", "special", "true"), - resource.TestCheckResourceAttr("random_string.min", "upper", "true"), - resource.TestCheckResourceAttr("random_string.min", "lower", "true"), - resource.TestCheckResourceAttr("random_string.min", "number", "true"), - resource.TestCheckResourceAttr("random_string.min", "numeric", "true"), - resource.TestCheckResourceAttr("random_string.min", "min_special", "1"), - resource.TestCheckResourceAttr("random_string.min", "min_upper", "3"), - resource.TestCheckResourceAttr("random_string.min", "min_lower", "2"), - resource.TestCheckResourceAttr("random_string.min", "min_numeric", "4"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + }, }, }, }) @@ -1229,22 +1267,22 @@ func TestAccResourceString_UpgradeFromVersion3_3_2(t *testing.T) { min_special = 1 min_numeric = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.min", "result", testCheckLen(12)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@])`)), - resource.TestCheckResourceAttr("random_string.min", "special", "true"), - resource.TestCheckResourceAttr("random_string.min", "upper", "true"), - resource.TestCheckResourceAttr("random_string.min", "lower", "true"), - resource.TestCheckResourceAttr("random_string.min", "number", "true"), - resource.TestCheckResourceAttr("random_string.min", "numeric", "true"), - resource.TestCheckResourceAttr("random_string.min", "min_special", "1"), - resource.TestCheckResourceAttr("random_string.min", "min_upper", "3"), - resource.TestCheckResourceAttr("random_string.min", "min_lower", "2"), - resource.TestCheckResourceAttr("random_string.min", "min_numeric", "4"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -1268,22 +1306,22 @@ func TestAccResourceString_UpgradeFromVersion3_3_2(t *testing.T) { min_special = 1 min_numeric = 4 }`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.min", "result", testCheckLen(12)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)), - resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@])`)), - resource.TestCheckResourceAttr("random_string.min", "special", "true"), - resource.TestCheckResourceAttr("random_string.min", "upper", "true"), - resource.TestCheckResourceAttr("random_string.min", "lower", "true"), - resource.TestCheckResourceAttr("random_string.min", "number", "true"), - resource.TestCheckResourceAttr("random_string.min", "numeric", "true"), - resource.TestCheckResourceAttr("random_string.min", "min_special", "1"), - resource.TestCheckResourceAttr("random_string.min", "min_upper", "3"), - resource.TestCheckResourceAttr("random_string.min", "min_lower", "2"), - resource.TestCheckResourceAttr("random_string.min", "min_numeric", "4"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([a-z].*){2,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([A-Z].*){3,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([0-9].*){4,}`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`([!#@])`))), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_special"), knownvalue.Int64Exact(1)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(3)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(2)), + statecheck.ExpectKnownValue("random_string.min", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(4)), + }, }, }, }) @@ -1707,17 +1745,21 @@ func TestAccResourceString_Import_FromVersion3_1_3(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_special"), knownvalue.Int64Exact(0)), + }, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.test", "result", testCheckLen(12)), - resource.TestCheckResourceAttr("random_string.test", "number", "true"), - resource.TestCheckResourceAttr("random_string.test", "numeric", "true"), - resource.TestCheckResourceAttr("random_string.test", "upper", "true"), - resource.TestCheckResourceAttr("random_string.test", "lower", "true"), - resource.TestCheckResourceAttr("random_string.test", "special", "true"), - resource.TestCheckResourceAttr("random_string.test", "min_numeric", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_upper", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_lower", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_special", "0"), testExtractResourceAttr("random_string.test", "result", &result2), testCheckAttributeValuesEqual(&result1, &result2), ), @@ -1762,17 +1804,21 @@ func TestAccResourceString_Import_FromVersion3_4_2(t *testing.T) { Config: `resource "random_string" "test" { length = 12 }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("result"), randomtest.StringLengthExact(12)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("number"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("numeric"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("upper"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("lower"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("special"), knownvalue.Bool(true)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_numeric"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_upper"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_lower"), knownvalue.Int64Exact(0)), + statecheck.ExpectKnownValue("random_string.test", tfjsonpath.New("min_special"), knownvalue.Int64Exact(0)), + }, + // TODO: Import state checks haven't been implemented in terraform-plugin-testing yet, so can't use value comparers for now + // https://github.com/hashicorp/terraform-plugin-testing/issues/365 Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrWith("random_string.test", "result", testCheckLen(12)), - resource.TestCheckResourceAttr("random_string.test", "number", "true"), - resource.TestCheckResourceAttr("random_string.test", "numeric", "true"), - resource.TestCheckResourceAttr("random_string.test", "upper", "true"), - resource.TestCheckResourceAttr("random_string.test", "lower", "true"), - resource.TestCheckResourceAttr("random_string.test", "special", "true"), - resource.TestCheckResourceAttr("random_string.test", "min_numeric", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_upper", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_lower", "0"), - resource.TestCheckResourceAttr("random_string.test", "min_special", "0"), testExtractResourceAttr("random_string.test", "result", &result2), testCheckAttributeValuesEqual(&result1, &result2), ), @@ -1835,24 +1881,3 @@ func TestAccResourceString_NumericNumberFalse(t *testing.T) { }, }) } - -func testCheckLen(expectedLen int) func(input string) error { - return func(input string) error { - if len(input) != expectedLen { - return fmt.Errorf("expected length %d, actual length %d", expectedLen, len(input)) - } - - return nil - } -} - -//nolint:unparam -func testCheckMinLen(minLen int) func(input string) error { - return func(input string) error { - if len(input) < minLen { - return fmt.Errorf("minimum length %d, actual length %d", minLen, len(input)) - } - - return nil - } -} diff --git a/internal/provider/resource_uuid_test.go b/internal/provider/resource_uuid_test.go index 6e492854..c05503a1 100644 --- a/internal/provider/resource_uuid_test.go +++ b/internal/provider/resource_uuid_test.go @@ -7,7 +7,11 @@ import ( "regexp" "testing" + "github.com/hashicorp/terraform-plugin-testing/compare" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" ) func TestAccResourceUUID(t *testing.T) { @@ -17,9 +21,9 @@ func TestAccResourceUUID(t *testing.T) { { Config: `resource "random_uuid" "basic" { }`, - Check: resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr("random_uuid.basic", "result", regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_uuid.basic", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`))), + }, }, { ResourceName: "random_uuid.basic", @@ -52,7 +56,8 @@ func TestAccResourceUUID_ImportWithoutKeepersProducesNoPlannedChanges(t *testing } func TestAccResourceUUID_Keepers_Keep_EmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -61,28 +66,28 @@ func TestAccResourceUUID_Keepers_Keep_EmptyMap(t *testing.T) { Config: `resource "random_uuid" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -91,10 +96,10 @@ func TestAccResourceUUID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { Config: `resource "random_uuid" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -103,18 +108,18 @@ func TestAccResourceUUID_Keepers_Keep_EmptyMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_NullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -122,27 +127,27 @@ func TestAccResourceUUID_Keepers_Keep_NullMap(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -150,10 +155,10 @@ func TestAccResourceUUID_Keepers_Keep_NullMapToNullValue(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -162,18 +167,18 @@ func TestAccResourceUUID_Keepers_Keep_NullMapToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_NullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -184,10 +189,10 @@ func TestAccResourceUUID_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -196,18 +201,18 @@ func TestAccResourceUUID_Keepers_Keep_NullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_NullValues(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -219,10 +224,10 @@ func TestAccResourceUUID_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -232,18 +237,18 @@ func TestAccResourceUUID_Keepers_Keep_NullValues(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_Value(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -254,10 +259,10 @@ func TestAccResourceUUID_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -266,18 +271,18 @@ func TestAccResourceUUID_Keepers_Keep_Value(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Keep_Values(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -289,10 +294,10 @@ func TestAccResourceUUID_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -302,18 +307,18 @@ func TestAccResourceUUID_Keepers_Keep_Values(t *testing.T) { "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_EmptyMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -322,10 +327,10 @@ func TestAccResourceUUID_Keepers_Replace_EmptyMapToValue(t *testing.T) { Config: `resource "random_uuid" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -334,18 +339,18 @@ func TestAccResourceUUID_Keepers_Replace_EmptyMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -353,10 +358,10 @@ func TestAccResourceUUID_Keepers_Replace_NullMapToValue(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -365,18 +370,18 @@ func TestAccResourceUUID_Keepers_Replace_NullMapToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_NullValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -387,10 +392,10 @@ func TestAccResourceUUID_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -399,18 +404,18 @@ func TestAccResourceUUID_Keepers_Replace_NullValueToValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_ValueToEmptyMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -421,28 +426,28 @@ func TestAccResourceUUID_Keepers_Replace_ValueToEmptyMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { keepers = {} }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(0)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_ValueToNullMap(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -453,27 +458,27 @@ func TestAccResourceUUID_Keepers_Replace_ValueToNullMap(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "test" { }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_ValueToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -484,10 +489,10 @@ func TestAccResourceUUID_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -496,18 +501,18 @@ func TestAccResourceUUID_Keepers_Replace_ValueToNullValue(t *testing.T) { "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_Replace_ValueToNewValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -518,10 +523,10 @@ func TestAccResourceUUID_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -530,18 +535,18 @@ func TestAccResourceUUID_Keepers_Replace_ValueToNewValue(t *testing.T) { "key" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -552,10 +557,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToNullValue(t *testin "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -564,18 +569,18 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToNullValue(t *testin "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -586,10 +591,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) "key" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -598,18 +603,18 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToValue(t *testing.T) "key" = "123" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -621,10 +626,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -634,18 +639,18 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleNullValue(t "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -657,10 +662,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *te "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "0"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.Null()), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -670,18 +675,18 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapToMultipleValue(t *te "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { - var id1, id2 string + // The id attribute values should be the same between test steps + assertIdSame := statecheck.CompareValue(compare.ValuesSame()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -693,10 +698,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -706,18 +711,18 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValue(t *testing.T) { "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesEqual(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdSame.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) } func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValueToValue(t *testing.T) { - var id1, id2 string + // The id attribute values should differ between test steps + assertIdDiffer := statecheck.CompareValue(compare.ValuesDiffer()) resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{ @@ -729,10 +734,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValueToValue(t *testi "key2" = null } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id1), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "1"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(1)), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -742,11 +747,10 @@ func TestAccResourceUUID_Keepers_FrameworkMigration_NullMapValueToValue(t *testi "key2" = "456" } }`, - Check: resource.ComposeTestCheckFunc( - testExtractResourceAttr("random_uuid.test", "id", &id2), - testCheckAttributeValuesDiffer(&id1, &id2), - resource.TestCheckResourceAttr("random_uuid.test", "keepers.%", "2"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + assertIdDiffer.AddStateValue("random_uuid.test", tfjsonpath.New("id")), + statecheck.ExpectKnownValue("random_uuid.test", tfjsonpath.New("keepers"), knownvalue.MapSizeExact(2)), + }, }, }, }) @@ -759,9 +763,9 @@ func TestAccResourceUUID_UpgradeFromVersion3_3_2(t *testing.T) { ExternalProviders: providerVersion332(), Config: `resource "random_uuid" "basic" { }`, - Check: resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr("random_uuid.basic", "result", regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_uuid.basic", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`))), + }, }, { ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -773,9 +777,9 @@ func TestAccResourceUUID_UpgradeFromVersion3_3_2(t *testing.T) { ProtoV5ProviderFactories: protoV5ProviderFactories(), Config: `resource "random_uuid" "basic" { }`, - Check: resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr("random_uuid.basic", "result", regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`)), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("random_uuid.basic", tfjsonpath.New("result"), knownvalue.StringRegexp(regexp.MustCompile(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`))), + }, }, }, }) diff --git a/internal/randomtest/bcrypt_hash_match.go b/internal/randomtest/bcrypt_hash_match.go new file mode 100644 index 00000000..57e48bb7 --- /dev/null +++ b/internal/randomtest/bcrypt_hash_match.go @@ -0,0 +1,40 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package randomtest + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-testing/compare" + "golang.org/x/crypto/bcrypt" +) + +var _ compare.ValueComparer = bcryptHashMatch{} + +type bcryptHashMatch struct{} + +// CompareValues determines whether the first value is a valid bcrypt hash of the second value. +func (v bcryptHashMatch) CompareValues(values ...any) error { + if len(values) != 2 { + return fmt.Errorf("expected to receive two values to compare, but got: %d", len(values)) + } + + hash, ok := values[0].(string) + if !ok { + return fmt.Errorf("expected bcrypt hash to be of type string, but got: %T", values[0]) + } + + plainTextVal, ok := values[1].(string) + if !ok { + return fmt.Errorf("expected plain text value to be of type string, but got: %T", values[1]) + } + + return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plainTextVal)) +} + +// BcryptHashMatch returns a ValueComparer for asserting that the first value in the sequence is a matching +// bcrypt hash of the second value. +func BcryptHashMatch() bcryptHashMatch { + return bcryptHashMatch{} +} diff --git a/internal/randomtest/bcrypt_hash_mismatch.go b/internal/randomtest/bcrypt_hash_mismatch.go new file mode 100644 index 00000000..08d49b0d --- /dev/null +++ b/internal/randomtest/bcrypt_hash_mismatch.go @@ -0,0 +1,46 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package randomtest + +import ( + "errors" + "fmt" + + "github.com/hashicorp/terraform-plugin-testing/compare" + "golang.org/x/crypto/bcrypt" +) + +var _ compare.ValueComparer = bcryptHashMismatch{} + +type bcryptHashMismatch struct{} + +// CompareValues determines whether the first value is a valid bcrypt hash of the second value. +func (v bcryptHashMismatch) CompareValues(values ...any) error { + if len(values) != 2 { + return fmt.Errorf("expected to receive two values to compare, but got: %d", len(values)) + } + + hash, ok := values[0].(string) + if !ok { + return fmt.Errorf("expected bcrypt hash to be of type string, but got: %T", values[0]) + } + + plainTextVal, ok := values[1].(string) + if !ok { + return fmt.Errorf("expected plain text value to be of type string, but got: %T", values[1]) + } + + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(plainTextVal)) + if !errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { + return fmt.Errorf("unexpected error: %s", err) + } + + return nil +} + +// BcryptHashMismatch returns a ValueComparer for asserting that the first value in the sequence is not a matching +// bcrypt hash of the second value. If there is an error parsing the hash, this compare will fail. +func BcryptHashMismatch() bcryptHashMismatch { + return bcryptHashMismatch{} +} diff --git a/internal/randomtest/expect_no_attribute.go b/internal/randomtest/expect_no_attribute.go new file mode 100644 index 00000000..65a8d744 --- /dev/null +++ b/internal/randomtest/expect_no_attribute.go @@ -0,0 +1,74 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package randomtest + +import ( + "context" + "fmt" + + tfjson "github.com/hashicorp/terraform-json" + + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" +) + +var _ statecheck.StateCheck = expectNoAttribute{} + +type expectNoAttribute struct { + resourceAddress string + attributePath tfjsonpath.Path +} + +// CheckState implements the state check logic. +func (e expectNoAttribute) CheckState(ctx context.Context, req statecheck.CheckStateRequest, resp *statecheck.CheckStateResponse) { + var resource *tfjson.StateResource + + if req.State == nil { + resp.Error = fmt.Errorf("state is nil") + + return + } + + if req.State.Values == nil { + resp.Error = fmt.Errorf("state does not contain any state values") + + return + } + + if req.State.Values.RootModule == nil { + resp.Error = fmt.Errorf("state does not contain a root module") + + return + } + + for _, r := range req.State.Values.RootModule.Resources { + if e.resourceAddress == r.Address { + resource = r + + break + } + } + + if resource == nil { + resp.Error = fmt.Errorf("%s - Resource not found in state", e.resourceAddress) + + return + } + + _, err := tfjsonpath.Traverse(resource.AttributeValues, e.attributePath) + + // Attribute doesn't exist in the resource, which is the success scenario for this state check. + if err != nil { + return + } + + resp.Error = fmt.Errorf("%s - Attribute %q was found in resource state, but was expected to not exist", e.resourceAddress, e.attributePath.String()) +} + +// ExpectNoAttribute returns a state check that asserts that the specified attribute at the given resource does not exist. +func ExpectNoAttribute(resourceAddress string, attributePath tfjsonpath.Path) statecheck.StateCheck { + return expectNoAttribute{ + resourceAddress: resourceAddress, + attributePath: attributePath, + } +} diff --git a/internal/randomtest/string_length_exact.go b/internal/randomtest/string_length_exact.go new file mode 100644 index 00000000..f9070671 --- /dev/null +++ b/internal/randomtest/string_length_exact.go @@ -0,0 +1,46 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package randomtest + +import ( + "fmt" + "strconv" + + "github.com/hashicorp/terraform-plugin-testing/knownvalue" +) + +var _ knownvalue.Check = stringLengthExact{} + +type stringLengthExact struct { + length int +} + +// CheckValue determines whether the passed value is of type string, and +// contains a matching length of bytes. +func (v stringLengthExact) CheckValue(other any) error { + otherVal, ok := other.(string) + + if !ok { + return fmt.Errorf("expected string value for StringLengthExact check, got: %T", other) + } + + if len(otherVal) != v.length { + return fmt.Errorf("expected string of length %d for StringLengthExact check, got: %d (value = %s)", v.length, len(otherVal), otherVal) + } + + return nil +} + +// String returns the string representation of the value. +func (v stringLengthExact) String() string { + return strconv.FormatInt(int64(v.length), 10) +} + +// StringLengthExact returns a Check for asserting the exact length of the +// value passed to the CheckValue method. +func StringLengthExact(length int) stringLengthExact { + return stringLengthExact{ + length: length, + } +} diff --git a/internal/randomtest/string_length_min.go b/internal/randomtest/string_length_min.go new file mode 100644 index 00000000..af9a1a9c --- /dev/null +++ b/internal/randomtest/string_length_min.go @@ -0,0 +1,46 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package randomtest + +import ( + "fmt" + "strconv" + + "github.com/hashicorp/terraform-plugin-testing/knownvalue" +) + +var _ knownvalue.Check = stringLengthMin{} + +type stringLengthMin struct { + minLength int +} + +// CheckValue determines whether the passed value is of type string, and +// contains a matching length of bytes. +func (v stringLengthMin) CheckValue(other any) error { + otherVal, ok := other.(string) + + if !ok { + return fmt.Errorf("expected string value for StringLengthMin check, got: %T", other) + } + + if len(otherVal) < v.minLength { + return fmt.Errorf("expected string length to be at least %d for StringLengthMin check, got: %d (value = %s)", v.minLength, len(otherVal), otherVal) + } + + return nil +} + +// String returns the string representation of the value. +func (v stringLengthMin) String() string { + return strconv.FormatInt(int64(v.minLength), 10) +} + +// StringLengthMin returns a Check for asserting the minimum length of the +// value passed to the CheckValue method. +func StringLengthMin(minLength int) stringLengthMin { + return stringLengthMin{ + minLength: minLength, + } +}