diff --git a/internal/command/state_pull_test.go b/internal/command/state_pull_test.go index 0d1ada98f8..a8ce5fb815 100644 --- a/internal/command/state_pull_test.go +++ b/internal/command/state_pull_test.go @@ -9,7 +9,10 @@ import ( "testing" "github.com/hashicorp/cli" + "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/backend" + backendInit "github.com/hashicorp/terraform/internal/backend/init" "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/states/statefile" @@ -181,6 +184,120 @@ func TestStatePull_noState(t *testing.T) { } } +func TestStatePull_constVariable(t *testing.T) { + t.Run("missing value", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &StatePullCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{} + if code := c.Run(args); code == 0 { + t.Fatalf("expected error, got 0") + } + + errStr := ui.ErrorWriter.String() + if !strings.Contains(errStr, "No value for required variable") { + t.Fatalf("expected missing variable error, got: %s", errStr) + } + }) + + t.Run("value via cli", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &StatePullCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{"-var", "module_name=child"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + expectedResource := ` + { + "module": "module.child", + "mode": "managed", + "type": "test_instance", + "name": "test", + "provider": "provider[\"registry.terraform.io/hashicorp/test\"]", + "instances": [ + { + "schema_version": 0, + "attributes": {}, + "sensitive_attributes": [], + "identity_schema_version": 0 + } + ] + } +` + actual := ui.OutputWriter.String() + if !strings.Contains(actual, expectedResource) { + t.Fatalf("expected state to contain: %s\n\nstate:%s", expectedResource, actual) + } + }) + + t.Run("value via backend", func(t *testing.T) { + mockBackend := TestNewVariableBackend(map[string]string{ + "module_name": "child", + }) + backendInit.Set("local-vars", func() backend.Backend { return mockBackend }) + defer backendInit.Set("local-vars", nil) + + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var-backend") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &StatePullCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + expectedResource := ` + { + "module": "module.child", + "mode": "managed", + "type": "test_instance", + "name": "test", + "provider": "provider[\"registry.terraform.io/hashicorp/test\"]", + "instances": [ + { + "schema_version": 0, + "attributes": {}, + "sensitive_attributes": [], + "identity_schema_version": 0 + } + ] + } +` + actual := ui.OutputWriter.String() + if !strings.Contains(actual, expectedResource) { + t.Fatalf("expected state to contain: %s\n\nstate:%s", expectedResource, actual) + } + }) +} + func TestStatePull_checkRequiredVersion(t *testing.T) { // Create a temporary working directory that is empty td := t.TempDir()