mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-23 16:11:24 +01:00
Add unit test for azure adapter
Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
parent
2097e928d0
commit
c305103e05
@ -78,7 +78,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
||||
}
|
||||
|
||||
// HealthCheck checks health status of a registry
|
||||
func (a adapter) HealthCheck() (model.HealthStatus, error) {
|
||||
func (a *adapter) HealthCheck() (model.HealthStatus, error) {
|
||||
err := a.PingGet()
|
||||
if err != nil {
|
||||
return model.Unhealthy, nil
|
||||
|
@ -1 +1,128 @@
|
||||
package azurecr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/utils/test"
|
||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
)
|
||||
|
||||
func getMockAdapter(t *testing.T, hasCred, health bool) (*adapter, *httptest.Server) {
|
||||
server := test.NewServer(
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/v2/_catalog",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"repositories": ["test1"]}`))
|
||||
},
|
||||
},
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/v2/{repo}/tags/list",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"name": "test1", "tags": ["latest"]}`))
|
||||
},
|
||||
},
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/v2/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.Method, r.URL)
|
||||
if health {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
},
|
||||
},
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.Method, r.URL)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
},
|
||||
},
|
||||
&test.RequestHandlerMapping{
|
||||
Method: http.MethodPost,
|
||||
Pattern: "/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.Method, r.URL)
|
||||
if buf, e := ioutil.ReadAll(&io.LimitedReader{R: r.Body, N: 80}); e == nil {
|
||||
fmt.Println("\t", string(buf))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
},
|
||||
},
|
||||
)
|
||||
registry := &model.Registry{
|
||||
Type: model.RegistryTypeAzureAcr,
|
||||
URL: server.URL,
|
||||
}
|
||||
if hasCred {
|
||||
registry.Credential = &model.Credential{
|
||||
AccessKey: "acr",
|
||||
AccessSecret: "pwd",
|
||||
}
|
||||
}
|
||||
|
||||
factory, err := adp.GetFactory(model.RegistryTypeAzureAcr)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, factory)
|
||||
a, err := factory(registry)
|
||||
|
||||
assert.Nil(t, err)
|
||||
return a.(*adapter), server
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
a, s := getMockAdapter(t, true, true)
|
||||
defer s.Close()
|
||||
info, err := a.Info()
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, info)
|
||||
assert.EqualValues(t, 1, len(info.SupportedResourceTypes))
|
||||
assert.EqualValues(t, model.ResourceTypeImage, info.SupportedResourceTypes[0])
|
||||
}
|
||||
|
||||
func TestHealthCheck(t *testing.T) {
|
||||
a, s := getMockAdapter(t, true, false)
|
||||
defer s.Close()
|
||||
status, err := a.HealthCheck()
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, status)
|
||||
assert.EqualValues(t, model.Unhealthy, status)
|
||||
a, s = getMockAdapter(t, true, true)
|
||||
defer s.Close()
|
||||
status, err = a.HealthCheck()
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, status)
|
||||
assert.EqualValues(t, model.Healthy, status)
|
||||
}
|
||||
|
||||
func TestPrepareForPush(t *testing.T) {
|
||||
a, s := getMockAdapter(t, true, true)
|
||||
defer s.Close()
|
||||
resources := []*model.Resource{
|
||||
{
|
||||
Type: model.ResourceTypeImage,
|
||||
Metadata: &model.ResourceMetadata{
|
||||
Repository: &model.Repository{
|
||||
Name: "busybox",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err := a.PrepareForPush(resources)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func Test_newAdapter(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := NewAdapter(tt.registry)
|
||||
got, err := newAdapter(tt.registry)
|
||||
if tt.wantErr {
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, got)
|
||||
|
Loading…
Reference in New Issue
Block a user