Skip to content

Commit a14b8af

Browse files
committed
add unit tests for coverage
1 parent dda9624 commit a14b8af

File tree

7 files changed

+130
-3
lines changed

7 files changed

+130
-3
lines changed

internal/mode/static/handler_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ var _ = Describe("eventHandler", func() {
184184
expectReconfig(dcfg, fakeCfgFiles)
185185
Expect(helpers.Diff(handler.GetLatestConfiguration(), &dcfg)).To(BeEmpty())
186186
})
187-
188187
It("should process Delete", func() {
189188
e := &events.DeleteEvent{
190189
Type: &gatewayv1.HTTPRoute{},
@@ -218,6 +217,46 @@ var _ = Describe("eventHandler", func() {
218217
return fakeStatusUpdater.UpdateGroupCallCount()
219218
}).Should(Equal(1))
220219
})
220+
It("should not build anything if graph is nil", func() {
221+
fakeProcessor.ProcessReturns(state.ClusterStateChange, nil)
222+
223+
e := &events.UpsertEvent{Resource: &gatewayv1.HTTPRoute{}}
224+
batch := []interface{}{e}
225+
226+
handler.HandleEventBatch(context.Background(), logr.Discard(), batch)
227+
228+
checkUpsertEventExpectations(e)
229+
Expect(fakeProvisioner.RegisterGatewayCallCount()).Should(Equal(0))
230+
Expect(fakeGenerator.GenerateCallCount()).Should(Equal(0))
231+
// status update for GatewayClass should not occur
232+
Eventually(
233+
func() int {
234+
return fakeStatusUpdater.UpdateGroupCallCount()
235+
}).Should(Equal(0))
236+
})
237+
It("should update gateway class even if gateway is invalid", func() {
238+
fakeProcessor.ProcessReturns(state.ClusterStateChange, &graph.Graph{
239+
Gateways: map[types.NamespacedName]*graph.Gateway{
240+
{Namespace: "test", Name: "gateway"}: {
241+
Valid: false,
242+
},
243+
},
244+
})
245+
246+
e := &events.UpsertEvent{Resource: &gatewayv1.HTTPRoute{}}
247+
batch := []interface{}{e}
248+
249+
handler.HandleEventBatch(context.Background(), logr.Discard(), batch)
250+
251+
checkUpsertEventExpectations(e)
252+
Expect(fakeProvisioner.RegisterGatewayCallCount()).Should(Equal(0))
253+
Expect(fakeGenerator.GenerateCallCount()).Should(Equal(0))
254+
// status update should still occur for GatewayClasses
255+
Eventually(
256+
func() int {
257+
return fakeStatusUpdater.UpdateGroupCallCount()
258+
}).Should(Equal(1))
259+
})
221260
})
222261

223262
When("a batch has multiple events", func() {

internal/mode/static/nginx/config/policies/validator_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ var _ = Describe("Policy CompositeValidator", func() {
2727
},
2828
}
2929

30+
bananaGVK := schema.GroupVersionKind{Group: "fruit", Version: "1", Kind: "banana"}
31+
bananaPolicy := &policiesfakes.FakePolicy{
32+
GetNameStub: func() string {
33+
return "banana"
34+
},
35+
}
36+
3037
mustExtractGVK := func(object client.Object) schema.GroupVersionKind {
3138
switch object.GetName() {
3239
case "apple":
@@ -64,6 +71,10 @@ var _ = Describe("Policy CompositeValidator", func() {
6471
},
6572
GVK: orangeGVK,
6673
},
74+
policies.ManagerConfig{
75+
Validator: &policiesfakes.FakeValidator{},
76+
GVK: bananaGVK,
77+
},
6778
)
6879

6980
Context("Validation", func() {
@@ -105,6 +116,13 @@ var _ = Describe("Policy CompositeValidator", func() {
105116
_ = mgr.Conflicts(&policiesfakes.FakePolicy{}, &policiesfakes.FakePolicy{})
106117
}
107118

119+
Expect(conflict).To(Panic())
120+
})
121+
It("panics on call to conflicts when no validator is registered for policy", func() {
122+
conflict := func() {
123+
_ = mgr.Conflicts(bananaPolicy, bananaPolicy)
124+
}
125+
108126
Expect(conflict).To(Panic())
109127
})
110128
})

internal/mode/static/state/graph/backend_tls_policy_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ func TestValidateBackendTLSPolicy(t *testing.T) {
9595
},
9696
}
9797

98+
targetRefInvalidKind := []v1alpha2.LocalPolicyTargetReferenceWithSectionName{
99+
{
100+
LocalPolicyTargetReference: v1alpha2.LocalPolicyTargetReference{
101+
Kind: "Invalid",
102+
Name: "service1",
103+
},
104+
},
105+
}
106+
98107
localObjectRefNormalCase := []gatewayv1.LocalObjectReference{
99108
{
100109
Kind: "ConfigMap",
@@ -301,7 +310,7 @@ func TestValidateBackendTLSPolicy(t *testing.T) {
301310
Namespace: "test",
302311
},
303312
Spec: v1alpha3.BackendTLSPolicySpec{
304-
TargetRefs: targetRefNormalCase,
313+
TargetRefs: targetRefInvalidKind,
305314
Validation: v1alpha3.BackendTLSPolicyValidation{
306315
CACertificateRefs: localObjectRefInvalidKind,
307316
Hostname: "foo.test.com",

internal/mode/static/state/graph/gateway_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ func TestBuildGateway(t *testing.T) {
331331
},
332332
Spec: ngfAPIv1alpha2.NginxProxySpec{
333333
Logging: &ngfAPIv1alpha2.NginxLogging{ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelError)},
334+
Metrics: &ngfAPIv1alpha2.Metrics{
335+
Disable: helpers.GetPointer(false),
336+
Port: helpers.GetPointer(int32(90)),
337+
},
334338
},
335339
}
336340
validGwNpRef := &v1.LocalParametersReference{
@@ -592,6 +596,10 @@ func TestBuildGateway(t *testing.T) {
592596
Logging: &ngfAPIv1alpha2.NginxLogging{
593597
ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelError),
594598
},
599+
Metrics: &ngfAPIv1alpha2.Metrics{
600+
Disable: helpers.GetPointer(false),
601+
Port: helpers.GetPointer(int32(90)),
602+
},
595603
},
596604
Conditions: []conditions.Condition{staticConds.NewGatewayResolvedRefs()},
597605
},
@@ -634,6 +642,10 @@ func TestBuildGateway(t *testing.T) {
634642
ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelError),
635643
},
636644
IPFamily: helpers.GetPointer(ngfAPIv1alpha2.Dual),
645+
Metrics: &ngfAPIv1alpha2.Metrics{
646+
Disable: helpers.GetPointer(false),
647+
Port: helpers.GetPointer(int32(90)),
648+
},
637649
},
638650
Conditions: []conditions.Condition{staticConds.NewGatewayResolvedRefs()},
639651
},

internal/mode/static/state/graph/graph_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,3 +1747,12 @@ func TestIsNGFPolicyRelevantPanics(t *testing.T) {
17471747

17481748
g.Expect(isRelevant).To(Panic())
17491749
}
1750+
1751+
func TestGatewayExists(t *testing.T) {
1752+
t.Parallel()
1753+
g := NewWithT(t)
1754+
gwNsName := types.NamespacedName{Namespace: "test", Name: "gw"}
1755+
1756+
result := gatewayExists(gwNsName, nil)
1757+
g.Expect(result).To(BeFalse())
1758+
}

internal/mode/static/state/graph/policies_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,44 @@ func TestAttachPolicyToService(t *testing.T) {
787787
expAttached: false,
788788
expAncestors: nil,
789789
},
790+
{
791+
name: "no attachment; does not belong to gateway",
792+
policy: &Policy{Source: &policiesfakes.FakePolicy{}, InvalidForGateways: map[types.NamespacedName]struct{}{}},
793+
svc: &ReferencedService{
794+
GatewayNsNames: map[types.NamespacedName]struct{}{
795+
gw2Nsname: {},
796+
},
797+
},
798+
gws: getGateway(true /*valid*/),
799+
expAttached: false,
800+
expAncestors: nil,
801+
},
802+
{
803+
name: "no attachment; gateway is invalid",
804+
policy: &Policy{
805+
Source: &policiesfakes.FakePolicy{},
806+
InvalidForGateways: map[types.NamespacedName]struct{}{
807+
gwNsname: {},
808+
},
809+
Ancestors: []PolicyAncestor{
810+
{
811+
Ancestor: getGatewayParentRef(gwNsname),
812+
},
813+
},
814+
},
815+
svc: &ReferencedService{
816+
GatewayNsNames: map[types.NamespacedName]struct{}{
817+
gwNsname: {},
818+
},
819+
},
820+
gws: getGateway(false),
821+
expAttached: false,
822+
expAncestors: []PolicyAncestor{
823+
{
824+
Ancestor: getGatewayParentRef(gwNsname),
825+
},
826+
},
827+
},
790828
}
791829

792830
for _, test := range tests {

internal/mode/static/state/graph/service_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ func TestBuildReferencedServices(t *testing.T) {
319319
},
320320
{
321321
name: "nil gateway",
322-
gws: nil,
322+
gws: map[types.NamespacedName]*Gateway{
323+
gwNsName: nil,
324+
},
323325
l7Routes: map[RouteKey]*L7Route{
324326
{NamespacedName: types.NamespacedName{Name: "no-service-nsname"}}: validRouteNoServiceNsName,
325327
},

0 commit comments

Comments
 (0)