From e7ffaecca5c5b29e2d10d3e6e767a1f68c2487a3 Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Wed, 28 Nov 2018 17:28:51 +0800 Subject: [PATCH] Fix global search 502 issue happened when chart repo is not enabled Signed-off-by: Steven Zou --- src/core/api/search.go | 28 +++++++++++------- src/core/api/search_test.go | 58 +++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/core/api/search.go b/src/core/api/search.go index 43dbdb459..eb821ee0e 100644 --- a/src/core/api/search.go +++ b/src/core/api/search.go @@ -24,6 +24,7 @@ import ( "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/utils" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/core/config" coreutils "github.com/goharbor/harbor/src/core/utils" "k8s.io/helm/cmd/helm/search" ) @@ -124,21 +125,26 @@ func (s *SearchAPI) Get() { s.CustomAbort(http.StatusInternalServerError, "") } - if searchHandler == nil { - searchHandler = chartController.SearchChart - } - - chartResults, err := searchHandler(keyword, proNames) - if err != nil { - log.Errorf("failed to filter charts: %v", err) - s.CustomAbort(http.StatusInternalServerError, err.Error()) - } - result := &searchResult{ Project: projectResult, Repository: repositoryResult, - Chart: chartResults, } + + // If enable chart repository + if config.WithChartMuseum() { + if searchHandler == nil { + searchHandler = chartController.SearchChart + } + + chartResults, err := searchHandler(keyword, proNames) + if err != nil { + log.Errorf("failed to filter charts: %v", err) + s.CustomAbort(http.StatusInternalServerError, err.Error()) + } + + result.Chart = chartResults + } + s.Data["json"] = result s.ServeJSON() } diff --git a/src/core/api/search_test.go b/src/core/api/search_test.go index ae3101a78..5f44cdd7c 100644 --- a/src/core/api/search_test.go +++ b/src/core/api/search_test.go @@ -16,10 +16,14 @@ package api import ( "fmt" "net/http" + "os" "testing" + "github.com/goharbor/harbor/src/core/config" + "github.com/goharbor/harbor/src/common" "github.com/goharbor/harbor/src/common/models" + "github.com/goharbor/harbor/src/common/utils/test" "k8s.io/helm/cmd/helm/search" "github.com/goharbor/harbor/src/common/dao" @@ -178,21 +182,43 @@ func TestSearch(t *testing.T) { _, exist = repositories["search-2/hello-world"] assert.True(t, exist) - // Search chart - err = handleAndParse(&testingRequest{ - method: http.MethodGet, - url: "/api/search", - queryStruct: struct { - Keyword string `url:"q"` - }{ - Keyword: "harbor", - }, - credential: sysAdmin, - }, result) - require.Nil(t, err) - require.Equal(t, 1, len(result.Chart)) - require.Equal(t, "library/harbor", result.Chart[0].Name) + currentAdminServerURL, ok := os.LookupEnv("ADMINSERVER_URL") + if ok { + chartSettings := map[string]interface{}{ + common.WithChartMuseum: true, + } + adminServer, err := test.NewAdminserver(chartSettings) + if err != nil { + t.Fatal(nil) + } + defer adminServer.Close() - // Restore chart search handler - searchHandler = nil + if err := config.InitByURL(adminServer.URL); err != nil { + t.Fatal(err) + } + defer func() { + // reset config + if err := config.InitByURL(currentAdminServerURL); err != nil { + t.Error(err) + } + }() + + // Search chart + err = handleAndParse(&testingRequest{ + method: http.MethodGet, + url: "/api/search", + queryStruct: struct { + Keyword string `url:"q"` + }{ + Keyword: "harbor", + }, + credential: sysAdmin, + }, result) + require.Nil(t, err) + require.Equal(t, 1, len(result.Chart)) + require.Equal(t, "library/harbor", result.Chart[0].Name) + + // Restore chart search handler + searchHandler = nil + } }