3
3
package integration
4
4
5
5
import (
6
- "fmt"
7
6
"io/fs"
8
7
"os"
9
8
"os/exec"
10
9
"path/filepath"
10
+ "strconv"
11
+ "strings"
11
12
"testing"
12
13
13
14
"github.com/stretchr/testify/assert"
@@ -22,6 +23,49 @@ import (
22
23
23
24
func TestValidateCheckExamples (t * testing.T ) {
24
25
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 ()
25
69
26
70
// TODO(nikpivkin): load examples from fs
27
71
rego .LoadAndRegister ()
@@ -32,85 +76,82 @@ func TestValidateCheckExamples(t *testing.T) {
32
76
continue
33
77
}
34
78
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 )
38
81
39
- if path == "" {
40
- return
41
- }
82
+ if path == "" {
83
+ continue
84
+ }
42
85
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
+ }
48
90
}
91
+
92
+ return targetDir
49
93
}
50
94
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 ) {
52
96
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 ))
76
101
}
77
102
}
78
103
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 )
81
106
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
+ }
84
114
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 )
87
117
88
- cacheDir := t .TempDir ()
118
+ parts := strings .Split (relPath , string (os .PathSeparator ))
119
+ require .Len (t , parts , 5 )
89
120
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 ]
92
122
93
- cmd = exec .Command ("tar" , "-zxf" , "bundle.tar.gz" , "-C" , policyDir )
94
- cmd .Dir = ".."
95
- require .NoError (t , cmd .Run ())
123
+ shouldBePresent := exampleType == "bad"
96
124
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 )
98
141
}
99
142
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 )
105
145
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 )
109
150
}
110
151
}
111
152
}
112
153
113
- return false
154
+ return ids
114
155
}
115
156
116
157
func extensionByProvider (provider string ) string {
0 commit comments