@@ -34,6 +34,9 @@ func Test_SynthesizeListenerCreate(t *testing.T) {
3434ResourceMeta : core .NewResourceMeta (stack , "AWS:VPCServiceNetwork::Listener" , "l-id" ),
3535Spec : model.ListenerSpec {
3636StackServiceId : "stack-svc-id" ,
37+ DefaultAction : & model.DefaultAction {
38+ FixedResponseStatusCode : aws .Int64 (404 ),
39+ },
3740},
3841}
3942assert .NoError (t , stack .AddResource (l ))
@@ -73,6 +76,9 @@ func Test_SynthesizeListener_CreatNewHTTPListener_DeleteStaleHTTPSListener(t *te
7376StackServiceId : "stack-svc-id" ,
7477Protocol : vpclattice .ListenerProtocolHttp ,
7578Port : 80 ,
79+ DefaultAction : & model.DefaultAction {
80+ FixedResponseStatusCode : aws .Int64 (404 ),
81+ },
7682},
7783}
7884assert .NoError (t , stack .AddResource (l ))
@@ -135,9 +141,9 @@ func Test_SynthesizeListener_CreatNewTLSPassthroughListener_DeleteStaleHTTPSList
135141}
136142assert .NoError (t , stack .AddResource (rule ))
137143mockTargetGroupManager .EXPECT ().ResolveRuleTgIds (gomock .Any (), gomock .Any (), gomock .Any ()).DoAndReturn (
138- func (ctx context.Context , rule * model.Rule , stack core.Stack ) error {
139- rule . Spec . Action .TargetGroups [0 ].LatticeTgId = "lattice-tg-id-1"
140- rule . Spec . Action .TargetGroups [1 ].LatticeTgId = "lattice-tg-id-2"
144+ func (ctx context.Context , ruleAction * model.RuleAction , stack core.Stack ) error {
145+ ruleAction .TargetGroups [0 ].LatticeTgId = "lattice-tg-id-1"
146+ ruleAction .TargetGroups [1 ].LatticeTgId = "lattice-tg-id-2"
141147return nil
142148})
143149l := & model.Listener {
@@ -146,6 +152,7 @@ func Test_SynthesizeListener_CreatNewTLSPassthroughListener_DeleteStaleHTTPSList
146152StackServiceId : "stack-svc-id" ,
147153Protocol : vpclattice .ListenerProtocolTlsPassthrough ,
148154Port : 80 ,
155+ DefaultAction : & model.DefaultAction {Forward : & model.RuleAction {TargetGroups : rule .Spec .Action .TargetGroups }},
149156},
150157}
151158assert .NoError (t , stack .AddResource (l ))
@@ -187,46 +194,60 @@ func Test_SynthesizeListener_CreatNewTLSPassthroughListener_DeleteStaleHTTPSList
187194}
188195
189196func Test_listenerSynthesizer_getLatticeListenerDefaultAction_FixedResponse404 (t * testing.T ) {
190- fixResponseAction404 := & vpclattice.RuleAction {
197+ latticeFixResponseAction404 := & vpclattice.RuleAction {
191198FixedResponse : & vpclattice.FixedResponseAction {
192199StatusCode : aws .Int64 (404 ),
193200},
194201}
195202tests := []struct {
196- name string
197- listenerProtocol string
198- want * vpclattice.RuleAction
199- wantErr error
203+ name string
204+ stackListenerDefaultAction * model.DefaultAction
205+ listenerProtocol string
206+ want * vpclattice.RuleAction
207+ wantErr error
200208}{
201209{
202- name : "HTTP protocol Listener has the 404 fixed response default action" ,
203- listenerProtocol : "HTTP" ,
204- want : fixResponseAction404 ,
205- wantErr : nil ,
210+ name : "HTTP protocol Listener has the 404 fixed response modelListenerDefaultAction, return lattice fixed response 404 DefaultAction" ,
211+ stackListenerDefaultAction : & model.DefaultAction {FixedResponseStatusCode : aws .Int64 (404 )},
212+ listenerProtocol : vpclattice .ListenerProtocolHttp ,
213+ want : latticeFixResponseAction404 ,
214+ wantErr : nil ,
206215},
207216{
208- name : "HTTPS protocol Listener has the 404 fixed response default action" ,
209- listenerProtocol : "HTTPS" ,
210- want : fixResponseAction404 ,
211- wantErr : nil ,
217+ name : "HTTPS protocol Listener has the 404 fixed response modelListenerDefaultAction, return lattice fixed response 404 DefaultAction" ,
218+ stackListenerDefaultAction : & model.DefaultAction {FixedResponseStatusCode : aws .Int64 (404 )},
219+ listenerProtocol : vpclattice .ListenerProtocolHttps ,
220+ want : latticeFixResponseAction404 ,
221+ wantErr : nil ,
212222},
213223}
214224
215225c := gomock .NewController (t )
216226defer c .Finish ()
217227mockListenerMgr := NewMockListenerManager (c )
218228mockTargetGroupManager := NewMockTargetGroupManager (c )
219- stack := core .NewDefaultStack (core.StackID {Name : "foo" , Namespace : "bar" })
220229
221230for _ , tt := range tests {
222231t .Run (tt .name , func (t * testing.T ) {
232+ stack := core .NewDefaultStack (core.StackID {Name : "foo" , Namespace : "bar" })
233+ modelListener := & model.Listener {
234+ ResourceMeta : core .NewResourceMeta (stack , "AWS:VPCServiceNetwork::Listener" , "modelListener-id" ),
235+ Spec : model.ListenerSpec {
236+ StackServiceId : "stack-svc-id" ,
237+ Protocol : tt .listenerProtocol ,
238+ Port : 80 ,
239+ DefaultAction : tt .stackListenerDefaultAction ,
240+ },
241+ }
242+ assert .NoError (t , stack .AddResource (modelListener ))
243+
223244l := & listenerSynthesizer {
224245log : gwlog .FallbackLogger ,
225246listenerMgr : mockListenerMgr ,
226247tgManager : mockTargetGroupManager ,
227248stack : stack ,
228249}
229- got , err := l .getLatticeListenerDefaultAction (context .TODO (), tt . listenerProtocol )
250+ got , err := l .getLatticeListenerDefaultAction (context .TODO (), modelListener )
230251
231252assert .Equalf (t , tt .want , got , "getLatticeListenerDefaultAction() listenerProtocol: %v" , tt .listenerProtocol )
232253assert .Equal (t , tt .wantErr , err )
@@ -245,49 +266,56 @@ func Test_listenerSynthesizer_getLatticeListenerDefaultAction_TLS_PASSTHROUGH_Li
245266mockListenerMgr := NewMockListenerManager (c )
246267mockTargetGroupManager := NewMockTargetGroupManager (c )
247268mockTargetGroupManager .EXPECT ().ResolveRuleTgIds (gomock .Any (), gomock .Any (), gomock .Any ()).DoAndReturn (
248- func (ctx context.Context , rule * model.Rule , stack core.Stack ) error {
249- rule . Spec . Action .TargetGroups [0 ].LatticeTgId = "lattice-service-export-tg-id-1"
250- rule . Spec . Action .TargetGroups [1 ].LatticeTgId = "lattice-tg-id-2"
251- rule . Spec . Action .TargetGroups [2 ].LatticeTgId = model .InvalidBackendRefTgId
269+ func (ctx context.Context , ruleAction * model.RuleAction , stack core.Stack ) error {
270+ ruleAction .TargetGroups [0 ].LatticeTgId = "lattice-service-export-tg-id-1"
271+ ruleAction .TargetGroups [1 ].LatticeTgId = "lattice-tg-id-2"
272+ ruleAction .TargetGroups [2 ].LatticeTgId = model .InvalidBackendRefTgId
252273return nil
253274
254275})
255276stack := core .NewDefaultStack (core.StackID {Name : "foo" , Namespace : "bar" })
256- stackRule := & model.Rule {
257- ResourceMeta : core .NewResourceMeta (stack , "AWS:VPCServiceNetwork::Rule" , "rule-id" ),
258- Spec : model.RuleSpec {
259- Action : model.RuleAction {
260- TargetGroups : []* model.RuleTargetGroup {
261- {
262- SvcImportTG : & model.SvcImportTargetGroup {
263- K8SClusterName : "cluster-name" ,
264- K8SServiceName : "svc-name" ,
265- K8SServiceNamespace : "ns" ,
266- VpcId : "vpc-id" ,
277+
278+ modelListener := & model.Listener {
279+ ResourceMeta : core .NewResourceMeta (stack , "AWS:VPCServiceNetwork::Listener" , "modelListener-id" ),
280+ Spec : model.ListenerSpec {
281+ StackServiceId : "stack-svc-id" ,
282+ Protocol : vpclattice .ListenerProtocolTlsPassthrough ,
283+ Port : 80 ,
284+ DefaultAction : & model.DefaultAction {
285+ Forward : & model.RuleAction {
286+ TargetGroups : []* model.RuleTargetGroup {
287+ {
288+ SvcImportTG : & model.SvcImportTargetGroup {
289+ K8SClusterName : "cluster-name" ,
290+ K8SServiceName : "svc-name" ,
291+ K8SServiceNamespace : "ns" ,
292+ VpcId : "vpc-id" ,
293+ },
294+ Weight : 10 ,
295+ },
296+ {
297+ StackTargetGroupId : "lattice-tg-id-2" ,
298+ Weight : 30 ,
299+ },
300+ {
301+ StackTargetGroupId : model .InvalidBackendRefTgId ,
302+ Weight : 60 ,
267303},
268- Weight : 10 ,
269- },
270- {
271- StackTargetGroupId : "stack-tg-id-2" ,
272- Weight : 30 ,
273- },
274- {
275- StackTargetGroupId : model .InvalidBackendRefTgId ,
276- Weight : 60 ,
277304},
278305},
279306},
280307},
281308}
282- assert .NoError (t , stack .AddResource (stackRule ))
309+ assert .NoError (t , stack .AddResource (modelListener ))
310+
283311l := & listenerSynthesizer {
284312log : gwlog .FallbackLogger ,
285313listenerMgr : mockListenerMgr ,
286314tgManager : mockTargetGroupManager ,
287315stack : stack ,
288316}
289- gotDefaultAction , err := l .getLatticeListenerDefaultAction (context .TODO (), tlspassthroughListenerProtocol )
290- wantedListenerDefaultAction := & vpclattice.RuleAction {
317+ gotDefaultAction , err := l .getLatticeListenerDefaultAction (context .TODO (), modelListener )
318+ wantedLatticeListenerDefaultAction := & vpclattice.RuleAction {
291319Forward : & vpclattice.ForwardAction {
292320TargetGroups : []* vpclattice.WeightedTargetGroup {
293321{
@@ -305,15 +333,15 @@ func Test_listenerSynthesizer_getLatticeListenerDefaultAction_TLS_PASSTHROUGH_Li
305333},
306334},
307335}
308- assert .Equalf (t , wantedListenerDefaultAction , gotDefaultAction , "getLatticeListenerDefaultAction() tlspassthroughListenerProtocol: %v" , tlspassthroughListenerProtocol )
336+ assert .Equalf (t , wantedLatticeListenerDefaultAction , gotDefaultAction , "getLatticeListenerDefaultAction() tlspassthroughListenerProtocol: %v" , tlspassthroughListenerProtocol )
309337assert .Nil (t , err )
310338})
311339
312340t .Run ("ResolveRuleTgIds failed, return err" , func (t * testing.T ) {
313341mockListenerMgr := NewMockListenerManager (c )
314342mockTargetGroupManager := NewMockTargetGroupManager (c )
315343mockTargetGroupManager .EXPECT ().ResolveRuleTgIds (gomock .Any (), gomock .Any (), gomock .Any ()).DoAndReturn (
316- func (ctx context.Context , rule * model.Rule , stack core.Stack ) error {
344+ func (ctx context.Context , ruleAction * model.RuleAction , stack core.Stack ) error {
317345return fmt .Errorf ("failed to resolve rule tg ids" )
318346})
319347stack := core .NewDefaultStack (core.StackID {Name : "foo" , Namespace : "bar" })
@@ -324,13 +352,31 @@ func Test_listenerSynthesizer_getLatticeListenerDefaultAction_TLS_PASSTHROUGH_Li
324352},
325353}
326354assert .NoError (t , stack .AddResource (stackRule ))
355+ modelListener := & model.Listener {
356+ ResourceMeta : core .NewResourceMeta (stack , "AWS:VPCServiceNetwork::Listener" , "modelListener-id" ),
357+ Spec : model.ListenerSpec {
358+ StackServiceId : "stack-svc-id" ,
359+ Protocol : vpclattice .ListenerProtocolTlsPassthrough ,
360+ Port : 80 ,
361+ DefaultAction : & model.DefaultAction {
362+ Forward : & model.RuleAction {
363+ TargetGroups : []* model.RuleTargetGroup {
364+ {
365+ StackTargetGroupId : "lattice-tg-id-1" ,
366+ Weight : 100 ,
367+ },
368+ },
369+ },
370+ },
371+ },
372+ }
327373l := & listenerSynthesizer {
328374log : gwlog .FallbackLogger ,
329375listenerMgr : mockListenerMgr ,
330376tgManager : mockTargetGroupManager ,
331377stack : stack ,
332378}
333- gotDefaultAction , err := l .getLatticeListenerDefaultAction (context .TODO (), tlspassthroughListenerProtocol )
379+ gotDefaultAction , err := l .getLatticeListenerDefaultAction (context .TODO (), modelListener )
334380assert .Nil (t , gotDefaultAction )
335381assert .NotNil (t , err )
336382
0 commit comments