mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
replace vsphere legacy provider "fixtures" runtime path lookup based on stack info with tesdata and relative paths
go test sets the current working directory to the package under test we can just use relative paths and a testdata directory instead of the brittle runtime path lookup logic
This commit is contained in:
parent
1a7cf6c91a
commit
1b5ef42681
@ -31,7 +31,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/legacy-cloud-providers/vsphere/vclib"
|
||||
"k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures"
|
||||
)
|
||||
|
||||
func createTestServer(
|
||||
@ -83,14 +82,14 @@ func createTestServer(
|
||||
func TestWithValidCaCert(t *testing.T) {
|
||||
handler, verifyConnectionWasMade := getRequestVerifier(t)
|
||||
|
||||
server, _ := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||
server, _ := createTestServer(t, "./testdata/ca.pem", "./testdata/server.pem", "./testdata/server.key", handler)
|
||||
server.StartTLS()
|
||||
u := mustParseURL(t, server.URL)
|
||||
|
||||
connection := &vclib.VSphereConnection{
|
||||
Hostname: u.Hostname(),
|
||||
Port: u.Port(),
|
||||
CACert: fixtures.CaCertPath,
|
||||
CACert: "./testdata/ca.pem",
|
||||
}
|
||||
|
||||
// Ignoring error here, because we only care about the TLS connection
|
||||
@ -102,7 +101,7 @@ func TestWithValidCaCert(t *testing.T) {
|
||||
func TestWithVerificationWithWrongThumbprint(t *testing.T) {
|
||||
handler, _ := getRequestVerifier(t)
|
||||
|
||||
server, _ := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||
server, _ := createTestServer(t, "./testdata/ca.pem", "./testdata/server.pem", "./testdata/server.key", handler)
|
||||
server.StartTLS()
|
||||
u := mustParseURL(t, server.URL)
|
||||
|
||||
@ -122,7 +121,7 @@ func TestWithVerificationWithWrongThumbprint(t *testing.T) {
|
||||
func TestWithVerificationWithoutCaCertOrThumbprint(t *testing.T) {
|
||||
handler, _ := getRequestVerifier(t)
|
||||
|
||||
server, _ := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||
server, _ := createTestServer(t, "./testdata/ca.pem", "./testdata/server.pem", "./testdata/server.key", handler)
|
||||
server.StartTLS()
|
||||
u := mustParseURL(t, server.URL)
|
||||
|
||||
@ -140,7 +139,7 @@ func TestWithValidThumbprint(t *testing.T) {
|
||||
handler, verifyConnectionWasMade := getRequestVerifier(t)
|
||||
|
||||
server, thumbprint :=
|
||||
createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||
createTestServer(t, "./testdata/ca.pem", "./testdata/server.pem", "./testdata/server.key", handler)
|
||||
server.StartTLS()
|
||||
u := mustParseURL(t, server.URL)
|
||||
|
||||
@ -173,7 +172,7 @@ func TestInvalidCaCert(t *testing.T) {
|
||||
connection := &vclib.VSphereConnection{
|
||||
Hostname: "should-not-matter",
|
||||
Port: "27015", // doesn't matter, but has to be a valid port
|
||||
CACert: fixtures.InvalidCertPath,
|
||||
CACert: "./testdata/invalid.pem",
|
||||
}
|
||||
|
||||
_, err := connection.NewClient(context.Background())
|
||||
|
@ -1,80 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
// CaCertPath is the filepath to a certificate that can be used as a CA
|
||||
// certificate.
|
||||
CaCertPath string
|
||||
// ServerCertPath is the filepath to a leaf certifiacte signed by the CA at
|
||||
// `CaCertPath`.
|
||||
ServerCertPath string
|
||||
// ServerKeyPath is the filepath to the private key for the ceritifiacte at
|
||||
// `ServerCertPath`.
|
||||
ServerKeyPath string
|
||||
// InvalidCertPath is the filepath to an invalid certificate.
|
||||
InvalidCertPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
fixturesDir, err := pkgPath()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Cannot get path to the fixtures: %s", err))
|
||||
}
|
||||
|
||||
CaCertPath = filepath.Join(fixturesDir, "ca.pem")
|
||||
ServerCertPath = filepath.Join(fixturesDir, "server.pem")
|
||||
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
|
||||
}
|
@ -54,7 +54,6 @@ import (
|
||||
|
||||
cloudprovider "k8s.io/cloud-provider"
|
||||
"k8s.io/legacy-cloud-providers/vsphere/vclib"
|
||||
"k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures"
|
||||
)
|
||||
|
||||
// localhostCert was generated from crypto/tls/generate_cert.go with the following command:
|
||||
@ -319,12 +318,12 @@ func TestVSphereLoginByToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVSphereLoginWithCaCert(t *testing.T) {
|
||||
caCertPEM, err := ioutil.ReadFile(fixtures.CaCertPath)
|
||||
caCertPEM, err := ioutil.ReadFile("./vclib/testdata/ca.pem")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not read ca cert from file")
|
||||
}
|
||||
|
||||
serverCert, err := tls.LoadX509KeyPair(fixtures.ServerCertPath, fixtures.ServerKeyPath)
|
||||
serverCert, err := tls.LoadX509KeyPair("./vclib/testdata/server.pem", "./vclib/testdata/server.key")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load server cert and server key from files: %#v", err)
|
||||
}
|
||||
@ -342,7 +341,7 @@ func TestVSphereLoginWithCaCert(t *testing.T) {
|
||||
cfg, cleanup := configFromSimWithTLS(&tlsConfig, false)
|
||||
defer cleanup()
|
||||
|
||||
cfg.Global.CAFile = fixtures.CaCertPath
|
||||
cfg.Global.CAFile = "./vclib/testdata/ca.pem"
|
||||
|
||||
// Create vSphere configuration object
|
||||
vs, err := newControllerNode(cfg)
|
||||
|
Loading…
Reference in New Issue
Block a user