From 25ad71787ad31d56c2f28be0cedf0da0a71a4f67 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 14 Oct 2020 10:29:17 -0700 Subject: [PATCH] test: minimize trivy db download tests to avoid api rate limit --- go.mod | 1 + pkg/extensions/search/cve/cve_test.go | 61 +++------------------------ 2 files changed, 8 insertions(+), 54 deletions(-) diff --git a/go.mod b/go.mod index b3ee67ff..20316b5f 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/smartystreets/goconvey v1.6.4 github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.6.1 + github.com/stretchr/testify v1.6.1 github.com/swaggo/http-swagger v0.0.0-20190614090009-c2865af9083e github.com/swaggo/swag v1.6.3 github.com/vektah/gqlparser/v2 v2.0.1 diff --git a/pkg/extensions/search/cve/cve_test.go b/pkg/extensions/search/cve/cve_test.go index d88390c4..cb7ad7b9 100644 --- a/pkg/extensions/search/cve/cve_test.go +++ b/pkg/extensions/search/cve/cve_test.go @@ -365,12 +365,10 @@ func makeHtpasswdFile() string { } func TestDownloadDB(t *testing.T) { - Convey("Download DB", t, func() { + Convey("Download DB passing invalid dir", t, func() { err := testSetup() So(err, ShouldBeNil) - err = cveinfo.UpdateCVEDb(dbDir, cve.Log) - So(err, ShouldBeNil) - + // Test Invalid dir download err = cveinfo.UpdateCVEDb("./testdata1", cve.Log) So(err, ShouldNotBeNil) }) @@ -462,6 +460,8 @@ func TestImageTag(t *testing.T) { func TestCVESearch(t *testing.T) { Convey("Test image vulenrability scanning", t, func() { + updateDuration, _ := time.ParseDuration("1h") + expectedDuration, _ := time.ParseDuration("2h") config := api.NewConfig() config.HTTP.Port = SecurePort1 htpasswdPath := makeHtpasswdFile() @@ -476,7 +476,7 @@ func TestCVESearch(t *testing.T) { defer os.RemoveAll(dbDir) c.Config.Storage.RootDirectory = dbDir cveConfig := &api.CVEConfig{ - UpdateInterval: 2, + UpdateInterval: updateDuration, } searchConfig := &api.SearchConfig{ CVE: cveConfig, @@ -508,6 +508,8 @@ func TestCVESearch(t *testing.T) { _ = c.Server.Shutdown(ctx) }() + So(c.Config.Extensions.Search.CVE.UpdateInterval, ShouldEqual, expectedDuration) + // without creds, should get access error resp, err := resty.R().Get(BaseURL1 + "/v2/") So(err, ShouldBeNil) @@ -684,52 +686,3 @@ func TestCVESearch(t *testing.T) { So(resp.StatusCode(), ShouldEqual, 200) }) } - -func TestCveConfig(t *testing.T) { - updateDuration, _ := time.ParseDuration("1h") - expectedDuration, _ := time.ParseDuration("2h") - - Convey("Make a new cve config", t, func() { - config := api.NewConfig() - config.HTTP.Port = SecurePort1 - cveConfig := &api.CVEConfig{ - UpdateInterval: updateDuration, - } - searchConfig := &api.SearchConfig{ - CVE: cveConfig, - } - config.Extensions = &api.ExtensionConfig{ - Search: searchConfig, - } - c := api.NewController(config) - dir, err := ioutil.TempDir("", "oci-repo-test") - if err != nil { - panic(err) - } - defer os.RemoveAll(dir) - c.Config.Storage.RootDirectory = dir - - go func() { - // this blocks - if err := c.Run(); err != nil { - return - } - }() - - // wait till ready - for { - _, err := resty.R().Get(BaseURL1) - if err == nil { - break - } - time.Sleep(100 * time.Millisecond) - } - - So(c.Config.Extensions.Search.CVE.UpdateInterval, ShouldEqual, expectedDuration) - - defer func() { - ctx := context.Background() - _ = c.Server.Shutdown(ctx) - }() - }) -}