-
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.
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
13 changed files
with
169 additions
and
146 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 |
---|---|---|
@@ -1,17 +1,14 @@ | ||
//go:build unix | ||
|
||
package plugin_test | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"context" | ||
"github.com/aquasecurity/trivy/pkg/clock" | ||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/utils/fsutils" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/sosedoff/gitkit" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
@@ -25,21 +22,6 @@ import ( | |
"github.com/aquasecurity/trivy/pkg/plugin" | ||
) | ||
|
||
func setupGitServer() (*httptest.Server, error) { | ||
service := gitkit.New(gitkit.Config{ | ||
Dir: "./testdata", | ||
AutoCreate: false, | ||
}) | ||
|
||
if err := service.Setup(); err != nil { | ||
return nil, err | ||
} | ||
|
||
ts := httptest.NewServer(service) | ||
|
||
return ts, nil | ||
} | ||
|
||
func TestManager_Run(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
// the test.sh script can't be run on windows so skipping | ||
|
@@ -200,129 +182,6 @@ func TestManager_Run(t *testing.T) { | |
} | ||
} | ||
|
||
func TestManager_Install(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
// the test.sh script can't be run on windows so skipping | ||
t.Skip("Test satisfied adequately by Linux tests") | ||
} | ||
|
||
ts, err := setupGitServer() | ||
require.NoError(t, err) | ||
defer ts.Close() | ||
|
||
wantPlugin := plugin.Plugin{ | ||
Name: "test_plugin", | ||
Repository: "github.com/aquasecurity/trivy-plugin-test", | ||
Version: "0.2.0", | ||
Summary: "test", | ||
Description: "test", | ||
Platforms: []plugin.Platform{ | ||
{ | ||
Selector: &plugin.Selector{ | ||
OS: "linux", | ||
Arch: "amd64", | ||
}, | ||
URI: "./test.sh", | ||
Bin: "./test.sh", | ||
}, | ||
}, | ||
Installed: plugin.Installed{ | ||
Platform: plugin.Selector{ | ||
OS: "linux", | ||
Arch: "amd64", | ||
}, | ||
}, | ||
} | ||
wantPluginWithVersion := wantPlugin | ||
wantPluginWithVersion.Version = "0.1.0" | ||
|
||
tests := []struct { | ||
name string | ||
pluginName string | ||
want plugin.Plugin | ||
wantFile string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "http", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "local path", | ||
pluginName: "testdata/test_plugin", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "git", | ||
pluginName: "git::" + ts.URL + "/test_plugin.git", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "with version", | ||
pluginName: "git::" + ts.URL + "/[email protected]", | ||
want: wantPluginWithVersion, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "via index", | ||
pluginName: "test", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "plugin not found", | ||
pluginName: "testdata/not_found", | ||
wantErr: "no such file or directory", | ||
}, | ||
{ | ||
name: "no plugin.yaml", | ||
pluginName: "testdata/no_yaml", | ||
wantErr: "file open error", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// The test plugin will be installed here | ||
dst := t.TempDir() | ||
t.Setenv("XDG_DATA_HOME", dst) | ||
|
||
// For plugin index | ||
fsutils.SetCacheDir("testdata") | ||
|
||
if tt.pluginName == "" { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
zr := zip.NewWriter(w) | ||
require.NoError(t, zr.AddFS(os.DirFS("testdata/test_plugin"))) | ||
require.NoError(t, zr.Close()) | ||
})) | ||
t.Cleanup(ts.Close) | ||
tt.pluginName = ts.URL + "/test_plugin.zip" | ||
} | ||
|
||
got, err := plugin.NewManager().Install(context.Background(), tt.pluginName, plugin.Options{ | ||
Platform: ftypes.Platform{ | ||
Platform: &v1.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
}, | ||
}) | ||
if tt.wantErr != "" { | ||
require.ErrorContains(t, err, tt.wantErr) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
|
||
assert.EqualExportedValues(t, tt.want, got) | ||
assert.FileExists(t, filepath.Join(dst, tt.wantFile)) | ||
}) | ||
} | ||
} | ||
|
||
func TestManager_Uninstall(t *testing.T) { | ||
ctx := clock.With(context.Background(), time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC)) | ||
pluginName := "test_plugin" | ||
|
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,155 @@ | ||
//go:build unix | ||
|
||
package plugin_test | ||
|
||
import ( | ||
"archive/zip" | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/sosedoff/gitkit" // Not work on Windows | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/plugin" | ||
"github.com/aquasecurity/trivy/pkg/utils/fsutils" | ||
) | ||
|
||
func setupGitServer() (*httptest.Server, error) { | ||
service := gitkit.New(gitkit.Config{ | ||
Dir: "./testdata", | ||
AutoCreate: false, | ||
}) | ||
|
||
if err := service.Setup(); err != nil { | ||
return nil, err | ||
} | ||
|
||
ts := httptest.NewServer(service) | ||
|
||
return ts, nil | ||
} | ||
|
||
func TestManager_Install(t *testing.T) { | ||
ts, err := setupGitServer() | ||
require.NoError(t, err) | ||
defer ts.Close() | ||
|
||
wantPlugin := plugin.Plugin{ | ||
Name: "test_plugin", | ||
Repository: "github.com/aquasecurity/trivy-plugin-test", | ||
Version: "0.2.0", | ||
Summary: "test", | ||
Description: "test", | ||
Platforms: []plugin.Platform{ | ||
{ | ||
Selector: &plugin.Selector{ | ||
OS: "linux", | ||
Arch: "amd64", | ||
}, | ||
URI: "./test.sh", | ||
Bin: "./test.sh", | ||
}, | ||
}, | ||
Installed: plugin.Installed{ | ||
Platform: plugin.Selector{ | ||
OS: "linux", | ||
Arch: "amd64", | ||
}, | ||
}, | ||
} | ||
wantPluginWithVersion := wantPlugin | ||
wantPluginWithVersion.Version = "0.1.0" | ||
|
||
tests := []struct { | ||
name string | ||
pluginName string | ||
want plugin.Plugin | ||
wantFile string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "http", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "local path", | ||
pluginName: "testdata/test_plugin", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "git", | ||
pluginName: "git::" + ts.URL + "/test_plugin.git", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "with version", | ||
pluginName: "git::" + ts.URL + "/[email protected]", | ||
want: wantPluginWithVersion, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "via index", | ||
pluginName: "test", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
}, | ||
{ | ||
name: "plugin not found", | ||
pluginName: "testdata/not_found", | ||
wantErr: "no such file or directory", | ||
}, | ||
{ | ||
name: "no plugin.yaml", | ||
pluginName: "testdata/no_yaml", | ||
wantErr: "file open error", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// The test plugin will be installed here | ||
dst := t.TempDir() | ||
t.Setenv("XDG_DATA_HOME", dst) | ||
|
||
// For plugin index | ||
fsutils.SetCacheDir("testdata") | ||
|
||
if tt.pluginName == "" { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
zr := zip.NewWriter(w) | ||
require.NoError(t, zr.AddFS(os.DirFS("testdata/test_plugin"))) | ||
require.NoError(t, zr.Close()) | ||
})) | ||
t.Cleanup(ts.Close) | ||
tt.pluginName = ts.URL + "/test_plugin.zip" | ||
} | ||
|
||
got, err := plugin.NewManager().Install(context.Background(), tt.pluginName, plugin.Options{ | ||
Platform: ftypes.Platform{ | ||
Platform: &v1.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
}, | ||
}) | ||
if tt.wantErr != "" { | ||
require.ErrorContains(t, err, tt.wantErr) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
|
||
assert.EqualExportedValues(t, tt.want, got) | ||
assert.FileExists(t, filepath.Join(dst, tt.wantFile)) | ||
}) | ||
} | ||
} |
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 @@ | ||
Unnamed repository; edit this file 'description' to name the repository. |
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,6 @@ | ||
# git ls-files --others --exclude-from=.git/info/exclude | ||
# Lines that start with '#' are comments. | ||
# For a project mostly in C, the following would be a good set of | ||
# exclude patterns (uncomment them if you want to use them): | ||
# *.[oa] | ||
# *~ |
Binary file removed
BIN
-345 Bytes
pkg/plugin/testdata/test_plugin.git/objects/0a/0290c4e05757f323e8307974b40f0282d69985
Binary file not shown.
Binary file removed
BIN
-91 Bytes
pkg/plugin/testdata/test_plugin.git/objects/2a/a2d6d72ba2d6f5c3ffd2ab0b98c4ec92f95e29
Binary file not shown.
Binary file added
BIN
+343 Bytes
pkg/plugin/testdata/test_plugin.git/objects/3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714
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
Binary file removed
BIN
-381 Bytes
pkg/plugin/testdata/test_plugin.git/objects/b0/219198cda98f46a2cef00561c00a251946e621
Binary file not shown.
Binary file added
BIN
+165 Bytes
pkg/plugin/testdata/test_plugin.git/objects/b8/fad1dc57e9b544da159fd2da39f82d4dcce34a
Binary file not shown.
Binary file added
BIN
+91 Bytes
pkg/plugin/testdata/test_plugin.git/objects/dd/c882ca64495a0498b3303be6a4ae58213f0485
Binary file not shown.
Binary file removed
BIN
-165 Bytes
pkg/plugin/testdata/test_plugin.git/objects/dd/e31a3d50b4279d4f84e38cd3fe154d884e4c09
Binary file not shown.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# pack-refs with: peeled fully-peeled sorted | ||
b0219198cda98f46a2cef00561c00a251946e621 refs/heads/main | ||
0a0290c4e05757f323e8307974b40f0282d69985 refs/tags/v0.1.0 | ||
b0219198cda98f46a2cef00561c00a251946e621 refs/tags/v0.2.0 | ||
96e190ae5bcaa3900e568608faf00f16c3ff2714 refs/heads/main | ||
3b147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf refs/tags/v0.1.0 | ||
96e190ae5bcaa3900e568608faf00f16c3ff2714 refs/tags/v0.2.0 |