From c305103e058ef48c3635eb7e46a0eacafbb3c61e Mon Sep 17 00:00:00 2001 From: cd1989 Date: Sat, 29 Jun 2019 18:47:45 +0800 Subject: [PATCH] Add unit test for azure adapter Signed-off-by: cd1989 --- src/replication/adapter/azurecr/adapter.go | 2 +- .../adapter/azurecr/adapter_test.go | 127 ++++++++++++++++++ .../adapter/native/adapter_test.go | 2 +- 3 files changed, 129 insertions(+), 2 deletions(-) diff --git a/src/replication/adapter/azurecr/adapter.go b/src/replication/adapter/azurecr/adapter.go index 6e77f5770..beaaf24c0 100644 --- a/src/replication/adapter/azurecr/adapter.go +++ b/src/replication/adapter/azurecr/adapter.go @@ -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 diff --git a/src/replication/adapter/azurecr/adapter_test.go b/src/replication/adapter/azurecr/adapter_test.go index 6a6df8166..08c8c3f8b 100644 --- a/src/replication/adapter/azurecr/adapter_test.go +++ b/src/replication/adapter/azurecr/adapter_test.go @@ -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) +} diff --git a/src/replication/adapter/native/adapter_test.go b/src/replication/adapter/native/adapter_test.go index e51675bf9..43d993fa0 100644 --- a/src/replication/adapter/native/adapter_test.go +++ b/src/replication/adapter/native/adapter_test.go @@ -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)