Skip to content

Commit f8d4dcc

Browse files
committed
Add test coverage of (m *Meta) prepareBackend()
1 parent c8cd67d commit f8d4dcc

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

internal/command/meta_backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ func (m *Meta) prepareBackend(root *configs.Module) (backendrun.OperationsBacken
15521552
return nil, diags
15531553
}
15541554

1555-
// TODO - Use locks from here in opts below
1555+
// TODO(SarahFrench/radeksimko): Use locks from here in opts below
15561556
_, lDiags := m.lockedDependencies()
15571557
diags = diags.Append(lDiags)
15581558
if lDiags.HasErrors() {
@@ -1562,7 +1562,7 @@ func (m *Meta) prepareBackend(root *configs.Module) (backendrun.OperationsBacken
15621562
opts = &BackendOpts{
15631563
StateStoreConfig: root.StateStore,
15641564
ProviderFactory: factory,
1565-
// TODO - update once other work is merged into main
1565+
// TODO(SarahFrench/radeksimko): update once other work is merged into main
15661566
// Locks: locks,
15671567
}
15681568
default:

internal/command/meta_backend_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/zclconf/go-cty/cty"
3232

3333
backendInit "github.com/hashicorp/terraform/internal/backend/init"
34+
"github.com/hashicorp/terraform/internal/backend/local"
3435
backendLocal "github.com/hashicorp/terraform/internal/backend/local"
3536
backendInmem "github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
3637
)
@@ -2520,6 +2521,106 @@ func TestMeta_getStateStoreProviderFactory(t *testing.T) {
25202521
})
25212522
}
25222523

2524+
func TestMetaBackend_prepareBackend(t *testing.T) {
2525+
t.Run("it returns a cloud backend from cloud backend config", func(t *testing.T) {
2526+
// Create a temporary working directory with cloud configuration in
2527+
td := t.TempDir()
2528+
testCopyDir(t, testFixturePath("cloud-config"), td)
2529+
t.Chdir(td)
2530+
2531+
m := testMetaBackend(t, nil)
2532+
2533+
// Get the cloud config
2534+
mod, loadDiags := m.loadSingleModule(td)
2535+
if loadDiags.HasErrors() {
2536+
t.Fatalf("unexpected error when loading test config: %s", loadDiags.Err())
2537+
}
2538+
2539+
// We cannot initialize a cloud backend so we instead check
2540+
// the init error is referencing HCP Terraform
2541+
_, bDiags := m.prepareBackend(mod)
2542+
if !bDiags.HasErrors() {
2543+
t.Fatal("expected error but got none")
2544+
}
2545+
wantErr := "HCP Terraform or Terraform Enterprise initialization required: please run \"terraform init\""
2546+
if !strings.Contains(bDiags.Err().Error(), wantErr) {
2547+
t.Fatalf("expected error to contain %q, but got: %q",
2548+
wantErr,
2549+
bDiags.Err())
2550+
}
2551+
})
2552+
t.Run("it returns a backend from backend config", func(t *testing.T) {
2553+
// Create a temporary working directory with backend configuration in
2554+
td := t.TempDir()
2555+
testCopyDir(t, testFixturePath("backend-unchanged"), td)
2556+
t.Chdir(td)
2557+
2558+
m := testMetaBackend(t, nil)
2559+
2560+
// Get the backend config
2561+
mod, loadDiags := m.loadSingleModule(td)
2562+
if loadDiags.HasErrors() {
2563+
t.Fatalf("unexpected error when loading test config: %s", loadDiags.Err())
2564+
}
2565+
2566+
b, bDiags := m.prepareBackend(mod)
2567+
if bDiags.HasErrors() {
2568+
t.Fatal("unexpected error: ", bDiags.Err())
2569+
}
2570+
2571+
if _, ok := b.(*local.Local); !ok {
2572+
t.Fatal("expected returned operations backend to be a Local backend")
2573+
}
2574+
})
2575+
2576+
t.Run("it returns a local backend when there is empty configuration", func(t *testing.T) {
2577+
m := testMetaBackend(t, nil)
2578+
emptyConfig := configs.NewEmptyConfig()
2579+
2580+
b, bDiags := m.prepareBackend(emptyConfig.Module)
2581+
if bDiags.HasErrors() {
2582+
t.Fatal("unexpected error: ", bDiags.Err())
2583+
}
2584+
2585+
if _, ok := b.(*local.Local); !ok {
2586+
t.Fatal("expected returned operations backend to be a Local backend")
2587+
}
2588+
})
2589+
2590+
t.Run("it returns a state_store from state_store config", func(t *testing.T) {
2591+
// Create a temporary working directory with backend configuration in
2592+
td := t.TempDir()
2593+
testCopyDir(t, testFixturePath("state-store-unchanged"), td)
2594+
t.Chdir(td)
2595+
2596+
m := testMetaBackend(t, nil)
2597+
m.AllowExperimentalFeatures = true
2598+
mock := testStateStoreMock(t)
2599+
m.testingOverrides = &testingOverrides{
2600+
Providers: map[addrs.Provider]providers.Factory{
2601+
addrs.NewDefaultProvider("test"): providers.FactoryFixed(mock),
2602+
},
2603+
}
2604+
2605+
// Get the backend config
2606+
mod, loadDiags := m.loadSingleModule(td)
2607+
if loadDiags.HasErrors() {
2608+
t.Fatalf("unexpected error when loading test config: %s", loadDiags.Err())
2609+
}
2610+
2611+
_, bDiags := m.prepareBackend(mod)
2612+
if !bDiags.HasErrors() {
2613+
// TODO(SarahFrench/radeksimko): In future, we don't expect an error here!
2614+
t.Fatal("expected errors while state_store, due to incomplete implementation")
2615+
}
2616+
if !strings.Contains(bDiags.Err().Error(), "Changing a state store configuration is not implemented yet") {
2617+
t.Fatal("unexpected error: ", bDiags.Err())
2618+
}
2619+
2620+
// TODO(SarahFrench/radeksimko): Make assertion about returned operations backend.
2621+
})
2622+
}
2623+
25232624
func testMetaBackend(t *testing.T, args []string) *Meta {
25242625
var m Meta
25252626
m.Ui = new(cli.MockUi)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 3,
3+
"serial": 0,
4+
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
5+
"state_store": {
6+
"type": "test_store",
7+
"config": {
8+
"value": "value"
9+
},
10+
"provider": {
11+
"version": "1.2.3",
12+
"source": "registry.terraform.io/hashicorp/test",
13+
"config": {},
14+
"hash": 12345
15+
},
16+
"hash": 12345
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
version = "1.2.3"
6+
}
7+
}
8+
state_store "test_store" {
9+
provider "test" {}
10+
11+
value = "value" # matches backend state file
12+
}
13+
}

0 commit comments

Comments
 (0)