Skip to content

Commit 36e60e7

Browse files
committed
address PR feedback
1 parent 9003bdb commit 36e60e7

File tree

8 files changed

+111
-141
lines changed

8 files changed

+111
-141
lines changed

internal/command/e2etest/meta_backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestMetaBackend_GetStateStoreProviderFactory(t *testing.T) {
6767

6868
// Setup the meta and test GetStateStoreProviderFactory
6969
m := command.Meta{}
70-
factory, diags := m.GetStateStoreProviderFactory(config, locks)
70+
factory, diags := m.StateStoreProviderFactoryFromConfig(config, locks)
7171
if diags.HasErrors() {
7272
t.Fatalf("unexpected error : %s", err)
7373
}

internal/command/init_run_experiment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (c *InitCommand) initPssBackend(ctx context.Context, root *configs.Module,
378378
return nil, true, diags
379379
case root.StateStore != nil:
380380
// state_store config present
381-
factory, fDiags := c.Meta.GetStateStoreProviderFactory(root.StateStore, configLocks)
381+
factory, fDiags := c.Meta.StateStoreProviderFactoryFromConfig(root.StateStore, configLocks)
382382
diags = diags.Append(fDiags)
383383
if fDiags.HasErrors() {
384384
return nil, true, diags

internal/command/init_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,6 +4116,105 @@ func TestInit_stateStore_unset(t *testing.T) {
41164116
}
41174117
}
41184118

4119+
func TestInit_stateStore_unset_withoutProviderRequirements(t *testing.T) {
4120+
// Create a temporary working directory that is empty
4121+
td := t.TempDir()
4122+
testCopyDir(t, testFixturePath("init-state-store"), td)
4123+
t.Chdir(td)
4124+
4125+
mockProvider := mockPluggableStateStorageProvider()
4126+
storeName := "test_store"
4127+
otherStoreName := "test_otherstore"
4128+
// Make the provider report that it contains a 2nd storage implementation with the above name
4129+
mockProvider.GetProviderSchemaResponse.StateStores[otherStoreName] = mockProvider.GetProviderSchemaResponse.StateStores[storeName]
4130+
mockProviderAddress := addrs.NewDefaultProvider("test")
4131+
providerSource, close := newMockProviderSource(t, map[string][]string{
4132+
"hashicorp/test": {"1.2.3"}, // Matches provider version in backend state file fixture
4133+
})
4134+
defer close()
4135+
4136+
{
4137+
log.Printf("[TRACE] TestInit_stateStore_unset_withoutProviderRequirements: beginning first init")
4138+
4139+
ui := cli.NewMockUi()
4140+
view, done := testView(t)
4141+
c := &InitCommand{
4142+
Meta: Meta{
4143+
testingOverrides: &testingOverrides{
4144+
Providers: map[addrs.Provider]providers.Factory{
4145+
mockProviderAddress: providers.FactoryFixed(mockProvider),
4146+
},
4147+
},
4148+
ProviderSource: providerSource,
4149+
Ui: ui,
4150+
View: view,
4151+
AllowExperimentalFeatures: true,
4152+
},
4153+
}
4154+
4155+
// Init
4156+
args := []string{
4157+
"-enable-pluggable-state-storage-experiment=true",
4158+
}
4159+
code := c.Run(args)
4160+
testOutput := done(t)
4161+
if code != 0 {
4162+
t.Fatalf("bad: \n%s", testOutput.All())
4163+
}
4164+
log.Printf("[TRACE] TestInit_stateStore_unset_withoutProviderRequirements: first init complete")
4165+
t.Logf("First run output:\n%s", testOutput.Stdout())
4166+
t.Logf("First run errors:\n%s", testOutput.Stderr())
4167+
4168+
if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil {
4169+
t.Fatalf("err: %s", err)
4170+
}
4171+
}
4172+
{
4173+
log.Printf("[TRACE] TestInit_stateStore_unset_withoutProviderRequirements: beginning second init")
4174+
// Unset state store and provider requirements
4175+
if err := os.WriteFile("main.tf", []byte(""), 0644); err != nil {
4176+
t.Fatalf("err: %s", err)
4177+
}
4178+
if err := os.WriteFile("providers.tf", []byte(""), 0644); err != nil {
4179+
t.Fatalf("err: %s", err)
4180+
}
4181+
4182+
ui := cli.NewMockUi()
4183+
view, done := testView(t)
4184+
c := &InitCommand{
4185+
Meta: Meta{
4186+
testingOverrides: &testingOverrides{
4187+
Providers: map[addrs.Provider]providers.Factory{
4188+
mockProviderAddress: providers.FactoryFixed(mockProvider),
4189+
},
4190+
},
4191+
ProviderSource: providerSource,
4192+
Ui: ui,
4193+
View: view,
4194+
AllowExperimentalFeatures: true,
4195+
},
4196+
}
4197+
4198+
args := []string{
4199+
"-enable-pluggable-state-storage-experiment=true",
4200+
"-force-copy",
4201+
}
4202+
code := c.Run(args)
4203+
testOutput := done(t)
4204+
if code != 0 {
4205+
t.Fatalf("bad: \n%s", testOutput.All())
4206+
}
4207+
log.Printf("[TRACE] TestInit_stateStore_unset_withoutProviderRequirements: second init complete")
4208+
t.Logf("Second run output:\n%s", testOutput.Stdout())
4209+
t.Logf("Second run errors:\n%s", testOutput.Stderr())
4210+
4211+
s := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
4212+
if !s.StateStore.Empty() {
4213+
t.Fatal("should not have StateStore config")
4214+
}
4215+
}
4216+
}
4217+
41194218
// newMockProviderSource is a helper to succinctly construct a mock provider
41204219
// source that contains a set of packages matching the given provider versions
41214220
// that are available for installation (from temporary local files).

internal/command/meta_backend.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ func (m *Meta) stateStoreConfig(opts *BackendOpts) (*configs.StateStore, int, tf
568568
return nil, 0, diags
569569
}
570570

571-
pFactory, pDiags := m.GetStateStoreProviderFactory(opts.StateStoreConfig, opts.Locks)
571+
pFactory, pDiags := m.StateStoreProviderFactoryFromConfig(opts.StateStoreConfig, opts.Locks)
572572
diags = diags.Append(pDiags)
573573
if pDiags.HasErrors() {
574574
return nil, 0, diags
@@ -1999,13 +1999,7 @@ func (m *Meta) savedStateStore(sMgr *clistate.LocalState) (backend.Backend, tfdi
19991999

20002000
s := sMgr.State()
20012001

2002-
locks, lDiags := m.lockedDependencies()
2003-
diags = diags.Append(lDiags)
2004-
if lDiags.HasErrors() {
2005-
return nil, diags
2006-
}
2007-
2008-
factory, pDiags := m.StateStoreProviderFactoryFromConfigState(s.StateStore, locks)
2002+
factory, pDiags := m.StateStoreProviderFactoryFromConfigState(s.StateStore)
20092003
diags = diags.Append(pDiags)
20102004
if pDiags.HasErrors() {
20112005
return nil, diags
@@ -2300,7 +2294,7 @@ func (m *Meta) backendInitFromConfig(c *configs.Backend) (backend.Backend, cty.V
23002294
func (m *Meta) stateStoreInitFromConfig(c *configs.StateStore, locks *depsfile.Locks) (backend.Backend, cty.Value, cty.Value, tfdiags.Diagnostics) {
23012295
var diags tfdiags.Diagnostics
23022296

2303-
factory, pDiags := m.GetStateStoreProviderFactory(c, locks)
2297+
factory, pDiags := m.StateStoreProviderFactoryFromConfig(c, locks)
23042298
diags = diags.Append(pDiags)
23052299
if pDiags.HasErrors() {
23062300
return nil, cty.NilVal, cty.NilVal, diags
@@ -2535,7 +2529,7 @@ func (m *Meta) assertSupportedCloudInitOptions(mode cloud.ConfigChangeMode) tfdi
25352529
return diags
25362530
}
25372531

2538-
func (m *Meta) GetStateStoreProviderFactory(config *configs.StateStore, locks *depsfile.Locks) (providers.Factory, tfdiags.Diagnostics) {
2532+
func (m *Meta) StateStoreProviderFactoryFromConfig(config *configs.StateStore, locks *depsfile.Locks) (providers.Factory, tfdiags.Diagnostics) {
25392533
var diags tfdiags.Diagnostics
25402534

25412535
if config == nil || locks == nil {
@@ -2586,11 +2580,11 @@ func (m *Meta) GetStateStoreProviderFactory(config *configs.StateStore, locks *d
25862580
return factory, diags
25872581
}
25882582

2589-
func (m *Meta) StateStoreProviderFactoryFromConfigState(cfgState *workdir.StateStoreConfigState, locks *depsfile.Locks) (providers.Factory, tfdiags.Diagnostics) {
2583+
func (m *Meta) StateStoreProviderFactoryFromConfigState(cfgState *workdir.StateStoreConfigState) (providers.Factory, tfdiags.Diagnostics) {
25902584
var diags tfdiags.Diagnostics
25912585

2592-
if cfgState == nil || locks == nil {
2593-
panic(fmt.Sprintf("nil config or nil locks passed to GetStateStoreProviderFactory: config %#v, locks %#v", cfgState, locks))
2586+
if cfgState == nil {
2587+
panic("nil config passed to StateStoreProviderFactoryFromConfigState")
25942588
}
25952589

25962590
if cfgState.Provider == nil || cfgState.Provider.Source.IsZero() {
@@ -2599,11 +2593,10 @@ func (m *Meta) StateStoreProviderFactoryFromConfigState(cfgState *workdir.StateS
25992593
Severity: hcl.DiagError,
26002594
Summary: "Unknown provider used for state storage",
26012595
Detail: "Terraform could not find the provider used with the state_store. This is a bug in Terraform and should be reported.",
2602-
// Subject: &cfgState.TypeRange,
26032596
})
26042597
}
26052598

2606-
factories, err := m.ProviderFactoriesFromLocks(locks)
2599+
factories, err := m.ProviderFactories()
26072600
if err != nil {
26082601
// This may happen if the provider isn't present in the provider cache.
26092602
// This should be caught earlier by logic that diffs the config against the backend state file.
@@ -2615,7 +2608,6 @@ func (m *Meta) StateStoreProviderFactoryFromConfigState(cfgState *workdir.StateS
26152608
cfgState.Provider.Source,
26162609
cfgState.Type,
26172610
err),
2618-
// Subject: &cfgState.TypeRange,
26192611
})
26202612
}
26212613

@@ -2629,7 +2621,6 @@ func (m *Meta) StateStoreProviderFactoryFromConfigState(cfgState *workdir.StateS
26292621
cfgState.Provider.Source,
26302622
cfgState.Type,
26312623
),
2632-
// Subject: &cfgState.TypeRange,
26332624
})
26342625
}
26352626

internal/command/meta_backend_test.go

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,92 +2088,6 @@ func Test_determineInitReason(t *testing.T) {
20882088
}
20892089
}
20902090

2091-
// Unsetting a saved state store
2092-
func TestMetaBackend_configuredStateStoreUnset(t *testing.T) {
2093-
td := t.TempDir()
2094-
testCopyDir(t, testFixturePath("state-store-unset"), td)
2095-
t.Chdir(td)
2096-
2097-
mock := testStateStoreMock(t)
2098-
2099-
// Setup the meta
2100-
m := testMetaBackend(t, nil)
2101-
m.forceInitCopy = true // to avoid UI prompt
2102-
m.testingOverrides = metaOverridesForProvider(mock)
2103-
m.AllowExperimentalFeatures = true
2104-
2105-
// Get the state store's config
2106-
mod, loadDiags := m.loadSingleModule(td)
2107-
if loadDiags.HasErrors() {
2108-
t.Fatalf("unexpected error when loading test config: %s", loadDiags.Err())
2109-
}
2110-
2111-
providerAddr := tfaddr.MustParseProviderSource("hashicorp/test")
2112-
constraint, err := providerreqs.ParseVersionConstraints(">1.0.0")
2113-
if err != nil {
2114-
t.Fatalf("test setup failed when making constraint: %s", err)
2115-
}
2116-
locks := depsfile.NewLocks()
2117-
locks.SetProvider(
2118-
providerAddr,
2119-
versions.MustParseVersion("1.2.3"),
2120-
constraint,
2121-
[]providerreqs.Hash{""},
2122-
)
2123-
2124-
// Get the operations backend
2125-
b, beDiags := m.Backend(&BackendOpts{
2126-
Init: true,
2127-
StateStoreConfig: mod.StateStore,
2128-
Locks: locks,
2129-
})
2130-
if beDiags.HasErrors() {
2131-
t.Fatalf("unexpected error: %s", beDiags.Err())
2132-
}
2133-
2134-
// Check the state
2135-
s, sDiags := b.StateMgr(backend.DefaultStateName)
2136-
if sDiags.HasErrors() {
2137-
t.Fatalf("unexpected error: %s", sDiags.Err())
2138-
}
2139-
if err := s.RefreshState(); err != nil {
2140-
t.Fatalf("unexpected error: %s", err)
2141-
}
2142-
state := s.State()
2143-
if state != nil {
2144-
t.Fatal("state should be nil")
2145-
}
2146-
2147-
// Verify the default paths don't exist
2148-
if !isEmptyState(DefaultStateFilename) {
2149-
data, _ := os.ReadFile(DefaultStateFilename)
2150-
t.Fatal("state should not exist, but contains:\n", string(data))
2151-
}
2152-
2153-
// Verify a backup doesn't exist
2154-
if !isEmptyState(DefaultStateFilename + DefaultBackupExtension) {
2155-
data, _ := os.ReadFile(DefaultStateFilename + DefaultBackupExtension)
2156-
t.Fatal("backup should not exist, but contains:\n", string(data))
2157-
}
2158-
2159-
// Write some state
2160-
s.WriteState(testState())
2161-
if err := s.PersistState(nil); err != nil {
2162-
t.Fatalf("unexpected error: %s", err)
2163-
}
2164-
2165-
// Verify it exists where we expect it to
2166-
if isEmptyState(DefaultStateFilename) {
2167-
t.Fatal(DefaultStateFilename, "is empty")
2168-
}
2169-
2170-
// Verify no backup since it was empty to start
2171-
if !isEmptyState(DefaultStateFilename + DefaultBackupExtension) {
2172-
data, _ := ioutil.ReadFile(DefaultStateFilename + DefaultBackupExtension)
2173-
t.Fatal("backup state should be empty, but contains:\n", string(data))
2174-
}
2175-
}
2176-
21772091
// Changing from using backend to state_store
21782092
//
21792093
// TODO(SarahFrench/radeksimko): currently this test only confirms that we're hitting the switch
@@ -2555,7 +2469,7 @@ func TestMetaBackend_GetStateStoreProviderFactory(t *testing.T) {
25552469

25562470
// Setup the meta and test providerFactoriesDuringInit
25572471
m := testMetaBackend(t, nil)
2558-
_, diags := m.GetStateStoreProviderFactory(config, locks)
2472+
_, diags := m.StateStoreProviderFactoryFromConfig(config, locks)
25592473
if !diags.HasErrors() {
25602474
t.Fatalf("expected error but got none")
25612475
}
@@ -2585,7 +2499,7 @@ func TestMetaBackend_GetStateStoreProviderFactory(t *testing.T) {
25852499

25862500
// Setup the meta and test providerFactoriesDuringInit
25872501
m := testMetaBackend(t, nil)
2588-
_, diags := m.GetStateStoreProviderFactory(config, locks)
2502+
_, diags := m.StateStoreProviderFactoryFromConfig(config, locks)
25892503
if !diags.HasErrors() {
25902504
t.Fatal("expected and error but got none")
25912505
}

internal/command/testdata/state-store-unset/.terraform.lock.hcl

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/command/testdata/state-store-unset/.terraform/terraform.tfstate

Lines changed: 0 additions & 19 deletions
This file was deleted.

internal/command/testdata/state-store-unset/main.tf

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)