diff --git a/applicationset/services/scm_provider/gitea_test.go b/applicationset/services/scm_provider/gitea_test.go index c585aafe1a679..4fb0f3702f7ff 100644 --- a/applicationset/services/scm_provider/gitea_test.go +++ b/applicationset/services/scm_provider/gitea_test.go @@ -17,28 +17,28 @@ func TestGiteaListRepos(t *testing.T) { filters []v1alpha1.SCMProviderGeneratorFilter }{ { - name: "blank protocol", + name: "blank protocol", allBranches: false, - url: "git@gitea.com:test-argocd/pr-test.git", - branches: []string{"main"}, + url: "git@gitea.com:test-argocd/pr-test.git", + branches: []string{"main"}, }, { - name: "ssh protocol", + name: "ssh protocol", allBranches: false, - proto: "ssh", - url: "git@gitea.com:test-argocd/pr-test.git", + proto: "ssh", + url: "git@gitea.com:test-argocd/pr-test.git", }, { - name: "https protocol", + name: "https protocol", allBranches: false, - proto: "https", - url: "https://gitea.com/test-argocd/pr-test", + proto: "https", + url: "https://gitea.com/test-argocd/pr-test", }, { - name: "other protocol", + name: "other protocol", allBranches: false, - proto: "other", - hasError: true, + proto: "other", + hasError: true, }, { name: "all branches", @@ -55,7 +55,6 @@ func TestGiteaListRepos(t *testing.T) { if c.hasError { assert.NotNil(t, err) } else { - checkRateLimit(t, err) assert.Nil(t, err) // Just check that this one project shows up. Not a great test but better thing nothing? repos := []*Repository{} diff --git a/applicationset/services/scm_provider/github_test.go b/applicationset/services/scm_provider/github_test.go index 78f9c487ec370..5cf729080d9bb 100644 --- a/applicationset/services/scm_provider/github_test.go +++ b/applicationset/services/scm_provider/github_test.go @@ -2,8 +2,9 @@ package scm_provider import ( "context" - "os" - "strings" + "io" + "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" @@ -11,27 +12,192 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" ) -func checkRateLimit(t *testing.T, err error) { - // Check if we've hit a rate limit, don't fail the test if so. - if err != nil && (strings.Contains(err.Error(), "rate limit exceeded") || - (strings.Contains(err.Error(), "API rate limit") && strings.Contains(err.Error(), "still exceeded"))) { - - // GitHub Actions add this environment variable to indicate branch ref you are running on - githubRef := os.Getenv("GITHUB_REF") - - // Only report rate limit errors as errors, when: - // - We are running in a GitHub action - // - AND, we are running that action on the 'master' or 'release-*' branch - // (unfortunately, for PRs, we don't have access to GitHub secrets that would allow us to embed a token) - failOnRateLimitErrors := os.Getenv("CI") != "" && (strings.Contains(githubRef, "/master") || strings.Contains(githubRef, "/release-")) - - t.Logf("Got a rate limit error, consider setting $GITHUB_TOKEN to increase your GitHub API rate limit: %v\n", err) - if failOnRateLimitErrors { - t.FailNow() - } else { - t.SkipNow() +func githubMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.RequestURI { + case "/api/v3/orgs/argoproj/repos?per_page=100": + _, err := io.WriteString(w, `[ + { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "argo-cd", + "full_name": "argoproj/argo-cd", + "owner": { + "login": "argoproj", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/argoproj_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/argoproj", + "html_url": "https://github.com/argoproj", + "followers_url": "https://api.github.com/users/argoproj/followers", + "following_url": "https://api.github.com/users/argoproj/following{/other_user}", + "gists_url": "https://api.github.com/users/argoproj/gists{/gist_id}", + "starred_url": "https://api.github.com/users/argoproj/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/argoproj/subscriptions", + "organizations_url": "https://api.github.com/users/argoproj/orgs", + "repos_url": "https://api.github.com/users/argoproj/repos", + "events_url": "https://api.github.com/users/argoproj/events{/privacy}", + "received_events_url": "https://api.github.com/users/argoproj/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/argoproj/argo-cd", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/argoproj/argo-cd", + "archive_url": "https://api.github.com/repos/argoproj/argo-cd/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/argoproj/argo-cd/assignees{/user}", + "blobs_url": "https://api.github.com/repos/argoproj/argo-cd/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/argoproj/argo-cd/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/argoproj/argo-cd/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/argoproj/argo-cd/comments{/number}", + "commits_url": "https://api.github.com/repos/argoproj/argo-cd/commits{/sha}", + "compare_url": "https://api.github.com/repos/argoproj/argo-cd/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/argoproj/argo-cd/contents/{path}", + "contributors_url": "https://api.github.com/repos/argoproj/argo-cd/contributors", + "deployments_url": "https://api.github.com/repos/argoproj/argo-cd/deployments", + "downloads_url": "https://api.github.com/repos/argoproj/argo-cd/downloads", + "events_url": "https://api.github.com/repos/argoproj/argo-cd/events", + "forks_url": "https://api.github.com/repos/argoproj/argo-cd/forks", + "git_commits_url": "https://api.github.com/repos/argoproj/argo-cd/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/argoproj/argo-cd/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/argoproj/argo-cd/git/tags{/sha}", + "git_url": "git:github.com/argoproj/argo-cd.git", + "issue_comment_url": "https://api.github.com/repos/argoproj/argo-cd/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/argoproj/argo-cd/issues/events{/number}", + "issues_url": "https://api.github.com/repos/argoproj/argo-cd/issues{/number}", + "keys_url": "https://api.github.com/repos/argoproj/argo-cd/keys{/key_id}", + "labels_url": "https://api.github.com/repos/argoproj/argo-cd/labels{/name}", + "languages_url": "https://api.github.com/repos/argoproj/argo-cd/languages", + "merges_url": "https://api.github.com/repos/argoproj/argo-cd/merges", + "milestones_url": "https://api.github.com/repos/argoproj/argo-cd/milestones{/number}", + "notifications_url": "https://api.github.com/repos/argoproj/argo-cd/notifications{?since,all,participating}", + "pulls_url": "https://api.github.com/repos/argoproj/argo-cd/pulls{/number}", + "releases_url": "https://api.github.com/repos/argoproj/argo-cd/releases{/id}", + "ssh_url": "git@github.com:argoproj/argo-cd.git", + "stargazers_url": "https://api.github.com/repos/argoproj/argo-cd/stargazers", + "statuses_url": "https://api.github.com/repos/argoproj/argo-cd/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/argoproj/argo-cd/subscribers", + "subscription_url": "https://api.github.com/repos/argoproj/argo-cd/subscription", + "tags_url": "https://api.github.com/repos/argoproj/argo-cd/tags", + "teams_url": "https://api.github.com/repos/argoproj/argo-cd/teams", + "trees_url": "https://api.github.com/repos/argoproj/argo-cd/git/trees{/sha}", + "clone_url": "https://github.com/argoproj/argo-cd.git", + "mirror_url": "git:git.example.com/argoproj/argo-cd", + "hooks_url": "https://api.github.com/repos/argoproj/argo-cd/hooks", + "svn_url": "https://svn.github.com/argoproj/argo-cd", + "homepage": "https://github.com", + "language": null, + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "is_template": false, + "topics": [ + "argoproj", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "archived": false, + "disabled": false, + "visibility": "public", + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "template_repository": null + } + ]`) + if err != nil { + t.Fail() + } + case "/api/v3/repos/argoproj/argo-cd/branches?per_page=100": + _, err := io.WriteString(w, `[ + { + "name": "master", + "commit": { + "sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc", + "url": "https://api.github.com/repos/argoproj/argo-cd/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc" + }, + "protected": true, + "protection": { + "required_status_checks": { + "enforcement_level": "non_admins", + "contexts": [ + "ci-test", + "linter" + ] + } + }, + "protection_url": "https://api.github.com/repos/argoproj/hello-world/branches/master/protection" + } + ] + `) + if err != nil { + t.Fail() + } + case "/api/v3/repos/argoproj/argo-cd/contents/pkg?ref=master": + _, err := io.WriteString(w, `{ + "type": "file", + "encoding": "base64", + "size": 5362, + "name": "pkg/", + "path": "pkg/", + "content": "encoded content ...", + "sha": "3d21ec53a331a6f037a91c368710b99387d012c1", + "url": "https://api.github.com/repos/octokit/octokit.rb/contents/README.md", + "git_url": "https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1", + "html_url": "https://github.com/octokit/octokit.rb/blob/master/README.md", + "download_url": "https://raw.githubusercontent.com/octokit/octokit.rb/master/README.md", + "_links": { + "git": "https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1", + "self": "https://api.github.com/repos/octokit/octokit.rb/contents/README.md", + "html": "https://github.com/octokit/octokit.rb/blob/master/README.md" + } + }`) + if err != nil { + t.Fail() + } + case "/api/v3/repos/argoproj/argo-cd/branches/master": + _, err := io.WriteString(w, `{ + "name": "master", + "commit": { + "sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc", + "url": "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc" + }, + "protected": true, + "protection": { + "required_status_checks": { + "enforcement_level": "non_admins", + "contexts": [ + "ci-test", + "linter" + ] + } + }, + "protection_url": "https://api.github.com/repos/octocat/hello-world/branches/master/protection" + }`) + if err != nil { + t.Fail() + } + default: + w.WriteHeader(404) } - } } @@ -66,18 +232,20 @@ func TestGithubListRepos(t *testing.T) { name: "all branches", allBranches: true, url: "git@github.com:argoproj/argo-cd.git", - branches: []string{"master", "release-0.11"}, + branches: []string{"master"}, }, } - + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + githubMockHandler(t)(w, r) + })) + defer ts.Close() for _, c := range cases { t.Run(c.name, func(t *testing.T) { - provider, _ := NewGithubProvider(context.Background(), "argoproj", "", "", c.allBranches) + provider, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, c.allBranches) rawRepos, err := ListRepos(context.Background(), provider, c.filters, c.proto) if c.hasError { assert.Error(t, err) } else { - checkRateLimit(t, err) assert.NoError(t, err) // Just check that this one project shows up. Not a great test but better thing nothing? repos := []*Repository{} @@ -99,25 +267,31 @@ func TestGithubListRepos(t *testing.T) { } func TestGithubHasPath(t *testing.T) { - host, _ := NewGithubProvider(context.Background(), "argoproj", "", "", false) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + githubMockHandler(t)(w, r) + })) + defer ts.Close() + host, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, false) repo := &Repository{ Organization: "argoproj", Repository: "argo-cd", Branch: "master", } ok, err := host.RepoHasPath(context.Background(), repo, "pkg/") - checkRateLimit(t, err) assert.Nil(t, err) assert.True(t, ok) ok, err = host.RepoHasPath(context.Background(), repo, "notathing/") - checkRateLimit(t, err) assert.Nil(t, err) assert.False(t, ok) } func TestGithubGetBranches(t *testing.T) { - host, _ := NewGithubProvider(context.Background(), "argoproj", "", "", false) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + githubMockHandler(t)(w, r) + })) + defer ts.Close() + host, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, false) repo := &Repository{ Organization: "argoproj", Repository: "argo-cd", @@ -125,19 +299,26 @@ func TestGithubGetBranches(t *testing.T) { } repos, err := host.GetBranches(context.Background(), repo) if err != nil { - checkRateLimit(t, err) assert.NoError(t, err) } else { assert.Equal(t, repos[0].Branch, "master") } - // Get all branches + //Branch Doesn't exists instead of error will return no error + repo2 := &Repository{ + Organization: "argoproj", + Repository: "applicationset", + Branch: "main", + } + _, err = host.GetBranches(context.Background(), repo2) + assert.NoError(t, err) + + // Get all branches host.allBranches = true repos, err = host.GetBranches(context.Background(), repo) if err != nil { - checkRateLimit(t, err) assert.NoError(t, err) } else { - // considering master and one release branch to always exist. - assert.Greater(t, len(repos), 1) + // considering master branch to exist. + assert.Equal(t, len(repos), 1) } } diff --git a/applicationset/services/scm_provider/gitlab_test.go b/applicationset/services/scm_provider/gitlab_test.go index cff8d74ec3280..c42b00252ea93 100644 --- a/applicationset/services/scm_provider/gitlab_test.go +++ b/applicationset/services/scm_provider/gitlab_test.go @@ -2,6 +2,10 @@ package scm_provider import ( "context" + "fmt" + "io" + "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" @@ -9,6 +13,275 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" ) +func gitlabMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.RequestURI { + case "/api/v4": + fmt.Println("here1") + case "/api/v4/groups/test-argocd-proton/projects?include_subgroups=false&per_page=100": + fmt.Println("here") + _, err := io.WriteString(w, `[{ + "id": 27084533, + "description": "", + "name": "argocd", + "name_with_namespace": "test argocd proton / argocd", + "path": "argocd", + "path_with_namespace": "test-argocd-proton/argocd", + "created_at": "2021-06-01T17:30:44.724Z", + "default_branch": "master", + "tag_list": [], + "topics": [], + "ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git", + "http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git", + "web_url": "https://gitlab.com/test-argocd-proton/argocd", + "readme_url": null, + "avatar_url": null, + "forks_count": 0, + "star_count": 0, + "last_activity_at": "2021-06-04T08:19:51.656Z", + "namespace": { + "id": 12258515, + "name": "test argocd proton", + "path": "test-argocd-proton", + "kind": "gro* Connection #0 to host gitlab.com left intact up ", + "full_path ": "test - argocd - proton ", + "parent_id ": null, + "avatar_url ": null, + "web_url ": "https: //gitlab.com/groups/test-argocd-proton" + }, + "container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/argocd", + "_links": { + "self": "https://gitlab.com/api/v4/projects/27084533", + "issues": "https://gitlab.com/api/v4/projects/27084533/issues", + "merge_requests": "https://gitlab.com/api/v4/projects/27084533/merge_requests", + "repo_branches": "https://gitlab.com/api/v4/projects/27084533/repository/branches", + "labels": "https://gitlab.com/api/v4/projects/27084533/labels", + "events": "https://gitlab.com/api/v4/projects/27084533/events", + "members": "https://gitlab.com/api/v4/projects/27084533/members", + "cluster_agents": "https://gitlab.com/api/v4/projects/27084533/cluster_agents" + }, + "packages_enabled": true, + "empty_repo": false, + "archived": false, + "visibility": "public", + "resolve_outdated_diff_discussions": false, + "container_expiration_policy": { + "cadence": "1d", + "enabled": false, + "keep_n": 10, + "older_than": "90d", + "name_regex": ".*", + "name_regex_keep": null, + "next_run_at": "2021-06-02T17:30:44.740Z" + }, + "issues_enabled": true, + "merge_requests_enabled": true, + "wiki_enabled": true, + "jobs_enabled": true, + "snippets_enabled": true, + "container_registry_enabled": true, + "service_desk_enabled": true, + "can_create_merge_request_in": false, + "issues_access_level": "enabled", + "repository_access_level": "enabled", + "merge_requests_access_level": "enabled", + "forking_access_level": "enabled", + "wiki_access_level": "enabled", + "builds_access_level": "enabled", + "snippets_access_level": "enabled", + "pages_access_level": "enabled", + "operations_access_level": "enabled", + "analytics_access_level": "enabled", + "container_registry_access_level": "enabled", + "security_and_compliance_access_level": "private", + "emails_disabled": null, + "shared_runners_enabled": true, + "lfs_enabled": true, + "creator_id": 2378866, + "import_status": "none", + "open_issues_count": 0, + "ci_default_git_depth": 50, + "ci_forward_deployment_enabled": true, + "ci_job_token_scope_enabled": false, + "public_jobs": true, + "build_timeout": 3600, + "auto_cancel_pending_pipelines": "enabled", + "ci_config_path": "", + "shared_with_groups": [], + "only_allow_merge_if_pipeline_succeeds": false, + "allow_merge_on_skipped_pipeline": null, + "restrict_user_defined_variables": false, + "request_access_enabled": true, + "only_allow_merge_if_all_discussions_are_resolved": false, + "remove_source_branch_after_merge": true, + "printing_merge_request_link_enabled": true, + "merge_method": "merge", + "squash_option": "default_off", + "suggestion_commit_message": null, + "merge_commit_template": null, + "squash_commit_template": null, + "auto_devops_enabled": false, + "auto_devops_deploy_strategy": "continuous", + "autoclose_referenced_issues": true, + "keep_latest_artifact": true, + "runner_token_expiration_interval": null, + "approvals_before_merge": 0, + "mirror": false, + "external_authorization_classification_label": "", + "marked_for_deletion_at": null, + "marked_for_deletion_on": null, + "requirements_enabled": true, + "requirements_access_level": "enabled", + "security_and_compliance_enabled": false, + "compliance_frameworks": [], + "issues_template": null, + "merge_requests_template": null, + "merge_pipelines_enabled": false, + "merge_trains_enabled": false + }]`) + if err != nil { + t.Fail() + } + case "/api/v4/projects/27084533/repository/branches/master": + fmt.Println("returning") + _, err := io.WriteString(w, `{ + "name": "master", + "commit": { + "id": "8898d7999fc99dd0fd578650b58b244fc63f6b53", + "short_id": "8898d799", + "created_at": "2021-06-04T08:24:44.000+00:00", + "parent_ids": ["3c9d50be1ef949ad28674e238c7e12a17b1e9706", "56482e001731640b4123cf177e51c696f08a3005"], + "title": "Merge branch 'pipeline-1317911429' into 'master'", + "message": "Merge branch 'pipeline-1317911429' into 'master'\n\n[testapp-ci] manifests/demo/test-app.yaml: release v1.1.0\n\nSee merge request test-argocd-proton/argocd!3", + "author_name": "Martin Vozník", + "author_email": "martin@voznik.cz", + "authored_date": "2021-06-04T08:24:44.000+00:00", + "committer_name": "Martin Vozník", + "committer_email": "martin@voznik.cz", + "committed_date": "2021-06-04T08:24:44.000+00:00", + "trailers": {}, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53" + }, + "merged": false, + "protected": true, + "developers_can_push": false, + "developers_can_merge": false, + "can_push": false, + "default": true, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/master" + }`) + if err != nil { + t.Fail() + } + case "/api/v4/projects/27084533/repository/branches?per_page=100": + _, err := io.WriteString(w, `[{ + "name": "master", + "commit": { + "id": "8898d7999fc99dd0fd578650b58b244fc63f6b53", + "short_id": "8898d799", + "created_at": "2021-06-04T08:24:44.000+00:00", + "parent_ids": null, + "title": "Merge branch 'pipeline-1317911429' into 'master'", + "message": "Merge branch 'pipeline-1317911429' into 'master'", + "author_name": "Martin Vozník", + "author_email": "martin@voznik.cz", + "authored_date": "2021-06-04T08:24:44.000+00:00", + "committer_name": "Martin Vozník", + "committer_email": "martin@voznik.cz", + "committed_date": "2021-06-04T08:24:44.000+00:00", + "trailers": null, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53" + }, + "merged": false, + "protected": true, + "developers_can_push": false, + "developers_can_merge": false, + "can_push": false, + "default": true, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/master" + }, { + "name": "pipeline-1310077506", + "commit": { + "id": "0f92540e5f396ba960adea4ed0aa905baf3f73d1", + "short_id": "0f92540e", + "created_at": "2021-06-01T18:39:59.000+00:00", + "parent_ids": null, + "title": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1", + "message": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1", + "author_name": "ci-test-app", + "author_email": "mvoznik+cicd@protonmail.com", + "authored_date": "2021-06-01T18:39:59.000+00:00", + "committer_name": "ci-test-app", + "committer_email": "mvoznik+cicd@protonmail.com", + "committed_date": "2021-06-01T18:39:59.000+00:00", + "trailers": null, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/0f92540e5f396ba960adea4ed0aa905baf3f73d1" + }, + "merged": false, + "protected": false, + "developers_can_push": false, + "developers_can_merge": false, + "can_push": false, + "default": false, + "web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/pipeline-1310077506" + }]`) + if err != nil { + t.Fail() + } + case "/api/v4/projects/test-argocd-proton%2Fargocd": + fmt.Println("auct") + _, err := io.WriteString(w, `{ + "id": 27084533, + "description": "", + "name": "argocd", + "name_with_namespace": "test argocd proton / argocd", + "path": "argocd", + "path_with_namespace": "test-argocd-proton/argocd", + "created_at": "2021-06-01T17:30:44.724Z", + "default_branch": "master", + "tag_list": [], + "topics": [], + "ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git", + "http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git", + "web_url": "https://gitlab.com/test-argocd-proton/argocd", + "readme_url": null, + "avatar_url": null, + "forks_count": 0, + "star_count": 0, + "last_activity_at": "2021-06-04T08:19:51.656Z", + "namespace": { + "id": 12258515, + "name": "test argocd proton", + "path": "test-argocd-proton", + "kind": "group", + "full_path": "test-argocd-proton", + "parent_id": null, + "avatar_url": null, + "web_url": "https://gitlab.com/groups/test-argocd-proton" + } + }`) + if err != nil { + t.Fail() + } + case "/api/v4/projects/27084533/repository/tree?path=argocd&ref=master": + _, err := io.WriteString(w, `[{"id":"ca14f2a3718159c74572a5325fb4bfb0662a2d3e","name":"ingress.yaml","type":"blob","path":"argocd/ingress.yaml","mode":"100644"},{"id":"de2a53a73b1550b3e0f4d37ea0a6d878bf9c5096","name":"install.yaml","type":"blob","path":"argocd/install.yaml","mode":"100644"}]`) + if err != nil { + t.Fail() + } + case "/api/v4/projects/27084533/repository/tree?path=.&ref=master": + _, err := io.WriteString(w, `[{"id":"f2bf99fa8f7a27df9c43d2dffc8c8cd747f3181a","name":"argocd","type":"tree","path":"argocd","mode":"040000"},{"id":"68a3125232e01c1583a6a6299534ce10c5e7dd83","name":"manifests","type":"tree","path":"manifests","mode":"040000"}]`) + if err != nil { + t.Fail() + } + default: + _, err := io.WriteString(w, `[]`) + if err != nil { + t.Fail() + } + } + } +} func TestGitlabListRepos(t *testing.T) { cases := []struct { name, proto, url string @@ -40,18 +313,19 @@ func TestGitlabListRepos(t *testing.T) { name: "all branches", allBranches: true, url: "git@gitlab.com:test-argocd-proton/argocd.git", - branches: []string{"master", "pipeline-1310077506"}, + branches: []string{"master"}, }, } - + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gitlabMockHandler(t)(w, r) + })) for _, c := range cases { t.Run(c.name, func(t *testing.T) { - provider, _ := NewGitlabProvider(context.Background(), "test-argocd-proton", "", "", c.allBranches, c.includeSubgroups) + provider, _ := NewGitlabProvider(context.Background(), "test-argocd-proton", "", ts.URL, c.allBranches, c.includeSubgroups) rawRepos, err := ListRepos(context.Background(), provider, c.filters, c.proto) if c.hasError { assert.NotNil(t, err) } else { - checkRateLimit(t, err) assert.Nil(t, err) // Just check that this one project shows up. Not a great test but better thing nothing? repos := []*Repository{} @@ -73,7 +347,10 @@ func TestGitlabListRepos(t *testing.T) { } func TestGitlabHasPath(t *testing.T) { - host, _ := NewGitlabProvider(context.Background(), "test-argocd-proton", "", "", false, true) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gitlabMockHandler(t)(w, r) + })) + host, _ := NewGitlabProvider(context.Background(), "test-argocd-proton", "", ts.URL, false, true) repo := &Repository{ Organization: "test-argocd-proton", Repository: "argocd", diff --git a/cmd/argocd/commands/testdata/config b/cmd/argocd/commands/testdata/config index 3ed7c1e307ebe..0df9325b6d339 100644 --- a/cmd/argocd/commands/testdata/config +++ b/cmd/argocd/commands/testdata/config @@ -28,4 +28,4 @@ current-context: localhost:8080 users: - name: argocd1.example.com:443 - name: argocd2.example.com:443 -- name: localhost:8080 +- name: localhost:8080 \ No newline at end of file diff --git a/test/container/Procfile b/test/container/Procfile index cb7f917fb7b85..c0b30152a2190 100644 --- a/test/container/Procfile +++ b/test/container/Procfile @@ -1,8 +1,8 @@ -controller: sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_BINARY_NAME=argocd-application-controller go run ./cmd/main.go --loglevel debug --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379} --repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081}" -api-server: sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_BINARY_NAME=argocd-server go run ./cmd/main.go --loglevel debug --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379} --disable-auth=${ARGOCD_E2E_DISABLE_AUTH:-'true'} --insecure --dex-server http://localhost:${ARGOCD_E2E_DEX_PORT:-5556} --repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081} --port ${ARGOCD_E2E_APISERVER_PORT:-8080}" +controller: [ "$BIN_MODE" == 'true' ] && COMMAND=./dist/argocd || COMMAND='go run ./cmd/main.go' && sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_TLS_DATA_PATH=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} ARGOCD_SSH_DATA_PATH=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} ARGOCD_BINARY_NAME=argocd-application-controller $COMMAND --loglevel debug --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379} --repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081}" +api-server: [ "$BIN_MODE" == 'true' ] && COMMAND=./dist/argocd || COMMAND='go run ./cmd/main.go' && sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_BINARY_NAME=argocd-server $COMMAND --loglevel debug --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379} --disable-auth=${ARGOCD_E2E_DISABLE_AUTH:-'true'} --insecure --dex-server http://localhost:${ARGOCD_E2E_DEX_PORT:-5556} --repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081} --port ${ARGOCD_E2E_APISERVER_PORT:-8080} " dex: sh -c "test $ARGOCD_IN_CI = true && exit 0; ARGOCD_BINARY_NAME=argocd-dex go run github.com/argoproj/argo-cd/cmd gendexcfg -o `pwd`/dist/dex.yaml && docker run --rm -p ${ARGOCD_E2E_DEX_PORT:-5556}:${ARGOCD_E2E_DEX_PORT:-5556} -v `pwd`/dist/dex.yaml:/dex.yaml ghcr.io/dexidp/dex:v2.30.0 serve /dex.yaml" redis: sh -c "/usr/local/bin/redis-server --save "" --appendonly no --port ${ARGOCD_E2E_REDIS_PORT:-6379}" -repo-server: sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_GNUPGHOME=${ARGOCD_GNUPGHOME:-/tmp/argocd-local/gpg/keys} ARGOCD_PLUGINSOCKFILEPATH=${ARGOCD_PLUGINSOCKFILEPATH:-/tmp/argo-e2e/app/config/plugin} ARGOCD_GPG_DATA_PATH=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} ARGOCD_BINARY_NAME=argocd-repo-server go run ./cmd/main.go --loglevel debug --port ${ARGOCD_E2E_REPOSERVER_PORT:-8081} --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379}" +repo-server: [ "$BIN_MODE" == 'true' ] && COMMAND=./dist/argocd || COMMAND='go run ./cmd/main.go' && sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_GNUPGHOME=${ARGOCD_GNUPGHOME:-/tmp/argocd-local/gpg/keys} ARGOCD_PLUGINSOCKFILEPATH=${ARGOCD_PLUGINSOCKFILEPATH:-./test/cmp} ARGOCD_GPG_DATA_PATH=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} ARGOCD_BINARY_NAME=argocd-repo-server $COMMAND --loglevel debug --port ${ARGOCD_E2E_REPOSERVER_PORT:-8081} --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379}" ui: sh -c "test $ARGOCD_IN_CI = true && exit 0; cd ui && ARGOCD_E2E_YARN_HOST=0.0.0.0 ${ARGOCD_E2E_YARN_CMD:-yarn} start" reaper: ./test/container/reaper.sh sshd: sudo sh -c "test $ARGOCD_E2E_TEST = true && /usr/sbin/sshd -p 2222 -D -e" @@ -10,5 +10,5 @@ fcgiwrap: sudo sh -c "test $ARGOCD_E2E_TEST = true && (fcgiwrap -s unix:/var/run nginx: sudo sh -c "test $ARGOCD_E2E_TEST = true && nginx -g 'daemon off;' -c $(pwd)/test/fixture/testrepos/nginx.conf" helm-registry: sudo sh -c "registry serve /etc/docker/registry/config.yml" dev-mounter: test "$ARGOCD_E2E_TEST" != "true" && go run hack/dev-mounter/main.go --configmap argocd-ssh-known-hosts-cm=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} --configmap argocd-tls-certs-cm=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} --configmap argocd-gpg-keys-cm=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} -applicationset-controller: sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_ASK_PASS_SOCK=/tmp/applicationset-ask-pass.sock ARGOCD_BINARY_NAME=argocd-applicationset-controller go run ./cmd/main.go --loglevel debug --metrics-addr localhost:12345 --probe-addr localhost:12346 --namespace argocd-e2e --loglevel debug --argocd-repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081}" -notification: sh -c "FORCE_LOG_COLORS=4 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_TLS_DATA_PATH=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} ARGOCD_BINARY_NAME=argocd-notifications go run ./cmd/main.go --loglevel debug" \ No newline at end of file +applicationset-controller: [ "$BIN_MODE" == 'true' ] && COMMAND=./dist/argocd || COMMAND='go run ./cmd/main.go' && sh -c "FORCE_LOG_COLORS=4 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_TLS_DATA_PATH=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} ARGOCD_ASK_PASS_SOCK=/tmp/applicationset-ask-pass.sock ARGOCD_SSH_DATA_PATH=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} ARGOCD_BINARY_NAME=argocd-applicationset-controller $COMMAND --loglevel debug --metrics-addr localhost:12345 --probe-addr localhost:12346 --argocd-repo-server localhost:${ARGOCD_E2E_REPOSERVER_PORT:-8081}" +notification: sh -c "FORCE_LOG_COLORS=4 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_TLS_DATA_PATH=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} ARGOCD_BINARY_NAME=argocd-notifications go run ./cmd/main.go --loglevel debug"