test: Add a test that shows a custom workspace being baked-into a planfile (#37917)

undeferred-components-should-not-have-unknown-inputs
Sarah French 6 months ago committed by GitHub
parent fe70f0a7db
commit 0941763d05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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()

@ -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`.

@ -0,0 +1,9 @@
{
"version": 3,
"terraform_version": "1.15.0",
"backend": {
"type": "inmem",
"config": {},
"hash": 3307601501
}
}

@ -0,0 +1,8 @@
terraform {
backend "inmem" {
}
}
resource "test_instance" "foo" {
ami = "bar"
}
Loading…
Cancel
Save