mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-11 08:55:27 +03:00
Add tests for graphQL costs (#1643)
Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
parent
de5224bbc5
commit
cda7a1b1d4
2
Makefile
2
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
|
||||
|
||||
|
@ -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 {
|
||||
|
59
clients/githubrepo/branches_e2e_test.go
Normal file
59
clients/githubrepo/branches_e2e_test.go
Normal 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())
|
||||
})
|
||||
})
|
||||
})
|
50
clients/githubrepo/githubrepo_suite_test.go
Normal file
50
clients/githubrepo/githubrepo_suite_test.go
Normal 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)
|
||||
})
|
@ -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 {
|
||||
|
61
clients/githubrepo/graphql_e2e_test.go
Normal file
61
clients/githubrepo/graphql_e2e_test.go
Normal 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))
|
||||
})
|
||||
})
|
||||
})
|
@ -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)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user