github.com/kevinklinger/open_terraform@v1.3.6/noninternal/cloud/migration_test.go (about)

     1  package cloud
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kevinklinger/open_terraform/noninternal/configs"
     7  	legacy "github.com/kevinklinger/open_terraform/noninternal/legacy/terraform"
     8  )
     9  
    10  func TestDetectConfigChangeType(t *testing.T) {
    11  	tests := map[string]struct {
    12  		stateType            string
    13  		configType           string
    14  		localStates          bool
    15  		want                 ConfigChangeMode
    16  		wantInvolvesCloud    bool
    17  		wantIsCloudMigration bool
    18  	}{
    19  		"init cloud": {
    20  			``, `cloud`, false,
    21  			ConfigChangeInPlace,
    22  			true, false,
    23  		},
    24  		"reinit cloud": {
    25  			`cloud`, `cloud`, false,
    26  			ConfigChangeInPlace,
    27  			true, false,
    28  		},
    29  		"migrate default local to cloud with existing local state": {
    30  			``, `cloud`, true,
    31  			ConfigMigrationIn,
    32  			true, true,
    33  		},
    34  		"migrate local to cloud": {
    35  			`local`, `cloud`, false,
    36  			ConfigMigrationIn,
    37  			true, true,
    38  		},
    39  		"migrate remote to cloud": {
    40  			`local`, `cloud`, false,
    41  			ConfigMigrationIn,
    42  			true, true,
    43  		},
    44  		"migrate cloud to local": {
    45  			`cloud`, `local`, false,
    46  			ConfigMigrationOut,
    47  			true, true,
    48  		},
    49  		"migrate cloud to remote": {
    50  			`cloud`, `remote`, false,
    51  			ConfigMigrationOut,
    52  			true, true,
    53  		},
    54  		"migrate cloud to default local": {
    55  			`cloud`, ``, false,
    56  			ConfigMigrationOut,
    57  			true, true,
    58  		},
    59  
    60  		// Various other cases can potentially be valid (decided by the
    61  		// Terraform CLI layer) but are irrelevant for Cloud mode purposes.
    62  		"init default local": {
    63  			``, ``, false,
    64  			ConfigChangeIrrelevant,
    65  			false, false,
    66  		},
    67  		"init default local with existing local state": {
    68  			``, ``, true,
    69  			ConfigChangeIrrelevant,
    70  			false, false,
    71  		},
    72  		"init remote backend": {
    73  			``, `remote`, false,
    74  			ConfigChangeIrrelevant,
    75  			false, false,
    76  		},
    77  		"init remote backend with existing local state": {
    78  			``, `remote`, true,
    79  			ConfigChangeIrrelevant,
    80  			false, false,
    81  		},
    82  		"reinit remote backend": {
    83  			`remote`, `remote`, false,
    84  			ConfigChangeIrrelevant,
    85  			false, false,
    86  		},
    87  		"migrate local to remote backend": {
    88  			`local`, `remote`, false,
    89  			ConfigChangeIrrelevant,
    90  			false, false,
    91  		},
    92  		"migrate remote to default local": {
    93  			`remote`, ``, false,
    94  			ConfigChangeIrrelevant,
    95  			false, false,
    96  		},
    97  	}
    98  
    99  	for name, test := range tests {
   100  		t.Run(name, func(t *testing.T) {
   101  			var state *legacy.BackendState
   102  			var config *configs.Backend
   103  			if test.stateType != "" {
   104  				state = &legacy.BackendState{
   105  					Type: test.stateType,
   106  					// everything else is irrelevant for our purposes here
   107  				}
   108  			}
   109  			if test.configType != "" {
   110  				config = &configs.Backend{
   111  					Type: test.configType,
   112  					// everything else is irrelevant for our purposes here
   113  				}
   114  			}
   115  			got := DetectConfigChangeType(state, config, test.localStates)
   116  
   117  			if got != test.want {
   118  				t.Errorf(
   119  					"wrong result\nstate type:   %s\nconfig type:  %s\nlocal states: %t\n\ngot:  %s\nwant: %s",
   120  					test.stateType, test.configType, test.localStates,
   121  					got, test.want,
   122  				)
   123  			}
   124  			if got, want := got.InvolvesCloud(), test.wantInvolvesCloud; got != want {
   125  				t.Errorf(
   126  					"wrong InvolvesCloud result\ngot:  %t\nwant: %t",
   127  					got, want,
   128  				)
   129  			}
   130  			if got, want := got.IsCloudMigration(), test.wantIsCloudMigration; got != want {
   131  				t.Errorf(
   132  					"wrong IsCloudMigration result\ngot:  %t\nwant: %t",
   133  					got, want,
   134  				)
   135  			}
   136  		})
   137  	}
   138  }