Merge pull request #7667 from ywk253100/190507_dockerhub_ut

Add more unit test cases for docker hub adapter
This commit is contained in:
Wenkai Yin 2019-05-08 16:35:06 +08:00 committed by GitHub
commit c557cb3ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.