Skip to content

Commit 4580e8c

Browse files
committed
debugging
1 parent 1c6ae7d commit 4580e8c

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

provisioner/terraform/executor.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Ver
112112
cmd := exec.CommandContext(ctx, binaryPath, "version", "-json")
113113
out, err := cmd.Output()
114114
if err != nil {
115-
return nil, err
115+
select {
116+
case <-ctx.Done():
117+
return nil, ctx.Err()
118+
default:
119+
return nil, err
120+
}
116121
}
117122
vj := tfjson.VersionOutput{}
118123
err = json.Unmarshal(out, &vj)

provisioner/terraform/serve.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ type ServeOptions struct {
4343
Logger slog.Logger
4444
}
4545

46-
func getAbsoluteBinaryPath(ctx context.Context) (string, bool) {
46+
func absoluteBinaryPath(ctx context.Context) (string, error) {
4747
binaryPath, err := safeexec.LookPath("terraform")
4848
if err != nil {
49-
return "", false
49+
return "", xerrors.Errorf("Terraform binary not found: %w", err)
5050
}
5151

5252
// If the "coder" binary is in the same directory as
@@ -56,30 +56,32 @@ func getAbsoluteBinaryPath(ctx context.Context) (string, bool) {
5656
// to execute this properly!
5757
absoluteBinary, err := filepath.Abs(binaryPath)
5858
if err != nil {
59-
return "", false
59+
return "", xerrors.Errorf("Terraform binary absolute path not found: %w", err)
6060
}
6161

6262
// Checking the installed version of Terraform.
6363
version, err := versionFromBinaryPath(ctx, absoluteBinary)
6464
if err != nil {
65-
return "", false
65+
return "", xerrors.Errorf("Terraform binary get version failed: %w", err)
6666
}
6767

6868
if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
69-
return "", false
69+
return "", xerrors.Errorf("Terraform binary minor version mismatch.")
7070
}
7171

72-
return absoluteBinary, true
72+
return absoluteBinary, nil
7373
}
7474

7575
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
7676
func Serve(ctx context.Context, options *ServeOptions) error {
7777
if options.BinaryPath == "" {
78-
absoluteBinary, ok := getAbsoluteBinaryPath(ctx)
78+
absoluteBinary, err := absoluteBinaryPath(ctx)
79+
80+
if err != nil {
81+
if xerrors.Is(err, context.Canceled) {
82+
return xerrors.Errorf("absolute binary context canceled: %w", err)
83+
}
7984

80-
if ok {
81-
options.BinaryPath = absoluteBinary
82-
} else {
8385
installer := &releases.ExactVersion{
8486
InstallDir: options.CachePath,
8587
Product: product.Terraform,
@@ -91,6 +93,8 @@ func Serve(ctx context.Context, options *ServeOptions) error {
9193
return xerrors.Errorf("install terraform: %w", err)
9294
}
9395
options.BinaryPath = execPath
96+
} else {
97+
options.BinaryPath = absoluteBinary
9498
}
9599
}
96100
return provisionersdk.Serve(ctx, &server{

provisioner/terraform/serve_internal_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,43 @@ import (
1010
"testing"
1111

1212
"github.com/stretchr/testify/require"
13+
"golang.org/x/xerrors"
1314
)
1415

1516
// nolint:paralleltest
16-
func Test_getAbsoluteBinaryPath(t *testing.T) {
17+
func Test_absoluteBinaryPath(t *testing.T) {
1718
type args struct {
1819
ctx context.Context
1920
}
2021
tests := []struct {
2122
name string
2223
args args
2324
terraformVersion string
24-
expectedOk bool
25+
expectedErr error
2526
}{
2627
{
2728
name: "TestCorrectVersion",
2829
args: args{ctx: context.Background()},
2930
terraformVersion: "1.1.9",
30-
expectedOk: true,
31+
expectedErr: nil,
3132
},
3233
{
3334
name: "TestOldVersion",
3435
args: args{ctx: context.Background()},
3536
terraformVersion: "1.0.9",
36-
expectedOk: false,
37+
expectedErr: xerrors.Errorf("Terraform binary minor version mismatch."),
3738
},
3839
{
3940
name: "TestNewVersion",
4041
args: args{ctx: context.Background()},
4142
terraformVersion: "1.2.9",
42-
expectedOk: false,
43+
expectedErr: xerrors.Errorf("Terraform binary minor version mismatch."),
4344
},
4445
{
4546
name: "TestMalformedVersion",
4647
args: args{ctx: context.Background()},
4748
terraformVersion: "version",
48-
expectedOk: false,
49+
expectedErr: xerrors.Errorf("Terraform binary get version failed: Malformed version: version"),
4950
},
5051
}
5152
// nolint:paralleltest
@@ -80,16 +81,22 @@ func Test_getAbsoluteBinaryPath(t *testing.T) {
8081
t.Setenv("PATH", strings.Join([]string{tempDir, pathVariable}, ":"))
8182

8283
var expectedAbsoluteBinary string
83-
if tt.expectedOk {
84+
if tt.expectedErr == nil {
8485
expectedAbsoluteBinary = filepath.Join(tempDir, "terraform")
8586
}
8687

87-
actualAbsoluteBinary, actualOk := getAbsoluteBinaryPath(tt.args.ctx)
88+
actualAbsoluteBinary, actualErr := absoluteBinaryPath(tt.args.ctx)
8889
if actualAbsoluteBinary != expectedAbsoluteBinary {
8990
t.Errorf("getAbsoluteBinaryPath() absoluteBinaryPath, actual = %v, expected %v", actualAbsoluteBinary, expectedAbsoluteBinary)
9091
}
91-
if actualOk != tt.expectedOk {
92-
t.Errorf("getAbsoluteBinaryPath() ok, actual = %v, expected %v", actualOk, tt.expectedOk)
92+
if tt.expectedErr == nil {
93+
if actualErr != nil {
94+
t.Errorf("getAbsoluteBinaryPath() error, actual = %v, expected %v", actualErr.Error(), tt.expectedErr)
95+
}
96+
} else {
97+
if actualErr.Error() != tt.expectedErr.Error() {
98+
t.Errorf("getAbsoluteBinaryPath() error, actual = %v, expected %v", actualErr.Error(), tt.expectedErr.Error())
99+
}
93100
}
94101
})
95102
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy