mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
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
This commit is contained in:
parent
b624868386
commit
77261377de
@ -17,10 +17,10 @@ limitations under the License.
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -38,24 +38,9 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, thisFile, _, ok := runtime.Caller(0)
|
fixturesDir, err := pkgPath()
|
||||||
if !ok {
|
|
||||||
panic("Cannot get path to the fixtures")
|
|
||||||
}
|
|
||||||
|
|
||||||
fixturesDir := filepath.Dir(thisFile)
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Cannot get CWD: " + err.Error())
|
panic(fmt.Sprintf("Cannot get path to the fixtures: %s", err))
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CaCertPath = filepath.Join(fixturesDir, "ca.pem")
|
CaCertPath = filepath.Join(fixturesDir, "ca.pem")
|
||||||
@ -63,3 +48,33 @@ func init() {
|
|||||||
ServerKeyPath = filepath.Join(fixturesDir, "server.key")
|
ServerKeyPath = filepath.Join(fixturesDir, "server.key")
|
||||||
InvalidCertPath = filepath.Join(fixturesDir, "invalid.pem")
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user