Skip to content

feat: Download default terraform version when minor version mismatches #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 22, 2022
Prev Previous commit
Next Next commit
add unit tests
  • Loading branch information
AbhineetJain committed Jun 22, 2022
commit c76885d302940008daa864683ab8a8c4d2e8cf44
95 changes: 95 additions & 0 deletions provisioner/terraform/serve_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package terraform

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func Test_getAbsoluteBinaryPath(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
terraformVersion string
expectedAbsoluteBinary string
expectedOk bool
}{
{
name: "TestCorrectVersion",
args: args{ctx: context.Background()},
terraformVersion: "1.1.9",
expectedAbsoluteBinary: "",
expectedOk: true,
},
{
name: "TestOldVersion",
args: args{ctx: context.Background()},
terraformVersion: "1.0.9",
expectedAbsoluteBinary: "",
expectedOk: false,
},
{
name: "TestNewVersion",
args: args{ctx: context.Background()},
terraformVersion: "1.2.9",
expectedAbsoluteBinary: "",
expectedOk: false,
},
{
name: "TestMalformedVersion",
args: args{ctx: context.Background()},
terraformVersion: "version",
expectedAbsoluteBinary: "",
expectedOk: false,
},
}
// nolint:paralleltest
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temp dir with the binary
tempDir := t.TempDir()
terraformBinaryOutput := fmt.Sprintf(`#!/bin/sh
cat <<-EOF
{
"terraform_version": "%s",
"platform": "linux_amd64",
"provider_selections": {},
"terraform_outdated": false
}
EOF`, tt.terraformVersion)

// #nosec
err := os.WriteFile(
filepath.Join(tempDir, "terraform"),
[]byte(terraformBinaryOutput),
0770,
)
require.NoError(t, err)

// Add the binary to PATH
pathVariable := os.Getenv("PATH")
t.Setenv("PATH", strings.Join([]string{tempDir, pathVariable}, ":"))

if tt.expectedOk {
tt.expectedAbsoluteBinary = filepath.Join(tempDir, "terraform")
}

actualAbsoluteBinary, actualOk := getAbsoluteBinaryPath(tt.args.ctx)
if actualAbsoluteBinary != tt.expectedAbsoluteBinary {
t.Errorf("getAbsoluteBinaryPath() absoluteBinaryPath, actual = %v, expected %v", actualAbsoluteBinary, tt.expectedAbsoluteBinary)
}
if actualOk != tt.expectedOk {
t.Errorf("getAbsoluteBinaryPath() ok, actual = %v, expected %v", actualOk, tt.expectedOk)
}
})
}
}