From 77261377dec3a22fef6cc7b5eb38322539bddee6 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Thu, 7 Jan 2021 15:26:18 -0800 Subject: [PATCH] fixup fixture logic There was a bug with init() but it was resolved in https://github.com/bazelbuild/rules_go/pull/2696 Changed to match other fixture methods. Change-Id: I882b8535e5c5c117fb10c41d34c8eed1ccdb74bb --- .../vsphere/vclib/fixtures/fixtures.go | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go index 610dbf9e10c..7ef4dc0347c 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go +++ b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go @@ -17,10 +17,10 @@ limitations under the License. package fixtures import ( + "fmt" "os" "path/filepath" "runtime" - "strings" ) var ( @@ -38,24 +38,9 @@ var ( ) func init() { - _, thisFile, _, ok := runtime.Caller(0) - if !ok { - panic("Cannot get path to the fixtures") - } - - fixturesDir := filepath.Dir(thisFile) - - cwd, err := os.Getwd() + fixturesDir, err := pkgPath() if err != nil { - panic("Cannot get CWD: " + err.Error()) - } - - // When tests run in a bazel sandbox `runtime.Caller()` - // returns a relative path, when run with plain `go test` the path - // returned is absolute. To make those fixtures work in both those cases, - // we prepend the CWD iff the CWD is not yet part of the path to the fixtures. - if !strings.HasPrefix(fixturesDir, cwd) { - fixturesDir = filepath.Join(cwd, fixturesDir) + panic(fmt.Sprintf("Cannot get path to the fixtures: %s", err)) } CaCertPath = filepath.Join(fixturesDir, "ca.pem") @@ -63,3 +48,33 @@ func init() { ServerKeyPath = filepath.Join(fixturesDir, "server.key") InvalidCertPath = filepath.Join(fixturesDir, "invalid.pem") } + +// pkgPath returns the absolute file path to this package's directory. With go +// test, we can just look at the runtime call stack. However, bazel compiles go +// binaries with the -trimpath option so the simple approach fails however we +// can consult environment variables to derive the path. +// +// The approach taken here works for both go test and bazel on the assumption +// that if and only if trimpath is passed, we are running under bazel. +func pkgPath() (string, error) { + _, thisFile, _, ok := runtime.Caller(1) + if !ok { + return "", fmt.Errorf("failed to get current file") + } + + pkgPath := filepath.Dir(thisFile) + + // If we find bazel env variables, then -trimpath was passed so we need to + // construct the path from the environment. + if testSrcdir, testWorkspace := os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"); testSrcdir != "" && testWorkspace != "" { + pkgPath = filepath.Join(testSrcdir, testWorkspace, pkgPath) + } + + // If the path is still not absolute, something other than bazel compiled + // with -trimpath. + if !filepath.IsAbs(pkgPath) { + return "", fmt.Errorf("can't construct an absolute path from %q", pkgPath) + } + + return pkgPath, nil +}