Skip to content

Commit

Permalink
add a snapshot ScanFS test
Browse files Browse the repository at this point in the history
  • Loading branch information
simar7 committed Feb 29, 2024
1 parent 46bf2fc commit 9f6b70f
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion pkg/iac/scanners/terraformplan/snapshot/scaner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snapshot

import (
"bytes"
"context"
"os"
"path/filepath"
Expand Down Expand Up @@ -28,7 +29,6 @@ func initScanner(opts ...options.ScannerOption) *Scanner {
}

func TestScanner_Scan(t *testing.T) {

tests := []struct {
name string
dir string
Expand Down Expand Up @@ -76,3 +76,59 @@ func TestScanner_Scan(t *testing.T) {
})
}
}

func Test_ScanFS(t *testing.T) {
t.Parallel()

tests := []struct {
dir string
expectedIDs []string
expectedDir string
expectedErr error
}{
{
dir: "just-resource",
expectedIDs: []string{"ID001"},
expectedDir: "main.tf",
},
{
dir: "with-local-module",
expectedIDs: []string{"ID001"},
expectedDir: filepath.Join("modules", "ec2", "main.tf"),
},
{
dir: "with-remote-module",
expectedIDs: []string{"ID001"},
expectedDir: filepath.Join("terraform-aws-modules", "s3-bucket", "aws", "main.tf"),
},
}

for _, tc := range tests {
t.Run(tc.dir, func(t *testing.T) {
fs := os.DirFS("testdata")

debugLog := bytes.NewBuffer([]byte{})
scanner := New(
options.ScannerWithDebug(debugLog),
options.ScannerWithPolicyDirs(filepath.Join(tc.dir, "checks")),
options.ScannerWithPolicyFilesystem(fs),
options.ScannerWithRegoOnly(true),
options.ScannerWithPolicyNamespaces("user"),
options.ScannerWithEmbeddedLibraries(false),
options.ScannerWithEmbeddedPolicies(false),
options.ScannerWithRegoErrorLimits(0),
)

results, err := scanner.ScanFS(context.TODO(), fs, filepath.Join(tc.dir, "tfplan"))
require.NoError(t, err)
require.Len(t, results, 1)

failed := results.GetFailed()
assert.Len(t, failed, 1)

occurrences := failed[0].Occurrences()
assert.Equal(t, tc.expectedDir, occurrences[0].Filename, tc.dir)
})
}

}

0 comments on commit 9f6b70f

Please sign in to comment.