Add tests for graphQL costs (#1643)

Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
Azeem Shaikh 2022-02-15 15:38:23 -08:00 committed by GitHub
parent de5224bbc5
commit cda7a1b1d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 188 additions and 3 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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())
})
})
})

View File

@ -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)
})

View File

@ -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 {

View File

@ -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))
})
})
})

View File

@ -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)
})