From ac28cbd7b7109e974a7e0e098e858e3831fc57a4 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Tue, 14 May 2024 16:10:50 +0400 Subject: [PATCH] test: run git server only on linux Signed-off-by: knqyf263 --- pkg/plugin/manager_test.go | 145 +--------------- pkg/plugin/manager_unix_test.go | 155 ++++++++++++++++++ .../testdata/test_plugin.git/description | 1 + .../testdata/test_plugin.git/info/exclude | 6 + .../0a/0290c4e05757f323e8307974b40f0282d69985 | Bin 345 -> 0 bytes .../2a/a2d6d72ba2d6f5c3ffd2ab0b98c4ec92f95e29 | Bin 91 -> 0 bytes .../3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf | Bin 0 -> 343 bytes .../96/e190ae5bcaa3900e568608faf00f16c3ff2714 | 2 + .../b0/219198cda98f46a2cef00561c00a251946e621 | Bin 381 -> 0 bytes .../b8/fad1dc57e9b544da159fd2da39f82d4dcce34a | Bin 0 -> 165 bytes .../dd/c882ca64495a0498b3303be6a4ae58213f0485 | Bin 0 -> 91 bytes .../dd/e31a3d50b4279d4f84e38cd3fe154d884e4c09 | Bin 165 -> 0 bytes .../testdata/test_plugin.git/packed-refs | 6 +- 13 files changed, 169 insertions(+), 146 deletions(-) create mode 100644 pkg/plugin/manager_unix_test.go create mode 100644 pkg/plugin/testdata/test_plugin.git/description create mode 100644 pkg/plugin/testdata/test_plugin.git/info/exclude delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/0a/0290c4e05757f323e8307974b40f0282d69985 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/2a/a2d6d72ba2d6f5c3ffd2ab0b98c4ec92f95e29 create mode 100644 pkg/plugin/testdata/test_plugin.git/objects/3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf create mode 100644 pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/b0/219198cda98f46a2cef00561c00a251946e621 create mode 100644 pkg/plugin/testdata/test_plugin.git/objects/b8/fad1dc57e9b544da159fd2da39f82d4dcce34a create mode 100644 pkg/plugin/testdata/test_plugin.git/objects/dd/c882ca64495a0498b3303be6a4ae58213f0485 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/dd/e31a3d50b4279d4f84e38cd3fe154d884e4c09 diff --git a/pkg/plugin/manager_test.go b/pkg/plugin/manager_test.go index e42191f7fdac..c831f77ed6f8 100644 --- a/pkg/plugin/manager_test.go +++ b/pkg/plugin/manager_test.go @@ -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 + "/test_plugin.git@v0.1.0", - 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" diff --git a/pkg/plugin/manager_unix_test.go b/pkg/plugin/manager_unix_test.go new file mode 100644 index 000000000000..f9f8cfecd008 --- /dev/null +++ b/pkg/plugin/manager_unix_test.go @@ -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 + "/test_plugin.git@v0.1.0", + 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)) + }) + } +} diff --git a/pkg/plugin/testdata/test_plugin.git/description b/pkg/plugin/testdata/test_plugin.git/description new file mode 100644 index 000000000000..498b267a8c78 --- /dev/null +++ b/pkg/plugin/testdata/test_plugin.git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/pkg/plugin/testdata/test_plugin.git/info/exclude b/pkg/plugin/testdata/test_plugin.git/info/exclude new file mode 100644 index 000000000000..a5196d1be8fb --- /dev/null +++ b/pkg/plugin/testdata/test_plugin.git/info/exclude @@ -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] +# *~ diff --git a/pkg/plugin/testdata/test_plugin.git/objects/0a/0290c4e05757f323e8307974b40f0282d69985 b/pkg/plugin/testdata/test_plugin.git/objects/0a/0290c4e05757f323e8307974b40f0282d69985 deleted file mode 100644 index 9245bbdf4797fe42e6b012167e68e9bfa385e753..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 345 zcmV-f0jBHw(Sw_ zn!}YLMT9TE(|cayqIx22{RRb9kd=ufYZ~NuiRZyUhj$(Qw=v)6ry_@sqh_Y<`Y;R~ z7`nDUjpCJg;{e8j@5X%POh*Mn8QwT8i0HSAkM+(gW5J7dBg>ylZ_$f$qjd7GZF4F$ z7Lc4x3?~Qh_-P$H-%I3Z`HeZA+#E)NDt@%ymGsy)himSz&Z3d&qTgd(s1m~!{K%96 z9BH7Zp~G1xwkK#c?<^U0Yie!EXM5ADbXj|ulU*p|8dFZ!>72WkONB|a$_&Q4mlekl rfFep0y*W#Wp7o%)EFAkV^AVK6i>Ff%bxD99;I&&<=SOw7$;xcgYjHeieTT>qBGJ(vH9 x`gZvFZ~~Q?o0=+=q!yRx6=yJbvYI|{e?E2T$zZl~8Zz@a&Tfvb0RX3r9+GWoD^LIc diff --git a/pkg/plugin/testdata/test_plugin.git/objects/3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf b/pkg/plugin/testdata/test_plugin.git/objects/3b/147ae5ffafba7c1d833b4f4bc8ac6a22a8c9bf new file mode 100644 index 0000000000000000000000000000000000000000..90ef65cdcbb0f3ea5eb0221c0917783cada503ae GIT binary patch literal 343 zcmV-d0jU0X0hN$TZ-X!pgnRa{@I7j3V;~^4RUuHI1S=#EQf`g|&WlnAdH(uBqTYJx zNGr{L((FpZ%c58}P#)lirp_|BTyjm5I8$X=R~V9YZ9xbki%exQ%M?w*BuBEQ0Cs3r zWevAq)g_ly0-t}~n<&_N*XL#N3h@9d14TwUf;}W71X|j@YqEa_#kbh61$^ve+Kxxz zbn3#X=TIYxC-#j47-QO5yhiPOh z0CqGqFWXwNlGKeTH;mN(%nzU}j#9Z0_@LJj-jpS2l}}Df4y7 psQ{$xIxeVt%;uJtJNka@oP_ZL?seGI`l}3ZM19;x;0LO$j3F)Mq{ILK literal 0 HcmV?d00001 diff --git a/pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714 b/pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714 new file mode 100644 index 000000000000..2bc33b30b6af --- /dev/null +++ b/pkg/plugin/testdata/test_plugin.git/objects/96/e190ae5bcaa3900e568608faf00f16c3ff2714 @@ -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�� \ No newline at end of file diff --git a/pkg/plugin/testdata/test_plugin.git/objects/b0/219198cda98f46a2cef00561c00a251946e621 b/pkg/plugin/testdata/test_plugin.git/objects/b0/219198cda98f46a2cef00561c00a251946e621 deleted file mode 100644 index 28d704514c7a0c09e14b7a1f40775f1619322b5e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 381 zcmV-@0fPQ`0hN$TZ=*mEgma!>F?TD@Jb27VyQ?^W0mmQ{!zSSdlK~9a*ciX~^$Uq| z%VkSysrr+u)hf|!x0?>CnBR4+l#oav6GB*;F@}*sWU4D4G}Sm3s?d@&OG&0nsepab z%3TLhf(S=Kkx125J)?w5Mv=~SB~?&{2qPaF=S&63sVkZmR=dM3BN~OjzTIQKO*ZvZ zG}}KA>sZy8!ZZ#iNI?h;ba=0m{~gTt`EirO`%zDp?fP)NcHr8z{aF~jSf3og2>Wh? zYG*bo7{>HQw-nTGmal&+uZ*x4RwK(FN^jZIXR~zjOR+naJPSmgG1JKbY~SVw&-Y?A zTwRk15}ye(CN&zPUyA$0wuWo&us($&GpOGu2Cie%#eQh10FE?R^yhVVHmv#V!W}DU z7uUF28Fq)QlibbiqHPMVcrRa9@28k$(dK4d$^KlmAx9VQV5U&Cg8 z<@3O(I44_1U$2IA!kGI>C(+9M49SsT03(pGM%U7&+5{3{(Ujg_KpAVK6i>Ff%bxD99;I&&<=SOw7$;*zxP)o$!}iU2ciazjVv; xhpz9L$6i2X=BB0!C8@poQBMNje*4#FT1ggx^Vo8C)XV@%{_3{bS2@Fxq6eR~0odz)`}W>(pt;Jn;U zUOFRS$cFMg+VVs{uw-H&%pn~Do|w0yxDIZTuOi9lk}w}rPPiZA*D*FX+f5=2zJ|?w z;q$<#I44_1U$2IA!kGI>C(+9M2+5IP03(pGM%U7&+5{3{(UjgwvH6m`ak*r9#~3tBvnrH diff --git a/pkg/plugin/testdata/test_plugin.git/packed-refs b/pkg/plugin/testdata/test_plugin.git/packed-refs index 1690e340878c..539634191094 100644 --- a/pkg/plugin/testdata/test_plugin.git/packed-refs +++ b/pkg/plugin/testdata/test_plugin.git/packed-refs @@ -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