Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ func (opt *VerifyPluginsOption) configureInit(conf *initConfig) {
func (tf *Terraform) configureInitOptions(ctx context.Context, c *initConfig, opts ...InitOption) error {
for _, o := range opts {
switch o.(type) {
case *LockOption, *LockTimeoutOption, *VerifyPluginsOption, *GetPluginsOption:
case *VerifyPluginsOption, *GetPluginsOption:
err := tf.compatible(ctx, nil, tf0_15_0)
if err != nil {
return fmt.Errorf("-lock, -lock-timeout, -verify-plugins, and -get-plugins options are no longer available as of Terraform 0.15: %w", err)
return fmt.Errorf("-verify-plugins and -get-plugins options are no longer available as of Terraform 0.15: %w", err)
}
case *LockOption, *LockTimeoutOption:
err := tf.incompatible(ctx, tf0_15_0, tf1_0_10)
if err != nil {
return fmt.Errorf("-lock, -lock-timeout options are not available in Terraform >=0.15 <1.0.10: %w", err)
}
}

Expand Down Expand Up @@ -196,12 +201,14 @@ func (tf *Terraform) buildInitArgs(ctx context.Context, c initConfig) ([]string,
args = append(args, "-from-module="+c.fromModule)
}

// string opts removed in 0.15: pass if set and <0.15
err := tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
// lock and lock-timeout opts removed in >=0.15 and restored in >=1.0.10
// https://github.com/hashicorp/terraform/pull/29773
err := tf.compatible(ctx, tf0_15_0, tf1_0_10)
if err != nil {
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
args = append(args, "-lock="+fmt.Sprint(c.lock))
}

// boolean opts: always pass
Expand All @@ -212,7 +219,6 @@ func (tf *Terraform) buildInitArgs(ctx context.Context, c initConfig) ([]string,
// boolean opts removed in 0.15: pass if <0.15
err = tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
args = append(args, "-lock="+fmt.Sprint(c.lock))
args = append(args, "-get-plugins="+fmt.Sprint(c.getPlugins))
args = append(args, "-verify-plugins="+fmt.Sprint(c.verifyPlugins))
}
Expand Down
18 changes: 18 additions & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
tf0_15_2 = version.Must(version.NewVersion("0.15.2"))
tf0_15_3 = version.Must(version.NewVersion("0.15.3"))
tf0_15_4 = version.Must(version.NewVersion("0.15.4"))
tf1_0_10 = version.Must(version.NewVersion("1.0.10"))
tf1_1_0 = version.Must(version.NewVersion("1.1.0"))
tf1_4_0 = version.Must(version.NewVersion("1.4.0"))
tf1_6_0 = version.Must(version.NewVersion("1.6.0"))
Expand Down Expand Up @@ -183,6 +184,23 @@ func (tf *Terraform) compatible(ctx context.Context, minInclusive *version.Versi
return nil
}

// incompatible asserts the cached terraform version is NOT in the specified range, and returns a well known error if it is.
func (tf *Terraform) incompatible(ctx context.Context, minInclusive *version.Version, maxExclusive *version.Version) error {
tfv, _, err := tf.Version(ctx, false)
if err != nil {
return err
}
if ok := versionInRange(tfv, minInclusive, maxExclusive); ok {
return &ErrVersionMismatch{
MinInclusive: errorVersionString(minInclusive),
MaxExclusive: errorVersionString(maxExclusive),
Actual: errorVersionString(tfv),
}
}

return nil
}

// experimentsEnabled asserts the cached terraform version has experiments enabled in the executable,
// and returns a well known error if not. Experiments are enabled in alpha and (potentially) dev builds of Terraform.
func (tf *Terraform) experimentsEnabled(ctx context.Context) error {
Expand Down
83 changes: 83 additions & 0 deletions tfexec/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func TestVersionInRange(t *testing.T) {
{true, "0.12.26", "0.12.26", ""},
{true, "0.12.26", "0.12.26", "0.12.27"},
{true, "0.12.26", "0.12.26", "0.13.0"},
{false, "0.15.0", "1.11.4", "1.0.10"},

{false, "0.12.26", "0.13.0-beta3", "0.13.0"},
{true, "0.12.26", "0.13.0-beta3", ""},
Expand Down Expand Up @@ -295,6 +296,88 @@ func TestCompatible(t *testing.T) {
}
}

func TestIncompatible(t *testing.T) {
ev := &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.0.9")),
}
ev.SetLogger(testutil.TestLogger())

ctx := context.Background()
t.Cleanup(func() { ev.Remove(ctx) })

tf1_0_9, err := ev.Install(ctx)
if err != nil {
t.Fatal(err)
}

ev = &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.13.5")),
}
ev.SetLogger(testutil.TestLogger())
t.Cleanup(func() { ev.Remove(ctx) })

tf1_13_5, err := ev.Install(ctx)
if err != nil {
t.Fatal(err)
}

for i, c := range []struct {
expected bool
min string
max string
binPath string
}{
{false, "0.15.0", "", tf1_0_9},
{false, "1.0.9", "", tf1_0_9},
{true, "1.13.5", "", tf1_0_9},

{false, "0.15.0", "1.13.5", tf1_0_9},
{true, "0.15.0", "1.0.9", tf1_13_5},
{true, "0.15.0", "1.13.5", tf1_13_5},
{false, "1.0.9", "1.13.5", tf1_0_9},
{true, "1.0.10", "1.13.5", tf1_0_9},

{false, "", "1.13.5", tf1_0_9},
{true, "", "1.13.5", tf1_13_5},
{true, "", "1.0.9", tf1_13_5},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
tf, err := NewTerraform(filepath.Dir(c.binPath), c.binPath)
if err != nil {
t.Fatal(err)
}

var min *version.Version
if c.min != "" {
min, err = version.NewVersion(c.min)
if err != nil {
t.Fatal(err)
}
}

var max *version.Version
if c.max != "" {
max, err = version.NewVersion(c.max)
if err != nil {
t.Fatal(err)
}
}
var mismatch *ErrVersionMismatch
err = tf.incompatible(context.Background(), min, max)
switch {
case c.expected && err != nil:
t.Fatal(err)
case !c.expected && err == nil:
t.Fatal("expected version mismatch error, no error returned")
case !c.expected && !errors.As(err, &mismatch):
t.Fatal(err)
}
})
}
}

func TestExperimentsEnabled(t *testing.T) {
testCases := map[string]struct {
tfVersion *version.Version
Expand Down