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"
"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)
}

View File

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