Resolve paths of test fixtures at runtime
This will help with bazel tests, which seem to use a different working directory from local test runs.
This commit is contained in:
parent
90012e513a
commit
0cbe25436d
@ -45,6 +45,7 @@ go_test(
|
||||
deps = [
|
||||
"//pkg/cloudprovider:go_default_library",
|
||||
"//pkg/cloudprovider/providers/vsphere/vclib:go_default_library",
|
||||
"//pkg/cloudprovider/providers/vsphere/vclib/fixtures:go_default_library",
|
||||
"//pkg/controller:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
|
@ -52,6 +52,7 @@ filegroup(
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/cloudprovider/providers/vsphere/vclib/diskmanagers:all-srcs",
|
||||
"//pkg/cloudprovider/providers/vsphere/vclib/fixtures:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
@ -76,5 +77,8 @@ go_test(
|
||||
go_test(
|
||||
name = "go_default_xtest",
|
||||
srcs = ["connection_test.go"],
|
||||
deps = [":go_default_library"],
|
||||
deps = [
|
||||
":go_default_library",
|
||||
"//pkg/cloudprovider/providers/vsphere/vclib/fixtures:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/fixtures"
|
||||
)
|
||||
|
||||
func createTestServer(t *testing.T, caCertPath, serverCertPath, serverKeyPath string, handler http.HandlerFunc) *httptest.Server {
|
||||
@ -57,16 +58,12 @@ func createTestServer(t *testing.T, caCertPath, serverCertPath, serverKeyPath st
|
||||
}
|
||||
|
||||
func TestWithValidCaCert(t *testing.T) {
|
||||
caCertPath := "fixtures/ca.pem"
|
||||
serverCertPath := "fixtures/server.pem"
|
||||
serverKeyPath := "fixtures/server.key"
|
||||
|
||||
gotRequest := false
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
gotRequest = true
|
||||
}
|
||||
|
||||
server := createTestServer(t, caCertPath, serverCertPath, serverKeyPath, handler)
|
||||
server := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||
server.StartTLS()
|
||||
|
||||
u, err := url.Parse(server.URL)
|
||||
@ -77,7 +74,7 @@ func TestWithValidCaCert(t *testing.T) {
|
||||
connection := &vclib.VSphereConnection{
|
||||
Hostname: u.Hostname(),
|
||||
Port: u.Port(),
|
||||
CACert: "fixtures/ca.pem",
|
||||
CACert: fixtures.CaCertPath,
|
||||
}
|
||||
|
||||
// Ignoring error here, because we only care about the TLS connection
|
||||
@ -106,7 +103,7 @@ func TestInvalidCaCert(t *testing.T) {
|
||||
connection := &vclib.VSphereConnection{
|
||||
Hostname: "should-not-matter",
|
||||
Port: "should-not-matter",
|
||||
CACert: "fixtures/invalid.pem",
|
||||
CACert: fixtures.InvalidCaCertPath,
|
||||
}
|
||||
|
||||
_, err := connection.NewClient(context.Background())
|
||||
@ -122,7 +119,7 @@ func TestUnsupportedTransport(t *testing.T) {
|
||||
connection := &vclib.VSphereConnection{
|
||||
Hostname: "should-not-matter",
|
||||
Port: "should-not-matter",
|
||||
CACert: "fixtures/ca.pem",
|
||||
CACert: fixtures.CaCertPath,
|
||||
}
|
||||
|
||||
err := connection.ConfigureTransportWithCA(notHttpTransport)
|
||||
|
26
pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD
Normal file
26
pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD
Normal file
@ -0,0 +1,26 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["fixtures.go"],
|
||||
data = glob([
|
||||
"*.pem",
|
||||
"*.key",
|
||||
]),
|
||||
importpath = "k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/fixtures",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes 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 fixtures
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
CaCertPath string
|
||||
ServerCertPath string
|
||||
ServerKeyPath string
|
||||
InvalidCaCertPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, thisFile, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
panic("Cannot get path to the fixtures")
|
||||
}
|
||||
|
||||
fixturesDir := filepath.Dir(thisFile)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
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)
|
||||
}
|
||||
|
||||
CaCertPath = filepath.Join(fixturesDir, "ca.pem")
|
||||
ServerCertPath = filepath.Join(fixturesDir, "server.pem")
|
||||
ServerKeyPath = filepath.Join(fixturesDir, "server.key")
|
||||
InvalidCaCertPath = filepath.Join(fixturesDir, "invalid.pem")
|
||||
}
|
@ -35,6 +35,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/fixtures"
|
||||
)
|
||||
|
||||
// localhostCert was generated from crypto/tls/generate_cert.go with the following command:
|
||||
@ -267,16 +268,12 @@ func TestVSphereLoginByToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVSphereLoginWithCaCert(t *testing.T) {
|
||||
caCertPath := "./vclib/fixtures/ca.pem"
|
||||
serverCertPath := "./vclib/fixtures/server.pem"
|
||||
serverKeyPath := "./vclib/fixtures/server.key"
|
||||
|
||||
caCertPEM, err := ioutil.ReadFile(caCertPath)
|
||||
caCertPEM, err := ioutil.ReadFile(fixtures.CaCertPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not read ca cert from file")
|
||||
}
|
||||
|
||||
serverCert, err := tls.LoadX509KeyPair(serverCertPath, serverKeyPath)
|
||||
serverCert, err := tls.LoadX509KeyPair(fixtures.ServerCertPath, fixtures.ServerKeyPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load server cert and server key from files: %#v", err)
|
||||
}
|
||||
@ -294,7 +291,7 @@ func TestVSphereLoginWithCaCert(t *testing.T) {
|
||||
cfg, cleanup := configFromSimWithTLS(&tlsConfig, false)
|
||||
defer cleanup()
|
||||
|
||||
cfg.Global.CAFile = caCertPath
|
||||
cfg.Global.CAFile = fixtures.CaCertPath
|
||||
|
||||
// Create vSphere configuration object
|
||||
vs, err := newControllerNode(cfg)
|
||||
|
Loading…
Reference in New Issue
Block a user