@@ -3367,6 +3367,99 @@ func TestInit_stateStore_newWorkingDir(t *testing.T) {
33673367 // > "during a non-init command, the command ends in with an error telling the user to run an init command"
33683368}
33693369
3370+ func TestInit_stateStore_configChanges (t * testing.T ) {
3371+ t .Run ("the -reconfigure flag makes Terraform ignore the backend state file during initialization" , func (t * testing.T ) {
3372+ // Create a temporary working directory with state store configuration
3373+ // that doesn't match the backend state file
3374+ td := t .TempDir ()
3375+ testCopyDir (t , testFixturePath ("state-store-reconfigure" ), td )
3376+ t .Chdir (td )
3377+
3378+ mockProvider := mockPluggableStateStorageProvider ()
3379+ mockProviderAddress := addrs .NewDefaultProvider ("test" )
3380+ providerSource , close := newMockProviderSource (t , map [string ][]string {
3381+ "hashicorp/test" : {"1.0.0" },
3382+ })
3383+ defer close ()
3384+
3385+ ui := new (cli.MockUi )
3386+ view , done := testView (t )
3387+ meta := Meta {
3388+ Ui : ui ,
3389+ View : view ,
3390+ AllowExperimentalFeatures : true ,
3391+ testingOverrides : & testingOverrides {
3392+ Providers : map [addrs.Provider ]providers.Factory {
3393+ mockProviderAddress : providers .FactoryFixed (mockProvider ),
3394+ },
3395+ },
3396+ ProviderSource : providerSource ,
3397+ }
3398+ c := & InitCommand {
3399+ Meta : meta ,
3400+ }
3401+
3402+ args := []string {
3403+ "-enable-pluggable-state-storage-experiment=true" ,
3404+ "-reconfigure" ,
3405+ }
3406+ code := c .Run (args )
3407+ testOutput := done (t )
3408+ if code != 0 {
3409+ t .Fatalf ("expected code 0 exit code, got %d, output: \n %s" , code , testOutput .All ())
3410+ }
3411+
3412+ // Check output
3413+ output := testOutput .All ()
3414+ expectedOutputs := []string {
3415+ "Initializing the state store..." ,
3416+ "Terraform has been successfully initialized!" ,
3417+ }
3418+ for _ , expected := range expectedOutputs {
3419+ if ! strings .Contains (output , expected ) {
3420+ t .Fatalf ("expected output to include %q, but got':\n %s" , expected , output )
3421+ }
3422+ }
3423+
3424+ // Assert the default workspace was created
3425+ if _ , exists := mockProvider .MockStates [backend .DefaultStateName ]; ! exists {
3426+ t .Fatal ("expected the default workspace to be created during init, but it is missing" )
3427+ }
3428+
3429+ // Assert contents of the backend state file
3430+ statePath := filepath .Join (meta .DataDir (), DefaultStateFilename )
3431+ sMgr := & clistate.LocalState {Path : statePath }
3432+ if err := sMgr .RefreshState (); err != nil {
3433+ t .Fatal ("Failed to load state:" , err )
3434+ }
3435+ s := sMgr .State ()
3436+ if s == nil {
3437+ t .Fatal ("expected backend state file to be created, but there isn't one" )
3438+ }
3439+ v1_0_0 , _ := version .NewVersion ("1.0.0" )
3440+ expectedState := & workdir.StateStoreConfigState {
3441+ Type : "test_store" ,
3442+ ConfigRaw : []byte ("{\n \" value\" : \" changed-value\" \n }" ),
3443+ Hash : uint64 (1417640992 ), // Hash affected by config
3444+ Provider : & workdir.ProviderConfigState {
3445+ Version : v1_0_0 ,
3446+ Source : & tfaddr.Provider {
3447+ Hostname : tfaddr .DefaultProviderRegistryHost ,
3448+ Namespace : "hashicorp" ,
3449+ Type : "test" ,
3450+ },
3451+ ConfigRaw : []byte ("{\n \" region\" : null\n }" ),
3452+ Hash : uint64 (3976463117 ), // Hash of empty config
3453+ },
3454+ }
3455+ if diff := cmp .Diff (s .StateStore , expectedState ); diff != "" {
3456+ t .Fatalf ("unexpected diff in backend state file's description of state store:\n %s" , diff )
3457+ }
3458+ })
3459+
3460+ // TODO - add more test cases, e.g. these context changes in the context of updating providers
3461+ }
3462+
33703463// newMockProviderSource is a helper to succinctly construct a mock provider
33713464// source that contains a set of packages matching the given provider versions
33723465// that are available for installation (from temporary local files).
@@ -3531,7 +3624,7 @@ func mockPluggableStateStorageProvider() *testing_provider.MockProvider {
35313624 pssName : {
35323625 Body : & configschema.Block {
35333626 Attributes : map [string ]* configschema.Attribute {
3534- "bar " : {
3627+ "value " : {
35353628 Type : cty .String ,
35363629 Required : true ,
35373630 },
0 commit comments