From 1b5ef42681c8fa68b426bd77f865034cc5a3f859 Mon Sep 17 00:00:00 2001 From: Benjamin Elder Date: Sun, 7 Mar 2021 14:19:05 -0800 Subject: [PATCH] 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 --- .../vsphere/vclib/connection_test.go | 13 ++- .../vsphere/vclib/fixtures/fixtures.go | 80 ------------------- .../vclib/{fixtures => testdata}/README.md | 0 .../vclib/{fixtures => testdata}/ca.key | 0 .../vclib/{fixtures => testdata}/ca.pem | 0 .../{fixtures => testdata}/createCerts.sh | 0 .../vclib/{fixtures => testdata}/invalid.pem | 0 .../vclib/{fixtures => testdata}/server.csr | 0 .../vclib/{fixtures => testdata}/server.key | 0 .../vclib/{fixtures => testdata}/server.pem | 0 .../vsphere/vsphere_test.go | 7 +- 11 files changed, 9 insertions(+), 91 deletions(-) delete mode 100644 staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/README.md (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/ca.key (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/ca.pem (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/createCerts.sh (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/invalid.pem (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/server.csr (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/server.key (100%) rename staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/{fixtures => testdata}/server.pem (100%) diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection_test.go b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection_test.go index 046b3de8a13..70301ebbbc5 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection_test.go @@ -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()) 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 deleted file mode 100644 index 7ef4dc0347c..00000000000 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/fixtures.go +++ /dev/null @@ -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 -} diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/README.md b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/README.md similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/README.md rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/README.md diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/ca.key b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/ca.key similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/ca.key rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/ca.key diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/ca.pem b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/ca.pem similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/ca.pem rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/ca.pem diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/createCerts.sh b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/createCerts.sh similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/createCerts.sh rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/createCerts.sh diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/invalid.pem b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/invalid.pem similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/invalid.pem rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/invalid.pem diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.csr b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.csr similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.csr rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.csr diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.key b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.key similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.key rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.key diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.pem b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.pem similarity index 100% rename from staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/fixtures/server.pem rename to staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/testdata/server.pem diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere_test.go b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere_test.go index 5fb91a96199..62962fb9941 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere_test.go @@ -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)