tests(Registries/Gitlab): Added tests for gitlab adapter

Signed-off-by: lxShaDoWxl <lxshadowxkingxl@gmail.com>
This commit is contained in:
lxShaDoWxl 2020-01-24 21:17:45 +06:00
parent 13892ba144
commit cc1938a04c
15 changed files with 655 additions and 0 deletions

View File

@ -0,0 +1,125 @@
package gitlab
import (
adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strconv"
"testing"
)
func mustWriteHTTPResponse(t *testing.T, w io.Writer, fixturePath string) {
f, err := os.Open(fixturePath)
if err != nil {
t.Fatalf("error opening fixture file: %v", err)
}
if _, err = io.Copy(w, f); err != nil {
t.Fatalf("error writing response: %v", err)
}
}
func getServer(t *testing.T) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Www-Authenticate", "Bearer realm=\"http://"+r.Host+"/jwt/auth\",service=\"container_registry\"")
w.WriteHeader(http.StatusUnauthorized)
})
mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
search := r.URL.Query().Get("search")
w.Header().Set("X-Next-Page", "")
switch search {
case "dev-docker":
mustWriteHTTPResponse(t, w, "testdata/projects/dev-docker.json")
break
case "dev-":
mustWriteHTTPResponse(t, w, "testdata/projects/dev-docker.json")
break
case "-docker":
mustWriteHTTPResponse(t, w, "testdata/projects/-docker.json")
break
case "":
mustWriteHTTPResponse(t, w, "testdata/projects/all.json")
break
default:
w.Header().Set("X-Next-Page", "")
w.Write([]byte(`[]`))
break
}
})
for projectID := 1; projectID <= 5; projectID++ {
mux.HandleFunc("/api/v4/projects/"+strconv.Itoa(projectID)+"/registry/repositories", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Next-Page", "")
re := regexp.MustCompile(`projects/(?P<id>\d+)/registry`)
match := re.FindStringSubmatch(r.RequestURI)
mustWriteHTTPResponse(t, w, "testdata/repositories/"+match[1]+".json")
})
for repositoryID := 1; repositoryID <= 5; repositoryID++ {
mux.HandleFunc("/api/v4/projects/"+strconv.Itoa(projectID)+"/registry/repositories/"+strconv.Itoa(repositoryID)+"1/tags", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Next-Page", "")
re := regexp.MustCompile(`repositories/(?P<id>\d+)/tags`)
match := re.FindStringSubmatch(r.RequestURI)
mustWriteHTTPResponse(t, w, "testdata/tags/"+match[1]+".json")
})
}
}
server := httptest.NewServer(mux)
return server
}
func getAdapter(t *testing.T) adp.Adapter {
assertions := assert.New(t)
factory, err := adp.GetFactory(model.RegistryTypeGitLab)
assertions.Nil(err)
assertions.NotNil(factory)
server := getServer(t)
adapter, err := newAdapter(&model.Registry{
Type: model.RegistryTypeGitLab,
URL: server.URL,
Credential: &model.Credential{
AccessKey: "test",
AccessSecret: "test",
},
})
assertions.Nil(err)
assertions.NotNil(adapter)
return adapter
}
func TestFetchImages(t *testing.T) {
assertions := assert.New(t)
ad := getAdapter(t)
adapter := ad.(*adapter)
templates := map[string]int{
"library/dev-docker": 1,
"library/*-docker": 1,
"library/dev-*": 2,
"*/dev-docker": 1,
"library/*": 2,
}
for k, v := range templates {
resources, err := adapter.FetchImages([]*model.Filter{
{
Type: model.FilterTypeName,
Value: k,
},
})
require.Nil(t, err, k, v)
assertions.Len(resources, v, k, v)
}
}

View File

@ -0,0 +1,97 @@
[
{
"id": 3,
"description": "",
"name": "dev-docker",
"name_with_namespace": "Library / dev-docker",
"path": "dev-docker",
"path_with_namespace": "library/dev-docker",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
},
{
"id": 4,
"description": "",
"name": "dev-docker-test",
"name_with_namespace": "Library / dev-docker-test",
"path": "dev-docker-test",
"path_with_namespace": "library/dev-docker-test",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
}
]

View File

@ -0,0 +1,96 @@
[
{
"id": 1,
"description": "",
"name": "dockers",
"name_with_namespace": "Library / dockers",
"path": "dockers",
"path_with_namespace": "library/dockers",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
},
{
"id": 5,
"description": "",
"name": "example-dockers",
"name_with_namespace": "Library / example-dockers",
"path": "example-dockers",
"path_with_namespace": "library/example-dockers",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
}
]

View File

@ -0,0 +1,96 @@
[
{
"id": 3,
"description": "",
"name": "dev-docker",
"name_with_namespace": "Library / dev-docker",
"path": "dev-docker",
"path_with_namespace": "library/dev-docker",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
},
{
"id": 4,
"description": "",
"name": "dev-docker-test",
"name_with_namespace": "Library / dev-docker-test",
"path": "dev-docker-test",
"path_with_namespace": "library/dev-docker-test",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
}
]

View File

@ -0,0 +1,96 @@
[
{
"id": 3,
"description": "",
"name": "dev-docker",
"name_with_namespace": "Library / dev-docker",
"path": "dev-docker",
"path_with_namespace": "library/dev-docker",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
},
{
"id": 4,
"description": "",
"name": "dev-docker-test",
"name_with_namespace": "Library / dev-docker-test",
"path": "dev-docker-test",
"path_with_namespace": "library/dev-docker-test",
"created_at": "2019-01-17T09:47:07.504Z",
"default_branch": "master",
"tag_list": [],
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"last_activity_at": "2019-06-09T15:18:10.045Z",
"empty_repo": false,
"archived": false,
"visibility": "private",
"resolve_outdated_diff_discussions": false,
"container_registry_enabled": true,
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"jobs_enabled": true,
"snippets_enabled": true,
"shared_runners_enabled": true,
"lfs_enabled": true,
"creator_id": 1,
"forked_from_project": {},
"import_status": "finished",
"open_issues_count": 0,
"ci_default_git_depth": null,
"public_jobs": true,
"ci_config_path": null,
"shared_with_groups": [],
"only_allow_merge_if_pipeline_succeeds": false,
"request_access_enabled": false,
"only_allow_merge_if_all_discussions_are_resolved": false,
"printing_merge_request_link_enabled": true,
"merge_method": "merge",
"external_authorization_classification_label": "",
"permissions": {
"project_access": null,
"group_access": null
},
"mirror": false
}
]

View File

@ -0,0 +1,10 @@
[
{
"id": 11,
"name": "",
"path": "library/dockers",
"project_id": 1,
"location": "registry.gitlab.com/library/dockers",
"created_at": "2019-01-17T09:47:07.504Z"
}
]

View File

@ -0,0 +1,10 @@
[
{
"id": 21,
"name": "",
"path": "library/test-docker",
"project_id": 2,
"location": "registry.gitlab.com/library/test-docker",
"created_at": "2019-01-17T09:47:07.504Z"
}
]

View File

@ -0,0 +1,10 @@
[
{
"id": 31,
"name": "",
"path": "library/dev-docker",
"project_id": 3,
"location": "registry.gitlab.com/library/dev-docker",
"created_at": "2019-01-17T09:47:07.504Z"
}
]

View File

@ -0,0 +1,10 @@
[
{
"id": 41,
"name": "",
"path": "library/dev-docker-test",
"project_id": 4,
"location": "registry.gitlab.com/library/dev-docker-test",
"created_at": "2019-01-17T09:47:07.504Z"
}
]

View File

@ -0,0 +1,10 @@
[
{
"id": 51,
"name": "",
"path": "library/example-dockers",
"project_id": 5,
"location": "registry.gitlab.com/library/example-dockers",
"created_at": "2019-01-17T09:47:07.504Z"
}
]

View File

@ -0,0 +1,17 @@
[
{
"name": "harbor",
"path": "library/dockers:harbor",
"location": "registry.gitlab.com/library/dockers:harbor"
},
{
"name": "latest",
"path": "library/dockers:latest",
"location": "registry.gitlab.com/library/dockers:latest"
},
{
"name": "v2",
"path": "library/dockers:v2",
"location": "registry.gitlab.com/library/dockers:v2"
}
]

View File

@ -0,0 +1,17 @@
[
{
"name": "harbor",
"path": "library/test-docker:harbor",
"location": "registry.gitlab.com/library/test-docker:harbor"
},
{
"name": "latest",
"path": "library/test-docker:latest",
"location": "registry.gitlab.com/library/test-docker:latest"
},
{
"name": "v2",
"path": "library/test-docker:v2",
"location": "registry.gitlab.com/library/test-docker:v2"
}
]

View File

@ -0,0 +1,17 @@
[
{
"name": "harbor",
"path": "library/dev-docker:harbor",
"location": "registry.gitlab.com/library/dev-docker:harbor"
},
{
"name": "latest",
"path": "library/dev-docker:latest",
"location": "registry.gitlab.com/library/dev-docker:latest"
},
{
"name": "v2",
"path": "library/dev-docker:v2",
"location": "registry.gitlab.com/library/dev-docker:v2"
}
]

View File

@ -0,0 +1,22 @@
[
{
"name": "harbor",
"path": "library/dev-docker-test:harbor",
"location": "registry.gitlab.com/library/dev-docker-test:harbor"
},
{
"name": "latest",
"path": "library/dev-docker-test:latest",
"location": "registry.gitlab.com/library/dev-docker-test:latest"
},
{
"name": "v2",
"path": "library/dev-docker-test:v2",
"location": "registry.gitlab.com/library/dev-docker-test:v2"
},
{
"name": "v3",
"path": "library/dev-docker-test:v3",
"location": "registry.gitlab.com/library/dev-docker-test:v3"
}
]

View File

@ -0,0 +1,22 @@
[
{
"name": "harbor",
"path": "library/example-dockers:harbor",
"location": "registry.gitlab.com/library/example-dockers:harbor"
},
{
"name": "latest",
"path": "library/example-dockers:latest",
"location": "registry.gitlab.com/library/example-dockers:latest"
},
{
"name": "v2",
"path": "library/example-dockers:v2",
"location": "registry.gitlab.com/library/example-dockers:v2"
},
{
"name": "v3",
"path": "library/example-dockers:v3",
"location": "registry.gitlab.com/library/example-dockers:v3"
}
]