Skip to content

Commit 0c43db6

Browse files
committed
updated unit test returns
1 parent 7913564 commit 0c43db6

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/pkg/errors"
1515
)
1616

17-
var defaultDenyEgressPolicy policy.Policy = getEndpointPolicy(cns.DirectionTypeOut)
17+
var defaultDenyEgressPolicy policy.Policy = mustGetEndpointPolicy(cns.DirectionTypeOut)
1818

19-
var defaultDenyIngressPolicy policy.Policy = getEndpointPolicy(cns.DirectionTypeIn)
19+
var defaultDenyIngressPolicy policy.Policy = mustGetEndpointPolicy(cns.DirectionTypeIn)
2020

2121
// for AKS L1VH, do not set default route on infraNIC to avoid customer pod reaching all infra vnet services
2222
// default route is set for secondary interface NIC(i.e,delegatedNIC)
@@ -70,20 +70,32 @@ func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gwIP st
7070
podIPInfo.Routes = append(podIPInfo.Routes, route)
7171
}
7272

73+
func mustGetEndpointPolicy(direction string) policy.Policy {
74+
endpointPolicy, err := getEndpointPolicy(direction)
75+
if err != nil {
76+
panic(err)
77+
}
78+
return endpointPolicy
79+
}
80+
7381
// get policy of type endpoint policy given the params
74-
func getEndpointPolicy(direction string) policy.Policy {
75-
endpointPolicy := createEndpointPolicy(direction)
82+
func getEndpointPolicy(direction string) (policy.Policy, error) {
83+
endpointPolicy, err := createEndpointPolicy(direction)
84+
85+
if err != nil {
86+
return policy.Policy{}, fmt.Errorf("error creating endpoint policy: %w", err)
87+
}
7688

7789
additionalArgs := policy.Policy{
7890
Type: policy.EndpointPolicy,
7991
Data: endpointPolicy,
8092
}
8193

82-
return additionalArgs
94+
return additionalArgs, nil
8395
}
8496

8597
// create policy given the params
86-
func createEndpointPolicy(direction string) []byte {
98+
func createEndpointPolicy(direction string) ([]byte, error) {
8799
endpointPolicy := struct {
88100
Type string `json:"Type"`
89101
Action string `json:"Action"`
@@ -98,10 +110,10 @@ func createEndpointPolicy(direction string) []byte {
98110

99111
rawPolicy, err := json.Marshal(endpointPolicy)
100112
if err != nil {
101-
logger.Errorf("error marshalling policy to json, err is: %v", err)
113+
return nil, fmt.Errorf("error marshalling policy to json, err is: %w", err)
102114
}
103115

104-
return rawPolicy
116+
return rawPolicy, nil
105117
}
106118

107119
// IPConfigsRequestHandlerWrapper is the middleware function for handling SWIFT v2 IP configs requests for AKS-SWIFT. This function wrapped the default SWIFT request
@@ -136,7 +148,7 @@ func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
136148
}
137149

138150
// GetDefaultDenyBool takes in mtpnc and returns the value of defaultDenyACLBool from it
139-
defaultDenyACLBool, err := GetDefaultDenyBool(mtpnc)
151+
defaultDenyACLBool := GetDefaultDenyBool(mtpnc)
140152

141153
// ipConfigsResp has infra IP configs -> if defaultDenyACLbool is enabled, add the default deny endpoint policies as a property in PodIpInfo
142154
for i := range ipConfigsResp.PodIPInfo {
@@ -193,7 +205,7 @@ func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
193205
}
194206
}
195207

196-
func GetDefaultDenyBool(mtpnc v1alpha1.MultitenantPodNetworkConfig) (bool, error) {
208+
func GetDefaultDenyBool(mtpnc v1alpha1.MultitenantPodNetworkConfig) bool {
197209
// returns the value of DefaultDenyACL from mtpnc
198-
return mtpnc.Status.DefaultDenyACL, nil
210+
return mtpnc.Status.DefaultDenyACL
199211
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
1111
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1212
"github.com/Azure/azure-container-networking/network/policy"
13+
"github.com/google/go-cmp/cmp"
1314
"github.com/stretchr/testify/require"
1415
"gotest.tools/v3/assert"
1516
)
@@ -150,16 +151,16 @@ func TestAddDefaultDenyACL(t *testing.T) {
150151
var defaultDenyEgressPolicy, defaultDenyIngressPolicy policy.Policy
151152
var err error
152153

153-
defaultDenyEgressPolicy = getEndpointPolicy("Out")
154-
defaultDenyIngressPolicy = getEndpointPolicy("In")
154+
defaultDenyEgressPolicy = mustGetEndpointPolicy("Out")
155+
defaultDenyIngressPolicy = mustGetEndpointPolicy("In")
155156

156157
allEndpoints = append(allEndpoints, defaultDenyEgressPolicy, defaultDenyIngressPolicy)
157158

158159
// Normalize both slices so there is no extra spacing, new lines, etc
159160
normalizedExpected := normalizeKVPairs(t, expectedDefaultDenyEndpoint)
160161
normalizedActual := normalizeKVPairs(t, allEndpoints)
161-
if !reflect.DeepEqual(normalizedExpected, normalizedActual) {
162-
t.Errorf("got '%+v', expected '%+v'", normalizedActual, normalizedExpected)
162+
if !cmp.Equal(normalizedExpected, normalizedActual) {
163+
t.Error("received policy differs from expectation: diff", cmp.Diff(normalizedExpected, normalizedActual))
163164
}
164165
assert.Equal(t, err, nil)
165166
}

0 commit comments

Comments
 (0)