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

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