From a36b3e1ec50897936c7d8bbeb6d704bdcb72ecf0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 24 Sep 2014 15:48:46 -0700 Subject: [PATCH] command: tests pass --- command/apply_test.go | 7 +++---- command/command_test.go | 27 +++++++++++++++++++++++++++ command/graph_test.go | 3 +-- command/show_test.go | 4 ++-- config/config.go | 4 ++++ config/config_test.go | 7 +++++++ terraform/context.go | 16 +++++++++------- 7 files changed, 53 insertions(+), 15 deletions(-) diff --git a/command/apply_test.go b/command/apply_test.go index 972371331..f745016d2 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -11,7 +11,6 @@ import ( "testing" "time" - "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -246,7 +245,7 @@ func TestApply_noArgs(t *testing.T) { func TestApply_plan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Config: new(config.Config), + Module: testModule(t, "apply"), }) statePath := testTempFile(t) @@ -294,7 +293,7 @@ func TestApply_planWithVarFile(t *testing.T) { } planPath := testPlanFile(t, &terraform.Plan{ - Config: new(config.Config), + Module: testModule(t, "apply"), }) statePath := testTempFile(t) @@ -345,7 +344,7 @@ func TestApply_planWithVarFile(t *testing.T) { func TestApply_planVars(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Config: new(config.Config), + Module: testModule(t, "apply"), }) statePath := testTempFile(t) diff --git a/command/command_test.go b/command/command_test.go index 8d994ff72..a6c2f173d 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/terraform" ) @@ -22,6 +23,18 @@ func init() { } } +func tempDir(t *testing.T) string { + dir, err := ioutil.TempDir("", "tf") + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.RemoveAll(dir); err != nil { + t.Fatalf("err: %s", err) + } + + return dir +} + func testFixturePath(name string) string { return filepath.Join(fixtureDir, name) } @@ -36,6 +49,20 @@ func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts { } } +func testModule(t *testing.T, name string) *module.Tree { + mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name)) + if err != nil { + t.Fatalf("err: %s", err) + } + + s := &module.FolderStorage{StorageDir: tempDir(t)} + if err := mod.Load(s, module.GetModeGet); err != nil { + t.Fatalf("err: %s", err) + } + + return mod +} + func testPlanFile(t *testing.T, plan *terraform.Plan) string { path := testTempFile(t) diff --git a/command/graph_test.go b/command/graph_test.go index cbc564e8f..29f3f5874 100644 --- a/command/graph_test.go +++ b/command/graph_test.go @@ -5,7 +5,6 @@ import ( "strings" "testing" - "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -81,7 +80,7 @@ func TestGraph_noArgs(t *testing.T) { func TestGraph_plan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Config: new(config.Config), + Module: testModule(t, "graph"), }) ui := new(cli.MockUi) diff --git a/command/show_test.go b/command/show_test.go index 3025f258e..c20f489b0 100644 --- a/command/show_test.go +++ b/command/show_test.go @@ -3,7 +3,7 @@ package command import ( "testing" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -43,7 +43,7 @@ func TestShow_noArgs(t *testing.T) { func TestShow_plan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ - Config: new(config.Config), + Module: new(module.Tree), }) ui := new(cli.MockUi) diff --git a/config/config.go b/config/config.go index b4b405107..45fb3028c 100644 --- a/config/config.go +++ b/config/config.go @@ -120,6 +120,10 @@ func (r *Resource) Id() string { // Validate does some basic semantic checking of the configuration. func (c *Config) Validate() error { + if c == nil { + return nil + } + var errs []error for _, k := range c.unknownKeys { diff --git a/config/config_test.go b/config/config_test.go index 26becdd30..76886c89b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -58,6 +58,13 @@ func TestConfigValidate_dupResource(t *testing.T) { } } +func TestConfigValidate_nil(t *testing.T) { + var c Config + if err := c.Validate(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestConfigValidate_outputBadField(t *testing.T) { c := testConfig(t, "validate-output-bad-field") if err := c.Validate(); err == nil { diff --git a/terraform/context.go b/terraform/context.go index 7ba25cc68..9abd4b180 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -215,14 +215,16 @@ func (c *Context) Stop() { func (c *Context) Validate() ([]string, []error) { var rerr *multierror.Error - // Validate the configuration itself - if err := c.module.Config().Validate(); err != nil { - rerr = multierror.ErrorAppend(rerr, err) - } + if config := c.module.Config(); config != nil { + // Validate the configuration itself + if err := config.Validate(); err != nil { + rerr = multierror.ErrorAppend(rerr, err) + } - // Validate the user variables - if errs := smcUserVariables(c.module.Config(), c.variables); len(errs) > 0 { - rerr = multierror.ErrorAppend(rerr, errs...) + // Validate the user variables + if errs := smcUserVariables(config, c.variables); len(errs) > 0 { + rerr = multierror.ErrorAppend(rerr, errs...) + } } // Validate the graph