diff --git a/Makefile b/Makefile index 4a7914ff..9fee00f4 100644 --- a/Makefile +++ b/Makefile @@ -261,7 +261,7 @@ test: $(test-targets) unit-test: ## Runs unit test without e2e # Run unit tests, ignoring e2e tests # run the go tests and gen the file coverage-all used to do the integration with codecov - go test -race -covermode=atomic -coverprofile=unit-coverage.out `go list ./... | grep -v e2e` + SKIP_GINKGO=1 go test -race -covermode=atomic -coverprofile=unit-coverage.out `go list ./...` $(GINKGO): install diff --git a/clients/githubrepo/branches.go b/clients/githubrepo/branches.go index 4bb7eba8..efc814ef 100644 --- a/clients/githubrepo/branches.go +++ b/clients/githubrepo/branches.go @@ -98,6 +98,7 @@ type branch struct { BranchProtectionRule *branchProtectionRule } +// nolint:govet // internal structure, ignore. type branchesData struct { Repository struct { DefaultBranchRef branch @@ -105,6 +106,9 @@ type branchesData struct { Nodes []branch } `graphql:"refs(first: $refsToAnalyze, refPrefix: $refPrefix)"` } `graphql:"repository(owner: $owner, name: $name)"` + RateLimit struct { + Cost *int + } } type branchesHandler struct { diff --git a/clients/githubrepo/branches_e2e_test.go b/clients/githubrepo/branches_e2e_test.go new file mode 100644 index 00000000..e905dd80 --- /dev/null +++ b/clients/githubrepo/branches_e2e_test.go @@ -0,0 +1,59 @@ +// Copyright 2021 Security 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 githubrepo + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/ossf/scorecard/v4/clients" +) + +var _ = Describe("E2E TEST: githubrepo.branchesHandler", func() { + var brancheshandler *branchesHandler + + BeforeEach(func() { + brancheshandler = &branchesHandler{ + graphClient: graphClient, + } + }) + + Context("E2E TEST: Validate query cost", func() { + It("Should not have increased for HEAD query", func() { + repourl := &repoURL{ + owner: "ossf", + repo: "scorecard", + commitSHA: clients.HeadSHA, + } + brancheshandler.init(context.Background(), repourl) + Expect(brancheshandler.setup()).Should(BeNil()) + Expect(brancheshandler.data).ShouldNot(BeNil()) + Expect(brancheshandler.data.RateLimit.Cost).ShouldNot(BeNil()) + Expect(*brancheshandler.data.RateLimit.Cost).Should(BeNumerically("<=", 1)) + }) + It("Should fail for non-HEAD query", func() { + repourl := &repoURL{ + owner: "ossf", + repo: "scorecard", + commitSHA: "de5224bbc56eceb7a25aece55d2d53bbc561ed2d", + } + brancheshandler.init(context.Background(), repourl) + Expect(brancheshandler.setup()).ShouldNot(BeNil()) + Expect(brancheshandler.data).Should(BeNil()) + }) + }) +}) diff --git a/clients/githubrepo/githubrepo_suite_test.go b/clients/githubrepo/githubrepo_suite_test.go new file mode 100644 index 00000000..13ecb8fb --- /dev/null +++ b/clients/githubrepo/githubrepo_suite_test.go @@ -0,0 +1,50 @@ +// Copyright 2021 Security 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 githubrepo + +import ( + "context" + "net/http" + "os" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/shurcooL/githubv4" + + "github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper" + "github.com/ossf/scorecard/v4/log" +) + +func TestGithubrepo(t *testing.T) { + if val, exists := os.LookupEnv("SKIP_GINKGO"); exists && val == "1" { + t.Skip() + } + t.Parallel() + RegisterFailHandler(Fail) + RunSpecs(t, "Githubrepo Suite") +} + +var graphClient *githubv4.Client + +var _ = BeforeSuite(func() { + ctx := context.Background() + logger := log.NewLogger(log.DebugLevel) + rt := roundtripper.NewTransport(ctx, logger) + httpClient := &http.Client{ + Transport: rt, + } + graphClient = githubv4.NewClient(httpClient) +}) diff --git a/clients/githubrepo/graphql.go b/clients/githubrepo/graphql.go index da7c8d88..9cd388a5 100644 --- a/clients/githubrepo/graphql.go +++ b/clients/githubrepo/graphql.go @@ -106,6 +106,9 @@ type graphqlData struct { } } `graphql:"issues(first: $issuesToAnalyze, orderBy:{field:UPDATED_AT, direction:DESC})"` } `graphql:"repository(owner: $owner, name: $name)"` + RateLimit struct { + Cost *int + } } type graphqlHandler struct { diff --git a/clients/githubrepo/graphql_e2e_test.go b/clients/githubrepo/graphql_e2e_test.go new file mode 100644 index 00000000..89b48fa5 --- /dev/null +++ b/clients/githubrepo/graphql_e2e_test.go @@ -0,0 +1,61 @@ +// Copyright 2021 Security 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 githubrepo + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/ossf/scorecard/v4/clients" +) + +var _ = Describe("E2E TEST: githubrepo.graphqlHandler", func() { + var graphqlhandler *graphqlHandler + + BeforeEach(func() { + graphqlhandler = &graphqlHandler{ + client: graphClient, + } + }) + + Context("E2E TEST: Validate query cost", func() { + It("Should not have increased for HEAD query", func() { + repourl := &repoURL{ + owner: "ossf", + repo: "scorecard", + commitSHA: clients.HeadSHA, + } + graphqlhandler.init(context.Background(), repourl) + Expect(graphqlhandler.setup()).Should(BeNil()) + Expect(graphqlhandler.data).ShouldNot(BeNil()) + Expect(graphqlhandler.data.RateLimit.Cost).ShouldNot(BeNil()) + Expect(*graphqlhandler.data.RateLimit.Cost).Should(BeNumerically("<=", 1)) + }) + It("Should not have increased for commit query", func() { + repourl := &repoURL{ + owner: "ossf", + repo: "scorecard", + commitSHA: "de5224bbc56eceb7a25aece55d2d53bbc561ed2d", + } + graphqlhandler.init(context.Background(), repourl) + Expect(graphqlhandler.setup()).Should(BeNil()) + Expect(graphqlhandler.data).ShouldNot(BeNil()) + Expect(graphqlhandler.data.RateLimit.Cost).ShouldNot(BeNil()) + Expect(*graphqlhandler.data.RateLimit.Cost).Should(BeNumerically("<=", 1)) + }) + }) +}) diff --git a/e2e/e2e_suite_test.go b/e2e/e2e_suite_test.go index 3170c16d..0e587652 100644 --- a/e2e/e2e_suite_test.go +++ b/e2e/e2e_suite_test.go @@ -15,6 +15,7 @@ package e2e import ( + "os" "testing" . "github.com/onsi/ginkgo/v2" @@ -23,10 +24,17 @@ import ( "github.com/ossf/scorecard/v4/log" ) -var logger *log.Logger - func TestE2e(t *testing.T) { + if val, exists := os.LookupEnv("SKIP_GINKGO"); exists && val == "1" { + t.Skip() + } t.Parallel() RegisterFailHandler(Fail) RunSpecs(t, "E2e Suite") } + +var logger *log.Logger + +var _ = BeforeSuite(func() { + logger = log.NewLogger(log.DebugLevel) +})