Merge pull request #4716 from munnerz/allow-integration-with-delve

Re-organise and extend path loading logic to make it easier to run integration tests using Delve/GoLand
This commit is contained in:
jetstack-bot 2022-01-06 19:25:23 +00:00 committed by GitHub
commit 2ebfdd29ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 69 additions and 23 deletions

View File

@ -16,6 +16,7 @@ filegroup(
"//internal/apis/config/webhook:all-srcs",
"//internal/apis/meta:all-srcs",
"//internal/ingress:all-srcs",
"//internal/test/paths:all-srcs",
"//internal/vault:all-srcs",
],
tags = ["automanaged"],

View File

@ -45,8 +45,8 @@ go_test(
embed = [":go_default_library"],
deps = [
"//internal/apis/acme/fuzzer:go_default_library",
"//internal/test/paths:go_default_library",
"//pkg/api:go_default_library",
"//pkg/api/testing:go_default_library",
"@com_github_munnerz_crd_schema_fuzz//:go_default_library",
"@io_k8s_apimachinery//pkg/api/apitesting/roundtrip:go_default_library",
],

View File

@ -22,8 +22,8 @@ import (
crdfuzz "github.com/munnerz/crd-schema-fuzz"
acmefuzzer "github.com/jetstack/cert-manager/internal/apis/acme/fuzzer"
apitesting "github.com/jetstack/cert-manager/internal/test/paths"
"github.com/jetstack/cert-manager/pkg/api"
apitesting "github.com/jetstack/cert-manager/pkg/api/testing"
)
func TestPruneTypes(t *testing.T) {

View File

@ -47,8 +47,8 @@ go_test(
embed = [":go_default_library"],
deps = [
"//internal/apis/certmanager/fuzzer:go_default_library",
"//internal/test/paths:go_default_library",
"//pkg/api:go_default_library",
"//pkg/api/testing:go_default_library",
"@com_github_munnerz_crd_schema_fuzz//:go_default_library",
"@io_k8s_apimachinery//pkg/api/apitesting/roundtrip:go_default_library",
],

View File

@ -22,8 +22,8 @@ import (
crdfuzz "github.com/munnerz/crd-schema-fuzz"
cmfuzzer "github.com/jetstack/cert-manager/internal/apis/certmanager/fuzzer"
apitesting "github.com/jetstack/cert-manager/internal/test/paths"
"github.com/jetstack/cert-manager/pkg/api"
apitesting "github.com/jetstack/cert-manager/pkg/api/testing"
)
func TestPruneTypes(t *testing.T) {

View File

@ -2,12 +2,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["bazel.go"],
srcs = [
"bazel.go",
"paths.go",
],
data = [
"//deploy/crds:crds.yaml",
],
importpath = "github.com/jetstack/cert-manager/pkg/api/testing",
visibility = ["//visibility:public"],
importpath = "github.com/jetstack/cert-manager/internal/test/paths",
visibility = ["//:__subpackages__"],
)
filegroup(

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package testing
package paths
import (
"fmt"
@ -41,20 +41,15 @@ func PathForCRD(t *testing.T, name string) string {
}
func CRDDirectory(t *testing.T) string {
bazelDir := BazelBinDir
path := filepath.Join(bazelDir, "deploy", "crds")
// prefer RUNFILES_DIR if set
runfiles := os.Getenv("RUNFILES_DIR")
// BAZEL_BIN_DIR allows the developer to set a path to the bazel bin directory.
// This allows for the tests to be ran outside of Bazel, for example with Delve
// the Bazel bin directory still needs to be generated using Bazel.
bazelDir := os.Getenv("BAZEL_BIN_DIR")
if runfiles == "" && bazelDir == "" {
t.Fatalf("integration tests can only run within 'bazel test' environment or have BAZEL_BIN_DIR set")
}
var path string
if bazelDir != "" {
path = filepath.Join(bazelDir, "deploy", "crds")
} else {
if runfiles != "" {
path = filepath.Join(runfiles, "com_github_jetstack_cert_manager", "deploy", "crds")
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)

View File

@ -0,0 +1,35 @@
/*
Copyright 2022 The cert-manager 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 paths
import (
"path/filepath"
"runtime"
)
var (
_, b, _, _ = runtime.Caller(0)
// ModuleRootDir is the filesystem path to the root of the repository.
ModuleRootDir = filepath.Join(filepath.Dir(b), "../../..")
// BazelBinDir is the filesystem path to the bazel-bin directory within the
// root of the repository.
// This will not be accessible when running within the bazel sandbox, but
// is useful for reading bazel files when running commands with `go test`.
BazelBinDir = filepath.Join(ModuleRootDir, "bazel-bin")
)

View File

@ -38,7 +38,6 @@ filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/api/testing:all-srcs",
"//pkg/api/util:all-srcs",
],
tags = ["automanaged"],

View File

@ -11,8 +11,8 @@ go_library(
deps = [
"//cmd/webhook/app:go_default_library",
"//cmd/webhook/app/testing:go_default_library",
"//internal/test/paths:go_default_library",
"//pkg/api:go_default_library",
"//pkg/api/testing:go_default_library",
"//pkg/client/clientset/versioned:go_default_library",
"//pkg/client/informers/externalversions:go_default_library",
"//pkg/controller:go_default_library",

View File

@ -40,8 +40,8 @@ import (
"github.com/jetstack/cert-manager/cmd/webhook/app"
webhooktesting "github.com/jetstack/cert-manager/cmd/webhook/app/testing"
"github.com/jetstack/cert-manager/internal/test/paths"
"github.com/jetstack/cert-manager/pkg/api"
apitesting "github.com/jetstack/cert-manager/pkg/api/testing"
"github.com/jetstack/cert-manager/pkg/webhook/handlers"
"github.com/jetstack/cert-manager/test/internal/apiserver"
)
@ -75,7 +75,7 @@ func WithWebhookConversionHandler(handler handlers.ConversionHook) RunControlPla
func RunControlPlane(t *testing.T, ctx context.Context, optionFunctions ...RunControlPlaneOption) (*rest.Config, StopFunc) {
options := &controlPlaneOptions{
crdsDir: pointer.StringPtr(apitesting.CRDDirectory(t)),
crdsDir: pointer.StringPtr(paths.CRDDirectory(t)),
}
for _, f := range optionFunctions {

View File

@ -14,6 +14,7 @@ go_library(
importpath = "github.com/jetstack/cert-manager/test/internal/apiserver",
visibility = ["//test:__subpackages__"],
deps = [
"//internal/test/paths:go_default_library",
"//test/internal/util:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/envtest:go_default_library",
],

View File

@ -20,7 +20,9 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/jetstack/cert-manager/internal/test/paths"
"github.com/jetstack/cert-manager/test/internal/util"
)
@ -45,11 +47,21 @@ Either re-run this test or set the %s environment variable.`, bin, key))
}
func getPath(name string, path ...string) (string, error) {
// Check to see if we are running in a `bazel test` environment and if so,
// use the RUNFILES_DIR environment variable to find dependencies.
bazelPath := util.GetTestPath(path...)
p, err := exec.LookPath(bazelPath)
if err == nil {
return p, nil
}
// Check for a bazel-bin directory which may contain the test dependencies
nextBazelPath := filepath.Join(append([]string{paths.BazelBinDir}, path...)...)
p, err = exec.LookPath(nextBazelPath)
if err == nil {
return p, nil
}
// Otherwise check the users PATH
return exec.LookPath(name)
}