From 8ff84e8b70abbffde0deed22a274235d329af090 Mon Sep 17 00:00:00 2001 From: James Munnelly Date: Thu, 6 Jan 2022 15:12:58 +0000 Subject: [PATCH] Re-organise and extend path loading logic to make it easier to run integration tests using Delve/GoLand Signed-off-by: James Munnelly --- internal/BUILD.bazel | 1 + internal/apis/acme/install/BUILD.bazel | 2 +- internal/apis/acme/install/pruning_test.go | 2 +- internal/apis/certmanager/install/BUILD.bazel | 2 +- .../apis/certmanager/install/pruning_test.go | 2 +- .../test/paths}/BUILD.bazel | 9 +++-- .../testing => internal/test/paths}/bazel.go | 7 ++-- internal/test/paths/paths.go | 35 +++++++++++++++++++ pkg/api/BUILD.bazel | 1 - test/integration/framework/BUILD.bazel | 2 +- test/integration/framework/apiserver.go | 4 +-- test/internal/apiserver/BUILD.bazel | 1 + test/internal/apiserver/envs.go | 12 +++++++ 13 files changed, 64 insertions(+), 16 deletions(-) rename {pkg/api/testing => internal/test/paths}/BUILD.bazel (71%) rename {pkg/api/testing => internal/test/paths}/bazel.go (86%) create mode 100644 internal/test/paths/paths.go diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 158de8e87..76f2f0861 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -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"], diff --git a/internal/apis/acme/install/BUILD.bazel b/internal/apis/acme/install/BUILD.bazel index 786f19181..10114a2d8 100644 --- a/internal/apis/acme/install/BUILD.bazel +++ b/internal/apis/acme/install/BUILD.bazel @@ -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", ], diff --git a/internal/apis/acme/install/pruning_test.go b/internal/apis/acme/install/pruning_test.go index 9c82bdb00..a51f6730a 100644 --- a/internal/apis/acme/install/pruning_test.go +++ b/internal/apis/acme/install/pruning_test.go @@ -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) { diff --git a/internal/apis/certmanager/install/BUILD.bazel b/internal/apis/certmanager/install/BUILD.bazel index 5e2f5c375..149c31c9b 100644 --- a/internal/apis/certmanager/install/BUILD.bazel +++ b/internal/apis/certmanager/install/BUILD.bazel @@ -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", ], diff --git a/internal/apis/certmanager/install/pruning_test.go b/internal/apis/certmanager/install/pruning_test.go index ab8c39ae3..de12eb8a4 100644 --- a/internal/apis/certmanager/install/pruning_test.go +++ b/internal/apis/certmanager/install/pruning_test.go @@ -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) { diff --git a/pkg/api/testing/BUILD.bazel b/internal/test/paths/BUILD.bazel similarity index 71% rename from pkg/api/testing/BUILD.bazel rename to internal/test/paths/BUILD.bazel index 927ac940a..468ff912e 100644 --- a/pkg/api/testing/BUILD.bazel +++ b/internal/test/paths/BUILD.bazel @@ -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( diff --git a/pkg/api/testing/bazel.go b/internal/test/paths/bazel.go similarity index 86% rename from pkg/api/testing/bazel.go rename to internal/test/paths/bazel.go index b35f9226d..2567debe5 100644 --- a/pkg/api/testing/bazel.go +++ b/internal/test/paths/bazel.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package testing +package paths import ( "fmt" @@ -42,10 +42,7 @@ func PathForCRD(t *testing.T, name string) string { func CRDDirectory(t *testing.T) string { 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") + bazelDir := BazelBinDir if runfiles == "" && bazelDir == "" { t.Fatalf("integration tests can only run within 'bazel test' environment or have BAZEL_BIN_DIR set") } diff --git a/internal/test/paths/paths.go b/internal/test/paths/paths.go new file mode 100644 index 000000000..21b2ee0ba --- /dev/null +++ b/internal/test/paths/paths.go @@ -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") +) diff --git a/pkg/api/BUILD.bazel b/pkg/api/BUILD.bazel index a32a6fad5..ff32bdacc 100644 --- a/pkg/api/BUILD.bazel +++ b/pkg/api/BUILD.bazel @@ -38,7 +38,6 @@ filegroup( name = "all-srcs", srcs = [ ":package-srcs", - "//pkg/api/testing:all-srcs", "//pkg/api/util:all-srcs", ], tags = ["automanaged"], diff --git a/test/integration/framework/BUILD.bazel b/test/integration/framework/BUILD.bazel index c00dec2b1..9ebf92b05 100644 --- a/test/integration/framework/BUILD.bazel +++ b/test/integration/framework/BUILD.bazel @@ -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", diff --git a/test/integration/framework/apiserver.go b/test/integration/framework/apiserver.go index 3b2d4b09e..752db3b4a 100644 --- a/test/integration/framework/apiserver.go +++ b/test/integration/framework/apiserver.go @@ -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 { diff --git a/test/internal/apiserver/BUILD.bazel b/test/internal/apiserver/BUILD.bazel index d8886869b..e545a20c3 100644 --- a/test/internal/apiserver/BUILD.bazel +++ b/test/internal/apiserver/BUILD.bazel @@ -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", ], diff --git a/test/internal/apiserver/envs.go b/test/internal/apiserver/envs.go index ecf9355e4..4108b000f 100644 --- a/test/internal/apiserver/envs.go +++ b/test/internal/apiserver/envs.go @@ -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) }