Skip to content

Commit 0d66093

Browse files
committed
added unit tests for swiftv2 windows
1 parent 4fa7140 commit 0d66093

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func addDefaultDenyACL(podIPInfo *cns.PodIpInfo) error {
7171
return err
7272
}
7373

74-
valueIn, err := getDefaultDenyACLPolicy(hcn.DirectionTypeOut)
74+
valueIn, err := getDefaultDenyACLPolicy(hcn.DirectionTypeIn)
7575
if err != nil {
7676
fmt.Printf("Failed to get default deny ACL policy ingress: %v\n", err)
7777
return err
@@ -91,11 +91,17 @@ func addDefaultDenyACL(podIPInfo *cns.PodIpInfo) error {
9191
}
9292

9393
func getDefaultDenyACLPolicy(direction hcn.DirectionType) ([]byte, error) {
94-
denyACL := map[string]interface{}{
95-
"Type": "ACL",
96-
"Action": hcn.ActionTypeBlock,
97-
"Direction": direction,
98-
"Priority": "10000",
94+
type DefaultDenyACL struct {
95+
Type string `json:"Type"`
96+
Action hcn.ActionType `json:"Action"`
97+
Direction hcn.DirectionType `json:"Direction"`
98+
Priority int `json:"Priority"`
99+
}
100+
denyACL := DefaultDenyACL{
101+
Type: "ACL",
102+
Action: hcn.ActionTypeBlock,
103+
Direction: direction,
104+
Priority: 10000,
99105
}
100106
denyACLJSON, err := json.Marshal(denyACL)
101107
if err != nil {

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 71 additions & 1 deletion
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

@@ -101,4 +104,71 @@ func TestAddDefaultRoute(t *testing.T) {
101104
}
102105
}
103106

104-
//Add a test here that checks for equality between the default deny policies (in json) created by the function vs an what we expect the default deny json to look like
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+
addDefaultDenyACL(&podIPInfo)
143+
144+
// Normalize both slices so there is no extra spacing, new lines, etc
145+
normalizedExpected := normalizeKVPairs(t, expectedDefaultDenyACL)
146+
normalizedActual := normalizeKVPairs(t, podIPInfo.DefaultDenyACL)
147+
if !reflect.DeepEqual(normalizedExpected, normalizedActual) {
148+
t.Errorf("got '%+v', expected '%+v'", podIPInfo.DefaultDenyACL, expectedDefaultDenyACL)
149+
}
150+
}
151+
152+
// 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
153+
func normalizeKVPairs(t *testing.T, kvPairs []cni.KVPair) []cni.KVPair {
154+
normalized := make([]cni.KVPair, len(kvPairs))
155+
156+
for i, kv := range kvPairs {
157+
var unmarshaledValue map[string]interface{}
158+
// Unmarshal the Value into a map
159+
err := json.Unmarshal(kv.Value, &unmarshaledValue)
160+
require.NoError(t, err, "Failed to unmarshal JSON value")
161+
162+
// Marshal it back to compact JSON
163+
normalizedValue, err := json.Marshal(unmarshaledValue)
164+
require.NoError(t, err, "Failed to re-marshal JSON value")
165+
166+
// Replace Value with the normalized compact JSON
167+
normalized[i] = cni.KVPair{
168+
Name: kv.Name,
169+
Value: normalizedValue,
170+
}
171+
}
172+
173+
return normalized
174+
}

0 commit comments

Comments
 (0)