Skip to content

Commit af0504a

Browse files
committed
test: add create endpoint info policy test
1 parent eb059cb commit af0504a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

cni/network/network_windows_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,36 @@ func GetTestCNSResponseSecondaryWindows(macAddress string) map[string]network.In
898898
},
899899
}
900900
}
901+
func GetRawACLPolicy() (ret json.RawMessage) {
902+
var data map[string]interface{}
903+
formatted := []byte(`{
904+
"Type": "ACL",
905+
"Protocols": "6",
906+
"Action": "Block",
907+
"Direction": "Out",
908+
"RemoteAddresses": "168.63.129.16/32",
909+
"RemotePorts": "80",
910+
"Priority": 200,
911+
"RuleType": "Switch"
912+
}`)
913+
json.Unmarshal(formatted, &data) // nolint
914+
minified, _ := json.Marshal(data) // nolint
915+
ret = json.RawMessage(minified)
916+
return ret
917+
}
918+
func GetRawOutBoundNATPolicy() (ret json.RawMessage) {
919+
var data map[string]interface{}
920+
formatted := []byte(`{
921+
"Type": "OutBoundNAT",
922+
"ExceptionList": [
923+
"10.224.0.0/16"
924+
]
925+
}`)
926+
json.Unmarshal(formatted, &data) // nolint
927+
minified, _ := json.Marshal(data) // nolint
928+
ret = json.RawMessage(minified)
929+
return ret
930+
}
901931

902932
// Happy path scenario for add and delete
903933
func TestPluginWindowsAdd(t *testing.T) {
@@ -908,6 +938,20 @@ func TestPluginWindowsAdd(t *testing.T) {
908938
MultiTenancy: true,
909939
EnableExactMatchForPodName: true,
910940
Master: "eth0",
941+
// these are added to test that policies propagate to endpoint info
942+
AdditionalArgs: []cni.KVPair{
943+
{
944+
Name: "EndpointPolicy",
945+
Value: GetRawOutBoundNATPolicy(),
946+
},
947+
{
948+
Name: "EndpointPolicy",
949+
Value: GetRawACLPolicy(),
950+
},
951+
},
952+
WindowsSettings: cni.WindowsSettings{ // included to test functionality
953+
EnableLoopbackDSR: true,
954+
},
911955
}
912956
nwCfg := cni.NetworkConfig{
913957
CNIVersion: "0.3.0",
@@ -1002,6 +1046,31 @@ func TestPluginWindowsAdd(t *testing.T) {
10021046
Gateway: net.ParseIP("20.0.0.1"),
10031047
},
10041048
},
1049+
EndpointPolicies: []policy.Policy{
1050+
{
1051+
Type: policy.EndpointPolicy,
1052+
Data: GetRawOutBoundNATPolicy(),
1053+
},
1054+
{
1055+
Type: policy.EndpointPolicy,
1056+
Data: GetRawACLPolicy(),
1057+
},
1058+
{
1059+
Type: policy.EndpointPolicy,
1060+
// if enabled we create a loopback dsr policy based on the cns ip config
1061+
Data: json.RawMessage(`{"Type":"LoopbackDSR","IPAddress":"20.0.0.10"}`),
1062+
},
1063+
},
1064+
NetworkPolicies: []policy.Policy{
1065+
{
1066+
Type: policy.EndpointPolicy,
1067+
Data: GetRawOutBoundNATPolicy(),
1068+
},
1069+
{
1070+
Type: policy.EndpointPolicy,
1071+
Data: GetRawACLPolicy(),
1072+
},
1073+
},
10051074
},
10061075
epIDRegex: `.*`,
10071076
},
@@ -1047,6 +1116,30 @@ func TestPluginWindowsAdd(t *testing.T) {
10471116
Gateway: net.ParseIP("10.0.0.1"),
10481117
},
10491118
},
1119+
EndpointPolicies: []policy.Policy{
1120+
{
1121+
Type: policy.EndpointPolicy,
1122+
Data: GetRawOutBoundNATPolicy(),
1123+
},
1124+
{
1125+
Type: policy.EndpointPolicy,
1126+
Data: GetRawACLPolicy(),
1127+
},
1128+
{
1129+
Type: policy.EndpointPolicy,
1130+
Data: json.RawMessage(`{"Type":"LoopbackDSR","IPAddress":"10.0.0.10"}`),
1131+
},
1132+
},
1133+
NetworkPolicies: []policy.Policy{
1134+
{
1135+
Type: policy.EndpointPolicy,
1136+
Data: GetRawOutBoundNATPolicy(),
1137+
},
1138+
{
1139+
Type: policy.EndpointPolicy,
1140+
Data: GetRawACLPolicy(),
1141+
},
1142+
},
10501143
},
10511144
epIDRegex: `.*`,
10521145
},
@@ -1211,6 +1304,37 @@ func TestPluginWindowsAdd(t *testing.T) {
12111304
require.NoError(t, err)
12121305
}
12131306

1307+
// confirm separate entities
1308+
// that is, if one is modified, the other should not be modified
1309+
epInfos := []*network.EndpointInfo{}
1310+
for _, val := range allEndpoints {
1311+
epInfos = append(epInfos, val)
1312+
}
1313+
if len(epInfos) > 1 {
1314+
// ensure the endpoint data and options are separate entities when in separate endpoint infos
1315+
epInfo1 := epInfos[0]
1316+
epInfo2 := epInfos[1]
1317+
epInfo1.Data["dummy"] = "dummy value"
1318+
epInfo1.Options["dummy"] = "another dummy value"
1319+
require.NotEqual(t, epInfo1.Data, epInfo2.Data)
1320+
require.NotEqual(t, epInfo1.Options, epInfo2.Options)
1321+
1322+
// ensure the endpoint policy slices are separate entities when in separate endpoint infos
1323+
if len(epInfo1.EndpointPolicies) > 0 {
1324+
epInfo1.EndpointPolicies[0] = policy.Policy{
1325+
Type: policy.ACLPolicy,
1326+
}
1327+
require.NotEqual(t, epInfo1.EndpointPolicies, epInfo2.EndpointPolicies)
1328+
}
1329+
// ensure the network policy slices are separate entities when in separate endpoint infos
1330+
if len(epInfo1.NetworkPolicies) > 0 {
1331+
epInfo1.NetworkPolicies[0] = policy.Policy{
1332+
Type: policy.ACLPolicy,
1333+
}
1334+
require.NotEqual(t, epInfo1.NetworkPolicies, epInfo2.NetworkPolicies)
1335+
}
1336+
}
1337+
12141338
// ensure deleted
12151339
require.Empty(t, allEndpoints)
12161340
})

0 commit comments

Comments
 (0)