From f70318097a7fb9d0ca13e655c7a10c62dfd4a700 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 1 Jun 2017 17:51:07 -0700 Subject: [PATCH] config: fix provider version constraint validation Previously we were using the "semver" library to parse version constraints, but we switched over to go-version and encapsulated it inside our own plugin/discovery package to reduce dependency sprawl in the code. This particular situation was missed when updating references to the new path, which meant that our validation code disagreed with the rest of the code about what is considered a valid version constraint string. By using the correct function, we ensure that we catch early any invalid versions. --- config/config.go | 4 ++-- config/config_test.go | 7 +++++++ .../validate-provider-version-invalid/main.tf | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 config/test-fixtures/validate-provider-version-invalid/main.tf diff --git a/config/config.go b/config/config.go index 0648d6917..3f756dcf4 100644 --- a/config/config.go +++ b/config/config.go @@ -8,11 +8,11 @@ import ( "strconv" "strings" - "github.com/blang/semver" "github.com/hashicorp/go-multierror" "github.com/hashicorp/hil" "github.com/hashicorp/hil/ast" "github.com/hashicorp/terraform/helper/hilmapstructure" + "github.com/hashicorp/terraform/plugin/discovery" "github.com/mitchellh/reflectwalk" ) @@ -391,7 +391,7 @@ func (c *Config) Validate() error { } if p.Version != "" { - _, err := semver.ParseRange(p.Version) + _, err := discovery.ConstraintStr(p.Version).Parse() if err != nil { errs = append(errs, fmt.Errorf( "provider.%s: invalid version constraint %q: %s", diff --git a/config/config_test.go b/config/config_test.go index db62e1c54..edcf36af2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -626,6 +626,13 @@ func TestConfigValidate_varModuleInvalid(t *testing.T) { } } +func TestConfigValidate_varProviderVersionInvalid(t *testing.T) { + c := testConfig(t, "validate-provider-version-invalid") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestNameRegexp(t *testing.T) { cases := []struct { Input string diff --git a/config/test-fixtures/validate-provider-version-invalid/main.tf b/config/test-fixtures/validate-provider-version-invalid/main.tf new file mode 100644 index 000000000..9a251a3b2 --- /dev/null +++ b/config/test-fixtures/validate-provider-version-invalid/main.tf @@ -0,0 +1,3 @@ +provider "test" { + version = "bananas!" +}