Skip to content

Commit 1703aea

Browse files
committed
Update test to assert backend state file contents
1 parent a8405da commit 1703aea

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

internal/command/init_test.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import (
1717
"github.com/google/go-cmp/cmp"
1818
"github.com/hashicorp/cli"
1919
version "github.com/hashicorp/go-version"
20+
tfaddr "github.com/hashicorp/terraform-registry-address"
2021
"github.com/zclconf/go-cty/cty"
2122

2223
"github.com/hashicorp/terraform/internal/addrs"
2324
"github.com/hashicorp/terraform/internal/backend"
2425
"github.com/hashicorp/terraform/internal/command/arguments"
26+
"github.com/hashicorp/terraform/internal/command/clistate"
2527
"github.com/hashicorp/terraform/internal/command/views"
28+
"github.com/hashicorp/terraform/internal/command/workdir"
2629
"github.com/hashicorp/terraform/internal/configs"
2730
"github.com/hashicorp/terraform/internal/configs/configschema"
2831
"github.com/hashicorp/terraform/internal/depsfile"
@@ -3230,7 +3233,7 @@ func TestInit_testsWithModule(t *testing.T) {
32303233

32313234
// Testing init's behaviors when run in an empty working directory
32323235
func TestInit_stateStore_newWorkingDir(t *testing.T) {
3233-
t.Run("an init command creates the default workspace by default", func(t *testing.T) {
3236+
t.Run("the init command creates a backend state file, and creates the default workspace by default", func(t *testing.T) {
32343237
// Create a temporary, uninitialized working directory with configuration including a state store
32353238
td := t.TempDir()
32363239
testCopyDir(t, testFixturePath("init-with-state-store"), td)
@@ -3245,18 +3248,19 @@ func TestInit_stateStore_newWorkingDir(t *testing.T) {
32453248

32463249
ui := new(cli.MockUi)
32473250
view, done := testView(t)
3248-
c := &InitCommand{
3249-
Meta: Meta{
3250-
Ui: ui,
3251-
View: view,
3252-
AllowExperimentalFeatures: true,
3253-
testingOverrides: &testingOverrides{
3254-
Providers: map[addrs.Provider]providers.Factory{
3255-
mockProviderAddress: providers.FactoryFixed(mockProvider),
3256-
},
3251+
meta := Meta{
3252+
Ui: ui,
3253+
View: view,
3254+
AllowExperimentalFeatures: true,
3255+
testingOverrides: &testingOverrides{
3256+
Providers: map[addrs.Provider]providers.Factory{
3257+
mockProviderAddress: providers.FactoryFixed(mockProvider),
32573258
},
3258-
ProviderSource: providerSource,
32593259
},
3260+
ProviderSource: providerSource,
3261+
}
3262+
c := &InitCommand{
3263+
Meta: meta,
32603264
}
32613265

32623266
args := []string{"-enable-pluggable-state-storage-experiment=true"}
@@ -3277,6 +3281,36 @@ func TestInit_stateStore_newWorkingDir(t *testing.T) {
32773281
if _, exists := mockProvider.MockStates[backend.DefaultStateName]; !exists {
32783282
t.Fatal("expected the default workspace to be created during init, but it is missing")
32793283
}
3284+
3285+
// Assert contents of the backend state file
3286+
statePath := filepath.Join(meta.DataDir(), DefaultStateFilename)
3287+
sMgr := &clistate.LocalState{Path: statePath}
3288+
if err := sMgr.RefreshState(); err != nil {
3289+
t.Fatal("Failed to load state:", err)
3290+
}
3291+
s := sMgr.State()
3292+
if s == nil {
3293+
t.Fatal("expected backend state file to be created, but there isn't one")
3294+
}
3295+
v1_0_0, _ := version.NewVersion("1.0.0")
3296+
expectedState := &workdir.StateStoreConfigState{
3297+
Type: "test_store",
3298+
ConfigRaw: []byte("{\n \"bar\": null\n }"),
3299+
Hash: uint64(3976463117), // Hash of empty config
3300+
Provider: &workdir.ProviderConfigState{
3301+
Version: v1_0_0,
3302+
Source: &tfaddr.Provider{
3303+
Hostname: tfaddr.DefaultProviderRegistryHost,
3304+
Namespace: "hashicorp",
3305+
Type: "test",
3306+
},
3307+
ConfigRaw: []byte("{\n \"region\": null\n }"),
3308+
Hash: uint64(3976463117), // Hash of empty config
3309+
},
3310+
}
3311+
if diff := cmp.Diff(s.StateStore, expectedState); diff != "" {
3312+
t.Fatalf("unexpected diff in backend state file's description of state store:\n%s", diff)
3313+
}
32803314
})
32813315

32823316
t.Run("an init command with the flag -create-default-workspace=false will not make the default workspace", func(t *testing.T) {

0 commit comments

Comments
 (0)