-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save golden to files add `update` flag use `pip` dir
- Loading branch information
1 parent
a06bb75
commit 60cf5a4
Showing
3 changed files
with
70 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,40 +3,47 @@ | |
package integration | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/utils/fsutils" | ||
) | ||
|
||
func TestPlugin(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
plugin string | ||
golden string | ||
name string | ||
plugin string | ||
pluginArgs string | ||
golden string | ||
}{ | ||
{ | ||
// TODO add plugin flags | ||
name: "count plugin installed from `index`", | ||
plugin: "[email protected]", | ||
golden: "Number of vulnerabilities: 5", | ||
golden: "testdata/count-0.2.0-plugin.txt.golden", | ||
}, | ||
{ | ||
name: "count plugin installed from github archive", | ||
plugin: "https://github.com/aquasecurity/trivy-plugin-count/archive/refs/tags/v0.1.0.zip", | ||
golden: "Number of vulnerabilities: 5", | ||
name: "count plugin installed from github archive", | ||
plugin: "https://github.com/aquasecurity/trivy-plugin-count/archive/refs/tags/v0.1.0.zip", | ||
pluginArgs: "--published-before=2020-01-01", | ||
golden: "testdata/count-0.1.0-plugin-with-before-flag.txt.golden", | ||
}, | ||
} | ||
|
||
// Set up testing DB | ||
cacheDir := initDB(t) | ||
tempStdOut := setTempStdout(t) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
t.Setenv("XDG_DATA_HOME", tmpDir) | ||
// We can overwrite stdout for `_default_Manager` only once. | ||
// So we need to clear the temporary stdout file before each test case. | ||
clearFile(t, tempStdOut) | ||
|
||
t.Setenv("XDG_DATA_HOME", t.TempDir()) | ||
|
||
// Install plugin | ||
err := execute([]string{ | ||
|
@@ -46,26 +53,59 @@ func TestPlugin(t *testing.T) { | |
}) | ||
require.NoError(t, err) | ||
|
||
// Overwrite Stdout to get output of plugin | ||
tmpFile, err := os.Create(filepath.Join(tmpDir, "tmp.txt")) | ||
// Get list of plugins | ||
err = execute([]string{ | ||
"plugin", | ||
"list", | ||
}) | ||
require.NoError(t, err) | ||
os.Stdout = tmpFile | ||
|
||
// Run Trivy with plugin as output | ||
err = execute([]string{ | ||
args := []string{ | ||
"--cache-dir", | ||
cacheDir, | ||
"fs", | ||
"-f", | ||
"json", | ||
"-o", | ||
"plugin=count", | ||
"testdata/fixtures/repo/gomod", | ||
}) | ||
"testdata/fixtures/repo/pip", | ||
} | ||
|
||
got, err := os.ReadFile(tmpFile.Name()) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.golden, strings.TrimSpace(string(got))) | ||
if tt.pluginArgs != "" { | ||
args = append(args, "--output-plugin-arg", tt.pluginArgs) | ||
} | ||
|
||
err = execute(args) | ||
|
||
if *update { | ||
fsutils.CopyFile(tempStdOut.Name(), tt.golden) | ||
} | ||
|
||
compareRawFiles(t, tt.golden, tempStdOut.Name()) | ||
}) | ||
} | ||
} | ||
|
||
func setTempStdout(t *testing.T) *os.File { | ||
tmpFile := filepath.Join(t.TempDir(), "output.txt") | ||
f, err := os.Create(tmpFile) | ||
require.NoError(t, err) | ||
|
||
// Overwrite Stdout to get output of plugin | ||
defaultStdout := os.Stdout | ||
os.Stdout = f | ||
t.Cleanup(func() { | ||
os.Stdout = defaultStdout | ||
f.Close() | ||
}) | ||
return f | ||
} | ||
|
||
func clearFile(t *testing.T, file *os.File) { | ||
_, err := file.Seek(0, io.SeekStart) | ||
require.NoError(t, err) | ||
|
||
_, err = file.Write([]byte{}) | ||
require.NoError(t, err) | ||
} |
5 changes: 5 additions & 0 deletions
5
integration/testdata/count-0.1.0-plugin-with-before-flag.txt.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Installed Plugins: | ||
Name: count | ||
Version: 0.1.0 | ||
|
||
Number of vulnerabilities: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Installed Plugins: | ||
Name: count | ||
Version: 0.2.0 | ||
|
||
Number of vulnerabilities: 2 |