github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/states/statemgr/migrate_test.go (about)

     1  package statemgr
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  
     8  	"github.com/hashicorp/terraform/addrs"
     9  	"github.com/hashicorp/terraform/states"
    10  	"github.com/hashicorp/terraform/states/statefile"
    11  )
    12  
    13  func TestCheckValidImport(t *testing.T) {
    14  	barState := states.BuildState(func(s *states.SyncState) {
    15  		s.SetOutputValue(
    16  			addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
    17  			cty.StringVal("bar"), false,
    18  		)
    19  	})
    20  	notBarState := states.BuildState(func(s *states.SyncState) {
    21  		s.SetOutputValue(
    22  			addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
    23  			cty.StringVal("not bar"), false,
    24  		)
    25  	})
    26  	emptyState := states.NewState()
    27  
    28  	tests := map[string]struct {
    29  		New      *statefile.File
    30  		Existing *statefile.File
    31  		WantErr  string
    32  	}{
    33  		"exact match": {
    34  			New:      statefile.New(barState, "lineage", 1),
    35  			Existing: statefile.New(barState, "lineage", 1),
    36  			WantErr:  ``,
    37  		},
    38  		"overwrite unrelated empty state": {
    39  			New:      statefile.New(barState, "lineage1", 1),
    40  			Existing: statefile.New(emptyState, "lineage2", 1),
    41  			WantErr:  ``,
    42  		},
    43  		"different state with same serial": {
    44  			New:      statefile.New(barState, "lineage", 1),
    45  			Existing: statefile.New(notBarState, "lineage", 1),
    46  			WantErr:  `cannot overwrite existing state with serial 1 with a different state that has the same serial`,
    47  		},
    48  		"different state with newer serial": {
    49  			New:      statefile.New(barState, "lineage", 2),
    50  			Existing: statefile.New(notBarState, "lineage", 1),
    51  			WantErr:  ``,
    52  		},
    53  		"different state with older serial": {
    54  			New:      statefile.New(barState, "lineage", 1),
    55  			Existing: statefile.New(notBarState, "lineage", 2),
    56  			WantErr:  `cannot import state with serial 1 over newer state with serial 2`,
    57  		},
    58  		"different lineage with same serial": {
    59  			New:      statefile.New(barState, "lineage1", 2),
    60  			Existing: statefile.New(notBarState, "lineage2", 2),
    61  			WantErr:  `cannot import state with lineage "lineage1" over unrelated state with lineage "lineage2"`,
    62  		},
    63  		"different lineage with different serial": {
    64  			New:      statefile.New(barState, "lineage1", 3),
    65  			Existing: statefile.New(notBarState, "lineage2", 2),
    66  			WantErr:  `cannot import state with lineage "lineage1" over unrelated state with lineage "lineage2"`,
    67  		},
    68  		"new state is legacy": {
    69  			New:      statefile.New(barState, "", 2),
    70  			Existing: statefile.New(notBarState, "lineage", 2),
    71  			WantErr:  ``,
    72  		},
    73  		"old state is legacy": {
    74  			New:      statefile.New(barState, "lineage", 2),
    75  			Existing: statefile.New(notBarState, "", 2),
    76  			WantErr:  ``,
    77  		},
    78  		"both states are legacy": {
    79  			New:      statefile.New(barState, "", 2),
    80  			Existing: statefile.New(notBarState, "", 2),
    81  			WantErr:  ``,
    82  		},
    83  	}
    84  
    85  	for name, test := range tests {
    86  		t.Run(name, func(t *testing.T) {
    87  			gotErr := CheckValidImport(test.New, test.Existing)
    88  
    89  			if test.WantErr == "" {
    90  				if gotErr != nil {
    91  					t.Errorf("unexpected error: %s", gotErr)
    92  				}
    93  			} else {
    94  				if gotErr == nil {
    95  					t.Errorf("succeeded, but want error: %s", test.WantErr)
    96  				} else if got, want := gotErr.Error(), test.WantErr; got != want {
    97  					t.Errorf("wrong error\ngot:  %s\nwant: %s", got, want)
    98  				}
    99  			}
   100  		})
   101  	}
   102  }