From 0cbe25436d500a45c34c72a81d14a1be4b88f8ff Mon Sep 17 00:00:00 2001 From: Maria Ntalla Date: Tue, 5 Jun 2018 14:37:44 +0100 Subject: [PATCH] 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. --- pkg/cloudprovider/providers/vsphere/BUILD | 1 + .../providers/vsphere/vclib/BUILD | 6 +- .../vsphere/vclib/connection_test.go | 13 ++--- .../providers/vsphere/vclib/fixtures/BUILD | 26 +++++++++ .../vsphere/vclib/fixtures/fixtures.go | 58 +++++++++++++++++++ .../providers/vsphere/vsphere_test.go | 11 ++-- 6 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD create mode 100644 pkg/cloudprovider/providers/vsphere/vclib/fixtures/fixtures.go diff --git a/pkg/cloudprovider/providers/vsphere/BUILD b/pkg/cloudprovider/providers/vsphere/BUILD index d6309bd52a9..8c0be034f9c 100644 --- a/pkg/cloudprovider/providers/vsphere/BUILD +++ b/pkg/cloudprovider/providers/vsphere/BUILD @@ -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", diff --git a/pkg/cloudprovider/providers/vsphere/vclib/BUILD b/pkg/cloudprovider/providers/vsphere/vclib/BUILD index 075f477f94b..94dafba45f9 100644 --- a/pkg/cloudprovider/providers/vsphere/vclib/BUILD +++ b/pkg/cloudprovider/providers/vsphere/vclib/BUILD @@ -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", + ], ) diff --git a/pkg/cloudprovider/providers/vsphere/vclib/connection_test.go b/pkg/cloudprovider/providers/vsphere/vclib/connection_test.go index 54adcf899ec..93f9bc08c8e 100644 --- a/pkg/cloudprovider/providers/vsphere/vclib/connection_test.go +++ b/pkg/cloudprovider/providers/vsphere/vclib/connection_test.go @@ -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) diff --git a/pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD b/pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD new file mode 100644 index 00000000000..026ee7bd229 --- /dev/null +++ b/pkg/cloudprovider/providers/vsphere/vclib/fixtures/BUILD @@ -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"], +) diff --git a/pkg/cloudprovider/providers/vsphere/vclib/fixtures/fixtures.go b/pkg/cloudprovider/providers/vsphere/vclib/fixtures/fixtures.go new file mode 100644 index 00000000000..a0838224bae --- /dev/null +++ b/pkg/cloudprovider/providers/vsphere/vclib/fixtures/fixtures.go @@ -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") +} diff --git a/pkg/cloudprovider/providers/vsphere/vsphere_test.go b/pkg/cloudprovider/providers/vsphere/vsphere_test.go index 7a13d6ef44b..e2b391fddd4 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere_test.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere_test.go @@ -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)