@@ -24,6 +24,7 @@ import (
2424 "github.com/hashicorp/terraform/internal/states/remote"
2525 "github.com/hashicorp/terraform/internal/states/statefile"
2626 "github.com/hashicorp/terraform/internal/states/statemgr"
27+ "github.com/hashicorp/terraform/internal/tfdiags"
2728)
2829
2930func TestRemoteClient_impl (t * testing.T ) {
@@ -385,38 +386,54 @@ func TestRemoteClient_stateChecksum(t *testing.T) {
385386
386387 // fetching an empty state through client1 should now error out due to a
387388 // mismatched checksum.
388- if _ , diags := client1 .Get (); ! IsA [badChecksumError ](diags .Err ()) {
389- t .Fatalf ("expected state checksum error: got %s" , diags .Err ())
390- } else if bse , ok := As [badChecksumError ](diags .Err ()); ok && len (bse .digest ) != 0 {
391- t .Fatalf ("expected empty checksum, got %x" , bse .digest )
392- }
389+ if _ , diags := client1 .Get (); diags .HasErrors () {
390+ var comparableErr error
391+ if errValue , isDiag := diags .Err ().(tfdiags.DiagnosticsAsError ); isDiag {
392+ // To use `As` below we need to extract the error that's wrapped
393+ // in a diagnostic.
394+ comparableErr = errValue .WrappedErrors ()[0 ]
395+ }
396+ if ! IsA [badChecksumError ](comparableErr ) {
397+ t .Fatalf ("expected state checksum error: got %s" , diags .Err ())
398+ } else if bse , ok := As [badChecksumError ](comparableErr ); ok && len (bse .digest ) != 0 {
399+ t .Fatalf ("expected empty checksum, got %x" , bse .digest )
400+ }
393401
394- // put the old state in place of the new, without updating the checksum
395- if diags := client2 .Put (oldState .Bytes ()); diags .HasErrors () {
396- t .Fatal (diags .Err ())
397- }
402+ // put the old state in place of the new, without updating the checksum
403+ if diags := client2 .Put (oldState .Bytes ()); diags .HasErrors () {
404+ t .Fatal (diags .Err ())
405+ }
398406
399- // fetching the wrong state through client1 should now error out due to a
400- // mismatched checksum.
401- if _ , diags := client1 .Get (); ! IsA [badChecksumError ](diags .Err ()) {
402- t .Fatalf ("expected state checksum error: got %s" , diags .Err ())
403- }
407+ // fetching the wrong state through client1 should now error out due to a
408+ // mismatched checksum.
409+ if _ , diags := client1 .Get (); diags .HasErrors () {
410+ var comparableErr error
411+ if errValue , isDiag := diags .Err ().(tfdiags.DiagnosticsAsError ); isDiag {
412+ // To use `As` below we need to extract the error that's wrapped
413+ // in a diagnostic.
414+ comparableErr = errValue .WrappedErrors ()[0 ]
415+ }
416+ if ! IsA [badChecksumError ](comparableErr ) {
417+ t .Fatalf ("expected state checksum error: got %s" , diags .Err ())
418+ }
419+ }
404420
405- // update the state with the correct one after we Get again
406- testChecksumHook = func () {
407- if diags := client2 .Put (newState .Bytes ()); diags .HasErrors () {
408- t .Fatal (diags .Err ())
421+ // update the state with the correct one after we Get again
422+ testChecksumHook = func () {
423+ if diags := client2 .Put (newState .Bytes ()); diags .HasErrors () {
424+ t .Fatal (diags .Err ())
425+ }
426+ testChecksumHook = nil
409427 }
410- testChecksumHook = nil
411- }
412428
413- consistencyRetryTimeout = origTimeout
429+ consistencyRetryTimeout = origTimeout
414430
415- // this final Get will fail to fail the checksum verification, the above
416- // callback will update the state with the correct version, and Get should
417- // retry automatically.
418- if _ , diags := client1 .Get (); diags .HasErrors () {
419- t .Fatal (diags .Err ())
431+ // this final Get will fail to fail the checksum verification, the above
432+ // callback will update the state with the correct version, and Get should
433+ // retry automatically.
434+ if _ , diags := client1 .Get (); diags .HasErrors () {
435+ t .Fatal (diags .Err ())
436+ }
420437 }
421438}
422439
0 commit comments