Skip to content

Commit

Permalink
test: run git server only on linux
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed May 15, 2024
1 parent b7a2f07 commit ac28cbd
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 146 deletions.
145 changes: 2 additions & 143 deletions pkg/plugin/manager_test.go
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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
155 changes: 155 additions & 0 deletions pkg/plugin/manager_unix_test.go
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))
})
}
}
1 change: 1 addition & 0 deletions pkg/plugin/testdata/test_plugin.git/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
6 changes: 6 additions & 0 deletions pkg/plugin/testdata/test_plugin.git/info/exclude
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 not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��Ko�@���_1{��<���V�@��@���nf<31�!Ї}!Qw��HW:����Ց��=���އ�S
%'��u1���u�b�ai�%Z�(e�+�Թ�T`���֚k��.��Җ�.�pWzB>�ϗ��/�5YR���jZ~l���~����c[Q8CB`��{���ף�'��5��n�[g~Qn�7`�3V�&�'����I�K��xJ���ibfQ�ɍn�:6?�ϯ��si���n��fie�� ɷDX�M�wU7���kl\W����x��*d8+"���,�D ����}|����e�Iv����˅�6�-bV(�kPC����L�{Y�|�xm�mbz�y�i���-���ཎ( �U���d���B��
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions pkg/plugin/testdata/test_plugin.git/packed-refs
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

0 comments on commit ac28cbd

Please sign in to comment.