-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add integration plugin tests #7299
Merged
knqyf263
merged 5 commits into
aquasecurity:main
from
DmitriyLewen:test-integration-plugin
Sep 2, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
24c7959
test(integration): add plugin tests
DmitriyLewen a06bb75
mage: add test:plugin
DmitriyLewen 60cf5a4
refactor:
DmitriyLewen eeda1c8
ci: add plugin tests to `test` workflow
DmitriyLewen 33cf9d5
refactor: run plugin tests with `integration` tests
DmitriyLewen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//go:build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/utils/fsutils" | ||
) | ||
|
||
func TestPlugin(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
plugin string | ||
pluginArgs string | ||
golden string | ||
}{ | ||
{ | ||
name: "count plugin installed from `index`", | ||
plugin: "[email protected]", | ||
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", | ||
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) { | ||
// 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{ | ||
"plugin", | ||
"install", | ||
tt.plugin, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Get list of plugins | ||
err = execute([]string{ | ||
"plugin", | ||
"list", | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Run Trivy with plugin as output | ||
args := []string{ | ||
"--cache-dir", | ||
cacheDir, | ||
"fs", | ||
"-f", | ||
"json", | ||
"-o", | ||
"plugin=count", | ||
"testdata/fixtures/repo/pip", | ||
} | ||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if passing io.Writer here?
trivy/integration/integration_test.go
Line 284 in 88ba460
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use your idea.
But we use
os.Stdout
inplugin
package.I used NewManager instead of defaultManager in
app.go
to useStdout
fromcobra
.It works, but there is problem with case when plugin is output.
We need to add output for plugin in Options:
trivy/pkg/flag/options.go
Lines 511 to 533 in 0c6687d
i am not sure that we need to do that now.
I would leave this workaround until the tests become more numerous and complex, which would require these changes.
But if you want to do it - tell me and I will continue the research
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, we can improve it later.