Skip to content

Commit fccd0d4

Browse files
committed
Replace test for reconfiguring a state_store when the configuration has changed
1 parent 1703aea commit fccd0d4

File tree

2 files changed

+94
-54
lines changed

2 files changed

+94
-54
lines changed

internal/command/init_test.go

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
},

internal/command/meta_backend_test.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,59 +2230,6 @@ func TestMetaBackend_configuredStateStoreUnset(t *testing.T) {
22302230
}
22312231
}
22322232

2233-
// Reconfiguring with an already configured state store.
2234-
// This should ignore the existing state_store config, and configure the new
2235-
// state store is if this is the first time.
2236-
//
2237-
// TODO(SarahFrench/radeksimko): currently this test only confirms that we're hitting the switch
2238-
// case for this scenario, and will need to be updated when that init feature is implemented.
2239-
func TestMetaBackend_reconfigureStateStoreChange(t *testing.T) {
2240-
td := t.TempDir()
2241-
testCopyDir(t, testFixturePath("state-store-reconfigure"), td)
2242-
t.Chdir(td)
2243-
2244-
// Setup the meta
2245-
m := testMetaBackend(t, nil)
2246-
m.AllowExperimentalFeatures = true
2247-
2248-
// this should not ask for input
2249-
m.input = false
2250-
2251-
// cli flag -reconfigure
2252-
m.reconfigure = true
2253-
2254-
// Get the state store's config
2255-
mod, loadDiags := m.loadSingleModule(td)
2256-
if loadDiags.HasErrors() {
2257-
t.Fatalf("unexpected error when loading test config: %s", loadDiags.Err())
2258-
}
2259-
2260-
// Get mock provider factory to be used during init
2261-
//
2262-
// This imagines a provider called foo that contains
2263-
// a pluggable state store implementation called bar.
2264-
mock := testStateStoreMock(t)
2265-
factory := func() (providers.Interface, error) {
2266-
return mock, nil
2267-
}
2268-
2269-
// Get the operations backend
2270-
_, beDiags := m.Backend(&BackendOpts{
2271-
Init: true,
2272-
StateStoreConfig: mod.StateStore,
2273-
ProviderFactory: factory,
2274-
})
2275-
2276-
if !beDiags.HasErrors() {
2277-
t.Fatal("expected an error to be returned during partial implementation of PSS")
2278-
}
2279-
wantErr := "Configuring a state store for the first time is not implemented yet"
2280-
if !strings.Contains(beDiags.Err().Error(), wantErr) {
2281-
t.Fatalf("expected the returned error to contain %q, but got: %s", wantErr, beDiags.Err())
2282-
}
2283-
2284-
}
2285-
22862233
// Changing a configured state store
22872234
//
22882235
// TODO(SarahFrench/radeksimko): currently this test only confirms that we're hitting the switch

0 commit comments

Comments
 (0)