From c72cfd5d32f3764128245c71a88c683a7dd9d0d0 Mon Sep 17 00:00:00 2001 From: Raghav Kaul <8695110+raghavkaul@users.noreply.github.com> Date: Thu, 29 Jun 2023 15:11:05 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20Gitlab:=20Move=20tests=20that=20?= =?UTF-8?q?connect=20to=20gitlab.com=20out=20of=20unit-tests=20(#3221)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move tests that connect to GitLab out of e2e Signed-off-by: Raghav Kaul * update Signed-off-by: Raghav Kaul * mark as pat test Signed-off-by: Raghav Kaul --------- Signed-off-by: Raghav Kaul --- Makefile | 2 +- clients/gitlabrepo/commits_e2e_test.go | 75 +++++++++++++++++++++++++ clients/gitlabrepo/commits_test.go | 45 --------------- clients/gitlabrepo/contributors_test.go | 53 ----------------- e2e/contributors_test.go | 26 +++++++++ 5 files changed, 102 insertions(+), 99 deletions(-) create mode 100644 clients/gitlabrepo/commits_e2e_test.go diff --git a/Makefile b/Makefile index fb27d9d3..076cc697 100644 --- a/Makefile +++ b/Makefile @@ -341,7 +341,7 @@ e2e-pat: build-scorecard check-env | $(GINKGO) e2e-gh-token: ## Runs e2e tests. Requires GITHUB_AUTH_TOKEN env var to be set to default GITHUB_TOKEN e2e-gh-token: build-scorecard check-env | $(GINKGO) # Run e2e tests. GITHUB_AUTH_TOKEN set to secrets.GITHUB_TOKEN must be used to run this. - TOKEN_TYPE="GITHUB_TOKEN" $(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out --keep-separate-coverprofiles ./... + GITLAB_AUTH_TOKEN="" TOKEN_TYPE="GITHUB_TOKEN" $(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out --keep-separate-coverprofiles ./... e2e-gitlab-token: ## Runs e2e tests that require a GITLAB_TOKEN e2e-gitlab-token: build-scorecard check-env-gitlab | $(GINKGO) diff --git a/clients/gitlabrepo/commits_e2e_test.go b/clients/gitlabrepo/commits_e2e_test.go new file mode 100644 index 00000000..cc187aa2 --- /dev/null +++ b/clients/gitlabrepo/commits_e2e_test.go @@ -0,0 +1,75 @@ +// Copyright 2023 OpenSSF Scorecard Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gitlabrepo + +import ( + "context" + "fmt" + "os" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +type tokenType int + +const ( + patTokenType tokenType = iota + githubWorkflowDefaultTokenType + gitlabPATTokenType +) + +var tokType tokenType + +func skipIfTokenIsNot(t tokenType, msg string) { + if tokType != t { + Skip(msg) + } +} + +var _ = BeforeSuite(func() { + tt := os.Getenv("TOKEN_TYPE") + switch tt { + case "PAT": + tokType = patTokenType + case "GITHUB_TOKEN": + tokType = githubWorkflowDefaultTokenType + case "GITLAB_PAT": + tokType = gitlabPATTokenType + default: + panic(fmt.Sprintf("invalid TOKEN_TYPE: %s", tt)) + } +}) + +var _ = Describe("E2E TEST: gitlabrepo.commitsHandler", func() { + Context("ListCommits", func() { + It("Checks whether commits are listed - GitLab", func() { + skipIfTokenIsNot(patTokenType, "PAT only") + repo, err := MakeGitlabRepo("https://gitlab.com/baserow/baserow") + Expect(err).Should(BeNil()) + + client, err := CreateGitlabClient(context.Background(), repo.Host()) + Expect(err).Should(BeNil()) + + err = client.InitRepo(repo, "8a38c9f724c19b5422e27864a108318d1f769b8a", 20) + Expect(err).Should(BeNil()) + + c, err := client.ListCommits() + Expect(err).Should(BeNil()) + + Expect(len(c)).Should(BeNumerically(">", 0)) + }) + }) +}) diff --git a/clients/gitlabrepo/commits_test.go b/clients/gitlabrepo/commits_test.go index 93c179fa..d7bc6b96 100644 --- a/clients/gitlabrepo/commits_test.go +++ b/clients/gitlabrepo/commits_test.go @@ -15,54 +15,9 @@ package gitlabrepo import ( - "context" "testing" ) -func Test_Setup(t *testing.T) { - t.Parallel() - tcs := []struct { - name string - repourl string - commit string - depth int - }{ - { - name: "check that listcommits works", - repourl: "https://gitlab.com/fdroid/fdroidclient", - commit: "a4bbef5c70fd2ac7c15437a22ef0f9ef0b447d08", - depth: 20, - }, - } - - for _, tt := range tcs { - t.Run(tt.name, func(t *testing.T) { - repo, err := MakeGitlabRepo(tt.repourl) - if err != nil { - t.Error("couldn't make gitlab repo", err) - } - - client, err := CreateGitlabClient(context.Background(), repo.Host()) - if err != nil { - t.Error("couldn't make gitlab client", err) - } - - err = client.InitRepo(repo, tt.commit, tt.depth) - if err != nil { - t.Error("couldn't init gitlab repo", err) - } - - c, err := client.ListCommits() - if err != nil { - t.Error("couldn't list gitlab repo commits", err) - } - if len(c) == 0 { - t.Error("couldn't get any commits from gitlab repo") - } - }) - } -} - func TestParsingEmail(t *testing.T) { t.Parallel() diff --git a/clients/gitlabrepo/contributors_test.go b/clients/gitlabrepo/contributors_test.go index 50eab3d7..4960a189 100644 --- a/clients/gitlabrepo/contributors_test.go +++ b/clients/gitlabrepo/contributors_test.go @@ -15,66 +15,13 @@ package gitlabrepo import ( - "context" - "fmt" "strconv" - "strings" "sync" "testing" "github.com/xanzy/go-gitlab" ) -func Test_ContributorsSetup(t *testing.T) { - t.Parallel() - tcs := []struct { - name string - repourl string - commit string - depth int - }{ - { - name: "check that Contributor works", - repourl: "https://gitlab.com/fdroid/fdroidclient", - commit: "HEAD", - depth: 20, - }, - } - - for _, tt := range tcs { - t.Run(tt.name, func(t *testing.T) { - repo, err := MakeGitlabRepo(tt.repourl) - if err != nil { - t.Error("couldn't make gitlab repo", err) - } - - client, err := CreateGitlabClientWithToken(context.Background(), "", repo.Host()) - if err != nil { - t.Error("couldn't make gitlab client", err) - } - - err = client.InitRepo(repo, tt.commit, tt.depth) - if err != nil { - t.Error("couldn't init gitlab repo", - err) - } - - c, err := client.ListContributors() - // Authentication is failing when querying users, not sure yet how to get around that - if err != nil { - errMsg := fmt.Sprintf("%v", err) - - if !(strings.Contains(errMsg, "error during Users.Get") && strings.Contains(errMsg, "401")) { - t.Error("couldn't list gitlab repo contributors", err) - } - } - if len(c) != 0 { - t.Error("couldn't get any contributors from gitlab repo") - } - }) - } -} - func TestContributors(t *testing.T) { t.Parallel() diff --git a/e2e/contributors_test.go b/e2e/contributors_test.go index 92c6a789..e3cce8aa 100644 --- a/e2e/contributors_test.go +++ b/e2e/contributors_test.go @@ -16,6 +16,8 @@ package e2e import ( "context" + "fmt" + "strings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -24,6 +26,7 @@ import ( "github.com/ossf/scorecard/v4/checks" "github.com/ossf/scorecard/v4/clients" "github.com/ossf/scorecard/v4/clients/githubrepo" + "github.com/ossf/scorecard/v4/clients/gitlabrepo" scut "github.com/ossf/scorecard/v4/utests" ) @@ -54,5 +57,28 @@ var _ = Describe("E2E TEST:"+checks.CheckContributors, func() { Expect(scut.ValidateTestReturn(nil, "several contributors", &expected, &result, &dl)).Should(BeTrue()) Expect(repoClient.Close()).Should(BeNil()) }) + + It("Should return valid project contributors - GitLab", func() { + skipIfTokenIsNot(gitlabPATTokenType, "PAT only") + repo, err := gitlabrepo.MakeGitlabRepo("https://gitlab.com/baserow/baserow") + Expect(err).Should(BeNil()) + + client, err := gitlabrepo.CreateGitlabClient(context.Background(), repo.Host()) + Expect(err).Should(BeNil()) + + err = client.InitRepo(repo, "HEAD", 20) + Expect(err).Should(BeNil()) + + c, err := client.ListContributors() + // Authentication is failing when querying users, not sure yet how to get around that + if err != nil { + errMsg := fmt.Sprintf("%v", err) + + if !(strings.Contains(errMsg, "error during Users.Get") && strings.Contains(errMsg, "401")) { + Fail(fmt.Sprintf("couldn't list gitlab repo contributors: %v", err)) + } + } + Expect(len(c)).Should(BeNumerically(">", 0)) + }) }) })