Skip to content

Commit 8ddde20

Browse files
committed
load tf vars for e2e
1 parent 71c12ca commit 8ddde20

4 files changed

Lines changed: 55 additions & 44 deletions

File tree

internal/verify/exec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ func (e WorkingExecutable) Init(ctx context.Context) error {
5656
return e.TF.Init(ctx, tfexec.Upgrade(true))
5757
}
5858

59-
func (e WorkingExecutable) Plan(ctx context.Context, outPath string) (bool, error) {
60-
changes, err := e.TF.Plan(ctx, tfexec.Out(outPath))
59+
func (e WorkingExecutable) Plan(ctx context.Context, outPath string, opts ...tfexec.PlanOption) (bool, error) {
60+
opts = append(opts, tfexec.Out(outPath))
61+
changes, err := e.TF.Plan(ctx, opts...)
6162
return changes, err
6263
}
6364

64-
func (e WorkingExecutable) Apply(ctx context.Context) ([]byte, error) {
65+
func (e WorkingExecutable) Apply(ctx context.Context, opts ...tfexec.ApplyOption) ([]byte, error) {
6566
var out bytes.Buffer
66-
err := e.TF.ApplyJSON(ctx, &out)
67+
err := e.TF.ApplyJSON(ctx, &out, opts...)
6768
return out.Bytes(), err
6869
}
6970

preview.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"io/fs"
88
"log/slog"
9-
"path/filepath"
10-
"strings"
119

1210
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
1311
"github.com/hashicorp/hcl/v2"
@@ -85,7 +83,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagn
8583
}
8684
}()
8785

88-
varFiles, err := tfVarFiles("", dir)
86+
varFiles, err := tfvars.TFVarFiles("", dir)
8987
if err != nil {
9088
return nil, hcl.Diagnostics{
9189
{
@@ -197,33 +195,3 @@ func (i Input) RichParameterValue(key string) (string, bool) {
197195
p, ok := i.ParameterValues[key]
198196
return p, ok
199197
}
200-
201-
// tfVarFiles extracts any .tfvars files from the given directory.
202-
// TODO: Test nested directories and how that should behave.
203-
func tfVarFiles(path string, dir fs.FS) ([]string, error) {
204-
dp := "."
205-
entries, err := fs.ReadDir(dir, dp)
206-
if err != nil {
207-
return nil, fmt.Errorf("read dir %q: %w", dp, err)
208-
}
209-
210-
files := make([]string, 0)
211-
for _, entry := range entries {
212-
if entry.IsDir() {
213-
subD, err := fs.Sub(dir, entry.Name())
214-
if err != nil {
215-
return nil, fmt.Errorf("sub dir %q: %w", entry.Name(), err)
216-
}
217-
newFiles, err := tfVarFiles(filepath.Join(path, entry.Name()), subD)
218-
if err != nil {
219-
return nil, err
220-
}
221-
files = append(files, newFiles...)
222-
}
223-
224-
if filepath.Ext(entry.Name()) == ".tfvars" || strings.HasSuffix(entry.Name(), ".tfvars.json") {
225-
files = append(files, filepath.Join(path, entry.Name()))
226-
}
227-
}
228-
return files, nil
229-
}

previewe2e_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/hashicorp/terraform-exec/tfexec"
1314
"github.com/stretchr/testify/require"
1415

1516
"github.com/coder/preview"
1617
"github.com/coder/preview/internal/verify"
18+
"github.com/coder/preview/tfvars"
1719
"github.com/coder/preview/types"
1820
)
1921

@@ -102,11 +104,11 @@ func Test_VerifyE2E(t *testing.T) {
102104

103105
entryWrkPath := t.TempDir()
104106

105-
for _, tfexec := range tfexecs {
106-
tfexec := tfexec
107+
for _, tfexecutable := range tfexecs {
108+
tfexecutable := tfexecutable
107109

108-
t.Run(tfexec.Version, func(t *testing.T) {
109-
wp := filepath.Join(entryWrkPath, tfexec.Version)
110+
t.Run(tfexecutable.Version, func(t *testing.T) {
111+
wp := filepath.Join(entryWrkPath, tfexecutable.Version)
110112
err := os.MkdirAll(wp, 0755)
111113
require.NoError(t, err, "creating working dir")
112114

@@ -118,17 +120,27 @@ func Test_VerifyE2E(t *testing.T) {
118120
err = verify.CopyTFFS(wp, subFS)
119121
require.NoError(t, err, "copying test data to working dir")
120122

121-
exe, err := tfexec.WorkingDir(wp)
123+
exe, err := tfexecutable.WorkingDir(wp)
122124
require.NoError(t, err, "creating working executable")
123125

124126
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
125127
defer cancel()
126128
err = exe.Init(ctx)
127129
require.NoError(t, err, "terraform init")
128130

131+
tfVarFiles, err := tfvars.TFVarFiles("", subFS)
132+
require.NoError(t, err, "loading tfvars files")
133+
134+
planOpts := make([]tfexec.PlanOption, 0)
135+
applyOpts := make([]tfexec.ApplyOption, 0)
136+
for _, varFile := range tfVarFiles {
137+
planOpts = append(planOpts, tfexec.VarFile(varFile))
138+
applyOpts = append(applyOpts, tfexec.VarFile(varFile))
139+
}
140+
129141
planOutFile := "tfplan"
130142
planOutPath := filepath.Join(wp, planOutFile)
131-
_, err = exe.Plan(ctx, planOutPath)
143+
_, err = exe.Plan(ctx, planOutPath, planOpts...)
132144
require.NoError(t, err, "terraform plan")
133145

134146
plan, err := exe.ShowPlan(ctx, planOutPath)
@@ -141,7 +153,7 @@ func Test_VerifyE2E(t *testing.T) {
141153
err = os.WriteFile(filepath.Join(wp, "plan.json"), pd, 0644)
142154
require.NoError(t, err, "writing plan.json")
143155

144-
_, err = exe.Apply(ctx)
156+
_, err = exe.Apply(ctx, applyOpts...)
145157
require.NoError(t, err, "terraform apply")
146158

147159
state, err := exe.Show(ctx)

tfvars/load.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ import (
1313
"github.com/zclconf/go-cty/cty"
1414
)
1515

16+
// TFVarFiles extracts any .tfvars files from the given directory.
17+
// TODO: Test nested directories and how that should behave.
18+
func TFVarFiles(path string, dir fs.FS) ([]string, error) {
19+
dp := "."
20+
entries, err := fs.ReadDir(dir, dp)
21+
if err != nil {
22+
return nil, fmt.Errorf("read dir %q: %w", dp, err)
23+
}
24+
25+
files := make([]string, 0)
26+
for _, entry := range entries {
27+
if entry.IsDir() {
28+
subD, err := fs.Sub(dir, entry.Name())
29+
if err != nil {
30+
return nil, fmt.Errorf("sub dir %q: %w", entry.Name(), err)
31+
}
32+
newFiles, err := TFVarFiles(filepath.Join(path, entry.Name()), subD)
33+
if err != nil {
34+
return nil, err
35+
}
36+
files = append(files, newFiles...)
37+
}
38+
39+
if filepath.Ext(entry.Name()) == ".tfvars" || strings.HasSuffix(entry.Name(), ".tfvars.json") {
40+
files = append(files, filepath.Join(path, entry.Name()))
41+
}
42+
}
43+
return files, nil
44+
}
45+
1646
func LoadTFVars(srcFS fs.FS, filenames []string) (map[string]cty.Value, error) {
1747
combinedVars := make(map[string]cty.Value)
1848

0 commit comments

Comments
 (0)