Skip to content

Commit

Permalink
Add tests for graphQL costs (#1643)
Browse files Browse the repository at this point in the history
Co-authored-by: Azeem Shaikh <[email protected]>
  • Loading branch information
azeemshaikh38 and azeemsgoogle authored Feb 15, 2022
1 parent de5224b commit cda7a1b
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions clients/githubrepo/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ type branch struct {
BranchProtectionRule *branchProtectionRule
}

// nolint:govet // internal structure, ignore.
type branchesData struct {
Repository struct {
DefaultBranchRef branch
Refs struct {
Nodes []branch
} `graphql:"refs(first: $refsToAnalyze, refPrefix: $refPrefix)"`
} `graphql:"repository(owner: $owner, name: $name)"`
RateLimit struct {
Cost *int
}
}

type branchesHandler struct {
Expand Down
59 changes: 59 additions & 0 deletions clients/githubrepo/branches_e2e_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
})
50 changes: 50 additions & 0 deletions clients/githubrepo/githubrepo_suite_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
3 changes: 3 additions & 0 deletions clients/githubrepo/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 61 additions & 0 deletions clients/githubrepo/graphql_e2e_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
12 changes: 10 additions & 2 deletions e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package e2e

import (
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -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)
})

0 comments on commit cda7a1b

Please sign in to comment.