fix: update adapter ut (mock http requests)

Closes: #15318

Signed-off-by: Shengwen Yu <yshengwen@vmware.com>
This commit is contained in:
Shengwen Yu 2021-07-19 16:28:57 +08:00 committed by Ziming
parent dd6fb049f5
commit 766e953325
7 changed files with 190 additions and 90 deletions

View File

@ -1,12 +1,32 @@
package artifacthub
import (
"net/http"
"testing"
adp "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/stretchr/testify/assert"
"testing"
"gopkg.in/h2non/gock.v1"
)
func mockRequest() *gock.Request {
return gock.New("https://artifacthub.io")
}
func getMockAdapter(t *testing.T) *adapter {
ahRegistry := &model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
}
a, err := newAdapter(ahRegistry)
if err != nil {
t.Fatalf("Failed to call newAdapter(), reason=[%v]", err)
}
gock.InterceptClient(a.client.httpClient)
return a
}
func TestAdapter_NewAdapter(t *testing.T) {
factory, err := adp.GetFactory("BadName")
assert.Nil(t, factory)
@ -25,10 +45,7 @@ func TestAdapter_NewAdapter(t *testing.T) {
}
func TestAdapter_Info(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
a := getMockAdapter(t)
info, err := a.Info()
assert.Nil(t, err)
assert.NotNil(t, info)
@ -39,29 +56,35 @@ func TestAdapter_Info(t *testing.T) {
}
func TestAdapter_HealthCheck(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
defer gock.Off()
gock.Observe(gock.DumpRequest)
mockRequest().Get("/").Reply(http.StatusOK).BodyString("{}")
a := getMockAdapter(t)
h, err := a.HealthCheck()
assert.Nil(t, err)
assert.EqualValues(t, model.Healthy, h)
}
func TestAdapter_PrepareForPush(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
a := getMockAdapter(t)
err := a.PrepareForPush(nil)
assert.NotNil(t, err)
}
func TestAdapter_ChartExist(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
defer gock.Off()
gock.Observe(gock.DumpRequest)
mockRequest().Get("/api/v1/packages/helm/harbor/harbor/1.5.0").
Reply(http.StatusOK).BodyString("{}")
mockRequest().Get("/api/v1/packages/helm/harbor/not-exists/1.5.0").
Reply(http.StatusNotFound).BodyString("{}")
mockRequest().Get("/api/v1/packages/helm/harbor/harbor/not-exists").
Reply(http.StatusNotFound).BodyString("{}")
a := getMockAdapter(t)
b, err := a.ChartExist("harbor/harbor", "1.5.0")
assert.Nil(t, err)
@ -77,11 +100,13 @@ func TestAdapter_ChartExist(t *testing.T) {
}
func TestAdapter_DownloadChart(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
defer gock.Off()
gock.Observe(gock.DumpRequest)
gock.New("https://helm.goharbor.io/").Get("/harbor-1.5.0.tgz").
Reply(http.StatusOK).BodyString("{}")
a := getMockAdapter(t)
data, err := a.DownloadChart("harbor/harbor", "1.5.0", "")
assert.NotNil(t, err)
assert.Nil(t, data)
@ -92,20 +117,14 @@ func TestAdapter_DownloadChart(t *testing.T) {
}
func TestAdapter_DeleteChart(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
a := getMockAdapter(t)
err := a.DeleteChart("harbor/harbor", "1.5.0")
assert.NotNil(t, err)
}
func TestAdapter_UploadChart(t *testing.T) {
a, _ := newAdapter(&model.Registry{
Type: model.RegistryTypeArtifactHub,
URL: "https://artifacthub.io",
})
a := getMockAdapter(t)
err := a.UploadChart("harbor/harbor", "1.5.0", nil)
assert.NotNil(t, err)

View File

@ -2,12 +2,13 @@ package dockerhub
import (
"fmt"
"net/http"
"testing"
adp "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
)
const (
@ -15,24 +16,26 @@ const (
testPassword = ""
)
func getAdapter(t *testing.T) adp.Adapter {
assert := assert.New(t)
factory, err := adp.GetFactory(model.RegistryTypeDockerHub)
assert.Nil(err)
assert.NotNil(factory)
func mockRequest() *gock.Request {
return gock.New("https://hub.docker.com")
}
adapter, err := newAdapter(&model.Registry{
func getMockAdapter(t *testing.T) *adapter {
r := &model.Registry{
Type: model.RegistryTypeDockerHub,
URL: baseURL,
Credential: &model.Credential{
AccessKey: testUser,
AccessSecret: testPassword,
},
})
assert.Nil(err)
assert.NotNil(adapter)
return adapter
}
ad, err := newAdapter(r)
if err != nil {
t.Fatalf("Failed to call newAdapter(), reason=[%v]", err)
}
a := ad.(*adapter)
gock.InterceptClient(a.client.client)
return a
}
func TestInfo(t *testing.T) {
@ -51,26 +54,32 @@ func TestListCandidateNamespaces(t *testing.T) {
require.Equal(t, 1, len(namespaces))
assert.Equal(t, "library", namespaces[0])
}
func TestListNamespaces(t *testing.T) {
if testUser == "" {
return
}
defer gock.Off()
gock.Observe(gock.DumpRequest)
assert := assert.New(t)
ad := getAdapter(t)
adapter := ad.(*adapter)
mockRequest().Get("/v2/repositories/namespaces").
Reply(http.StatusOK).BodyString("{}")
namespaces, err := adapter.listNamespaces()
assert.Nil(err)
a := getMockAdapter(t)
namespaces, err := a.listNamespaces()
assert.Nil(t, err)
for _, ns := range namespaces {
fmt.Println(ns)
}
}
func TestFetchArtifacts(t *testing.T) {
ad := getAdapter(t)
adapter := ad.(*adapter)
_, err := adapter.FetchArtifacts([]*model.Filter{
defer gock.Off()
gock.Observe(gock.DumpRequest)
mockRequest().Get("/v2/repositories/goharbor/").
Reply(http.StatusOK).BodyString("{}")
a := getMockAdapter(t)
_, err := a.FetchArtifacts([]*model.Filter{
{
Type: model.FilterTypeName,
Value: "goharbor/harbor-core",

View File

@ -15,19 +15,14 @@
package huawei
import (
"os"
"testing"
adp "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/stretchr/testify/assert"
gock "gopkg.in/h2non/gock.v1"
)
var hwAdapter adp.Adapter
func init() {
var err error
func getMockAdapter(t *testing.T) *adapter {
hwRegistry := &model.Registry{
ID: 1,
Name: "Huawei",
@ -39,18 +34,22 @@ func init() {
Status: "",
}
hwAdapter, err = newAdapter(hwRegistry)
hwAdapter, err := newAdapter(hwRegistry)
if err != nil {
os.Exit(1)
t.Fatalf("Failed to call newAdapter(), reason=[%v]", err)
}
a := hwAdapter.(*adapter)
gock.InterceptClient(a.client.GetClient())
gock.InterceptClient(a.oriClient)
return a
}
func TestAdapter_Info(t *testing.T) {
info, err := hwAdapter.Info()
a := getMockAdapter(t)
info, err := a.Info()
if err != nil {
t.Error(err)
}
@ -67,6 +66,8 @@ func TestAdapter_PrepareForPush(t *testing.T) {
mockRequest().Post("/dockyard/v2/namespaces").BodyString(`{"namespace":"domain_repo_new"}`).
Reply(200)
a := getMockAdapter(t)
repository := &model.Repository{
Name: "domain_repo_new",
Metadata: make(map[string]interface{}),
@ -76,7 +77,7 @@ func TestAdapter_PrepareForPush(t *testing.T) {
Repository: repository,
}
resource.Metadata = metadata
err := hwAdapter.PrepareForPush([]*model.Resource{resource})
err := a.PrepareForPush([]*model.Resource{resource})
assert.NoError(t, err)
}
@ -84,7 +85,9 @@ func TestAdapter_HealthCheck(t *testing.T) {
defer gock.Off()
gock.Observe(gock.DumpRequest)
health, err := hwAdapter.HealthCheck()
a := getMockAdapter(t)
health, err := a.HealthCheck()
if err != nil {
t.Error(err)
}

View File

@ -16,7 +16,6 @@ package huawei
import (
"fmt"
"os"
"testing"
"github.com/docker/distribution"
@ -25,9 +24,11 @@ import (
gock "gopkg.in/h2non/gock.v1"
)
var HWAdapter adapter
func mockRequest() *gock.Request {
return gock.New("https://swr.cn-north-1.myhuaweicloud.com")
}
func init() {
func getHwMockAdapter(t *testing.T) *adapter {
hwRegistry := &model.Registry{
ID: 1,
Name: "Huawei",
@ -40,16 +41,14 @@ func init() {
}
adp, err := newAdapter(hwRegistry)
if err != nil {
os.Exit(1)
t.Fatalf("Failed to call newAdapter(), reason=[%v]", err)
}
HWAdapter = *adp.(*adapter)
a := adp.(*adapter)
gock.InterceptClient(HWAdapter.client.GetClient())
gock.InterceptClient(HWAdapter.oriClient)
}
gock.InterceptClient(a.client.GetClient())
gock.InterceptClient(a.oriClient)
func mockRequest() *gock.Request {
return gock.New("https://swr.cn-north-1.myhuaweicloud.com")
return a
}
func mockGetJwtToken(repository string) {
@ -73,7 +72,8 @@ func TestAdapter_FetchArtifacts(t *testing.T) {
{Name: "name2"},
})
resources, err := HWAdapter.FetchArtifacts(nil)
a := getHwMockAdapter(t)
resources, err := a.FetchArtifacts(nil)
assert.NoError(t, err)
assert.Len(t, resources, 2)
}
@ -89,7 +89,8 @@ func TestAdapter_ManifestExist(t *testing.T) {
MediaType: distribution.ManifestMediaTypes()[0],
})
exist, _, err := HWAdapter.ManifestExist("sundaymango_mango/hello-world", "latest")
a := getHwMockAdapter(t)
exist, _, err := a.ManifestExist("sundaymango_mango/hello-world", "latest")
assert.NoError(t, err)
assert.True(t, exist)
}
@ -101,6 +102,7 @@ func TestAdapter_DeleteManifest(t *testing.T) {
mockGetJwtToken("sundaymango_mango/hello-world")
mockRequest().Delete("/v2/sundaymango_mango/hello-world/manifests/latest").Reply(200)
err := HWAdapter.DeleteManifest("sundaymango_mango/hello-world", "latest")
a := getHwMockAdapter(t)
err := a.DeleteManifest("sundaymango_mango/hello-world", "latest")
assert.NoError(t, err)
}

View File

@ -1,22 +1,51 @@
package quay
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/goharbor/harbor/src/common/utils/test"
adp "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/stretchr/testify/assert"
)
func getMockAdapter(t *testing.T) adp.Adapter {
func getMockAdapter(t *testing.T) (*adapter, *httptest.Server) {
server := test.NewServer(
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/quay/busybox/manifests/latest",
Handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
// sample test data
data := `{"name":"quay/busybox","tag":"latest","architecture":"amd64","fsLayers":[{"blobSum":"sha256:ee780d08a5b4de5192a526d422987f451d9a065e6da42aefe8c3b20023a250c7"},{"blobSum":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"},{"blobSum":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"},{"blobSum":"sha256:9c075fe2c773108d2fe2c18ea170548b0ee30ef4e5e072d746e3f934e788b734"}],"history":[{"v1Compatibility":"{\"architecture\":\"amd64\",\"config\":{\"Env\":[\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\",\"TERM=xterm\",\"container=podman\",\"HOSTNAME=\"],\"Cmd\":[\"/bin/sh\"],\"WorkingDir\":\"/\"},\"created\":\"2020-09-04T16:08:39.002329276Z\",\"id\":\"94cc2cc706952747940d82296ae52449f4bb31a5ee40bb5a36145929775d80a5\",\"os\":\"linux\",\"parent\":\"17d40c067945c6ba5bd16e84fd2454b645bc85471e9222f64cc4af9dfbead85d\"}"},{"v1Compatibility":"{\"id\":\"17d40c067945c6ba5bd16e84fd2454b645bc85471e9222f64cc4af9dfbead85d\",\"parent\":\"6e85bfb29b9b917e6b97a3aa6339f150072a7953210013390c7cbf405eaea31c\",\"comment\":\"Updated at 2020-09-04 15:06:14 +0000\",\"created\":\"2020-09-04T15:06:15.059006882Z\",\"container_config\":{\"Cmd\":[\"sh echo \\\"2020-09-04 15:06:14 +0000\\\" \\u003e foo\"]},\"throwaway\":true}"},{"v1Compatibility":"{\"id\":\"6e85bfb29b9b917e6b97a3aa6339f150072a7953210013390c7cbf405eaea31c\",\"parent\":\"dacca7329c6e094ed91cc673249ff788cb7959ef6f53517f363660145bbc7464\",\"created\":\"2020-09-01T00:36:18.335938403Z\",\"container_config\":{\"Cmd\":[\"/bin/sh -c #(nop) CMD [\\\"sh\\\"]\"]},\"throwaway\":true}"},{"v1Compatibility":"{\"id\":\"dacca7329c6e094ed91cc673249ff788cb7959ef6f53517f363660145bbc7464\",\"created\":\"2020-09-01T00:36:18.002487153Z\",\"container_config\":{\"Cmd\":[\"/bin/sh -c #(nop) ADD file:4e5169fa630e0afede3b7db9a6d0ca063df3fe69cc2873a6c50e9801d61f563f in / \"]}}"}],"schemaVersion":1,"signatures":[{"header":{"jwk":{"crv":"P-256","kid":"BU64:Q7B6:FC7P:O23C:H46C:5J5O:DRH6:F7XL:MB2U:YD4Z:4IT2:4XAT","kty":"EC","x":"xOLxWrHtPC8j0WFZd4l5TErbzO_p5FWiYYamVEOtEjI","y":"Ze-LCvjtbpEJRSKbRlQXQYlBcAjoTNNTj_GvaDieXy0"},"alg":"ES256"},"signature":"8XK-3ScG84TOXGxX-Cwi0j1nEjqOIaR0U0n6_N4BpK0X9hCvf3aMEbeln_bO2mrKTfuJWP5KAbN-SgekcJm92Q","protected":"eyJmb3JtYXRMZW5ndGgiOjE4OTUsImZvcm1hdFRhaWwiOiJmUSIsInRpbWUiOiIyMDIwLTA5LTA0VDE2OjA5OjA5WiJ9"}]}`
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(data))
},
},
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/",
Handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
fmt.Println(555)
w.WriteHeader(http.StatusOK)
},
},
)
factory, _ := adp.GetFactory(model.RegistryTypeQuay)
adapter, err := factory.Create(&model.Registry{
ad, err := factory.Create(&model.Registry{
Type: model.RegistryTypeQuay,
URL: "https://quay.io",
URL: server.URL,
})
assert.Nil(t, err)
return adapter
a := ad.(*adapter)
return a, server
}
func TestAdapter_NewAdapter(t *testing.T) {
factory, err := adp.GetFactory("BadName")
assert.Nil(t, factory)
@ -28,21 +57,29 @@ func TestAdapter_NewAdapter(t *testing.T) {
}
func TestAdapter_HealthCheck(t *testing.T) {
health, err := getMockAdapter(t).HealthCheck()
a, s := getMockAdapter(t)
defer s.Close()
health, err := a.HealthCheck()
assert.Nil(t, err)
assert.Equal(t, string(health), model.Healthy)
}
func TestAdapter_Info(t *testing.T) {
info, err := getMockAdapter(t).Info()
a, s := getMockAdapter(t)
defer s.Close()
info, err := a.Info()
assert.Nil(t, err)
t.Log(info)
}
func TestAdapter_PullManifests(t *testing.T) {
quayAdapter := getMockAdapter(t)
registry, _, err := quayAdapter.(*adapter).PullManifest("quay/busybox", "latest")
a, s := getMockAdapter(t)
defer s.Close()
registry, _, err := a.PullManifest("quay/busybox", "latest")
assert.Nil(t, err)
assert.NotNil(t, registry)
t.Log(registry)
}

View File

@ -89,9 +89,12 @@ func newAdapter(registry *model.Registry) (a *adapter, err error) {
var registryURL *url.URL
registryURL, _ = url.Parse(registry.URL)
if strings.Index(registryURL.Host, ".tencentcloudcr.com") < 0 {
log.Errorf("[tencent-tcr.newAdapter] errInvalidTcrEndpoint=%v", err)
return nil, errInvalidTcrEndpoint
// only validate registryURL.Host in non-UT scenario
if os.Getenv("UTTEST") != "true" {
if strings.Index(registryURL.Host, ".tencentcloudcr.com") < 0 {
log.Errorf("[tencent-tcr.newAdapter] errInvalidTcrEndpoint=%v", err)
return nil, errInvalidTcrEndpoint
}
}
realm, service, err := ping(registry)

View File

@ -26,6 +26,8 @@ var (
)
func setup() {
os.Setenv("UTTEST", "true")
if ak := os.Getenv("TENCENT_AK"); ak != "" {
log.Info("USE AK from ENV")
mockAccessKey = ak
@ -70,6 +72,10 @@ func TestAdapter_NewAdapter_NilAKSK(t *testing.T) {
}
func TestAdapter_NewAdapter_InvalidEndpoint(t *testing.T) {
res := os.Getenv("UTTEST")
os.Unsetenv("UTTEST")
defer os.Setenv("UTTEST", res)
// Invaild endpoint
adapter, err := newAdapter(&model.Registry{
Type: model.RegistryTypeTencentTcr,
@ -111,14 +117,32 @@ func TestAdapter_NewAdapter_InvalidAKSK(t *testing.T) {
assert.Nil(t, adapter)
}
func getTestServer() *httptest.Server {
server := test.NewServer(
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/",
Handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Www-Authenticate", `Bearer realm="https://harbor-community.tencentcloudcr.com/service/token",service="harbor-registry"`)
w.WriteHeader(http.StatusUnauthorized)
},
},
)
return server
}
func TestAdapter_NewAdapter_Ok(t *testing.T) {
server := getTestServer()
defer server.Close()
adapter, err := newAdapter(&model.Registry{
Type: model.RegistryTypeTencentTcr,
Credential: &model.Credential{
AccessKey: mockAccessKey,
AccessSecret: mockAccessSecret,
},
URL: "https://harbor-community.tencentcloudcr.com",
URL: server.URL,
})
if sdkerr, ok := err.(*errors.TencentCloudSDKError); ok {
log.Infof("sdk error, error=%v", sdkerr)
@ -130,6 +154,9 @@ func TestAdapter_NewAdapter_Ok(t *testing.T) {
}
func TestAdapter_NewAdapter_InsecureOk(t *testing.T) {
server := getTestServer()
defer server.Close()
adapter, err := newAdapter(&model.Registry{
Type: model.RegistryTypeTencentTcr,
Credential: &model.Credential{
@ -137,7 +164,7 @@ func TestAdapter_NewAdapter_InsecureOk(t *testing.T) {
AccessSecret: mockAccessSecret,
},
Insecure: true,
URL: "https://harbor-community.tencentcloudcr.com",
URL: server.URL,
})
if sdkerr, ok := err.(*errors.TencentCloudSDKError); ok {
log.Infof("sdk error, error=%v", sdkerr)