Skip to content

Commit 897dc66

Browse files
authored
Merge pull request kubernetes#92400 from spiffxp/decouple-testfiles
decouple testfiles from framework
2 parents 9a8f6cf + 2876816 commit 897dc66

File tree

12 files changed

+87
-38
lines changed

12 files changed

+87
-38
lines changed

test/e2e/e2e_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ func TestMain(m *testing.M) {
110110
File string `yaml:"file"`
111111
}
112112

113-
data := testfiles.ReadOrDie("test/conformance/testdata/conformance.yaml")
113+
data, err := testfiles.Read("test/conformance/testdata/conformance.yaml")
114+
if err != nil {
115+
fmt.Fprintln(os.Stderr, err)
116+
os.Exit(1)
117+
}
114118
if err := yaml.Unmarshal(data, &tests); err != nil {
115119
fmt.Fprintln(os.Stderr, err)
116120
os.Exit(1)

test/e2e/examples.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,9 @@ var _ = framework.KubeDescribe("[Feature:Example]", func() {
155155

156156
func readFile(test, file string) string {
157157
from := filepath.Join(test, file)
158-
return commonutils.SubstituteImageName(string(testfiles.ReadOrDie(from)))
158+
data, err := testfiles.Read(from)
159+
if err != nil {
160+
framework.Fail(err.Error())
161+
}
162+
return commonutils.SubstituteImageName(string(data))
159163
}

test/e2e/framework/ingress/ingress_utils.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,18 @@ func NewIngressTestJig(c clientset.Interface) *TestJig {
445445
func (j *TestJig) CreateIngress(manifestPath, ns string, ingAnnotations map[string]string, svcAnnotations map[string]string) {
446446
var err error
447447
read := func(file string) string {
448-
return string(e2etestfiles.ReadOrDie(filepath.Join(manifestPath, file)))
448+
data, err := e2etestfiles.Read(filepath.Join(manifestPath, file))
449+
if err != nil {
450+
framework.Fail(err.Error())
451+
}
452+
return string(data)
449453
}
450454
exists := func(file string) bool {
451-
return e2etestfiles.Exists(filepath.Join(manifestPath, file))
455+
found, err := e2etestfiles.Exists(filepath.Join(manifestPath, file))
456+
if err != nil {
457+
framework.Fail(fmt.Sprintf("fatal error looking for test file %s: %s", file, err))
458+
}
459+
return found
452460
}
453461

454462
j.Logger.Infof("creating replication controller")
@@ -1018,8 +1026,13 @@ func (cont *NginxIngressController) Init() {
10181026
framework.ExpectNoError(err)
10191027

10201028
read := func(file string) string {
1021-
return string(e2etestfiles.ReadOrDie(filepath.Join(IngressManifestPath, "nginx", file)))
1029+
data, err := e2etestfiles.Read(filepath.Join(IngressManifestPath, "nginx", file))
1030+
if err != nil {
1031+
framework.Fail(err.Error())
1032+
}
1033+
return string(data)
10221034
}
1035+
10231036
framework.Logf("initializing nginx ingress controller")
10241037
framework.RunKubectlOrDieInput(cont.Ns, read("rc.yaml"), "create", "-f", "-", fmt.Sprintf("--namespace=%v", cont.Ns))
10251038

test/e2e/framework/testfiles/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go_library(
55
srcs = ["testfiles.go"],
66
importpath = "k8s.io/kubernetes/test/e2e/framework/testfiles",
77
visibility = ["//visibility:public"],
8-
deps = ["//test/e2e/framework:go_default_library"],
98
)
109

1110
filegroup(

test/e2e/framework/testfiles/testfiles.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
"path/filepath"
3434
"sort"
3535
"strings"
36-
37-
"k8s.io/kubernetes/test/e2e/framework"
3836
)
3937

4038
var filesources []FileSource
@@ -66,18 +64,6 @@ type FileSource interface {
6664
DescribeFiles() string
6765
}
6866

69-
// ReadOrDie tries to retrieve the desired file content from
70-
// one of the registered file sources. In contrast to FileSource, it
71-
// will either return a valid slice or abort the test by calling the fatal function,
72-
// i.e. the caller doesn't have to implement error checking.
73-
func ReadOrDie(filePath string) []byte {
74-
data, err := Read(filePath)
75-
if err != nil {
76-
framework.Fail(err.Error(), 1)
77-
}
78-
return data
79-
}
80-
8167
// Read tries to retrieve the desired file content from
8268
// one of the registered file sources.
8369
func Read(filePath string) ([]byte, error) {
@@ -106,17 +92,17 @@ func Read(filePath string) ([]byte, error) {
10692
// Exists checks whether a file could be read. Unexpected errors
10793
// are handled by calling the fail function, which then should
10894
// abort the current test.
109-
func Exists(filePath string) bool {
95+
func Exists(filePath string) (bool, error) {
11096
for _, filesource := range filesources {
11197
data, err := filesource.ReadTestFile(filePath)
11298
if err != nil {
113-
framework.Fail(fmt.Sprintf("fatal error looking for test file %s: %s", filePath, err), 1)
99+
return false, err
114100
}
115101
if data != nil {
116-
return true
102+
return true, nil
117103
}
118104
}
119-
return false
105+
return false, nil
120106
}
121107

122108
// RootFileSource looks for files relative to a root directory.

test/e2e/kubectl/kubectl.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ func assertCleanup(ns string, selectors ...string) {
210210
}
211211

212212
func readTestFileOrDie(file string) []byte {
213-
return e2etestfiles.ReadOrDie(path.Join(kubeCtlManifestPath, file))
213+
data, err := e2etestfiles.Read(path.Join(kubeCtlManifestPath, file))
214+
if err != nil {
215+
framework.Fail(err.Error(), 1)
216+
}
217+
return data
214218
}
215219

216220
func runKubectlRetryOrDie(ns string, args ...string) string {
@@ -302,7 +306,11 @@ var _ = SIGDescribe("Kubectl client", func() {
302306
var nautilus string
303307
ginkgo.BeforeEach(func() {
304308
updateDemoRoot := "test/fixtures/doc-yaml/user-guide/update-demo"
305-
nautilus = commonutils.SubstituteImageName(string(e2etestfiles.ReadOrDie(filepath.Join(updateDemoRoot, "nautilus-rc.yaml.in"))))
309+
data, err := e2etestfiles.Read(filepath.Join(updateDemoRoot, "nautilus-rc.yaml.in"))
310+
if err != nil {
311+
framework.Fail(err.Error())
312+
}
313+
nautilus = commonutils.SubstituteImageName(string(data))
306314
})
307315
/*
308316
Release : v1.9
@@ -350,7 +358,11 @@ var _ = SIGDescribe("Kubectl client", func() {
350358
"agnhost-primary-deployment.yaml.in",
351359
"agnhost-replica-deployment.yaml.in",
352360
} {
353-
contents := commonutils.SubstituteImageName(string(e2etestfiles.ReadOrDie(filepath.Join(guestbookRoot, gbAppFile))))
361+
data, err := e2etestfiles.Read(filepath.Join(guestbookRoot, gbAppFile))
362+
if err != nil {
363+
framework.Fail(err.Error())
364+
}
365+
contents := commonutils.SubstituteImageName(string(data))
354366
run(contents)
355367
}
356368
}

test/e2e/storage/flexvolume.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func installFlex(c clientset.Interface, node *v1.Node, vendor, driver, filePath
9191
cmd := fmt.Sprintf("sudo mkdir -p %s", flexDir)
9292
sshAndLog(cmd, host, true /*failOnError*/)
9393

94-
data := e2etestfiles.ReadOrDie(filePath)
94+
data, err := e2etestfiles.Read(filePath)
95+
if err != nil {
96+
framework.Fail(err.Error())
97+
}
9598
cmd = fmt.Sprintf("sudo tee <<'EOF' %s\n%s\nEOF", flexFile, string(data))
9699
sshAndLog(cmd, host, true /*failOnError*/)
97100

test/e2e/upgrades/cassandra.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func (CassandraUpgradeTest) Skip(upgCtx UpgradeContext) bool {
6060
}
6161

6262
func cassandraKubectlCreate(ns, file string) {
63-
input := string(e2etestfiles.ReadOrDie(filepath.Join(cassandraManifestPath, file)))
63+
data, err := e2etestfiles.Read(filepath.Join(cassandraManifestPath, file))
64+
if err != nil {
65+
framework.Fail(err.Error())
66+
}
67+
input := string(data)
6468
framework.RunKubectlOrDieInput(ns, input, "create", "-f", "-", fmt.Sprintf("--namespace=%s", ns))
6569
}
6670

test/e2e/upgrades/etcd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func (EtcdUpgradeTest) Skip(upgCtx UpgradeContext) bool {
5959
}
6060

6161
func kubectlCreate(ns, file string) {
62-
input := string(e2etestfiles.ReadOrDie(filepath.Join(manifestPath, file)))
62+
data, err := e2etestfiles.Read(filepath.Join(manifestPath, file))
63+
if err != nil {
64+
framework.Fail(err.Error())
65+
}
66+
input := string(data)
6367
framework.RunKubectlOrDieInput(ns, input, "create", "-f", "-", fmt.Sprintf("--namespace=%s", ns))
6468
}
6569

test/e2e/upgrades/mysql.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ func (MySQLUpgradeTest) Skip(upgCtx UpgradeContext) bool {
6161
}
6262

6363
func mysqlKubectlCreate(ns, file string) {
64-
input := string(e2etestfiles.ReadOrDie(filepath.Join(mysqlManifestPath, file)))
64+
data, err := e2etestfiles.Read(filepath.Join(mysqlManifestPath, file))
65+
if err != nil {
66+
framework.Fail(err.Error())
67+
}
68+
input := string(data)
6569
framework.RunKubectlOrDieInput(ns, input, "create", "-f", "-", fmt.Sprintf("--namespace=%s", ns))
6670
}
6771

0 commit comments

Comments
 (0)