From 2d849f8650a9bfa484a07136cfae3c80b294527e Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 28 Aug 2017 23:55:16 +0530 Subject: [PATCH] command/init: check required_version Previously we were checking required_version only during "real" operations, and not during initialization. Catching it during init is better because that's the first command users run on a new working directory. --- command/init.go | 5 +++++ command/init_test.go | 21 +++++++++++++++++++ .../init-check-required-version/main.tf | 3 +++ terraform/context.go | 2 +- terraform/version_required.go | 6 +++--- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 command/test-fixtures/init-check-required-version/main.tf diff --git a/command/init.go b/command/init.go index 3665b5b1d7..2de7d701d9 100644 --- a/command/init.go +++ b/command/init.go @@ -288,6 +288,11 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade return err } + if err := terraform.CheckRequiredVersion(mod); err != nil { + c.Ui.Error(err.Error()) + return err + } + var available discovery.PluginMetaSet if upgrade { // If we're in upgrade mode, we ignore any auto-installed plugins diff --git a/command/init_test.go b/command/init_test.go index ef192b7e54..8b013bdaec 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -855,6 +855,27 @@ func TestInit_getProviderHaveLegacyVersion(t *testing.T) { } } +func TestInit_getProviderCheckRequiredVersion(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + copy.CopyDir(testFixturePath("init-check-required-version"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + args := []string{} + if code := c.Run(args); code != 1 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } +} + func TestInit_providerLockFile(t *testing.T) { // Create a temporary working directory that is empty td := tempDir(t) diff --git a/command/test-fixtures/init-check-required-version/main.tf b/command/test-fixtures/init-check-required-version/main.tf new file mode 100644 index 0000000000..8f65421946 --- /dev/null +++ b/command/test-fixtures/init-check-required-version/main.tf @@ -0,0 +1,3 @@ +terraform { + required_version = "~> 0.9.0" +} diff --git a/terraform/context.go b/terraform/context.go index aaa77abf96..28ca14cbf4 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -122,7 +122,7 @@ type Context struct { func NewContext(opts *ContextOpts) (*Context, error) { // Validate the version requirement if it is given if opts.Module != nil { - if err := checkRequiredVersion(opts.Module); err != nil { + if err := CheckRequiredVersion(opts.Module); err != nil { return nil, err } } diff --git a/terraform/version_required.go b/terraform/version_required.go index 3cbbf56085..723ec39632 100644 --- a/terraform/version_required.go +++ b/terraform/version_required.go @@ -8,17 +8,17 @@ import ( "github.com/hashicorp/terraform/config/module" ) -// checkRequiredVersion verifies that any version requirements specified by +// CheckRequiredVersion verifies that any version requirements specified by // the configuration are met. // // This checks the root module as well as any additional version requirements // from child modules. // // This is tested in context_test.go. -func checkRequiredVersion(m *module.Tree) error { +func CheckRequiredVersion(m *module.Tree) error { // Check any children for _, c := range m.Children() { - if err := checkRequiredVersion(c); err != nil { + if err := CheckRequiredVersion(c); err != nil { return err } }