Skip to content

Commit e8004e0

Browse files
authored
Improve kubefinder tests stability (#309)
1 parent ff1c3a0 commit e8004e0

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/mapper/pkg/kubefinder/kubefinder_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ func (s *KubeFinderTestSuite) TestIsSrcIpClusterInternal() {
128128
s.Require().True(s.Mgr.GetCache().WaitForCacheSync(context.Background()))
129129

130130
// Check pod doesn't exist in the manager's cache
131-
pod, err = s.kubeFinder.ResolveIPToPod(context.Background(), "1.1.1.1")
132-
s.Require().Nil(pod)
133-
s.Require().Error(err)
131+
s.WaitForObjectToBeDeleted(pod)
134132

135133
// Check isInternal with the deleted pod's ip
136134
isInternal, err = s.kubeFinder.IsSrcIpClusterInternal(context.Background(), "1.1.1.1")

src/shared/testbase/testsuitebase.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"time"
2525
)
2626

27-
const waitForCreationInterval = 200 * time.Millisecond
28-
const waitForCreationTimeout = 3 * time.Second
27+
const waitForInterval = 200 * time.Millisecond
28+
const waitForTimeout = 3 * time.Second
2929

3030
type ControllerManagerTestSuiteBase struct {
3131
suite.Suite
@@ -92,8 +92,8 @@ func (s *ControllerManagerTestSuiteBase) TearDownTest() {
9292
// waitForObjectToBeCreated tries to get an object multiple times until it is available in the k8s API server
9393
func (s *ControllerManagerTestSuiteBase) waitForObjectToBeCreated(obj client.Object) {
9494
s.Require().NoError(wait.PollUntilContextTimeout(context.Background(),
95-
waitForCreationInterval,
96-
waitForCreationTimeout,
95+
waitForInterval,
96+
waitForTimeout,
9797
true,
9898
func(ctx context.Context) (done bool, err error) {
9999
err = s.Mgr.GetClient().Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
@@ -108,6 +108,24 @@ func (s *ControllerManagerTestSuiteBase) waitForObjectToBeCreated(obj client.Obj
108108
)
109109
}
110110

111+
func (s *ControllerManagerTestSuiteBase) WaitForObjectToBeDeleted(obj client.Object) {
112+
s.Require().NoError(wait.PollUntilContextTimeout(context.Background(),
113+
waitForInterval,
114+
waitForTimeout,
115+
true,
116+
func(ctx context.Context) (done bool, err error) {
117+
err = s.Mgr.GetClient().Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
118+
if k8serrors.IsNotFound(err) {
119+
return true, nil
120+
}
121+
if err != nil {
122+
return false, errors.Wrap(err)
123+
}
124+
return false, nil
125+
}),
126+
)
127+
}
128+
111129
func (s *ControllerManagerTestSuiteBase) AddPod(name string, podIp string, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.Pod {
112130
pod := &corev1.Pod{
113131
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: s.TestNamespace, Labels: labels},
@@ -305,8 +323,8 @@ func (s *ControllerManagerTestSuiteBase) AddIngress(name string, serviceName str
305323
func (s *ControllerManagerTestSuiteBase) GetAPIServerService() *corev1.Service {
306324
service := &corev1.Service{}
307325
s.Require().NoError(wait.PollUntilContextTimeout(context.Background(),
308-
waitForCreationInterval,
309-
waitForCreationTimeout,
326+
waitForInterval,
327+
waitForTimeout,
310328
true,
311329
func(ctx context.Context) (done bool, err error) {
312330
err = s.Mgr.GetClient().Get(ctx, types.NamespacedName{Name: "kubernetes", Namespace: "default"}, service)
@@ -325,8 +343,8 @@ func (s *ControllerManagerTestSuiteBase) GetAPIServerService() *corev1.Service {
325343
func (s *ControllerManagerTestSuiteBase) GetAPIServerEndpoints() *corev1.Endpoints {
326344
endpoints := &corev1.Endpoints{}
327345
s.Require().NoError(wait.PollUntilContextTimeout(context.Background(),
328-
waitForCreationInterval,
329-
waitForCreationTimeout,
346+
waitForInterval,
347+
waitForTimeout,
330348
true,
331349
func(ctx context.Context) (done bool, err error) {
332350
err = s.Mgr.GetClient().Get(ctx, types.NamespacedName{Name: "kubernetes", Namespace: "default"}, endpoints)

0 commit comments

Comments
 (0)