decouple testfiles from framework

This drops testfiles.ReadOrDie and updated testfiles.Exists to return an
error, forcing the caller to decide whether to call framework.Fail or do
something else.

It makes for a slightly less friendly API, but also means the package is
decoupled from framework again, as per the comments at the top of the
file
This commit is contained in:
Aaron Crickenberger
2020-06-22 15:33:41 -07:00
parent 71c352dee3
commit 28768166f5
12 changed files with 87 additions and 38 deletions

View File

@@ -33,8 +33,6 @@ import (
"path/filepath"
"sort"
"strings"
"k8s.io/kubernetes/test/e2e/framework"
)
var filesources []FileSource
@@ -66,18 +64,6 @@ type FileSource interface {
DescribeFiles() string
}
// ReadOrDie tries to retrieve the desired file content from
// one of the registered file sources. In contrast to FileSource, it
// will either return a valid slice or abort the test by calling the fatal function,
// i.e. the caller doesn't have to implement error checking.
func ReadOrDie(filePath string) []byte {
data, err := Read(filePath)
if err != nil {
framework.Fail(err.Error(), 1)
}
return data
}
// Read tries to retrieve the desired file content from
// one of the registered file sources.
func Read(filePath string) ([]byte, error) {
@@ -106,17 +92,17 @@ func Read(filePath string) ([]byte, error) {
// Exists checks whether a file could be read. Unexpected errors
// are handled by calling the fail function, which then should
// abort the current test.
func Exists(filePath string) bool {
func Exists(filePath string) (bool, error) {
for _, filesource := range filesources {
data, err := filesource.ReadTestFile(filePath)
if err != nil {
framework.Fail(fmt.Sprintf("fatal error looking for test file %s: %s", filePath, err), 1)
return false, err
}
if data != nil {
return true
return true, nil
}
}
return false
return false, nil
}
// RootFileSource looks for files relative to a root directory.