diff --git a/src/chartserver/reverse_proxy_test.go b/src/chartserver/reverse_proxy_test.go new file mode 100644 index 000000000..44cfa0964 --- /dev/null +++ b/src/chartserver/reverse_proxy_test.go @@ -0,0 +1,55 @@ +package chartserver + +import ( + "net/http" + "testing" +) + +// Test the URL rewrite function +func TestURLRewrite(t *testing.T) { + req, err := createRequest(http.MethodGet, "/api/chartrepo/health") + if err != nil { + t.Fatal(err) + } + rewriteURLPath(req) + if req.URL.Path != "/health" { + t.Fatalf("Expect url format %s but got %s", "/health", req.URL.Path) + } + + req, err = createRequest(http.MethodGet, "/api/chartrepo/library/charts") + if err != nil { + t.Fatal(err) + } + rewriteURLPath(req) + if req.URL.Path != "/api/library/charts" { + t.Fatalf("Expect url format %s but got %s", "/api/library/charts", req.URL.Path) + } + + req, err = createRequest(http.MethodPost, "/api/chartrepo/charts") + if err != nil { + t.Fatal(err) + } + rewriteURLPath(req) + if req.URL.Path != "/api/library/charts" { + t.Fatalf("Expect url format %s but got %s", "/api/library/charts", req.URL.Path) + } + + req, err = createRequest(http.MethodGet, "/chartrepo/library/index.yaml") + if err != nil { + t.Fatal(err) + } + rewriteURLPath(req) + if req.URL.Path != "/library/index.yaml" { + t.Fatalf("Expect url format %s but got %s", "/library/index.yaml", req.URL.Path) + } +} + +func createRequest(method string, url string) (*http.Request, error) { + req, err := http.NewRequest(method, url, nil) + if err != nil { + return nil, err + } + req.RequestURI = url + + return req, nil +} diff --git a/src/chartserver/utils_test.go b/src/chartserver/utils_test.go index fba83321c..7fb276e7f 100644 --- a/src/chartserver/utils_test.go +++ b/src/chartserver/utils_test.go @@ -1,6 +1,8 @@ package chartserver import ( + "encoding/json" + "os" "strings" "testing" ) @@ -40,3 +42,61 @@ func TestParseRedisConfig(t *testing.T) { } } } + +func TestGetCacheConfig(t *testing.T) { + // case 1: no cache set + cacheConf, err := getCacheConfig() + if err != nil || cacheConf != nil { + t.Fatal("expect nil cache config and nil error but got non-nil one when parsing empty cache settings") + } + + // case 2: unknown cache type + os.Setenv(cacheDriverENVKey, "unknown") + _, err = getCacheConfig() + if err == nil { + t.Fatal("expect non-nil error but got nil one when parsing unknown cache type") + } + + // case 3: in memory cache type + os.Setenv(cacheDriverENVKey, cacheDriverMem) + memCacheConf, err := getCacheConfig() + if err != nil || memCacheConf == nil || memCacheConf.DriverType != cacheDriverMem { + t.Fatal("expect in memory cache driver but got invalid one") + } + + // case 4: wrong redis cache conf + os.Setenv(cacheDriverENVKey, cacheDriverRedis) + os.Setenv(redisENVKey, "") + _, err = getCacheConfig() + if err == nil { + t.Fatal("expect non-nil error but got nil one when parsing a invalid redis cache conf") + } + + // case 5: redis cache conf + os.Setenv(redisENVKey, "redis:6379,100,Passw0rd,1") + redisConf, err := getCacheConfig() + if err != nil { + t.Fatalf("expect nil error but got non-nil one when parsing valid redis conf") + } + + if redisConf == nil || redisConf.DriverType != cacheDriverRedis { + t.Fatal("expect redis cache driver but got invalid one") + } + + conf := make(map[string]string) + if err = json.Unmarshal([]byte(redisConf.Config), &conf); err != nil { + t.Fatal(err) + } + + if v, ok := conf["conn"]; !ok { + t.Fatal("expect 'conn' filed in the parsed conf but got nothing") + } else { + if v != "redis:6379" { + t.Fatalf("expect %s but got %s", "redis:6379", v) + } + } + + // clear + os.Unsetenv(cacheDriverENVKey) + os.Unsetenv(redisENVKey) +} diff --git a/src/core/api/chart_repository.go b/src/core/api/chart_repository.go index 59e0df9fc..4f069d347 100644 --- a/src/core/api/chart_repository.go +++ b/src/core/api/chart_repository.go @@ -12,7 +12,7 @@ import ( "strings" "github.com/goharbor/harbor/src/common" - "github.com/goharbor/harbor/src/ui/label" + "github.com/goharbor/harbor/src/core/label" "github.com/goharbor/harbor/src/chartserver" hlog "github.com/goharbor/harbor/src/common/utils/log"