Add more unit test cases for docker hub adapter

Add more unit test cases for docker hub adapter

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2019-05-07 13:32:56 +08:00
parent 09c5f9cfc8
commit 4f7fe1452f
2 changed files with 34 additions and 4 deletions

View File

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/stretchr/testify/require"
adp "github.com/goharbor/harbor/src/replication/adapter" adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TODO add more unit test
const ( const (
testUser = "" testUser = ""
testPassword = "" testPassword = ""
@ -35,6 +35,21 @@ func getAdapter(t *testing.T) adp.Adapter {
return 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) { func TestListNamespaces(t *testing.T) {
if testUser == "" { if testUser == "" {
return return
@ -50,3 +65,15 @@ func TestListNamespaces(t *testing.T) {
fmt.Println(ns) 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)
}

View File

@ -10,6 +10,7 @@ import (
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/util"
) )
// Client is a client to interact with DockerHub // Client is a client to interact with DockerHub
@ -24,7 +25,9 @@ type Client struct {
func NewClient(registry *model.Registry) (*Client, error) { func NewClient(registry *model.Registry) (*Client, error) {
client := &Client{ client := &Client{
host: registry.URL, host: registry.URL,
client: http.DefaultClient, client: &http.Client{
Transport: util.GetHTTPTransport(false),
},
} }
// For anonymous access, no need to refresh token. // For anonymous access, no need to refresh token.