From 7d7852d3efb0ae2ac73f6dcb416721adbe7d9476 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Thu, 29 Oct 2020 10:51:35 -0700 Subject: [PATCH 1/3] bump bazel to 3.4.1 Change-Id: I7ae75acba5518782a99ede2d896a787238d7b744 --- build/root/.bazelversion | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/root/.bazelversion b/build/root/.bazelversion index ccbccc3dc62..47b322c971c 100644 --- a/build/root/.bazelversion +++ b/build/root/.bazelversion @@ -1 +1 @@ -2.2.0 +3.4.1 From b6248683861b6a960d1ed7b82cadf670d81001e5 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Tue, 5 Jan 2021 19:31:59 -0800 Subject: [PATCH 2/3] bump repo-infra to 0.2.0 Change-Id: Id4fa13ca3cf092867bb0973294605962a8e5019c --- build/dependencies.yaml | 2 +- build/root/WORKSPACE | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build/dependencies.yaml b/build/dependencies.yaml index cd0bf6ff1be..c7e99f01741 100644 --- a/build/dependencies.yaml +++ b/build/dependencies.yaml @@ -17,7 +17,7 @@ dependencies: # Bazel - name: "repo-infra" - version: 0.1.3 + version: 0.2.0 refPaths: - path: build/root/WORKSPACE match: strip_prefix = "repo-infra-\d+.\d+.\d+" diff --git a/build/root/WORKSPACE b/build/root/WORKSPACE index 8ce0d2c0424..e89ca943cf2 100644 --- a/build/root/WORKSPACE +++ b/build/root/WORKSPACE @@ -5,10 +5,10 @@ load("//build:workspace_mirror.bzl", "mirror") http_archive( name = "io_k8s_repo_infra", - strip_prefix = "repo-infra-0.1.3", - sha256 = "46933bedbd22bc6a26ec6116d0d3e1475ad6b23447648d19abd6493241323311", + sha256 = "2f30f87259fed7262d9b95b2665e3d3ecd928d174a4f0356063bc99056b6f84c", + strip_prefix = "repo-infra-0.2.0", urls = [ - "https://github.com/kubernetes/repo-infra/archive/v0.1.3.tar.gz", + "https://github.com/kubernetes/repo-infra/archive/v0.2.0.tar.gz", ], ) @@ -24,7 +24,7 @@ load("@io_k8s_repo_infra//:repos.bzl", repo_infra_configure = "configure", repo_ # by kubernetes/repo-infra repo_infra_configure( go_version = "1.15.5", - minimum_bazel_version = "2.2.0", + minimum_bazel_version = "3.4.1", ) repo_infra_go_repositories() From 77261377dec3a22fef6cc7b5eb38322539bddee6 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Thu, 7 Jan 2021 15:26:18 -0800 Subject: [PATCH 3/3] 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 +}