Skip to content

Commit 56106c5

Browse files
authored
refactor: refactor the test util func (#333)
1 parent f877730 commit 56106c5

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

test/common/trafficmanager/validator/backend.go

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,48 +46,56 @@ var (
4646
// while ignoring the generated endpoint name.
4747
func ValidateTrafficManagerBackendIfAcceptedAndIgnoringEndpointName(ctx context.Context, k8sClient client.Client, backendName types.NamespacedName, isAccepted bool, wantEndpoints []fleetnetv1beta1.TrafficManagerEndpointStatus, timeout time.Duration) fleetnetv1beta1.TrafficManagerBackendStatus {
4848
var gotStatus fleetnetv1beta1.TrafficManagerBackendStatus
49+
var err error
4950
gomega.Eventually(func() error {
50-
backend := &fleetnetv1beta1.TrafficManagerBackend{}
51-
if err := k8sClient.Get(ctx, backendName, backend); err != nil {
52-
return err
53-
}
54-
var wantStatus fleetnetv1beta1.TrafficManagerBackendStatus
55-
if !isAccepted {
56-
wantStatus = fleetnetv1beta1.TrafficManagerBackendStatus{
57-
Conditions: []metav1.Condition{
58-
{
59-
Status: metav1.ConditionFalse,
60-
Type: string(fleetnetv1beta1.TrafficManagerBackendConditionAccepted),
61-
Reason: string(fleetnetv1beta1.TrafficManagerBackendReasonInvalid),
62-
ObservedGeneration: backend.Generation,
63-
},
64-
},
65-
Endpoints: wantEndpoints,
66-
}
67-
} else {
68-
wantStatus = fleetnetv1beta1.TrafficManagerBackendStatus{
69-
Conditions: []metav1.Condition{
70-
{
71-
Status: metav1.ConditionTrue,
72-
Type: string(fleetnetv1beta1.TrafficManagerBackendConditionAccepted),
73-
Reason: string(fleetnetv1beta1.TrafficManagerBackendReasonAccepted),
74-
ObservedGeneration: backend.Generation,
75-
},
51+
gotStatus, err = CheckIfTrafficManagerBackendIfAcceptedAndIgnoringEndpointName(ctx, k8sClient, backendName, isAccepted, wantEndpoints)
52+
return err
53+
}, timeout, interval).Should(gomega.Succeed(), "Get() trafficManagerBackend status mismatch")
54+
return gotStatus
55+
}
56+
57+
// CheckIfTrafficManagerBackendIfAcceptedAndIgnoringEndpointName checks if the TrafficManagerBackend is accepted while ignoring the generated endpoint name.
58+
func CheckIfTrafficManagerBackendIfAcceptedAndIgnoringEndpointName(ctx context.Context, k8sClient client.Client, backendName types.NamespacedName, isAccepted bool, wantEndpoints []fleetnetv1beta1.TrafficManagerEndpointStatus) (fleetnetv1beta1.TrafficManagerBackendStatus, error) {
59+
var gotStatus fleetnetv1beta1.TrafficManagerBackendStatus
60+
backend := &fleetnetv1beta1.TrafficManagerBackend{}
61+
if err := k8sClient.Get(ctx, backendName, backend); err != nil {
62+
return gotStatus, err
63+
}
64+
var wantStatus fleetnetv1beta1.TrafficManagerBackendStatus
65+
if !isAccepted {
66+
wantStatus = fleetnetv1beta1.TrafficManagerBackendStatus{
67+
Conditions: []metav1.Condition{
68+
{
69+
Status: metav1.ConditionFalse,
70+
Type: string(fleetnetv1beta1.TrafficManagerBackendConditionAccepted),
71+
Reason: string(fleetnetv1beta1.TrafficManagerBackendReasonInvalid),
72+
ObservedGeneration: backend.Generation,
7673
},
77-
Endpoints: wantEndpoints,
78-
}
74+
},
75+
Endpoints: wantEndpoints,
7976
}
80-
gotStatus = backend.Status
81-
if diff := cmp.Diff(
82-
gotStatus,
83-
wantStatus,
84-
cmpTrafficManagerBackendStatusByIgnoringEndpointName,
85-
); diff != "" {
86-
return fmt.Errorf("trafficManagerBackend status diff (-got, +want): \n%s, got %+v", diff, gotStatus)
77+
} else {
78+
wantStatus = fleetnetv1beta1.TrafficManagerBackendStatus{
79+
Conditions: []metav1.Condition{
80+
{
81+
Status: metav1.ConditionTrue,
82+
Type: string(fleetnetv1beta1.TrafficManagerBackendConditionAccepted),
83+
Reason: string(fleetnetv1beta1.TrafficManagerBackendReasonAccepted),
84+
ObservedGeneration: backend.Generation,
85+
},
86+
},
87+
Endpoints: wantEndpoints,
8788
}
88-
return nil
89-
}, timeout, interval).Should(gomega.Succeed(), "Get() trafficManagerBackend status mismatch")
90-
return gotStatus
89+
}
90+
gotStatus = backend.Status
91+
if diff := cmp.Diff(
92+
gotStatus,
93+
wantStatus,
94+
cmpTrafficManagerBackendStatusByIgnoringEndpointName,
95+
); diff != "" {
96+
return gotStatus, fmt.Errorf("trafficManagerBackend status diff (-got, +want): \n%s, got %+v", diff, gotStatus)
97+
}
98+
return gotStatus, nil
9199
}
92100

93101
// ValidateTrafficManagerBackendStatusAndIgnoringEndpointNameConsistently validates the trafficManagerBackend status consistently

0 commit comments

Comments
 (0)