Skip to content

Commit d6678cf

Browse files
committed
fx trivy tests
1 parent b96fe38 commit d6678cf

File tree

2 files changed

+302
-0
lines changed

2 files changed

+302
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
package tools
2+
3+
import (
4+
"testing"
5+
6+
"codacy/cli-v2/tools"
7+
"codacy/cli-v2/tools/trivy"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func testTrivyConfig(t *testing.T, configuration tools.ToolConfiguration, expected string) {
13+
actual := trivy.CreateTrivyConfig(configuration)
14+
assert.Equal(t, expected, actual)
15+
}
16+
17+
func TestCreateTrivyConfigEmptyConfig(t *testing.T) {
18+
testTrivyConfig(t,
19+
tools.ToolConfiguration{},
20+
`severity:
21+
- LOW
22+
- MEDIUM
23+
- HIGH
24+
- CRITICAL
25+
26+
scan:
27+
scanners:
28+
- vuln
29+
- secret
30+
`)
31+
}
32+
33+
func TestCreateTrivyConfigAllEnabled(t *testing.T) {
34+
testTrivyConfig(t,
35+
tools.ToolConfiguration{
36+
PatternsConfiguration: []tools.PatternConfiguration{
37+
{
38+
PatternId: "Trivy_vulnerability_minor",
39+
ParameterConfigurations: []tools.PatternParameterConfiguration{
40+
{
41+
Name: "enabled",
42+
Value: "true",
43+
},
44+
},
45+
},
46+
{
47+
PatternId: "Trivy_vulnerability_medium",
48+
ParameterConfigurations: []tools.PatternParameterConfiguration{
49+
{
50+
Name: "enabled",
51+
Value: "true",
52+
},
53+
},
54+
},
55+
{
56+
PatternId: "Trivy_vulnerability",
57+
ParameterConfigurations: []tools.PatternParameterConfiguration{
58+
{
59+
Name: "enabled",
60+
Value: "true",
61+
},
62+
},
63+
},
64+
{
65+
PatternId: "Trivy_secret",
66+
ParameterConfigurations: []tools.PatternParameterConfiguration{
67+
{
68+
Name: "enabled",
69+
Value: "true",
70+
},
71+
},
72+
},
73+
},
74+
},
75+
`severity:
76+
- LOW
77+
- MEDIUM
78+
- HIGH
79+
- CRITICAL
80+
81+
scan:
82+
scanners:
83+
- vuln
84+
- secret
85+
`)
86+
}
87+
88+
func TestCreateTrivyConfigNoLow(t *testing.T) {
89+
testTrivyConfig(t,
90+
tools.ToolConfiguration{
91+
PatternsConfiguration: []tools.PatternConfiguration{
92+
{
93+
PatternId: "Trivy_vulnerability_minor",
94+
ParameterConfigurations: []tools.PatternParameterConfiguration{
95+
{
96+
Name: "enabled",
97+
Value: "false",
98+
},
99+
},
100+
},
101+
},
102+
},
103+
`severity:
104+
- MEDIUM
105+
- HIGH
106+
- CRITICAL
107+
108+
scan:
109+
scanners:
110+
- vuln
111+
- secret
112+
`)
113+
}
114+
115+
func TestCreateTrivyConfigOnlyHigh(t *testing.T) {
116+
testTrivyConfig(t,
117+
tools.ToolConfiguration{
118+
PatternsConfiguration: []tools.PatternConfiguration{
119+
{
120+
PatternId: "Trivy_vulnerability_minor",
121+
ParameterConfigurations: []tools.PatternParameterConfiguration{
122+
{
123+
Name: "enabled",
124+
Value: "false",
125+
},
126+
},
127+
},
128+
{
129+
PatternId: "Trivy_vulnerability_medium",
130+
ParameterConfigurations: []tools.PatternParameterConfiguration{
131+
{
132+
Name: "enabled",
133+
Value: "false",
134+
},
135+
},
136+
},
137+
{
138+
PatternId: "Trivy_secret",
139+
ParameterConfigurations: []tools.PatternParameterConfiguration{
140+
{
141+
Name: "enabled",
142+
Value: "false",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
`severity:
149+
- HIGH
150+
- CRITICAL
151+
152+
scan:
153+
scanners:
154+
- vuln
155+
`)
156+
}
157+
158+
func TestCreateTrivyConfigNoVulnerabilities(t *testing.T) {
159+
testTrivyConfig(t,
160+
tools.ToolConfiguration{
161+
PatternsConfiguration: []tools.PatternConfiguration{
162+
{
163+
PatternId: "Trivy_vulnerability_minor",
164+
ParameterConfigurations: []tools.PatternParameterConfiguration{
165+
{
166+
Name: "enabled",
167+
Value: "false",
168+
},
169+
},
170+
},
171+
{
172+
PatternId: "Trivy_vulnerability_medium",
173+
ParameterConfigurations: []tools.PatternParameterConfiguration{
174+
{
175+
Name: "enabled",
176+
Value: "false",
177+
},
178+
},
179+
},
180+
{
181+
PatternId: "Trivy_vulnerability",
182+
ParameterConfigurations: []tools.PatternParameterConfiguration{
183+
{
184+
Name: "enabled",
185+
Value: "false",
186+
},
187+
},
188+
},
189+
},
190+
},
191+
`severity:
192+
193+
scan:
194+
scanners:
195+
- vuln
196+
- secret
197+
`)
198+
}
199+
200+
func TestCreateTrivyConfigOnlySecretsLow(t *testing.T) {
201+
testTrivyConfig(t,
202+
tools.ToolConfiguration{
203+
PatternsConfiguration: []tools.PatternConfiguration{
204+
{
205+
PatternId: "Trivy_vulnerability_minor",
206+
ParameterConfigurations: []tools.PatternParameterConfiguration{
207+
{
208+
Name: "enabled",
209+
Value: "true",
210+
},
211+
},
212+
},
213+
{
214+
PatternId: "Trivy_vulnerability_medium",
215+
ParameterConfigurations: []tools.PatternParameterConfiguration{
216+
{
217+
Name: "enabled",
218+
Value: "false",
219+
},
220+
},
221+
},
222+
{
223+
PatternId: "Trivy_vulnerability",
224+
ParameterConfigurations: []tools.PatternParameterConfiguration{
225+
{
226+
Name: "enabled",
227+
Value: "false",
228+
},
229+
},
230+
},
231+
},
232+
},
233+
`severity:
234+
- LOW
235+
236+
scan:
237+
scanners:
238+
- vuln
239+
- secret
240+
`)
241+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"codacy/cli-v2/tools/trivy"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestRunTrivyToFile(t *testing.T) {
17+
homeDirectory, err := os.UserHomeDir()
18+
if err != nil {
19+
log.Fatal(err.Error())
20+
}
21+
currentDirectory, err := os.Getwd()
22+
if err != nil {
23+
log.Fatal(err.Error())
24+
}
25+
26+
testDirectory := currentDirectory
27+
tempResultFile := filepath.Join(os.TempDir(), "trivy.sarif")
28+
defer os.Remove(tempResultFile)
29+
30+
repositoryToAnalyze := filepath.Join(testDirectory, "src")
31+
32+
trivyBinary := filepath.Join(homeDirectory, ".cache/codacy/tools/[email protected]/trivy")
33+
34+
err = trivy.RunTrivy(repositoryToAnalyze, trivyBinary, nil, tempResultFile, "sarif")
35+
if err != nil {
36+
t.Fatalf("Failed to run trivy: %v", err)
37+
}
38+
39+
// Check if the output file was created
40+
obtainedSarifBytes, err := os.ReadFile(tempResultFile)
41+
if err != nil {
42+
t.Fatalf("Failed to read output file: %v", err)
43+
}
44+
obtainedSarif := string(obtainedSarifBytes)
45+
filePrefix := "file://" + currentDirectory + "/"
46+
fmt.Println(filePrefix)
47+
actualSarif := strings.ReplaceAll(obtainedSarif, filePrefix, "")
48+
49+
// Normalize paths in the SARIF output
50+
actualSarif = strings.ReplaceAll(actualSarif, `"uri": "src/"`, `"uri": "testdata/repositories/trivy/src/"`)
51+
52+
// Read the expected SARIF
53+
expectedSarifFile := filepath.Join(testDirectory, "expected.sarif")
54+
expectedSarifBytes, err := os.ReadFile(expectedSarifFile)
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
expectedSarif := strings.TrimSpace(string(expectedSarifBytes))
59+
60+
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
61+
}

0 commit comments

Comments
 (0)