Skip to content

Commit 0941763

Browse files
authored
test: Add a test that shows a custom workspace being baked-into a planfile (#37917)
1 parent fe70f0a commit 0941763

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

internal/command/plan_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,79 @@ func TestPlan_outBackend(t *testing.T) {
473473
}
474474
}
475475

476+
// When using "-out" with a backend, the plan should encode the backend config
477+
// and also the selected workspace, if workspaces are supported by the backend.
478+
//
479+
// This test demonstrates that setting the workspace in the backend plan
480+
// responds to the selected workspace, versus other tests that show the same process
481+
// when defaulting to the default workspace when there's a lack of information about
482+
// the selected workspace.
483+
//
484+
// To test planning with a non-default workspace we need to use a backend that supports
485+
// workspaces. In this test the `inmem` backend is used.
486+
func TestPlan_outBackend_withWorkspace(t *testing.T) {
487+
// Create a temporary working directory
488+
td := t.TempDir()
489+
testCopyDir(t, testFixturePath("plan-out-backend-workspace"), td)
490+
t.Chdir(td)
491+
492+
// These values are coupled with the test fixture used above.
493+
expectedBackendType := "inmem"
494+
expectedWorkspace := "custom-workspace"
495+
496+
outPath := "foo"
497+
p := testProvider()
498+
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
499+
ResourceTypes: map[string]providers.Schema{
500+
"test_instance": {
501+
Body: &configschema.Block{
502+
Attributes: map[string]*configschema.Attribute{
503+
"id": {
504+
Type: cty.String,
505+
Computed: true,
506+
},
507+
"ami": {
508+
Type: cty.String,
509+
Optional: true,
510+
},
511+
},
512+
},
513+
},
514+
},
515+
}
516+
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
517+
return providers.PlanResourceChangeResponse{
518+
PlannedState: req.ProposedNewState,
519+
}
520+
}
521+
view, done := testView(t)
522+
c := &PlanCommand{
523+
Meta: Meta{
524+
testingOverrides: metaOverridesForProvider(p),
525+
View: view,
526+
},
527+
}
528+
529+
args := []string{
530+
"-out", outPath,
531+
}
532+
code := c.Run(args)
533+
output := done(t)
534+
if code != 0 {
535+
t.Logf("stdout: %s", output.Stdout())
536+
t.Fatalf("plan command failed with exit code %d\n\n%s", code, output.Stderr())
537+
}
538+
539+
plan := testReadPlan(t, outPath)
540+
541+
if got, want := plan.Backend.Type, expectedBackendType; got != want {
542+
t.Errorf("wrong backend type %q; want %q", got, want)
543+
}
544+
if got, want := plan.Backend.Workspace, expectedWorkspace; got != want {
545+
t.Errorf("wrong backend workspace %q; want %q", got, want)
546+
}
547+
}
548+
476549
func TestPlan_refreshFalse(t *testing.T) {
477550
// Create a temporary working directory that is empty
478551
td := t.TempDir()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The test using this test fixture asserts that a plan generated from this configuration includes both:
2+
1. Details of the backend defined in the config when the plan was created
3+
2. Details of the workspace that was selected when the plan was generated
4+
5+
The `inmem` backend is used because it supports the use of CE workspaces.
6+
We set a non-default workspace in `internal/command/testdata/plan-out-backend-workspace/.terraform/environment`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom-workspace
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": 3,
3+
"terraform_version": "1.15.0",
4+
"backend": {
5+
"type": "inmem",
6+
"config": {},
7+
"hash": 3307601501
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
backend "inmem" {
3+
}
4+
}
5+
6+
resource "test_instance" "foo" {
7+
ami = "bar"
8+
}

0 commit comments

Comments
 (0)