Skip to content

Commit 2f381e0

Browse files
authored
Merge pull request kubernetes#90872 from mikedanese/integ
fix some fixture path calculations
2 parents 620b772 + bd290e9 commit 2f381e0

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

cmd/kube-apiserver/app/testing/testserver.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"os"
2525
"path"
26+
"path/filepath"
2627
"runtime"
2728
"time"
2829

@@ -161,11 +162,11 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
161162

162163
s.SecureServing.ExternalAddress = s.SecureServing.Listener.Addr().(*net.TCPAddr).IP // use listener addr although it is a loopback device
163164

164-
_, thisFile, _, ok := runtime.Caller(0)
165-
if !ok {
166-
return result, fmt.Errorf("failed to get current file")
165+
pkgPath, err := pkgPath(t)
166+
if err != nil {
167+
return result, err
167168
}
168-
s.SecureServing.ServerCert.FixtureDirectory = path.Join(path.Dir(thisFile), "testdata")
169+
s.SecureServing.ServerCert.FixtureDirectory = filepath.Join(pkgPath, "testdata")
169170

170171
s.ServiceClusterIPRanges = "10.0.0.0/16"
171172
s.Etcd.StorageConfig = *storageConfig
@@ -279,3 +280,36 @@ func createLocalhostListenerOnFreePort() (net.Listener, int, error) {
279280

280281
return ln, tcpAddr.Port, nil
281282
}
283+
284+
// pkgPath returns the absolute file path to this package's directory. With go
285+
// test, we can just look at the runtime call stack. However, bazel compiles go
286+
// binaries with the -trimpath option so the simple approach fails however we
287+
// can consult environment variables to derive the path.
288+
//
289+
// The approach taken here works for both go test and bazel on the assumption
290+
// that if and only if trimpath is passed, we are running under bazel.
291+
func pkgPath(t Logger) (string, error) {
292+
_, thisFile, _, ok := runtime.Caller(0)
293+
if !ok {
294+
return "", fmt.Errorf("failed to get current file")
295+
}
296+
297+
pkgPath := filepath.Dir(thisFile)
298+
299+
// If we find bazel env variables, then -trimpath was passed so we need to
300+
// construct the path from the environment.
301+
if testSrcdir, testWorkspace := os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"); testSrcdir != "" && testWorkspace != "" {
302+
t.Logf("Detected bazel env varaiables: TEST_SRCDIR=%q TEST_WORKSPACE=%q", testSrcdir, testWorkspace)
303+
pkgPath = filepath.Join(testSrcdir, testWorkspace, pkgPath)
304+
}
305+
306+
// If the path is still not absolute, something other than bazel compiled
307+
// with -trimpath.
308+
if !filepath.IsAbs(pkgPath) {
309+
return "", fmt.Errorf("can't construct an absolute path from %q", pkgPath)
310+
}
311+
312+
t.Logf("Resolved testserver package path to: %q", pkgPath)
313+
314+
return pkgPath, nil
315+
}

staging/src/k8s.io/apiextensions-apiserver/pkg/cmd/server/testing/testserver.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io/ioutil"
2323
"net"
2424
"os"
25-
"path"
25+
"path/filepath"
2626
"runtime"
2727
"time"
2828

@@ -119,11 +119,11 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
119119
s.RecommendedOptions.SecureServing.ServerCert.CertDirectory = result.TmpDir
120120
s.RecommendedOptions.SecureServing.ExternalAddress = s.RecommendedOptions.SecureServing.Listener.Addr().(*net.TCPAddr).IP // use listener addr although it is a loopback device
121121

122-
_, thisFile, _, ok := runtime.Caller(0)
123-
if !ok {
124-
return result, fmt.Errorf("failed to get current file")
122+
pkgPath, err := pkgPath(t)
123+
if err != nil {
124+
return result, err
125125
}
126-
s.RecommendedOptions.SecureServing.ServerCert.FixtureDirectory = path.Join(path.Dir(thisFile), "testdata")
126+
s.RecommendedOptions.SecureServing.ServerCert.FixtureDirectory = filepath.Join(pkgPath, "testdata")
127127

128128
if storageConfig != nil {
129129
s.RecommendedOptions.Etcd.StorageConfig = *storageConfig
@@ -217,3 +217,36 @@ func createLocalhostListenerOnFreePort() (net.Listener, int, error) {
217217

218218
return ln, tcpAddr.Port, nil
219219
}
220+
221+
// pkgPath returns the absolute file path to this package's directory. With go
222+
// test, we can just look at the runtime call stack. However, bazel compiles go
223+
// binaries with the -trimpath option so the simple approach fails however we
224+
// can consult environment variables to derive the path.
225+
//
226+
// The approach taken here works for both go test and bazel on the assumption
227+
// that if and only if trimpath is passed, we are running under bazel.
228+
func pkgPath(t Logger) (string, error) {
229+
_, thisFile, _, ok := runtime.Caller(0)
230+
if !ok {
231+
return "", fmt.Errorf("failed to get current file")
232+
}
233+
234+
pkgPath := filepath.Dir(thisFile)
235+
236+
// If we find bazel env variables, then -trimpath was passed so we need to
237+
// construct the path from the environment.
238+
if testSrcdir, testWorkspace := os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"); testSrcdir != "" && testWorkspace != "" {
239+
t.Logf("Detected bazel env varaiables: TEST_SRCDIR=%q TEST_WORKSPACE=%q", testSrcdir, testWorkspace)
240+
pkgPath = filepath.Join(testSrcdir, testWorkspace, pkgPath)
241+
}
242+
243+
// If the path is still not absolute, something other than bazel compiled
244+
// with -trimpath.
245+
if !filepath.IsAbs(pkgPath) {
246+
return "", fmt.Errorf("can't construct an absolute path from %q", pkgPath)
247+
}
248+
249+
t.Logf("Resolved testserver package path to: %q", pkgPath)
250+
251+
return pkgPath, nil
252+
}

0 commit comments

Comments
 (0)