Merge pull request #6375 from steven-zou/fix_global_search_502_issue

Fix global search 502 issue happened when chart repo is not enabled
This commit is contained in:
Steven Zou 2018-11-29 16:29:08 +08:00 committed by GitHub
commit 68b1b98f0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 27 deletions

View File

@ -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()
}

View File

@ -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
}
}