diff --git a/internal/command/plan_test.go b/internal/command/plan_test.go index 710dfef29e..1439e60d5f 100644 --- a/internal/command/plan_test.go +++ b/internal/command/plan_test.go @@ -473,6 +473,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() diff --git a/internal/command/testdata/plan-out-backend-workspace/.terraform/README.md b/internal/command/testdata/plan-out-backend-workspace/.terraform/README.md new file mode 100644 index 0000000000..856b5d04bb --- /dev/null +++ b/internal/command/testdata/plan-out-backend-workspace/.terraform/README.md @@ -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`. \ No newline at end of file diff --git a/internal/command/testdata/plan-out-backend-workspace/.terraform/environment b/internal/command/testdata/plan-out-backend-workspace/.terraform/environment new file mode 100644 index 0000000000..aa9f8408ce --- /dev/null +++ b/internal/command/testdata/plan-out-backend-workspace/.terraform/environment @@ -0,0 +1 @@ +custom-workspace \ No newline at end of file diff --git a/internal/command/testdata/plan-out-backend-workspace/.terraform/terraform.tfstate b/internal/command/testdata/plan-out-backend-workspace/.terraform/terraform.tfstate new file mode 100644 index 0000000000..bd6f707f4f --- /dev/null +++ b/internal/command/testdata/plan-out-backend-workspace/.terraform/terraform.tfstate @@ -0,0 +1,9 @@ +{ + "version": 3, + "terraform_version": "1.15.0", + "backend": { + "type": "inmem", + "config": {}, + "hash": 3307601501 + } +} \ No newline at end of file diff --git a/internal/command/testdata/plan-out-backend-workspace/main.tf b/internal/command/testdata/plan-out-backend-workspace/main.tf new file mode 100644 index 0000000000..c19de98f18 --- /dev/null +++ b/internal/command/testdata/plan-out-backend-workspace/main.tf @@ -0,0 +1,8 @@ +terraform { + backend "inmem" { + } +} + +resource "test_instance" "foo" { + ami = "bar" +}