Skip to content

Commit 5d3221d

Browse files
committed
Cleanup code from comments
1 parent 59992f1 commit 5d3221d

File tree

7 files changed

+72
-32
lines changed

7 files changed

+72
-32
lines changed

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ coverage:
1616
status:
1717
project:
1818
default:
19-
target: 80%
19+
target: 75%
2020
informational: yes
2121

2222
ignore:

provisioner/terraform/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func convertVariableToParameter(variable *tfconfig.Variable) (*proto.ParameterSc
4949
}
5050

5151
if len(variable.Validations) > 0 && variable.Validations[0].Condition != nil {
52-
// Only use the first validation for now.
52+
// Terraform can contain multiple validation blocks, but it's used sparingly
53+
// from what it appears.
5354
validation := variable.Validations[0]
5455
filedata, err := os.ReadFile(variable.Pos.Filename)
5556
if err != nil {

provisioner/terraform/parse_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ import (
1414
)
1515

1616
func TestParse(t *testing.T) {
17+
t.Parallel()
18+
19+
// Create an in-memory provisioner to communicate with.
1720
client, server := provisionersdk.TransportPipe()
18-
defer client.Close()
19-
defer server.Close()
2021
ctx, cancelFunc := context.WithCancel(context.Background())
21-
defer cancelFunc()
22+
t.Cleanup(func() {
23+
_ = client.Close()
24+
_ = server.Close()
25+
cancelFunc()
26+
})
2227
go func() {
23-
err := Serve(ctx, &provisionersdk.ServeOptions{
24-
Transport: server,
28+
err := Serve(ctx, &ServeOptions{
29+
ServeOptions: &provisionersdk.ServeOptions{
30+
Transport: server,
31+
},
2532
})
2633
require.NoError(t, err)
2734
}()
@@ -32,7 +39,7 @@ func TestParse(t *testing.T) {
3239
Files map[string]string
3340
Response *proto.Parse_Response
3441
}{{
35-
Name: "basic",
42+
Name: "single-variable",
3643
Files: map[string]string{
3744
"main.tf": `variable "A" {
3845
description = "Testing!"
@@ -45,7 +52,7 @@ func TestParse(t *testing.T) {
4552
}},
4653
},
4754
}, {
48-
Name: "default-value",
55+
Name: "default-variable-value",
4956
Files: map[string]string{
5057
"main.tf": `variable "A" {
5158
default = "wow"
@@ -58,7 +65,7 @@ func TestParse(t *testing.T) {
5865
}},
5966
},
6067
}, {
61-
Name: "validation",
68+
Name: "variable-validation",
6269
Files: map[string]string{
6370
"main.tf": `variable "A" {
6471
validation {
@@ -73,7 +80,11 @@ func TestParse(t *testing.T) {
7380
}},
7481
},
7582
}} {
83+
tc := tc
7684
t.Run(tc.Name, func(t *testing.T) {
85+
t.Parallel()
86+
87+
// Write all files to the temporary test directory.
7788
directory := t.TempDir()
7889
for path, content := range tc.Files {
7990
err := os.WriteFile(filepath.Join(directory, path), []byte(content), 0644)
@@ -85,9 +96,9 @@ func TestParse(t *testing.T) {
8596
})
8697
require.NoError(t, err)
8798

99+
// Ensure the want and got are equivalent!
88100
want, err := json.Marshal(tc.Response)
89101
require.NoError(t, err)
90-
91102
got, err := json.Marshal(response)
92103
require.NoError(t, err)
93104

provisioner/terraform/provision.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (t *terraform) Provision(ctx context.Context, request *proto.Provision_Requ
2727
if err != nil {
2828
return nil, xerrors.Errorf("get terraform version: %w", err)
2929
}
30-
if !version.GreaterThanOrEqual(t.minimumVersion) {
31-
return nil, xerrors.Errorf("terraform version %q is too old. required >= %q", version.String(), t.minimumVersion.String())
30+
if !version.GreaterThanOrEqual(minimumTerraformVersion) {
31+
return nil, xerrors.Errorf("terraform version %q is too old. required >= %q", version.String(), minimumTerraformVersion.String())
3232
}
3333

3434
err = terraform.Init(ctx)

provisioner/terraform/provision_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ import (
1414
)
1515

1616
func TestProvision(t *testing.T) {
17+
t.Parallel()
18+
1719
client, server := provisionersdk.TransportPipe()
18-
defer client.Close()
19-
defer server.Close()
2020
ctx, cancelFunc := context.WithCancel(context.Background())
21-
defer cancelFunc()
21+
t.Cleanup(func() {
22+
_ = client.Close()
23+
_ = server.Close()
24+
cancelFunc()
25+
})
2226
go func() {
23-
err := Serve(ctx, &provisionersdk.ServeOptions{
24-
Transport: server,
27+
err := Serve(ctx, &ServeOptions{
28+
ServeOptions: &provisionersdk.ServeOptions{
29+
Transport: server,
30+
},
2531
})
2632
require.NoError(t, err)
2733
}()
@@ -66,13 +72,16 @@ func TestProvision(t *testing.T) {
6672
}},
6773
},
6874
}, {
69-
Name: "invalid-source",
75+
Name: "invalid-sourcecode",
7076
Files: map[string]string{
7177
"main.tf": `a`,
7278
},
7379
Error: true,
7480
}} {
81+
tc := tc
7582
t.Run(tc.Name, func(t *testing.T) {
83+
t.Parallel()
84+
7685
directory := t.TempDir()
7786
for path, content := range tc.Files {
7887
err := os.WriteFile(filepath.Join(directory, path), []byte(content), 0644)

provisioner/terraform/serve.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,42 @@ import (
99
"golang.org/x/xerrors"
1010
)
1111

12+
var (
13+
// The minimum version of Terraform supported by the provisioner.
14+
// Validation came out in 0.13.0, which was released August 10th, 2020.
15+
// https://www.hashicorp.com/blog/announcing-hashicorp-terraform-0-13
16+
minimumTerraformVersion = func() *version.Version {
17+
v, err := version.NewSemver("0.13.0")
18+
if err != nil {
19+
panic(err)
20+
}
21+
return v
22+
}()
23+
)
24+
25+
type ServeOptions struct {
26+
*provisionersdk.ServeOptions
27+
28+
// BinaryPath specifies the "terraform" binary to use.
29+
// If omitted, the $PATH will attempt to find it.
30+
BinaryPath string
31+
}
32+
1233
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
13-
func Serve(ctx context.Context, options *provisionersdk.ServeOptions) error {
14-
minimumVersion, err := version.NewSemver("0.13.0")
15-
if err != nil {
16-
return xerrors.New("parse minimum version")
17-
}
18-
binaryPath, err := exec.LookPath("terraform")
19-
if err != nil {
20-
return xerrors.Errorf("terraform binary not found: %w", err)
34+
func Serve(ctx context.Context, options *ServeOptions) error {
35+
if options.BinaryPath == "" {
36+
binaryPath, err := exec.LookPath("terraform")
37+
if err != nil {
38+
return xerrors.Errorf("terraform binary not found: %w", err)
39+
}
40+
options.BinaryPath = binaryPath
2141
}
42+
2243
return provisionersdk.Serve(ctx, &terraform{
23-
binaryPath: binaryPath,
24-
minimumVersion: minimumVersion,
25-
}, options)
44+
binaryPath: options.BinaryPath,
45+
}, options.ServeOptions)
2646
}
2747

2848
type terraform struct {
29-
binaryPath string
30-
minimumVersion *version.Version
49+
binaryPath string
3150
}

provisioner/terraform/terraform.tfstate

Whitespace-only changes.

0 commit comments

Comments
 (0)