Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Add exponential retry to AssertResourceDoesNotExist to reduce test fl…
Browse files Browse the repository at this point in the history
…akiness
  • Loading branch information
jrhouston committed May 10, 2020
1 parent ad329ed commit 9f400ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
9 changes: 9 additions & 0 deletions test/acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func randName() string {
return fmt.Sprintf("tf-acc-test-%s", string(b))
}

// randString does exactly what it sounds like it should do
func randString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

// TFVARS is a convenience type for supplying vars to the loadTerraformConfig func
type TFVARS map[string]interface{}

Expand Down
44 changes: 30 additions & 14 deletions test/helper/kubernetes/kubernetes_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kubernetes

import (
"context"
"math"
"testing"
"time"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -117,14 +119,21 @@ func (k *Helper) AssertNamespacedResourceDoesNotExist(t *testing.T, gv, resource
t.Helper()

gvr := createGroupVersionResource(gv, resource)
_, err := k.client.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return
}

if err != nil {
t.Errorf("Error when trying to get resource %s/%s: %v", namespace, name, err)
return
for i := 1; i <= 3; i++ {
_, err := k.client.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return
}

if err != nil {
t.Errorf("Error when trying to get resource %s/%s: %v", namespace, name, err)
return
}

// NOTE some resources take a few seconds to delete so here we wait and retry so
// we don't polute the tests with retry logic
time.Sleep(time.Duration(math.Exp2(float64(i))) * time.Second)
}

t.Errorf("Resource %s/%s still exists", namespace, name)
Expand All @@ -135,14 +144,21 @@ func (k *Helper) AssertResourceDoesNotExist(t *testing.T, gv, resource, name str
t.Helper()

gvr := createGroupVersionResource(gv, resource)
_, err := k.client.Resource(gvr).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return
}

if err != nil {
t.Errorf("Error when trying to get resource %s: %v", name, err)
return
for i := 1; i <= 3; i++ {
_, err := k.client.Resource(gvr).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return
}

if err != nil {
t.Errorf("Error when trying to get resource %s: %v", name, err)
return
}

// NOTE some resources take a second to delete so here we wait and retry so
// we don't polute the tests with retry logic
time.Sleep(time.Duration(math.Exp2(float64(i))) * time.Second)
}

t.Errorf("Resource %s still exists", name)
Expand Down

0 comments on commit 9f400ea

Please sign in to comment.