From 4f7fe1452f26a459549cd206a7c6cb1738d98ff2 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 7 May 2019 13:32:56 +0800 Subject: [PATCH] Add more unit test cases for docker hub adapter Add more unit test cases for docker hub adapter Signed-off-by: Wenkai Yin --- .../adapter/dockerhub/adapter_test.go | 31 +++++++++++++++++-- src/replication/adapter/dockerhub/client.go | 7 +++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/replication/adapter/dockerhub/adapter_test.go b/src/replication/adapter/dockerhub/adapter_test.go index 9ffc61f50..7d73da49d 100644 --- a/src/replication/adapter/dockerhub/adapter_test.go +++ b/src/replication/adapter/dockerhub/adapter_test.go @@ -4,13 +4,13 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + adp "github.com/goharbor/harbor/src/replication/adapter" "github.com/goharbor/harbor/src/replication/model" "github.com/stretchr/testify/assert" ) -// TODO add more unit test - const ( testUser = "" testPassword = "" @@ -35,6 +35,21 @@ func getAdapter(t *testing.T) adp.Adapter { return adapter } +func TestInfo(t *testing.T) { + adapter := &adapter{} + info, err := adapter.Info() + require.Nil(t, err) + require.Equal(t, 1, len(info.SupportedResourceTypes)) + assert.Equal(t, model.ResourceTypeImage, info.SupportedResourceTypes[0]) +} + +func TestListCandidateNamespaces(t *testing.T) { + adapter := &adapter{} + namespaces, err := adapter.listCandidateNamespaces("library/*") + require.Nil(t, err) + require.Equal(t, 1, len(namespaces)) + assert.Equal(t, "library", namespaces[0]) +} func TestListNamespaces(t *testing.T) { if testUser == "" { return @@ -50,3 +65,15 @@ func TestListNamespaces(t *testing.T) { fmt.Println(ns) } } + +func TestFetchImages(t *testing.T) { + ad := getAdapter(t) + adapter := ad.(*adapter) + _, err := adapter.FetchImages([]*model.Filter{ + { + Type: model.FilterTypeName, + Value: "goharbor/harbor-core", + }, + }) + require.Nil(t, err) +} diff --git a/src/replication/adapter/dockerhub/client.go b/src/replication/adapter/dockerhub/client.go index 9bba6ea63..1db72eca3 100644 --- a/src/replication/adapter/dockerhub/client.go +++ b/src/replication/adapter/dockerhub/client.go @@ -10,6 +10,7 @@ import ( "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/replication/model" + "github.com/goharbor/harbor/src/replication/util" ) // Client is a client to interact with DockerHub @@ -23,8 +24,10 @@ type Client struct { // NewClient creates a new DockerHub client. func NewClient(registry *model.Registry) (*Client, error) { client := &Client{ - host: registry.URL, - client: http.DefaultClient, + host: registry.URL, + client: &http.Client{ + Transport: util.GetHTTPTransport(false), + }, } // For anonymous access, no need to refresh token.