@@ -4,19 +4,19 @@ import (
44 "context"
55 "fmt"
66 "testing"
7+ "time"
78
89 "github.com/stretchr/testify/assert"
910 "github.com/stretchr/testify/require"
1011 k8serrors "k8s.io/apimachinery/pkg/api/errors"
1112 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12- "k8s.io/apimachinery/pkg/runtime"
1313 "k8s.io/apimachinery/pkg/runtime/schema"
1414 "sigs.k8s.io/controller-runtime/pkg/client"
1515 "sigs.k8s.io/controller-runtime/pkg/client/fake"
1616 "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1717)
1818
19- func testObj () runtime. Object {
19+ func testObj () * unstructured. Unstructured {
2020 obj := & unstructured.Unstructured {}
2121 obj .SetGroupVersionKind (schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "ConfigMap" })
2222 obj .SetName ("cm" )
@@ -26,9 +26,8 @@ func testObj() runtime.Object {
2626
2727func TestWaitForDelete_AlreadyGone (t * testing.T ) {
2828 cl := fake .NewClientBuilder ().Build ()
29- rc := & RetryClient {Client : cl }
3029
31- err := WaitForDelete (rc , []runtime .Object {testObj ()})
30+ err := WaitForDelete (cl , []client .Object {testObj ()}, time . Second * 2 )
3231 require .NoError (t , err )
3332}
3433
@@ -43,9 +42,8 @@ func TestWaitForDelete_TransientErrorThenGone(t *testing.T) {
4342 return k8serrors .NewNotFound (schema.GroupResource {Resource : "configmaps" }, "cm" )
4443 },
4544 }).Build ()
46- rc := & RetryClient {Client : cl }
4745
48- err := WaitForDelete (rc , []runtime .Object {testObj ()})
46+ err := WaitForDelete (cl , []client .Object {testObj ()}, time . Second * 2 )
4947 require .NoError (t , err )
5048 assert .Greater (t , callCount , 3 )
5149}
@@ -61,22 +59,28 @@ func TestWaitForDelete_StillExistsThenGone(t *testing.T) {
6159 return k8serrors .NewNotFound (schema.GroupResource {Resource : "configmaps" }, "cm" )
6260 },
6361 }).Build ()
64- rc := & RetryClient {Client : cl }
6562
66- err := WaitForDelete (rc , []runtime .Object {testObj ()})
63+ err := WaitForDelete (cl , []client .Object {testObj ()}, time . Second * 2 )
6764 require .NoError (t , err )
6865 assert .Greater (t , callCount , 2 )
6966}
7067
7168func TestWaitForDelete_PersistentErrorTimesOut (t * testing.T ) {
69+ callCount := 0
7270 cl := fake .NewClientBuilder ().WithInterceptorFuncs (interceptor.Funcs {
7371 Get : func (context.Context , client.WithWatch , client.ObjectKey , client.Object , ... client.GetOption ) error {
72+ callCount ++
73+ if callCount <= 2 {
74+ return fmt .Errorf ("initial transient error" )
75+ }
7476 return fmt .Errorf ("persistent API error" )
7577 },
7678 }).Build ()
77- rc := & RetryClient {Client : cl }
7879
79- err := WaitForDelete (rc , []runtime .Object {testObj ()})
80+ err := WaitForDelete (cl , []client .Object {testObj ()}, time . Second * 2 )
8081 assert .Error (t , err )
8182 assert .ErrorIs (t , err , context .DeadlineExceeded )
83+ assert .ErrorContains (t , err , "result of last check was:" )
84+ assert .ErrorContains (t , err , "failed: persistent API error" )
85+ assert .NotContains (t , err .Error (), "initial transient error" )
8286}
0 commit comments