Skip to content

Commit 22b28f5

Browse files
nikpivkinsimar7
authored andcommitted
refactor(test): run Trivy once
Signed-off-by: Nikita Pivkin <nikita.pivkin@smartforce.io>
1 parent 06c0196 commit 22b28f5

File tree

2 files changed

+100
-59
lines changed

2 files changed

+100
-59
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test:
88

99
.PHONY: integration-test
1010
test-integration:
11-
go test -v -timeout 15m -tags=integration ./integration/...
11+
go test -v -timeout 5m -tags=integration ./integration/...
1212

1313
.PHONY: rego
1414
rego: fmt-rego test-rego

integration/check_examples_test.go

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
package integration
44

55
import (
6-
"fmt"
76
"io/fs"
87
"os"
98
"os/exec"
109
"path/filepath"
10+
"strconv"
11+
"strings"
1112
"testing"
1213

1314
"github.com/stretchr/testify/assert"
@@ -22,6 +23,49 @@ import (
2223

2324
func TestValidateCheckExamples(t *testing.T) {
2425
cacheDir := setupCache(t)
26+
targetDir := setupTarget(t)
27+
outputFile := filepath.Join(t.TempDir(), "report.json")
28+
29+
args := []string{
30+
"conf",
31+
"--skip-check-update",
32+
"--quiet",
33+
"--format", "json",
34+
"--output", outputFile,
35+
"--cache-dir", cacheDir,
36+
targetDir,
37+
}
38+
runTrivy(t, args)
39+
40+
report := readTrivyReport(t, outputFile)
41+
42+
verifyExamples(t, report, targetDir)
43+
}
44+
45+
func setupCache(t *testing.T) string {
46+
t.Helper()
47+
48+
cmd := exec.Command("make", "create-bundle")
49+
cmd.Dir = ".."
50+
require.NoError(t, cmd.Run())
51+
defer os.Remove("../bundle.tar.gz")
52+
53+
cacheDir := t.TempDir()
54+
55+
policyDir := filepath.Join(cacheDir, "policy", "content")
56+
require.NoError(t, os.MkdirAll(policyDir, os.ModePerm))
57+
58+
cmd = exec.Command("tar", "-zxf", "bundle.tar.gz", "-C", policyDir)
59+
cmd.Dir = ".."
60+
require.NoError(t, cmd.Run())
61+
62+
return cacheDir
63+
}
64+
65+
func setupTarget(t *testing.T) string {
66+
t.Helper()
67+
68+
targetDir := t.TempDir()
2569

2670
// TODO(nikpivkin): load examples from fs
2771
rego.LoadAndRegister()
@@ -32,85 +76,82 @@ func TestValidateCheckExamples(t *testing.T) {
3276
continue
3377
}
3478

35-
t.Run(r.AVDID, func(t *testing.T) {
36-
examples, path, err := examples.GetCheckExamples(r.Rule)
37-
require.NoError(t, err)
79+
examples, path, err := examples.GetCheckExamples(r.Rule)
80+
require.NoError(t, err)
3881

39-
if path == "" {
40-
return
41-
}
82+
if path == "" {
83+
continue
84+
}
4285

43-
for provider, providerExamples := range examples {
44-
validateExamples(t, providerExamples.Bad.ToStrings(), provider, cacheDir, r.AVDID, true)
45-
validateExamples(t, providerExamples.Good.ToStrings(), provider, cacheDir, r.AVDID, false)
46-
}
47-
})
86+
for provider, providerExamples := range examples {
87+
writeExamples(t, providerExamples.Bad.ToStrings(), provider, targetDir, r.AVDID, "bad")
88+
writeExamples(t, providerExamples.Good.ToStrings(), provider, targetDir, r.AVDID, "good")
89+
}
4890
}
91+
92+
return targetDir
4993
}
5094

51-
func validateExamples(t *testing.T, examples []string, provider, cacheDir, avdID string, expected bool) {
95+
func writeExamples(t *testing.T, examples []string, provider, cacheDir string, id string, typ string) {
5296
for i, example := range examples {
53-
fileName := fmt.Sprintf("test-%d%s", i, extensionByProvider(provider))
54-
t.Run(fileName, func(t *testing.T) {
55-
targetFile := filepath.Join(t.TempDir(), fileName)
56-
57-
require.NoError(t, os.WriteFile(targetFile, []byte(example), fs.ModePerm))
58-
59-
outputFile := filepath.Join(t.TempDir(), "report.json")
60-
61-
args := []string{
62-
"conf",
63-
"--skip-check-update",
64-
"--quiet",
65-
"--format", "json",
66-
"--output", outputFile,
67-
"--cache-dir", cacheDir,
68-
targetFile,
69-
}
70-
runTrivy(t, args)
71-
72-
report := readTrivyReport(t, outputFile)
73-
74-
assert.Equal(t, expected, reportContainsMisconfig(report, fileName, avdID))
75-
})
97+
name := "test" + extensionByProvider(provider)
98+
file := filepath.Join(cacheDir, id, provider, typ, strconv.Itoa(i), name)
99+
require.NoError(t, os.MkdirAll(filepath.Dir(file), fs.ModePerm))
100+
require.NoError(t, os.WriteFile(file, []byte(example), fs.ModePerm))
76101
}
77102
}
78103

79-
func setupCache(t *testing.T) string {
80-
t.Helper()
104+
func verifyExamples(t *testing.T, report types.Report, targetDir string) {
105+
got := getFailureIDs(report)
81106

82-
cmd := exec.Command("make", "create-bundle")
83-
cmd.Dir = ".."
107+
err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error {
108+
if err != nil {
109+
return err
110+
}
111+
if info.IsDir() {
112+
return nil
113+
}
84114

85-
require.NoError(t, cmd.Run())
86-
defer os.Remove("bundle.tar.gz")
115+
relPath, err := filepath.Rel(targetDir, path)
116+
require.NoError(t, err)
87117

88-
cacheDir := t.TempDir()
118+
parts := strings.Split(relPath, string(os.PathSeparator))
119+
require.Len(t, parts, 5)
89120

90-
policyDir := filepath.Join(cacheDir, "policy", "content")
91-
require.NoError(t, os.MkdirAll(policyDir, os.ModePerm))
121+
id, _, exampleType := parts[0], parts[1], parts[2]
92122

93-
cmd = exec.Command("tar", "-zxf", "bundle.tar.gz", "-C", policyDir)
94-
cmd.Dir = ".."
95-
require.NoError(t, cmd.Run())
123+
shouldBePresent := exampleType == "bad"
96124

97-
return cacheDir
125+
t.Run(relPath, func(t *testing.T) {
126+
if shouldBePresent {
127+
ids, exists := got[relPath]
128+
assert.True(t, exists)
129+
assert.Contains(t, ids, id)
130+
} else {
131+
ids, exists := got[relPath]
132+
if exists {
133+
assert.NotContains(t, ids, id)
134+
}
135+
}
136+
})
137+
return nil
138+
})
139+
140+
require.NoError(t, err)
98141
}
99142

100-
func reportContainsMisconfig(report types.Report, path string, id string) bool {
101-
for _, res := range report.Results {
102-
if res.Target != path {
103-
continue
104-
}
143+
func getFailureIDs(report types.Report) map[string][]string {
144+
ids := make(map[string][]string)
105145

106-
for _, misconf := range res.Misconfigurations {
107-
if misconf.AVDID == id && misconf.Status == types.MisconfStatusFailure {
108-
return true
146+
for _, result := range report.Results {
147+
for _, misconf := range result.Misconfigurations {
148+
if misconf.Status == types.MisconfStatusFailure {
149+
ids[result.Target] = append(ids[result.Target], misconf.AVDID)
109150
}
110151
}
111152
}
112153

113-
return false
154+
return ids
114155
}
115156

116157
func extensionByProvider(provider string) string {

0 commit comments

Comments
 (0)