Skip to content

Commit 7b01c07

Browse files
committed
added unit tests
1 parent 3489f9c commit 7b01c07

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/Azure/azure-container-networking/cni"
77
"github.com/Azure/azure-container-networking/cns"
8-
"github.com/Azure/azure-container-networking/cns/logger"
98
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
109
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1110
"github.com/Microsoft/hcsshim/hcn"
@@ -89,8 +88,6 @@ func addDefaultDenyACL(podIpInfo *cns.PodIpInfo) error {
8988

9089
podIpInfo.DefaultDenyACL = append(podIpInfo.DefaultDenyACL, additionalArgs...)
9190

92-
logger.Printf("The length of podIpInfo.DefaultDenyACL is: %v", len(podIpInfo.DefaultDenyACL))
93-
9491
return nil
9592
}
9693

@@ -114,8 +111,6 @@ func getDefaultDenyACLPolicy(direction hcn.DirectionType) ([]byte, error) {
114111

115112
denyACLJSON, err := json.Marshal(denyACL)
116113

117-
logger.Printf("ACL Created for direction %s is : %s", direction, denyACLJSON)
118-
119114
if err != nil {
120115
return nil, errors.Wrap(err, "error marshalling default deny policy to json")
121116
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package middlewares
22

33
import (
4+
"encoding/json"
45
"reflect"
56
"testing"
67

8+
"github.com/Azure/azure-container-networking/cni"
79
"github.com/Azure/azure-container-networking/cns"
810
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
911
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
12+
"github.com/stretchr/testify/require"
1013
"gotest.tools/v3/assert"
1114
)
1215

@@ -100,3 +103,73 @@ func TestAddDefaultRoute(t *testing.T) {
100103
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, expectedRoutes)
101104
}
102105
}
106+
107+
func TestAddDefaultDenyACL(t *testing.T) {
108+
valueOut := []byte(`{
109+
"Type": "ACL",
110+
"Action": "Block",
111+
"Direction": "Out",
112+
"Priority": 10000
113+
}`)
114+
115+
valueIn := []byte(`{
116+
"Type": "ACL",
117+
"Action": "Block",
118+
"Direction": "In",
119+
"Priority": 10000
120+
}`)
121+
122+
expectedDefaultDenyACL := []cni.KVPair{
123+
{
124+
Name: "EndpointPolicy",
125+
Value: valueOut,
126+
},
127+
{
128+
Name: "EndpointPolicy",
129+
Value: valueIn,
130+
},
131+
}
132+
133+
podIPInfo := cns.PodIpInfo{
134+
PodIPConfig: cns.IPSubnet{
135+
IPAddress: "20.240.1.242",
136+
PrefixLength: 32,
137+
},
138+
NICType: cns.DelegatedVMNIC,
139+
MacAddress: "12:34:56:78:9a:bc",
140+
}
141+
142+
err := addDefaultDenyACL(&podIPInfo)
143+
assert.Equal(t, err, nil)
144+
145+
// Normalize both slices so there is no extra spacing, new lines, etc
146+
normalizedExpected := normalizeKVPairs(t, expectedDefaultDenyACL)
147+
normalizedActual := normalizeKVPairs(t, podIPInfo.DefaultDenyACL)
148+
if !reflect.DeepEqual(normalizedExpected, normalizedActual) {
149+
t.Errorf("got '%+v', expected '%+v'", podIPInfo.DefaultDenyACL, expectedDefaultDenyACL)
150+
}
151+
}
152+
153+
// normalizeKVPairs normalizes the JSON values in the KV pairs by unmarshaling them into a map, then marshaling them back to compact JSON to remove any extra space, new lines, etc
154+
func normalizeKVPairs(t *testing.T, kvPairs []cni.KVPair) []cni.KVPair {
155+
normalized := make([]cni.KVPair, len(kvPairs))
156+
157+
for i, kv := range kvPairs {
158+
var unmarshaledValue map[string]interface{}
159+
// Unmarshal the Value into a map
160+
err := json.Unmarshal(kv.Value, &unmarshaledValue)
161+
require.NoError(t, err, "Failed to unmarshal JSON value")
162+
163+
// Marshal it back to compact JSON
164+
normalizedValue, err := json.Marshal(unmarshaledValue)
165+
require.NoError(t, err, "Failed to re-marshal JSON value")
166+
167+
// Replace Value with the normalized compact JSON
168+
normalized[i] = cni.KVPair{
169+
Name: kv.Name,
170+
Value: normalizedValue,
171+
}
172+
}
173+
174+
return normalized
175+
}

0 commit comments

Comments
 (0)