Skip to content

Commit ea04d99

Browse files
committed
test: add UT for error message check
1 parent ea56b16 commit ea04d99

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ var (
2020
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
2121
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
2222
errMTPNCNotReady = errors.New("network is not ready - mtpnc is not ready")
23+
errMTPNCNotFound = errors.New("network is not ready - mtpnc is not found")
2324
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
2425
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
2526
)
2627

28+
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
29+
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
30+
31+
var getMTPNCNotFoundErr = func(err error) error {
32+
return fmt.Errorf("%w: %v", errMTPNCNotFound, err)
33+
}
34+
2735
const (
2836
prefixLength = 32
2937
overlayGatewayv4 = "169.254.1.1"
@@ -74,7 +82,7 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
7482
mtpnc := v1alpha1.MultitenantPodNetworkConfig{}
7583
mtpncNamespacedName := k8stypes.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
7684
if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
77-
return nil, errors.Wrapf(err, "failed to get pod's mtpnc from cache")
85+
return nil, getMTPNCNotFoundErr(err)
7886
}
7987

8088
// Check if the MTPNC CRD is ready. If one of the fields is empty, return error
@@ -192,7 +200,7 @@ func (k *K8sSWIFTv2Middleware) getMTPNC(ctx context.Context, podInfo cns.PodInfo
192200
mtpnc := v1alpha1.MultitenantPodNetworkConfig{}
193201
mtpncNamespacedName := k8stypes.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
194202
if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
195-
return v1alpha1.MultitenantPodNetworkConfig{}, types.UnexpectedError, fmt.Errorf("failed to get pod's mtpnc from cache : %w", err).Error()
203+
return v1alpha1.MultitenantPodNetworkConfig{}, types.UnexpectedError, getMTPNCNotFoundErr(err).Error()
196204
}
197205
// Check if the MTPNC CRD is ready. If one of the fields is empty, return error
198206
if !mtpnc.IsReady() {

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middlewares
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78
"testing"
@@ -203,17 +204,20 @@ func TestValidateMultitenantIPConfigsRequestFailure(t *testing.T) {
203204
_, respCode, _ = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
204205
assert.Equal(t, respCode, types.UnexpectedError)
205206

207+
kubeletExpectedString := "network is not ready"
206208
// Failed to get MTPNC
207209
b, _ = testPod3Info.OrchestratorContext()
208210
failReq.OrchestratorContext = b
209-
_, respCode, _ = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
211+
_, respCode, msg := middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
210212
assert.Equal(t, respCode, types.UnexpectedError)
213+
assert.Assert(t, strings.Contains(msg, kubeletExpectedString), "expected error message to be '%s', got '%s'", kubeletExpectedString, msg)
211214

212215
// MTPNC not ready
213216
b, _ = testPod4Info.OrchestratorContext()
214217
failReq.OrchestratorContext = b
215-
_, respCode, _ = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
218+
_, respCode, msg = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
216219
assert.Equal(t, respCode, types.UnexpectedError)
220+
assert.Assert(t, strings.Contains(msg, kubeletExpectedString), "expected error message to be '%s', got '%s'", kubeletExpectedString, msg)
217221
}
218222

219223
func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {
@@ -232,27 +236,21 @@ func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {
232236
assert.Equal(t, ipInfos[0].SkipDefaultRoutes, false)
233237
}
234238

239+
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
240+
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
241+
// This test is to ensure that the error string contains "network is not ready"
235242
func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
236243
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
237244

238245
// Pod's MTPNC doesn't exist in cache test
239246
_, err := middleware.getIPConfig(context.TODO(), testPod3Info)
240-
assert.ErrorContains(t, err, mock.ErrMTPNCNotFound.Error())
247+
assert.Assert(t, errors.Is(err, errMTPNCNotFound), "expected error to wrap errMTPNCNotFound, got: %v", err)
248+
assert.ErrorContains(t, err, "network is not ready")
241249

242250
// Pod's MTPNC is not ready test
243251
_, err = middleware.getIPConfig(context.TODO(), testPod4Info)
244252
assert.Error(t, err, errMTPNCNotReady.Error())
245-
}
246-
247-
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
248-
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
249-
// This test is to ensure that the error string errMTPNCNotReady contains "network is not ready"
250-
func TestMTPNCNotReadyErrorMessage(t *testing.T) {
251-
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
252-
// Pod's MTPNC is not ready test
253-
_, err := middleware.getIPConfig(context.TODO(), testPod4Info)
254-
255-
assert.Equal(t, strings.Contains(err.Error(), "network is not ready"), true)
253+
assert.ErrorContains(t, err, "network is not ready")
256254
}
257255

258256
func TestSetRoutesSuccess(t *testing.T) {

0 commit comments

Comments
 (0)