4
4
package policy
5
5
6
6
import (
7
+ "encoding/json"
7
8
"testing"
8
9
10
+ "github.com/Microsoft/hcsshim/hcn"
9
11
. "github.com/onsi/ginkgo"
10
12
. "github.com/onsi/gomega"
11
13
)
@@ -40,11 +42,104 @@ var _ = Describe("Windows Policies", func() {
40
42
}}` ),
41
43
}
42
44
43
- expected_policy := `{"InboundProxyPort":"15003","OutboundProxyPort":"15001","FilterTuple":{"Protocols":"6"},"UserSID":"S-1-5-32-556","InboundExceptions":{},"OutboundExceptions":{}}`
45
+ expectedPolicy := `{"InboundProxyPort":"15003","OutboundProxyPort":"15001","FilterTuple":{"Protocols":"6"},"UserSID":"S-1-5-32-556","InboundExceptions":{},"OutboundExceptions":{}}`
44
46
45
47
generatedPolicy , err := GetHcnL4WFPProxyPolicy (policy )
46
48
Expect (err ).To (BeNil ())
47
- Expect (string (generatedPolicy .Settings )).To (Equal (expected_policy ))
49
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
50
+ })
51
+ })
52
+
53
+ Describe ("Test GetHcnACLPolicy" , func () {
54
+ It ("Should raise error for invalid json" , func () {
55
+ policy := Policy {
56
+ Type : ACLPolicy ,
57
+ Data : []byte (`invalid json` ),
58
+ }
59
+
60
+ _ , err := GetHcnACLPolicy (policy )
61
+ Expect (err ).NotTo (BeNil ())
62
+ })
63
+
64
+ It ("Should marshall the ACL policy correctly" , func () {
65
+ policy := Policy {
66
+ Type : ACLPolicy ,
67
+ Data : []byte (`{
68
+ "Type": "ACL",
69
+ "Protocols": "TCP",
70
+ "Direction": "In",
71
+ "Action": "Allow"
72
+ }` ),
73
+ }
74
+ expectedPolicy := `{"Protocols":"TCP","Action":"Allow","Direction":"In"}`
75
+
76
+ generatedPolicy , err := GetHcnACLPolicy (policy )
77
+ Expect (err ).To (BeNil ())
78
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
79
+ })
80
+ })
81
+
82
+ Describe ("Test GetHcnOutBoundNATPolicy" , func () {
83
+ It ("Should raise error for invalid json" , func () {
84
+ policy := Policy {
85
+ Type : OutBoundNatPolicy ,
86
+ Data : []byte (`invalid json` ),
87
+ }
88
+
89
+ _ , err := GetHcnOutBoundNATPolicy (policy , nil )
90
+ Expect (err ).NotTo (BeNil ())
91
+ })
92
+
93
+ It ("Should marshall the OutBoundNAT policy correctly" , func () {
94
+ policy := Policy {
95
+ Type : OutBoundNatPolicy ,
96
+ Data : []byte (`{
97
+ "Type": "OutBoundNAT",
98
+ "ExceptionList": ["10.240.0.0/16","10.0.0.0/8"]
99
+ }` ),
100
+ }
101
+ expectedPolicy := `{"Exceptions":["10.240.0.0/16","10.0.0.0/8"]}`
102
+
103
+ generatedPolicy , err := GetHcnOutBoundNATPolicy (policy , nil )
104
+ Expect (err ).To (BeNil ())
105
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
106
+
107
+ // test getHncOutBoundNATPolicy with epInfoData
108
+ expectedPolicy = `{"Exceptions":["10.240.0.0/16","10.0.0.0/8","50.1.1.1","60.1.1.1"]}`
109
+
110
+ epInfoData := make (map [string ]interface {})
111
+ epInfoData [CnetAddressSpace ] = []string {"50.1.1.1" , "60.1.1.1" }
112
+ generatedPolicy , err = GetHcnOutBoundNATPolicy (policy , epInfoData )
113
+ Expect (err ).To (BeNil ())
114
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
115
+ })
116
+ })
117
+
118
+ Describe ("Test GetHcnRoutePolicy" , func () {
119
+ It ("Should raise error for invalid json" , func () {
120
+ policy := Policy {
121
+ Type : RoutePolicy ,
122
+ Data : []byte (`invalid json` ),
123
+ }
124
+
125
+ _ , err := GetHcnRoutePolicy (policy )
126
+ Expect (err ).NotTo (BeNil ())
127
+ })
128
+
129
+ It ("Should marshall the Route policy correctly" , func () {
130
+ policy := Policy {
131
+ Type : RoutePolicy ,
132
+ Data : []byte (`{
133
+ "Type": "ROUTE",
134
+ "DestinationPrefix": "10.0.0.0/8",
135
+ "NeedEncap": true
136
+ }` ),
137
+ }
138
+ expectedPolicy := `{"DestinationPrefix":"10.0.0.0/8","NeedEncap":true}`
139
+
140
+ generatedPolicy , err := GetHcnRoutePolicy (policy )
141
+ Expect (err ).To (BeNil ())
142
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
48
143
})
49
144
})
50
145
@@ -57,4 +152,138 @@ var _ = Describe("Windows Policies", func() {
57
152
Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
58
153
})
59
154
})
155
+
156
+ Describe ("Test AddNATPolicyV1" , func () {
157
+ It ("Should marshall the NAT policy v1 correctly" , func () {
158
+ expectedPolicy := `{"Type":"OutBoundNAT","Destinations":["168.63.129.16"]}`
159
+
160
+ generatedPolicy , err := AddDnsNATPolicyV1 ()
161
+ Expect (err ).To (BeNil ())
162
+ Expect (string (generatedPolicy )).To (Equal (expectedPolicy ))
163
+ })
164
+ })
165
+
166
+ Describe ("Test AddNATPolicyV2" , func () {
167
+ It ("Should marshall the NAT policy v2 correctly" , func () {
168
+ vip := "vip"
169
+ destinations := []string {"192.168.1.1" , "192.169.1.1" }
170
+
171
+ expectedPolicy := `{"VirtualIP":"vip","Destinations":["192.168.1.1","192.169.1.1"]}`
172
+
173
+ generatedPolicy , err := AddNATPolicyV2 (vip , destinations )
174
+ Expect (err ).To (BeNil ())
175
+ Expect (string (generatedPolicy .Settings )).To (Equal (expectedPolicy ))
176
+ })
177
+ })
178
+
179
+ Describe ("Test GetHcnEndpointPolicies" , func () {
180
+ It ("Should marshall the policy correctly" , func () {
181
+ testPolicies := []Policy {}
182
+
183
+ rawPortMappingPolicy , _ := json .Marshal (& hcn.PortMappingPolicySetting {
184
+ ExternalPort : 8008 ,
185
+ InternalPort : 8080 ,
186
+ })
187
+
188
+ portMappingPolicy , _ := json .Marshal (& hcn.EndpointPolicy {
189
+ Type : hcn .PortMapping ,
190
+ Settings : rawPortMappingPolicy ,
191
+ })
192
+
193
+ hnsPolicy := Policy {
194
+ Type : PortMappingPolicy ,
195
+ Data : portMappingPolicy ,
196
+ }
197
+
198
+ testPolicies = append (testPolicies , hnsPolicy )
199
+
200
+ generatedPolicy , err := GetHcnEndpointPolicies (PortMappingPolicy , testPolicies , nil , false , true , nil )
201
+ Expect (err ).To (BeNil ())
202
+ Expect (string (generatedPolicy [0 ].Settings )).To (Equal (string (rawPortMappingPolicy )))
203
+ })
204
+ })
205
+
206
+ Describe ("Test GetHcnEndpointPolicies with invalid policy type" , func () {
207
+ It ("Should return error with invalid policy type" , func () {
208
+ testPolicies := []Policy {}
209
+
210
+ rawPortMappingPolicy , _ := json .Marshal (& hcn.PortMappingPolicySetting {
211
+ ExternalPort : 8008 ,
212
+ InternalPort : 8080 ,
213
+ })
214
+
215
+ portMappingPolicy , _ := json .Marshal (& hcn.EndpointPolicy {
216
+ Type : "invalidType" , // should return error with invalid policy type
217
+ Settings : rawPortMappingPolicy ,
218
+ })
219
+
220
+ hnsPolicy := Policy {
221
+ Type : PortMappingPolicy ,
222
+ Data : portMappingPolicy ,
223
+ }
224
+
225
+ testPolicies = append (testPolicies , hnsPolicy )
226
+
227
+ _ , err := GetHcnEndpointPolicies (PortMappingPolicy , testPolicies , nil , false , true , nil )
228
+ Expect (err ).NotTo (BeNil ())
229
+ })
230
+ })
231
+
232
+ Describe ("Test GetHcnEndpointPolicies with multiple policies" , func () {
233
+ It ("Should marshall all policies correctly" , func () {
234
+ testPolicies := []Policy {}
235
+
236
+ // add first portMapping policy to testPolicies
237
+ rawPortMappingPolicyOne , _ := json .Marshal (& hcn.PortMappingPolicySetting {
238
+ ExternalPort : 8008 ,
239
+ InternalPort : 8080 ,
240
+ })
241
+
242
+ portMappingPolicyOne , _ := json .Marshal (& hcn.EndpointPolicy {
243
+ Type : hcn .PortMapping ,
244
+ Settings : rawPortMappingPolicyOne ,
245
+ })
246
+
247
+ portMappinghnsPolicyOne := Policy {
248
+ Type : PortMappingPolicy ,
249
+ Data : portMappingPolicyOne ,
250
+ }
251
+
252
+ testPolicies = append (testPolicies , portMappinghnsPolicyOne )
253
+
254
+ // add second portMapping policy to testPolicies
255
+ rawPortMappingPolicyTwo , _ := json .Marshal (& hcn.PortMappingPolicySetting {
256
+ ExternalPort : 9008 ,
257
+ InternalPort : 9090 ,
258
+ })
259
+
260
+ portMappingPolicyTwo , _ := json .Marshal (& hcn.EndpointPolicy {
261
+ Type : hcn .PortMapping ,
262
+ Settings : rawPortMappingPolicyTwo ,
263
+ })
264
+
265
+ portMappinghnsPolicyTwo := Policy {
266
+ Type : PortMappingPolicy ,
267
+ Data : portMappingPolicyTwo ,
268
+ }
269
+
270
+ testPolicies = append (testPolicies , portMappinghnsPolicyTwo )
271
+
272
+ generatedPolicy , err := GetHcnEndpointPolicies (PortMappingPolicy , testPolicies , nil , false , true , nil )
273
+ Expect (err ).To (BeNil ())
274
+
275
+ expectedPolicy := []hcn.EndpointPolicy {
276
+ {
277
+ Type : "PortMapping" ,
278
+ Settings : []byte (`{"InternalPort":8080,"ExternalPort":8008}` ),
279
+ },
280
+ {
281
+ Type : "PortMapping" ,
282
+ Settings : []byte (`{"InternalPort":9090,"ExternalPort":9008}` ),
283
+ },
284
+ }
285
+
286
+ Expect (generatedPolicy ).To (Equal (expectedPolicy ))
287
+ })
288
+ })
60
289
})
0 commit comments