github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/resource_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package states
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestResourceInstanceDeposeCurrentObject(t *testing.T) {
    11  	obj := &ResourceInstanceObjectSrc{
    12  		// Empty for the sake of this test, because we're just going to
    13  		// compare by pointer below anyway.
    14  	}
    15  
    16  	is := NewResourceInstance()
    17  	is.Current = obj
    18  	var dk DeposedKey
    19  
    20  	t.Run("first depose", func(t *testing.T) {
    21  		dk = is.deposeCurrentObject(NotDeposed) // dk is randomly-generated but should be eight characters long
    22  		t.Logf("deposedKey is %q", dk)
    23  
    24  		if got := is.Current; got != nil {
    25  			t.Errorf("current is %#v; want nil", got)
    26  		}
    27  		if got, want := is.Deposed[dk], obj; got != want {
    28  			t.Errorf("deposed object pointer is %#v; want %#v", got, want)
    29  		}
    30  		if got, want := len(is.Deposed), 1; got != want {
    31  			t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
    32  		}
    33  		if got, want := len(dk), 8; got != want {
    34  			t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
    35  		}
    36  	})
    37  
    38  	t.Run("second depose", func(t *testing.T) {
    39  		notDK := is.deposeCurrentObject(NotDeposed)
    40  		if notDK != NotDeposed {
    41  			t.Errorf("got deposedKey %q; want NotDeposed", notDK)
    42  		}
    43  
    44  		// Make sure we really did abort early, and haven't corrupted the
    45  		// state somehow.
    46  		if got := is.Current; got != nil {
    47  			t.Errorf("current is %#v; want nil", got)
    48  		}
    49  		if got, want := is.Deposed[dk], obj; got != want {
    50  			t.Errorf("deposed object pointer is %#v; want %#v", got, want)
    51  		}
    52  		if got, want := len(is.Deposed), 1; got != want {
    53  			t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
    54  		}
    55  		if got, want := len(dk), 8; got != want {
    56  			t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
    57  		}
    58  	})
    59  }