Skip to content

Commit

Permalink
Added WaitForEntity e2e func
Browse files Browse the repository at this point in the history
  • Loading branch information
f41gh7 committed Jun 17, 2020
1 parent 1ca40f5 commit 6c59a0a
Showing 1 changed file with 41 additions and 58 deletions.
99 changes: 41 additions & 58 deletions e2e/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package e2e

import (
"errors"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -9,91 +11,72 @@ import (
"time"
)

func WaitForSts(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
sts, err := kubeclient.AppsV1().StatefulSets(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of StatefulSet: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}
var (
NeedToWaitError = fmt.Errorf("needToWaitErr")
)

if int(sts.Status.ReadyReplicas) >= replicas {
return true, nil
}
t.Logf("Waiting for full availability of %s sts (%d/%d)\n", name,
sts.Status.ReadyReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("StatefulSet available (%d/%d)\n", replicas, replicas)
return nil
}
type waitForFunc func() error

func WaitForService(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Service: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
waitForService := func()error{
_, err := kubeclient.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("Service available (%s)\n", name)
return nil
return waitForEntity(retryInterval,timeout,waitForService)
}

func WaitForConfigMap(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of ConfigMap: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
return err
waitForCm := func()error {
_, err := kubeclient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("StatefulSet available %s\n", name)
return nil
return waitForEntity(retryInterval,timeout,waitForCm)
}

func WaitForSecret(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {

waitForSecret := func() error {

_, err := kubeclient.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("Secret available %s\n", name)
return waitForEntity(retryInterval, timeout, waitForSecret)
}

func WaitForSts(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
waitForSts := func() error {
sts, err := kubeclient.AppsV1().StatefulSets(namespace).Get(name, metav1.GetOptions{})
if sts != nil {
if int(sts.Status.ReadyReplicas) < replicas {
t.Logf("Waiting for availability of Sts: %s in Namespace: %s \n", name, namespace)
return NeedToWaitError
}
}
return err
}
t.Logf("Sts available %s\n", name)
return waitForEntity(retryInterval, timeout, waitForSts)
}

func waitForEntity(retryInterval, timeout time.Duration, fn waitForFunc) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
err = fn()
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Secret: %s in Namespace: %s \n", name, namespace)
if apierrors.IsNotFound(err) || errors.Is(err, NeedToWaitError) {
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
return err
}
t.Logf("Secret available %s\n", name)
return nil
}

0 comments on commit 6c59a0a

Please sign in to comment.