8000 feat: Download default terraform version when minor version mismatches by AbhineetJain · Pull Request #1775 · coder/coder · GitHub
[go: up one dir, main page]

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
simplify tests
  • Loading branch information
AbhineetJain committed Jun 22, 2022
commit 0c58641416444df0e1ee2f74f5b527ff49eb6efa
9 changes: 8 additions & 1 deletion provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Ver
cmd := exec.CommandContext(ctx, binaryPath, "version", "-json")
out, err := cmd.Output()
if err != nil {
return nil, err
select {
// `exec` library throws a `signal: killed`` error instead of the canceled context.
// Since we know the cause for the killed signal, we are throwing the relevant error here.
case <-ctx.Done():
return nil, ctx.Err()
default:
return nil, err
}
}
vj := tfjson.VersionOutput{}
err = json.Unmarshal(out, &vj)
Expand Down
4 changes: 3 additions & 1 deletion provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (
}()
)

var terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.")

type ServeOptions struct {
*provisionersdk.ServeOptions

Expand Down Expand Up @@ -66,7 +68,7 @@ func absoluteBinaryPath(ctx context.Context) (string, error) {
}

if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
return "", xerrors.Errorf("Terraform binary minor version mismatch.")
return "", terraformMinorVersionMismatch
}

return absoluteBinary, nil
Expand Down
17 changes: 6 additions & 11 deletions provisioner/terraform/serve_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func Test_absoluteBinaryPath(t *testing.T) {
name: "TestOldVersion",
args: args{ctx: context.Background()},
terraformVersion: "1.0.9",
expectedErr: xerrors.Errorf("Terraform binary minor version mismatch."),
expectedErr: terraformMinorVersionMismatch,
},
{
name: "TestNewVersion",
args: args{ctx: context.Background()},
terraformVersion: "1.2.9",
expectedErr: xerrors.Errorf("Terraform binary minor version mismatch."),
expectedErr: terraformMinorVersionMismatch,
},
{
name: "TestMalformedVersion",
Expand Down Expand Up @@ -86,17 +86,12 @@ func Test_absoluteBinaryPath(t *testing.T) {
}

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

require.Equal(t, expectedAbsoluteBinary, actualAbsoluteBinary)
if tt.expectedErr == nil {
if actualErr != nil {
t.Errorf("getAbsoluteBinaryPath() error, actual = %v, expected %v", actualErr.Error(), tt.expectedErr)
}
require.NoError(t, actualErr)
} else {
if actualErr.Error() != tt.expectedErr.Error() {
t.Errorf("getAbsoluteBinaryPath() error, actual = %v, expected %v", actualErr.Error(), tt.expectedErr.Error())
}
require.EqualError(t, actualErr, tt.expectedErr.Error())
}
})
}
Expand Down
0