Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions internal/command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,79 @@ func TestPlan_outBackend(t *testing.T) {
}
}

// When using "-out" with a backend, the plan should encode the backend config
// and also the selected workspace, if workspaces are supported by the backend.
//
// This test demonstrates that setting the workspace in the backend plan
// responds to the selected workspace, versus other tests that show the same process
// when defaulting to the default workspace when there's a lack of information about
// the selected workspace.
//
// To test planning with a non-default workspace we need to use a backend that supports
// workspaces. In this test the `inmem` backend is used.
func TestPlan_outBackend_withWorkspace(t *testing.T) {
// Create a temporary working directory
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-out-backend-workspace"), td)
t.Chdir(td)

// These values are coupled with the test fixture used above.
expectedBackendType := "inmem"
expectedWorkspace := "custom-workspace"

outPath := "foo"
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Body: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"ami": {
Type: cty.String,
Optional: true,
},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}

args := []string{
"-out", outPath,
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Logf("stdout: %s", output.Stdout())
t.Fatalf("plan command failed with exit code %d\n\n%s", code, output.Stderr())
}

plan := testReadPlan(t, outPath)

if got, want := plan.Backend.Type, expectedBackendType; got != want {
t.Errorf("wrong backend type %q; want %q", got, want)
}
if got, want := plan.Backend.Workspace, expectedWorkspace; got != want {
t.Errorf("wrong backend workspace %q; want %q", got, want)
}
}

func TestPlan_refreshFalse(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The test using this test fixture asserts that a plan generated from this configuration includes both:
1. Details of the backend defined in the config when the plan was created
2. Details of the workspace that was selected when the plan was generated

The `inmem` backend is used because it supports the use of CE workspaces.
We set a non-default workspace in `internal/command/testdata/plan-out-backend-workspace/.terraform/environment`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 3,
"terraform_version": "1.15.0",
"backend": {
"type": "inmem",
"config": {},
"hash": 3307601501
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
backend "inmem" {
}
}

resource "test_instance" "foo" {
ami = "bar"
}
Loading