[go: nahoru, domu]

Skip to content

Commit

Permalink
fix: update adapter ut (mock http requests)
Browse files Browse the repository at this point in the history
Closes: goharbor#15318

Signed-off-by: Shengwen Yu <yshengwen@vmware.com>
  • Loading branch information
Shengwen Yu authored and bitsf committed Jul 22, 2021
1 parent dd6fb04 commit 766e953
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 90 deletions.
77 changes: 48 additions & 29 deletions src/pkg/reg/adapter/artifacthub/adapter_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
55 changes: 32 additions & 23 deletions src/pkg/reg/adapter/dockerhub/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@ 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 (
testUser = ""
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) {
Expand All @@ -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)

mockRequest().Get("/v2/repositories/namespaces").
Reply(http.StatusOK).BodyString("{}")

assert := assert.New(t)
ad := getAdapter(t)
adapter := ad.(*adapter)
a := getMockAdapter(t)

namespaces, err := adapter.listNamespaces()
assert.Nil(err)
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",
Expand Down
25 changes: 14 additions & 11 deletions src/pkg/reg/adapter/huawei/huawei_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
Expand All @@ -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{}),
Expand All @@ -76,15 +77,17 @@ 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)
}

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)
}
Expand Down
28 changes: 15 additions & 13 deletions src/pkg/reg/adapter/huawei/image_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package huawei

import (
"fmt"
"os"
"testing"

"github.com/docker/distribution"
Expand All @@ -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",
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Loading

0 comments on commit 766e953

Please sign in to comment.