@@ -3,38 +3,25 @@ package rego_test
33import (
44 "bytes"
55 "fmt"
6- "io/fs"
76 "os"
87 "path/filepath"
98 "strings"
109 "testing"
1110 "testing/fstest"
1211
13- "github.com/liamg/memoryfs"
1412 "github.com/stretchr/testify/assert"
1513 "github.com/stretchr/testify/require"
1614
15+ "github.com/aquasecurity/trivy/internal/testutil"
1716 "github.com/aquasecurity/trivy/pkg/iac/rego"
1817 "github.com/aquasecurity/trivy/pkg/iac/scanners/options"
1918 "github.com/aquasecurity/trivy/pkg/iac/severity"
2019 "github.com/aquasecurity/trivy/pkg/iac/types"
2120)
2221
23- func CreateFS (t * testing.T , files map [string ]string ) fs.FS {
24- memfs := memoryfs .New ()
25- for name , content := range files {
26- name := strings .TrimPrefix (name , "/" )
27- err := memfs .MkdirAll (filepath .Dir (name ), 0o700 )
28- require .NoError (t , err )
29- err = memfs .WriteFile (name , []byte (content ), 0o644 )
30- require .NoError (t , err )
31- }
32- return memfs
33- }
34-
3522func Test_RegoScanning_Deny (t * testing.T ) {
3623
37- srcFS := CreateFS (t , map [string ]string {
24+ srcFS := testutil . CreateFS (map [string ]string {
3825 "policies/test.rego" : `
3926# METADATA
4027# title: Custom policy
@@ -128,7 +115,7 @@ deny {
128115}
129116
130117func Test_RegoScanning_Allow (t * testing.T ) {
131- srcFS := CreateFS (t , map [string ]string {
118+ srcFS := testutil . CreateFS (map [string ]string {
132119 "policies/test.rego" : `# METADATA
133120# title: Custom policy
134121# description: Custom policy for testing
@@ -176,7 +163,7 @@ func Test_RegoScanning_WithRuntimeValues(t *testing.T) {
176163
177164 t .Setenv ("DEFSEC_RUNTIME_VAL" , "AOK" )
178165
179- srcFS := CreateFS (t , map [string ]string {
166+ srcFS := testutil . CreateFS (map [string ]string {
180167 "policies/test.rego" : `# METADATA
181168# title: Custom policy
182169# description: Custom policy for testing
@@ -220,7 +207,7 @@ deny_evil {
220207}
221208
222209func Test_RegoScanning_WithDenyMessage (t * testing.T ) {
223- srcFS := CreateFS (t , map [string ]string {
210+ srcFS := testutil . CreateFS (map [string ]string {
224211 "policies/test.rego" : `# METADATA
225212# title: Custom policy
226213# description: Custom policy for testing
@@ -267,7 +254,7 @@ deny[msg] {
267254}
268255
269256func Test_RegoScanning_WithDenyMetadata_ImpliedPath (t * testing.T ) {
270- srcFS := CreateFS (t , map [string ]string {
257+ srcFS := testutil . CreateFS (map [string ]string {
271258 "policies/test.rego" : `
272259# METADATA
273260# title: Custom policy
@@ -322,7 +309,7 @@ deny[res] {
322309}
323310
324311func Test_RegoScanning_WithDenyMetadata_PersistedPath (t * testing.T ) {
325- srcFS := CreateFS (t , map [string ]string {
312+ srcFS := testutil . CreateFS (map [string ]string {
326313 "policies/test.rego" : `
327314# METADATA
328315# title: Custom policy
@@ -378,7 +365,7 @@ deny[res] {
378365}
379366
380367func Test_RegoScanning_WithStaticMetadata (t * testing.T ) {
381- srcFS := CreateFS (t , map [string ]string {
368+ srcFS := testutil . CreateFS (map [string ]string {
382369 "policies/test.rego" : `
383370package defsec.test
384371
@@ -439,7 +426,7 @@ deny[res] {
439426}
440427
441428func Test_RegoScanning_WithMatchingInputSelector (t * testing.T ) {
442- srcFS := CreateFS (t , map [string ]string {
429+ srcFS := testutil . CreateFS (map [string ]string {
443430 "policies/test.rego" : `# METADATA
444431# title: Custom policy
445432# description: Custom policy for testing
@@ -487,7 +474,7 @@ deny {
487474}
488475
489476func Test_RegoScanning_WithNonMatchingInputSelector (t * testing.T ) {
490- srcFS := CreateFS (t , map [string ]string {
477+ srcFS := testutil . CreateFS (map [string ]string {
491478 "policies/test.rego" : `
492479package defsec.test
493480
@@ -521,7 +508,7 @@ deny {
521508
522509func Test_RegoScanning_NoTracingByDefault (t * testing.T ) {
523510
524- srcFS := CreateFS (t , map [string ]string {
511+ srcFS := testutil . CreateFS (map [string ]string {
525512 "policies/test.rego" : `# METADATA
526513# title: Custom policy
527514# description: Custom policy for testing
@@ -567,7 +554,7 @@ deny {
567554
568555func Test_RegoScanning_GlobalTracingEnabled (t * testing.T ) {
569556
570- srcFS := CreateFS (t , map [string ]string {
557+ srcFS := testutil . CreateFS (map [string ]string {
571558 "policies/test.rego" : `# METADATA
572559# title: Custom policy
573560# description: Custom policy for testing
@@ -617,7 +604,7 @@ deny {
617604
618605func Test_RegoScanning_PerResultTracingEnabled (t * testing.T ) {
619606
620- srcFS := CreateFS (t , map [string ]string {
607+ srcFS := testutil . CreateFS (map [string ]string {
621608 "policies/test.rego" : `# METADATA
622609# title: Custom policy
623610# description: Custom policy for testing
@@ -663,7 +650,7 @@ deny {
663650
664651func Test_dynamicMetadata (t * testing.T ) {
665652
666- srcFS := CreateFS (t , map [string ]string {
653+ srcFS := testutil . CreateFS (map [string ]string {
667654 "policies/test.rego" : `
668655package defsec.test
669656
@@ -695,7 +682,7 @@ deny {
695682
696683func Test_staticMetadata (t * testing.T ) {
697684
698- srcFS := CreateFS (t , map [string ]string {
685+ srcFS := testutil . CreateFS (map [string ]string {
699686 "policies/test.rego" : `
700687package defsec.test
701688
@@ -727,7 +714,7 @@ deny {
727714
728715func Test_annotationMetadata (t * testing.T ) {
729716
730- srcFS := CreateFS (t , map [string ]string {
717+ srcFS := testutil . CreateFS (map [string ]string {
731718 "policies/test.rego" : `# METADATA
732719# title: i am a title
733720# description: i am a description
@@ -782,7 +769,7 @@ deny {
782769
783770func Test_RegoScanning_WithInvalidInputSchema (t * testing.T ) {
784771
785- srcFS := CreateFS (t , map [string ]string {
772+ srcFS := testutil . CreateFS (map [string ]string {
786773 "policies/test.rego" : `# METADATA
787774# schemas:
788775# - input: schema["input"]
@@ -802,7 +789,7 @@ deny {
802789
803790func Test_RegoScanning_WithValidInputSchema (t * testing.T ) {
804791
805- srcFS := CreateFS (t , map [string ]string {
792+ srcFS := testutil . CreateFS (map [string ]string {
806793 "policies/test.rego" : `# METADATA
807794# schemas:
808795# - input: schema["input"]
@@ -821,7 +808,7 @@ deny {
821808}
822809
823810func Test_RegoScanning_WithFilepathToSchema (t * testing.T ) {
824- srcFS := CreateFS (t , map [string ]string {
811+ srcFS := testutil . CreateFS (map [string ]string {
825812 "policies/test.rego" : `# METADATA
826813# schemas:
827814# - input: schema["dockerfile"]
@@ -846,7 +833,7 @@ deny {
846833}
847834
848835func Test_RegoScanning_CustomData (t * testing.T ) {
849- srcFS := CreateFS (t , map [string ]string {
836+ srcFS := testutil . CreateFS (map [string ]string {
850837 "policies/test.rego" : `# METADATA
851838# title: Custom policy
852839# description: Custom policy for testing
@@ -871,7 +858,7 @@ deny {
871858` ,
872859 })
873860
874- dataFS := CreateFS (t , map [string ]string {
861+ dataFS := testutil . CreateFS (map [string ]string {
875862 "data/data.json" : `{
876863 "settings": {
877864 "DS123":{
@@ -899,7 +886,7 @@ deny {
899886}
900887
901888func Test_RegoScanning_InvalidFS (t * testing.T ) {
902- srcFS := CreateFS (t , map [string ]string {
889+ srcFS := testutil . CreateFS (map [string ]string {
903890 "policies/test.rego" : `# METADATA
904891# title: Custom policy
905892# description: Custom policy for testing
@@ -924,7 +911,7 @@ deny {
924911` ,
925912 })
926913
927- dataFS := CreateFS (t , map [string ]string {
914+ dataFS := testutil . CreateFS (map [string ]string {
928915 "data/data.json" : `{
929916 "settings": {
930917 "DS123":{
0 commit comments