From eb4d7cc2632a3b79232ca68065a8157d5838f459 Mon Sep 17 00:00:00 2001 From: Austin Valle Date: Wed, 7 Aug 2024 12:12:08 -0400 Subject: [PATCH 1/5] testing: Replace state extract with new value comparers --- go.mod | 17 ++-- go.sum | 34 ++++---- internal/provider/resource_time_sleep_test.go | 47 +++-------- .../provider/resource_time_static_test.go | 16 ++-- internal/timetesting/extract_state_check.go | 84 ------------------- 5 files changed, 45 insertions(+), 153 deletions(-) delete mode 100644 internal/timetesting/extract_state_check.go diff --git a/go.mod b/go.mod index 70a844e..93471e4 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/hashicorp/terraform-plugin-testing v1.9.0 + github.com/hashicorp/terraform-plugin-testing v1.9.1-0.20240807155751-bcdd810e0d34 ) require ( @@ -29,9 +29,10 @@ 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-uuid v1.0.3 // 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 @@ -51,13 +52,13 @@ 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/crypto v0.25.0 // indirect - golang.org/x/mod v0.17.0 // indirect + github.com/zclconf/go-cty v1.15.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/mod v0.19.0 // indirect golang.org/x/net v0.25.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect diff --git a/go.sum b/go.sum index ee1d30e..9f9199d 100644 --- a/go.sum +++ b/go.sum @@ -59,13 +59,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.9.1-0.20240807155751-bcdd810e0d34 h1:jUoFWQELkP8NzUirfum70Nv3EcYloP3Aub/QsyJX5eA= +github.com/hashicorp/terraform-plugin-testing v1.9.1-0.20240807155751-bcdd810e0d34/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,17 +153,17 @@ 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= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +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= @@ -171,8 +173,8 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -185,8 +187,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -194,8 +196,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/internal/provider/resource_time_sleep_test.go b/internal/provider/resource_time_sleep_test.go index 02ebb99..11a322b 100644 --- a/internal/provider/resource_time_sleep_test.go +++ b/internal/provider/resource_time_sleep_test.go @@ -13,11 +13,11 @@ import ( r "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/hashicorp/terraform-provider-time/internal/timetesting" ) // Since the acceptance testing framework can introduce uncontrollable time delays, @@ -161,10 +161,8 @@ func TestResourceTimeSleepDelete(t *testing.T) { func TestAccTimeSleep_CreateDuration(t *testing.T) { resourceName := "time_sleep.test" - // These ID comparisons can eventually be replaced by the multiple value checks once released - // in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 - captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) - captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) + // Due to the time.Sleep, the id attribute should differ between test steps + assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -175,7 +173,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("1ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - captureTimeState1, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, // This test may work in local execution but typically does not work in CI because of its reliance @@ -192,25 +190,18 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("2ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - captureTimeState2, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, }, }) - - // Ensure the id time value is different due to the sleep - if captureTimeState1.Value == captureTimeState2.Value { - t.Fatal("attribute values are the same") - } } func TestAccTimeSleep_DestroyDuration(t *testing.T) { resourceName := "time_sleep.test" - // These ID comparisons can eventually be replaced by the multiple value checks once released - // in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 - captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) - captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) + // Due to the time.Sleep, the id attribute should differ between test steps + assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -221,7 +212,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("1ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - captureTimeState1, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, // This test may work in local execution but typically does not work in CI because of its reliance @@ -238,25 +229,18 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("2ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - captureTimeState2, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, }, }) - - // Ensure the id time value is different due to the sleep - if captureTimeState1.Value == captureTimeState2.Value { - t.Fatal("attribute values are the same") - } } func TestAccTimeSleep_Triggers(t *testing.T) { resourceName := "time_sleep.test" - // These ID comparisons can eventually be replaced by the multiple value checks once released - // in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 - captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) - captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) + // Due to the time.Sleep, the id attribute should differ between test steps + assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -269,7 +253,7 @@ func TestAccTimeSleep_Triggers(t *testing.T) { statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.NotNull()), - captureTimeState1, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, // This test may work in local execution but typically does not work in CI because of its reliance @@ -289,16 +273,11 @@ func TestAccTimeSleep_Triggers(t *testing.T) { statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1updated")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.NotNull()), - captureTimeState2, + assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, }, }) - - // Ensure the id time value is different due to the sleep - if captureTimeState1.Value == captureTimeState2.Value { - t.Fatal("attribute values are the same") - } } func TestAccTimeSleep_Upgrade(t *testing.T) { diff --git a/internal/provider/resource_time_static_test.go b/internal/provider/resource_time_static_test.go index 370a3fa..11d4080 100644 --- a/internal/provider/resource_time_static_test.go +++ b/internal/provider/resource_time_static_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "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" @@ -51,10 +52,8 @@ func TestAccTimeStatic_Triggers(t *testing.T) { resourceName := "time_static.test" - // These ID comparisons can eventually be replaced by the multiple value checks once released - // in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 - captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("rfc3339")) - captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("rfc3339")) + // Due to the time.Sleep, the rfc3339 attribute should differ between test steps + assertRfc3339Updated := statecheck.CompareValue(compare.ValuesDiffer()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -66,7 +65,7 @@ func TestAccTimeStatic_Triggers(t *testing.T) { statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers"), knownvalue.MapSizeExact(1)), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rfc3339"), knownvalue.NotNull()), - captureTimeState1, + assertRfc3339Updated.AddStateValue(resourceName, tfjsonpath.New("rfc3339")), }, }, { @@ -85,16 +84,11 @@ func TestAccTimeStatic_Triggers(t *testing.T) { statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers"), knownvalue.MapSizeExact(1)), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1updated")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rfc3339"), knownvalue.NotNull()), - captureTimeState2, + assertRfc3339Updated.AddStateValue(resourceName, tfjsonpath.New("rfc3339")), }, }, }, }) - - // Ensure the rfc3339 time value is different due to the sleep - if captureTimeState1.Value == captureTimeState2.Value { - t.Fatal("attribute values are the same") - } } func TestAccTimeStatic_Rfc3339(t *testing.T) { diff --git a/internal/timetesting/extract_state_check.go b/internal/timetesting/extract_state_check.go deleted file mode 100644 index a38227c..0000000 --- a/internal/timetesting/extract_state_check.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package timetesting - -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 = &ExtractState{} - -type ExtractState struct { - resourceAddress string - attributePath tfjsonpath.Path - - // Value contains the string state value after the check has run. - Value *string -} - -func (e *ExtractState) 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 - } - - result, err := tfjsonpath.Traverse(resource.AttributeValues, e.attributePath) - - if err != nil { - resp.Error = err - - return - } - - strValue, ok := result.(string) - if !ok { - resp.Error = fmt.Errorf("error checking value for attribute at path: %s.%s, expected a string value, receieved %T", e.resourceAddress, e.attributePath.String(), result) - - return - } - - e.Value = &strValue -} - -// NewExtractState returns a state check that will extract a state value into an accessible string pointer `(*ExtractState).Value`. -func NewExtractState(resourceAddress string, attributePath tfjsonpath.Path) *ExtractState { - return &ExtractState{ - resourceAddress: resourceAddress, - attributePath: attributePath, - } -} From 76b812fd591fac1a611564690c986490dc1fd038 Mon Sep 17 00:00:00 2001 From: Austin Valle Date: Wed, 7 Aug 2024 13:17:30 -0400 Subject: [PATCH 2/5] fix tests --- internal/provider/resource_time_sleep_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/provider/resource_time_sleep_test.go b/internal/provider/resource_time_sleep_test.go index 11a322b..be7561d 100644 --- a/internal/provider/resource_time_sleep_test.go +++ b/internal/provider/resource_time_sleep_test.go @@ -162,7 +162,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { resourceName := "time_sleep.test" // Due to the time.Sleep, the id attribute should differ between test steps - assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) + assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -201,7 +201,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { resourceName := "time_sleep.test" // Due to the time.Sleep, the id attribute should differ between test steps - assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) + assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), From c3b335e34ce442ae3a5eafe965d4379f90e99b6f Mon Sep 17 00:00:00 2001 From: Austin Valle Date: Wed, 7 Aug 2024 13:18:54 -0400 Subject: [PATCH 3/5] update comment --- internal/provider/resource_time_sleep_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/provider/resource_time_sleep_test.go b/internal/provider/resource_time_sleep_test.go index be7561d..dde4ccc 100644 --- a/internal/provider/resource_time_sleep_test.go +++ b/internal/provider/resource_time_sleep_test.go @@ -161,7 +161,7 @@ func TestResourceTimeSleepDelete(t *testing.T) { func TestAccTimeSleep_CreateDuration(t *testing.T) { resourceName := "time_sleep.test" - // Due to the time.Sleep, the id attribute should differ between test steps + // The id attribute should not change between test steps assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ @@ -200,7 +200,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { func TestAccTimeSleep_DestroyDuration(t *testing.T) { resourceName := "time_sleep.test" - // Due to the time.Sleep, the id attribute should differ between test steps + // The id attribute should not change between test steps assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ From 9588e4c1e1df52defb3db47d785cda209aabb17b Mon Sep 17 00:00:00 2001 From: Austin Valle Date: Wed, 7 Aug 2024 13:23:33 -0400 Subject: [PATCH 4/5] go mod tidy and var name --- go.mod | 2 +- internal/provider/resource_time_sleep_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 93471e4..08c1fa3 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ toolchain go1.21.6 require ( github.com/google/go-cmp v0.6.0 - github.com/hashicorp/terraform-json v0.22.1 github.com/hashicorp/terraform-plugin-framework v1.11.0 github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 @@ -36,6 +35,7 @@ require ( 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-sdk/v2 v2.34.0 // indirect github.com/hashicorp/terraform-registry-address v0.2.3 // indirect github.com/hashicorp/terraform-svchost v0.1.1 // indirect diff --git a/internal/provider/resource_time_sleep_test.go b/internal/provider/resource_time_sleep_test.go index dde4ccc..2d680cf 100644 --- a/internal/provider/resource_time_sleep_test.go +++ b/internal/provider/resource_time_sleep_test.go @@ -162,7 +162,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { resourceName := "time_sleep.test" // The id attribute should not change between test steps - assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) + assertIDSame := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -173,7 +173,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("1ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), + assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, // This test may work in local execution but typically does not work in CI because of its reliance @@ -190,7 +190,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("2ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), + assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, }, @@ -201,7 +201,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { resourceName := "time_sleep.test" // The id attribute should not change between test steps - assertIDUpdated := statecheck.CompareValue(compare.ValuesSame()) + assertIDSame := statecheck.CompareValue(compare.ValuesSame()) resource.UnitTest(t, resource.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), @@ -212,7 +212,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("1ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), + assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, // This test may work in local execution but typically does not work in CI because of its reliance @@ -229,7 +229,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("2ms")), statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), - assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), + assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), }, }, }, From 6dd19b2ceb27c55372cb8ed2d4862a702b8421c2 Mon Sep 17 00:00:00 2001 From: Austin Valle Date: Thu, 8 Aug 2024 17:33:04 -0400 Subject: [PATCH 5/5] update to latest testing version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 08c1fa3..b8837ac 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/hashicorp/terraform-plugin-testing v1.9.1-0.20240807155751-bcdd810e0d34 + github.com/hashicorp/terraform-plugin-testing v1.10.0 ) require ( diff --git a/go.sum b/go.sum index 9f9199d..e9f4cd1 100644 --- a/go.sum +++ b/go.sum @@ -88,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.1-0.20240807155751-bcdd810e0d34 h1:jUoFWQELkP8NzUirfum70Nv3EcYloP3Aub/QsyJX5eA= -github.com/hashicorp/terraform-plugin-testing v1.9.1-0.20240807155751-bcdd810e0d34/go.mod h1:iWRW3+loP33WMch2P/TEyCxxct/ZEcCGMquSLSCVsrc= +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=