-
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.
feat: check if the same version installed
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
22 changed files
with
132 additions
and
61 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
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
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
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
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 |
---|---|---|
|
@@ -4,19 +4,24 @@ package plugin_test | |
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/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" | ||
|
||
"github.com/aquasecurity/trivy/pkg/clock" | ||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/log" | ||
"github.com/aquasecurity/trivy/pkg/plugin" | ||
"github.com/aquasecurity/trivy/pkg/utils/fsutils" | ||
) | ||
|
@@ -37,13 +42,20 @@ func setupGitServer() (*httptest.Server, error) { | |
} | ||
|
||
func TestManager_Install(t *testing.T) { | ||
ts, err := setupGitServer() | ||
gs, err := setupGitServer() | ||
require.NoError(t, err) | ||
defer ts.Close() | ||
t.Cleanup(gs.Close) | ||
|
||
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) | ||
|
||
wantPlugin := plugin.Plugin{ | ||
Name: "test_plugin", | ||
Repository: "github.com/aquasecurity/trivy-plugin-test", | ||
Repository: "testdata/test_plugin", | ||
Version: "0.2.0", | ||
Summary: "test", | ||
Description: "test", | ||
|
@@ -67,41 +79,75 @@ func TestManager_Install(t *testing.T) { | |
wantPluginWithVersion := wantPlugin | ||
wantPluginWithVersion.Version = "0.1.0" | ||
|
||
wantLogs := `2021-08-25T12:20:30Z INFO Installing the plugin... src="%s" | ||
2021-08-25T12:20:30Z INFO Plugin successfully installed name="test_plugin" version="%s" | ||
` | ||
|
||
tests := []struct { | ||
name string | ||
pluginName string | ||
installed *plugin.Plugin | ||
want plugin.Plugin | ||
wantFile string | ||
wantLogs string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "http", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
name: "http", | ||
pluginName: ts.URL + "/test_plugin.zip", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
wantLogs: fmt.Sprintf(wantLogs, ts.URL+"/test_plugin.zip", "0.2.0"), | ||
}, | ||
{ | ||
name: "local path", | ||
pluginName: "testdata/test_plugin", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
wantLogs: fmt.Sprintf(wantLogs, "testdata/test_plugin", "0.2.0"), | ||
}, | ||
{ | ||
name: "git", | ||
pluginName: "git::" + ts.URL + "/test_plugin.git", | ||
pluginName: "git::" + gs.URL + "/test_plugin.git", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.2.0"), | ||
}, | ||
{ | ||
name: "with version", | ||
pluginName: "git::" + ts.URL + "/[email protected]", | ||
pluginName: "git::" + gs.URL + "/[email protected]", | ||
want: wantPluginWithVersion, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.1.0"), | ||
}, | ||
{ | ||
name: "via index", | ||
pluginName: "test", | ||
pluginName: "test_plugin", | ||
want: wantPlugin, | ||
wantFile: ".trivy/plugins/test_plugin/test.sh", | ||
wantLogs: fmt.Sprintf(wantLogs, "testdata/test_plugin", "0.2.0"), | ||
}, | ||
{ | ||
name: "installed", | ||
pluginName: "test_plugin", | ||
installed: &plugin.Plugin{ | ||
Name: "test_plugin", | ||
Repository: "testdata/test_plugin", | ||
Version: "0.2.0", | ||
}, | ||
want: wantPlugin, | ||
wantLogs: "2021-08-25T12:20:30Z INFO The plugin is already installed name=\"test_plugin\"\n", | ||
}, | ||
{ | ||
name: "different version installed", | ||
pluginName: "[email protected]", | ||
installed: &plugin.Plugin{ | ||
Name: "test_plugin", | ||
Repository: "testdata/test_plugin", | ||
Version: "0.1.0", | ||
}, | ||
want: wantPlugin, | ||
wantLogs: fmt.Sprintf(wantLogs, "testdata/test_plugin", "0.2.0"), | ||
}, | ||
{ | ||
name: "plugin not found", | ||
|
@@ -124,17 +170,16 @@ func TestManager_Install(t *testing.T) { | |
// 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" | ||
if tt.installed != nil { | ||
setupInstalledPlugin(t, dst, *tt.installed) | ||
} | ||
|
||
got, err := plugin.NewManager().Install(context.Background(), tt.pluginName, plugin.Options{ | ||
var gotLogs bytes.Buffer | ||
logger := log.New(log.NewHandler(&gotLogs, nil)) | ||
|
||
ctx := clock.With(context.Background(), time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC)) | ||
|
||
got, err := plugin.NewManager(plugin.WithLogger(logger)).Install(ctx, tt.pluginName, plugin.Options{ | ||
Platform: ftypes.Platform{ | ||
Platform: &v1.Platform{ | ||
Architecture: "amd64", | ||
|
@@ -149,7 +194,10 @@ func TestManager_Install(t *testing.T) { | |
assert.NoError(t, err) | ||
|
||
assert.EqualExportedValues(t, tt.want, got) | ||
assert.FileExists(t, filepath.Join(dst, tt.wantFile)) | ||
if tt.wantFile != "" { | ||
assert.FileExists(t, filepath.Join(dst, tt.wantFile)) | ||
} | ||
assert.Equal(t, tt.wantLogs, gotLogs.String()) | ||
}) | ||
} | ||
} |
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 added
BIN
+147 Bytes
pkg/plugin/testdata/test_plugin.git/objects/08/6aefb548a1150b765d1e163a5e542fc80bd660
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
pkg/plugin/testdata/test_plugin.git/objects/0a/e1413e3807e024dbc7de4129d12bdcae7dea61
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
-91 Bytes
pkg/plugin/testdata/test_plugin.git/objects/2e/cea228bf881042eb74d3b691c5c6abfb2f7ee4
Binary file not shown.
Binary file removed
BIN
-343 Bytes
pkg/plugin/testdata/test_plugin.git/objects/3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf
Binary file not shown.
Binary file added
BIN
+344 Bytes
pkg/plugin/testdata/test_plugin.git/objects/92/9b4718db99b64a38b4e8c3ec8e673976821c08
Binary file not shown.
2 changes: 0 additions & 2 deletions
2
pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714
This file was deleted.
Oops, something went wrong.
Binary file added
BIN
+90 Bytes
pkg/plugin/testdata/test_plugin.git/objects/a0/82cf7b16998b8f048e7d2bf8207d9525688a9f
Binary file not shown.
Binary file removed
BIN
-165 Bytes
pkg/plugin/testdata/test_plugin.git/objects/b0/b5089c438d32daafa5a10acb7836280f31fd1b
Binary file not shown.
Binary file removed
BIN
-165 Bytes
pkg/plugin/testdata/test_plugin.git/objects/b8/fad1dc57e9b544da159fd2da39f82d4dcce34a
Binary file not shown.
Binary file added
BIN
+380 Bytes
pkg/plugin/testdata/test_plugin.git/objects/d7/8abde66b1d35bdac65402f0e2cddf3a96cd377
Binary file not shown.
Binary file added
BIN
+91 Bytes
pkg/plugin/testdata/test_plugin.git/objects/dc/135ebfc7f680300c981029184a492bbdfa6db3
Binary file not shown.
Binary file removed
BIN
-91 Bytes
pkg/plugin/testdata/test_plugin.git/objects/dd/c882ca64495a0498b3303be6a4ae58213f0485
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 | ||
96e190ae5bcaa3900e568608faf00f16c3ff2714 refs/heads/main | ||
3b147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf refs/tags/v0.1.0 | ||
96e190ae5bcaa3900e568608faf00f16c3ff2714 refs/tags/v0.2.0 | ||
d78abde66b1d35bdac65402f0e2cddf3a96cd377 refs/heads/main | ||
929b4718db99b64a38b4e8c3ec8e673976821c08 refs/tags/v0.1.0 | ||
d78abde66b1d35bdac65402f0e2cddf3a96cd377 refs/tags/v0.2.0 |
Empty file.
Empty file.
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