From a270a18a4d7b3fd95a2cc959010d5bbea653c250 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 22 May 2018 19:33:45 -0700 Subject: [PATCH] command: Update tests for changes in "terraform" package This is not exhaustive, but it gets the tests in this package compiling again and gets most of them working. --- command/apply_destroy_test.go | 21 ++++++++++++++++++++- command/apply_test.go | 32 ++++++++++++++++++++++++++------ command/command_test.go | 33 +++++++++++++++++++++------------ command/graph_test.go | 2 +- command/meta_backend_test.go | 18 +++++++++--------- command/plan_test.go | 2 +- command/show_test.go | 5 +++-- 7 files changed, 81 insertions(+), 32 deletions(-) diff --git a/command/apply_destroy_test.go b/command/apply_destroy_test.go index e9779190bd..109b5061ae 100644 --- a/command/apply_destroy_test.go +++ b/command/apply_destroy_test.go @@ -5,8 +5,10 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/config/configschema" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" + "github.com/zclconf/go-cty/cty" ) func TestApply_destroy(t *testing.T) { @@ -29,6 +31,8 @@ func TestApply_destroy(t *testing.T) { statePath := testStateFile(t, originalState) p := testProvider() + p.GetSchemaReturn = applyFixtureSchema() + ui := new(cli.MockUi) c := &ApplyCommand{ Destroy: true, @@ -147,7 +151,7 @@ func TestApply_destroyLockedState(t *testing.T) { func TestApply_destroyPlan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), }) p := testProvider() @@ -195,6 +199,21 @@ func TestApply_destroyTargeted(t *testing.T) { statePath := testStateFile(t, originalState) p := testProvider() + p.GetSchemaReturn = &terraform.ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Computed: true}, + }, + }, + "test_load_balancer": { + Attributes: map[string]*configschema.Attribute{ + "instances": {Type: cty.List(cty.String), Optional: true}, + }, + }, + }, + } + ui := new(cli.MockUi) c := &ApplyCommand{ Destroy: true, diff --git a/command/apply_test.go b/command/apply_test.go index 19862257b1..a876c5eefc 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -16,15 +16,20 @@ import ( "testing" "time" + "github.com/mitchellh/cli" + "github.com/zclconf/go-cty/cty" + + "github.com/hashicorp/terraform/config/configschema" "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" - "github.com/mitchellh/cli" ) func TestApply(t *testing.T) { statePath := testTempFile(t) p := testProvider() + p.GetSchemaReturn = applyFixtureSchema() + ui := new(cli.MockUi) c := &ApplyCommand{ Meta: Meta{ @@ -495,7 +500,7 @@ func TestApply_plan(t *testing.T) { defaultInputWriter = new(bytes.Buffer) planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), }) statePath := testTempFile(t) @@ -620,7 +625,7 @@ func TestApply_plan_remoteState(t *testing.T) { state.Remote = conf planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), State: state, }) @@ -664,7 +669,7 @@ func TestApply_planWithVarFile(t *testing.T) { } planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), }) statePath := testTempFile(t) @@ -706,7 +711,7 @@ func TestApply_planWithVarFile(t *testing.T) { func TestApply_planVars(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), }) statePath := testTempFile(t) @@ -739,7 +744,7 @@ func TestApply_planNoModuleFiles(t *testing.T) { p := testProvider() planFile := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply-plan-no-module"), + Config: testModule(t, "apply-plan-no-module"), }) apply := &ApplyCommand{ @@ -1527,6 +1532,21 @@ func testHttpHandlerHeader(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) } +// applyFixtureSchema returns a schema suitable for processing the +// configuration in test-fixtures/apply . This schema should be +// assigned to a mock provider named "test". +func applyFixtureSchema() *terraform.ProviderSchema { + return &terraform.ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "ami": {Type: cty.String, Optional: true}, + }, + }, + }, + } +} + const applyVarFile = ` foo = "bar" ` diff --git a/command/command_test.go b/command/command_test.go index d0284480f4..0e00c7ed53 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -19,8 +19,8 @@ import ( "syscall" "testing" - backendInit "github.com/hashicorp/terraform/backend/init" - "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/configs" + "github.com/hashicorp/terraform/configs/configload" "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" ) @@ -113,21 +113,30 @@ func metaOverridesForProviderAndProvisioner(p terraform.ResourceProvider, pr ter } } -func testModule(t *testing.T, name string) *module.Tree { +func testModule(t *testing.T, name string) *configs.Config { t.Helper() - mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name)) - if err != nil { - t.Fatalf("err: %s", err) + dir := filepath.Join(fixtureDir, name) + + // FIXME: We're not dealing with the cleanup function here because + // this testModule function is used all over and so we don't want to + // change its interface at this late stage. + loader, _ := configload.NewLoaderForTests(t) + + // Test modules usually do not refer to remote sources, and for local + // sources only this ultimately just records all of the module paths + // in a JSON file so that we can load them below. + diags := loader.InstallModules(dir, true, configload.InstallHooksImpl{}) + if diags.HasErrors() { + t.Fatal(diags.Error()) } - s := module.NewStorage(tempDir(t), nil) - s.Mode = module.GetModeGet - if err := mod.Load(s); err != nil { - t.Fatalf("err: %s", err) + config, diags := loader.LoadConfig(dir) + if diags.HasErrors() { + t.Fatal(diags.Error()) } - return mod + return config } // testPlan returns a non-nil noop plan. @@ -141,7 +150,7 @@ func testPlan(t *testing.T) *terraform.Plan { } return &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), State: state, } } diff --git a/command/graph_test.go b/command/graph_test.go index 89033b01bf..131312b1f9 100644 --- a/command/graph_test.go +++ b/command/graph_test.go @@ -121,7 +121,7 @@ func TestGraph_plan(t *testing.T) { }, }, - Module: testModule(t, "graph"), + Config: testModule(t, "graph"), }) ui := new(cli.MockUi) diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index e5ff66895e..ba39e2fb58 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -2742,7 +2742,7 @@ func TestMetaBackend_planLocal(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-local"), + Config: testModule(t, "backend-plan-local"), State: nil, } @@ -2829,7 +2829,7 @@ func TestMetaBackend_planLocalStatePath(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-local"), + Config: testModule(t, "backend-plan-local"), State: original, } @@ -2925,7 +2925,7 @@ func TestMetaBackend_planLocalMatch(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-local-match"), + Config: testModule(t, "backend-plan-local-match"), State: testStateRead(t, DefaultStateFilename), } @@ -3018,7 +3018,7 @@ func TestMetaBackend_planLocalMismatchLineage(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-local-mismatch-lineage"), + Config: testModule(t, "backend-plan-local-mismatch-lineage"), State: planState, } @@ -3070,7 +3070,7 @@ func TestMetaBackend_planLocalNewer(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-local-newer"), + Config: testModule(t, "backend-plan-local-newer"), State: planState, } @@ -3124,7 +3124,7 @@ func TestMetaBackend_planBackendEmptyDir(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-backend-empty-config"), + Config: testModule(t, "backend-plan-backend-empty-config"), State: planState, Backend: backendState.Backend, } @@ -3226,7 +3226,7 @@ func TestMetaBackend_planBackendMatch(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-backend-empty-config"), + Config: testModule(t, "backend-plan-backend-empty-config"), State: planState, Backend: backendState.Backend, } @@ -3331,7 +3331,7 @@ func TestMetaBackend_planBackendMismatchLineage(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-backend-empty-config"), + Config: testModule(t, "backend-plan-backend-empty-config"), State: planState, Backend: backendState.Backend, } @@ -3390,7 +3390,7 @@ func TestMetaBackend_planLegacy(t *testing.T) { // Create the plan plan := &terraform.Plan{ - Module: testModule(t, "backend-plan-legacy-data"), + Config: testModule(t, "backend-plan-legacy-data"), State: planState, } diff --git a/command/plan_test.go b/command/plan_test.go index 2da48ae000..baf8ff210b 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -84,7 +84,7 @@ func TestPlan_plan(t *testing.T) { defer testFixCwd(t, tmp, cwd) planPath := testPlanFile(t, &terraform.Plan{ - Module: testModule(t, "apply"), + Config: testModule(t, "apply"), }) p := testProvider() diff --git a/command/show_test.go b/command/show_test.go index 53c2cdcaa3..61ae55b967 100644 --- a/command/show_test.go +++ b/command/show_test.go @@ -5,7 +5,8 @@ import ( "strings" "testing" - "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/configs" + "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -68,7 +69,7 @@ func TestShow_noArgsNoState(t *testing.T) { func TestShow_plan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Module: new(module.Tree), + Config: configs.NewEmptyConfig(), }) ui := new(cli.MockUi)