From 2e4a1e910bfabe620e06f1c57537e9ac8aee8a94 Mon Sep 17 00:00:00 2001 From: Zach Loafman Date: Mon, 9 Feb 2015 07:10:02 -0800 Subject: [PATCH] Change loadClientOrDie to return an error Also remove a bunch of dead code. This is a step along the path to getting rid of all the glog in util.go. --- test/e2e/basic.go | 6 ++-- test/e2e/endpoints.go | 6 ++-- test/e2e/events.go | 4 ++- test/e2e/liveness.go | 12 ++++---- test/e2e/networking.go | 4 ++- test/e2e/pods.go | 4 ++- test/e2e/private.go | 6 ++-- test/e2e/service.go | 4 ++- test/e2e/util.go | 65 ++++-------------------------------------- 9 files changed, 33 insertions(+), 78 deletions(-) diff --git a/test/e2e/basic.go b/test/e2e/basic.go index a73cf816c00..0eea514edee 100644 --- a/test/e2e/basic.go +++ b/test/e2e/basic.go @@ -199,8 +199,8 @@ func TestBasic(c *client.Client) bool { var _ = Describe("TestBasic", func() { It("should pass", func() { - // TODO: Instead of OrDie, client should Fail the test if there's a problem. - // In general tests should Fail() instead of glog.Fatalf(). - Expect(TestBasic(loadClientOrDie())).To(BeTrue()) + c, err := loadClient() + Expect(err).NotTo(HaveOccurred()) + Expect(TestBasic(c)).To(BeTrue()) }) }) diff --git a/test/e2e/endpoints.go b/test/e2e/endpoints.go index 19c220077b3..35be6f7cdf9 100644 --- a/test/e2e/endpoints.go +++ b/test/e2e/endpoints.go @@ -189,8 +189,8 @@ func TestEndpoints(c *client.Client) bool { var _ = Describe("TestEndpoints", func() { It("should pass", func() { - // TODO: Instead of OrDie, client should Fail the test if there's a problem. - // In general tests should Fail() instead of glog.Fatalf(). - Expect(TestEndpoints(loadClientOrDie())).To(BeTrue()) + c, err := loadClient() + Expect(err).NotTo(HaveOccurred()) + Expect(TestEndpoints(c)).To(BeTrue()) }) }) diff --git a/test/e2e/events.go b/test/e2e/events.go index 7c14d1b9755..73f894c77b7 100644 --- a/test/e2e/events.go +++ b/test/e2e/events.go @@ -34,7 +34,9 @@ var _ = Describe("Events", func() { var c *client.Client BeforeEach(func() { - c = loadClientOrDie() + var err error + c, err = loadClient() + Expect(err).NotTo(HaveOccurred()) }) It("should be sent by kubelets and the scheduler about pods scheduling and running", func() { diff --git a/test/e2e/liveness.go b/test/e2e/liveness.go index 9a5af6a3db5..63aee5a681b 100644 --- a/test/e2e/liveness.go +++ b/test/e2e/liveness.go @@ -137,16 +137,16 @@ func TestLivenessExec(c *client.Client) bool { var _ = Describe("TestLivenessHttp", func() { It("should pass", func() { - // TODO: Instead of OrDie, client should Fail the test if there's a problem. - // In general tests should Fail() instead of glog.Fatalf(). - Expect(TestLivenessHttp(loadClientOrDie())).To(BeTrue()) + c, err := loadClient() + Expect(err).NotTo(HaveOccurred()) + Expect(TestLivenessHttp(c)).To(BeTrue()) }) }) var _ = Describe("TestLivenessExec", func() { It("should pass", func() { - // TODO: Instead of OrDie, client should Fail the test if there's a problem. - // In general tests should Fail() instead of glog.Fatalf(). - Expect(TestLivenessExec(loadClientOrDie())).To(BeTrue()) + c, err := loadClient() + Expect(err).NotTo(HaveOccurred()) + Expect(TestLivenessExec(c)).To(BeTrue()) }) }) diff --git a/test/e2e/networking.go b/test/e2e/networking.go index eeadb0a4faf..367d7300af4 100644 --- a/test/e2e/networking.go +++ b/test/e2e/networking.go @@ -33,7 +33,9 @@ var _ = Describe("Networking", func() { var c *client.Client BeforeEach(func() { - c = loadClientOrDie() + var err error + c, err = loadClient() + Expect(err).NotTo(HaveOccurred()) }) It("should function for pods", func() { diff --git a/test/e2e/pods.go b/test/e2e/pods.go index 1a82cd010b1..01ac3018435 100644 --- a/test/e2e/pods.go +++ b/test/e2e/pods.go @@ -36,7 +36,9 @@ var _ = Describe("Pods", func() { ) BeforeEach(func() { - c = loadClientOrDie() + var err error + c, err = loadClient() + Expect(err).NotTo(HaveOccurred()) }) It("should be submitted and removed", func() { diff --git a/test/e2e/private.go b/test/e2e/private.go index 94076f69beb..b67040c8300 100644 --- a/test/e2e/private.go +++ b/test/e2e/private.go @@ -39,8 +39,8 @@ func TestPrivate(c *client.Client) bool { var _ = Describe("TestPrivate", func() { It("should pass", func() { - // TODO: Instead of OrDie, client should Fail the test if there's a problem. - // In general tests should Fail() instead of glog.Fatalf(). - Expect(TestPrivate(loadClientOrDie())).To(BeTrue()) + c, err := loadClient() + Expect(err).NotTo(HaveOccurred()) + Expect(TestPrivate(c)).To(BeTrue()) }) }) diff --git a/test/e2e/service.go b/test/e2e/service.go index 2c079d9500b..e64d6491af3 100644 --- a/test/e2e/service.go +++ b/test/e2e/service.go @@ -32,7 +32,9 @@ var _ = Describe("Services", func() { var c *client.Client BeforeEach(func() { - c = loadClientOrDie() + var err error + c, err = loadClient() + Expect(err).NotTo(HaveOccurred()) }) It("should provide DNS for the cluster", func() { diff --git a/test/e2e/util.go b/test/e2e/util.go index b5e2d9d1a13..7ea2592ae0f 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -18,17 +18,14 @@ package e2e import ( "fmt" - "io/ioutil" "math/rand" "path/filepath" "strconv" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth" - "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/golang/glog" . "github.com/onsi/ginkgo" @@ -114,80 +111,30 @@ func waitForPodSuccess(c *client.Client, podName string, contName string) bool { return false } -// assetPath returns a path to the requested file; safe on all -// OSes. NOTE: If you use an asset in this test, you MUST add it to -// the KUBE_TEST_PORTABLE array in hack/lib/golang.sh. -func assetPath(pathElements ...string) string { - return filepath.Join(testContext.repoRoot, filepath.Join(pathElements...)) -} - -func loadObjectOrDie(filePath string) runtime.Object { - data, err := ioutil.ReadFile(filePath) - if err != nil { - glog.Fatalf("Failed to read object: %v", err) - } - return decodeObjectOrDie(data) -} - -func decodeObjectOrDie(data []byte) runtime.Object { - obj, err := latest.Codec.Decode(data) - if err != nil { - glog.Fatalf("Failed to decode object: %v", err) - } - return obj -} - -func loadPodOrDie(filePath string) *api.Pod { - obj := loadObjectOrDie(filePath) - pod, ok := obj.(*api.Pod) - if !ok { - glog.Fatalf("Failed to load pod: %v", obj) - } - return pod -} - -func loadClientOrDie() *client.Client { +func loadClient() (*client.Client, error) { config := client.Config{ Host: testContext.host, } info, err := clientauth.LoadFromFile(testContext.authConfig) if err != nil { - glog.Fatalf("Error loading auth: %v", err) + return nil, fmt.Errorf("Error loading auth: %v", err.Error()) } // If the certificate directory is provided, set the cert paths to be there. if testContext.certDir != "" { - glog.Infof("Expecting certs in %v.", testContext.certDir) + By(fmt.Sprintf("Expecting certs in %v.", testContext.certDir)) info.CAFile = filepath.Join(testContext.certDir, "ca.crt") info.CertFile = filepath.Join(testContext.certDir, "kubecfg.crt") info.KeyFile = filepath.Join(testContext.certDir, "kubecfg.key") } config, err = info.MergeWithConfig(config) if err != nil { - glog.Fatalf("Error creating client") + return nil, fmt.Errorf("Error creating client: %v", err.Error()) } c, err := client.New(&config) if err != nil { - glog.Fatalf("Error creating client") + return nil, fmt.Errorf("Error creating client: %v", err.Error()) } - return c -} - -func parsePodOrDie(json string) *api.Pod { - obj := decodeObjectOrDie([]byte(json)) - pod, ok := obj.(*api.Pod) - if !ok { - glog.Fatalf("Failed to cast pod: %v", obj) - } - return pod -} - -func parseServiceOrDie(json string) *api.Service { - obj := decodeObjectOrDie([]byte(json)) - service, ok := obj.(*api.Service) - if !ok { - glog.Fatalf("Failed to cast service: %v", obj) - } - return service + return c, nil } // TODO: Allow service names to have the same form as names