Skip to content

Commit

Permalink
E2E - modify CheckResultFunc to return status message
Browse files Browse the repository at this point in the history
CheckResultFunc now returns an optional string which is used by AwaitUntil
to provide additional context when an error is returned from PollImmediate.

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis authored and mangelajo committed Nov 27, 2019
1 parent fbb35df commit f126dd4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
18 changes: 13 additions & 5 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type PatchStringValue struct {
}

type DoOperationFunc func() (interface{}, error)
type CheckResultFunc func(result interface{}) (bool, error)
type CheckResultFunc func(result interface{}) (bool, string, error)

// Framework supports common operations used by e2e tests; it will keep a client & a namespace for you.
// Eventual goal is to merge this with integration test framework.
Expand Down Expand Up @@ -115,6 +115,7 @@ func (f *Framework) BeforeEach() {
f.cleanupHandle = AddCleanupAction(f.AfterEach)

ginkgo.By("Creating kubernetes clients")

for _, context := range TestContext.KubeContexts {
client := f.createKubernetesClient(context)
f.ClusterClients = append(f.ClusterClients, client)
Expand Down Expand Up @@ -311,14 +312,15 @@ func DoPatchOperation(path string, value string, patchFunc PatchFunc) {
}, NoopCheckResult)
}

func NoopCheckResult(interface{}) (bool, error) {
return true, nil
func NoopCheckResult(interface{}) (bool, string, error) {
return true, "", nil
}

// AwaitUntil periodically performs the given operation until the given CheckResultFunc returns true, an error, or a
// timeout is reached.
func AwaitUntil(opMsg string, doOperation DoOperationFunc, checkResult CheckResultFunc) interface{} {
var finalResult interface{}
var lastMsg string
err := wait.PollImmediate(5*time.Second, 1*time.Minute, func() (bool, error) {
result, err := doOperation()
if err != nil {
Expand All @@ -328,7 +330,7 @@ func AwaitUntil(opMsg string, doOperation DoOperationFunc, checkResult CheckResu
return false, err
}

ok, err := checkResult(result)
ok, msg, err := checkResult(result)
if err != nil {
return false, err
}
Expand All @@ -338,9 +340,15 @@ func AwaitUntil(opMsg string, doOperation DoOperationFunc, checkResult CheckResu
return true, nil
}

lastMsg = msg
return false, nil
})

Expect(err).NotTo(HaveOccurred(), "Failed to "+opMsg)
errMsg := "Failed to " + opMsg
if lastMsg != "" {
errMsg += ". " + lastMsg
}

Expect(err).NotTo(HaveOccurred(), errMsg)
return finalResult
}
16 changes: 8 additions & 8 deletions test/e2e/framework/network_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ func (np *NetworkPod) AwaitReady() {

np.Pod = AwaitUntil("get pod", func() (interface{}, error) {
return pods.Get(np.Pod.Name, metav1.GetOptions{})
}, func(result interface{}) (bool, error) {
}, func(result interface{}) (bool, string, error) {
pod := result.(*v1.Pod)
if pod.Status.Phase != v1.PodRunning {
if pod.Status.Phase != v1.PodPending {
return false, fmt.Errorf("expected pod to be in phase \"Pending\" or \"Running\"")
return false, "", fmt.Errorf("expected pod to be in phase \"Pending\" or \"Running\"")
}
return false, nil // pod is still pending
return false, "Pod is still pending", nil
}

return true, nil // pod is running
return true, "", nil // pod is running
}).(*v1.Pod)
}

Expand All @@ -99,16 +99,16 @@ func (np *NetworkPod) AwaitSuccessfulFinish() {

np.Pod = AwaitUntil("get pod", func() (interface{}, error) {
return pods.Get(np.Pod.Name, metav1.GetOptions{})
}, func(result interface{}) (bool, error) {
}, func(result interface{}) (bool, string, error) {
np.Pod = result.(*v1.Pod)

switch np.Pod.Status.Phase {
case v1.PodSucceeded:
return true, nil
return true, "", nil
case v1.PodFailed:
return true, nil
return true, "", nil
default:
return false, nil
return false, fmt.Sprintf("Pod status is %v", np.Pod.Status.Phase), nil
}
}).(*v1.Pod)

Expand Down
9 changes: 5 additions & 4 deletions test/e2e/framework/pods.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package framework

import (
"fmt"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -12,14 +14,13 @@ func (f *Framework) AwaitPodsByAppLabel(cluster ClusterIndex, appName string, na
return f.ClusterClients[cluster].CoreV1().Pods(namespace).List(metav1.ListOptions{
LabelSelector: "app=" + appName,
})
}, func(result interface{}) (bool, error) {
}, func(result interface{}) (bool, string, error) {
pods := result.(*v1.PodList)
if expectedCount < 0 || len(pods.Items) == expectedCount {
return true, nil
return true, "", nil
}

Logf("Actual pod count %d does not match the expected pod count %d - retrying...", len(pods.Items), expectedCount)
return false, nil
return false, fmt.Sprintf("Actual pod count %d does not match the expected pod count %d", len(pods.Items), expectedCount), nil
}).(*v1.PodList)
}

Expand Down

0 comments on commit f126dd4

Please sign in to comment.