Skip to content

Commit 85657cc

Browse files
committed
Add graph check back to tests
1 parent 5934655 commit 85657cc

File tree

1 file changed

+181
-18
lines changed

1 file changed

+181
-18
lines changed

internal/state/change_processor_test.go

Lines changed: 181 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package state_test
33
import (
44
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/format"
67
apiv1 "k8s.io/api/core/v1"
78
discoveryV1 "k8s.io/api/discovery/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -18,6 +19,8 @@ import (
1819
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
1920
"github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index"
2021
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
22+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/conditions"
23+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
2124
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship"
2225
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship/relationshipfakes"
2326
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets/secretsfakes"
@@ -187,6 +190,7 @@ func createScheme() *runtime.Scheme {
187190
}
188191

189192
var _ = Describe("ChangeProcessor", func() {
193+
format.MaxLength = 0
190194
Describe("Normal cases of processing changes", func() {
191195
var (
192196
gc = &v1beta1.GatewayClass{
@@ -220,9 +224,11 @@ var _ = Describe("ChangeProcessor", func() {
220224

221225
Describe("Process gateway resources", Ordered, func() {
222226
var (
223-
gcUpdated *v1beta1.GatewayClass
224-
hr1, hr1Updated, hr2 *v1beta1.HTTPRoute
225-
gw1, gw1Updated, gw2 *v1beta1.Gateway
227+
gcUpdated *v1beta1.GatewayClass
228+
hr1, hr1Updated, hr2 *v1beta1.HTTPRoute
229+
gw1, gw1Updated, gw2 *v1beta1.Gateway
230+
expGraph *graph.Graph
231+
expRouteHR1, expRouteHR2 *graph.Route
226232
)
227233
BeforeAll(func() {
228234
gcUpdated = gc.DeepCopy()
@@ -242,6 +248,87 @@ var _ = Describe("ChangeProcessor", func() {
242248

243249
gw2 = createGatewayWithTLSListener("gateway-2")
244250
})
251+
BeforeEach(func() {
252+
expRouteHR1 = &graph.Route{
253+
Source: hr1,
254+
ParentRefs: []graph.ParentRef{
255+
{
256+
Attachment: &graph.ParentRefAttachmentStatus{
257+
AcceptedHostnames: map[string][]string{"listener-80-1": {"foo.example.com"}},
258+
Attached: true,
259+
},
260+
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-1"},
261+
},
262+
{
263+
Attachment: &graph.ParentRefAttachmentStatus{
264+
AcceptedHostnames: map[string][]string{"listener-443-1": {"foo.example.com"}},
265+
Attached: true,
266+
},
267+
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-1"},
268+
Idx: 1,
269+
},
270+
},
271+
Rules: []graph.Rule{{ValidMatches: true, ValidFilters: true}},
272+
Valid: true,
273+
}
274+
275+
expRouteHR2 = &graph.Route{
276+
Source: hr2,
277+
ParentRefs: []graph.ParentRef{
278+
{
279+
Attachment: &graph.ParentRefAttachmentStatus{
280+
AcceptedHostnames: map[string][]string{"listener-80-1": {"bar.example.com"}},
281+
Attached: true,
282+
},
283+
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-2"},
284+
},
285+
{
286+
Attachment: &graph.ParentRefAttachmentStatus{
287+
AcceptedHostnames: map[string][]string{"listener-443-1": {"bar.example.com"}},
288+
Attached: true,
289+
},
290+
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-2"},
291+
Idx: 1,
292+
},
293+
},
294+
Rules: []graph.Rule{{ValidMatches: true, ValidFilters: true}},
295+
Valid: true,
296+
}
297+
298+
// This is the base case expected graph. Tests will manipulate this to add or remove elements
299+
// to fit the expected output of the input under test.
300+
expGraph = &graph.Graph{
301+
GatewayClass: &graph.GatewayClass{
302+
Source: gc,
303+
Valid: true,
304+
},
305+
Gateway: &graph.Gateway{
306+
Source: gw1,
307+
Listeners: map[string]*graph.Listener{
308+
"listener-80-1": {
309+
Source: gw1.Spec.Listeners[0],
310+
Valid: true,
311+
Routes: map[types.NamespacedName]*graph.Route{
312+
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
313+
},
314+
},
315+
"listener-443-1": {
316+
Source: gw1.Spec.Listeners[1],
317+
Valid: true,
318+
Routes: map[types.NamespacedName]*graph.Route{
319+
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
320+
},
321+
SecretPath: "path/to/cert",
322+
},
323+
},
324+
Valid: true,
325+
},
326+
IgnoredGateways: map[types.NamespacedName]*v1beta1.Gateway{},
327+
Routes: map[types.NamespacedName]*graph.Route{
328+
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
329+
},
330+
}
331+
})
245332

246333
When("no upsert has occurred", func() {
247334
It("returns empty graph", func() {
@@ -258,17 +345,32 @@ var _ = Describe("ChangeProcessor", func() {
258345

259346
changed, graphCfg := processor.Process()
260347
Expect(changed).To(BeTrue())
261-
Expect(graphCfg.Routes).To(BeNil())
348+
Expect(graphCfg).To(Equal(&graph.Graph{}))
262349
})
263350
})
264351
When("the first Gateway is upserted", func() {
265352
It("returns populated graph", func() {
266353
processor.CaptureUpsertChange(gw1)
267354

355+
expGraph.GatewayClass = nil
356+
357+
expGraph.Gateway.Conditions = conditions.NewGatewayInvalid("GatewayClass doesn't exist")
358+
expGraph.Gateway.Valid = false
359+
expGraph.Gateway.Listeners = nil
360+
361+
hrName := types.NamespacedName{Namespace: "test", Name: "hr-1"}
362+
expGraph.Routes[hrName].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
363+
AcceptedHostnames: map[string][]string{},
364+
FailedCondition: conditions.NewRouteInvalidGateway(),
365+
}
366+
expGraph.Routes[hrName].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
367+
AcceptedHostnames: map[string][]string{},
368+
FailedCondition: conditions.NewRouteInvalidGateway(),
369+
}
370+
268371
changed, graphCfg := processor.Process()
269372
Expect(changed).To(BeTrue())
270-
Expect(graphCfg.Gateway).ToNot(BeNil())
271-
Expect(graphCfg.Routes).To(HaveLen(1))
373+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
272374
})
273375
})
274376
})
@@ -279,7 +381,7 @@ var _ = Describe("ChangeProcessor", func() {
279381

280382
changed, graphCfg := processor.Process()
281383
Expect(changed).To(BeTrue())
282-
Expect(graphCfg.GatewayClass).ToNot(BeNil())
384+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
283385
})
284386
})
285387
When("the first HTTPRoute without a generation changed is processed", func() {
@@ -297,9 +399,13 @@ var _ = Describe("ChangeProcessor", func() {
297399
It("returns populated graph", func() {
298400
processor.CaptureUpsertChange(hr1Updated)
299401

402+
hrName := types.NamespacedName{Namespace: "test", Name: "hr-1"}
403+
expGraph.Gateway.Listeners["listener-443-1"].Routes[hrName].Source.Generation = hr1Updated.Generation
404+
expGraph.Gateway.Listeners["listener-80-1"].Routes[hrName].Source.Generation = hr1Updated.Generation
405+
300406
changed, graphCfg := processor.Process()
301407
Expect(changed).To(BeTrue())
302-
Expect(graphCfg.Routes).To(HaveLen(1))
408+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
303409
},
304410
)
305411
})
@@ -318,9 +424,11 @@ var _ = Describe("ChangeProcessor", func() {
318424
It("returns populated graph", func() {
319425
processor.CaptureUpsertChange(gw1Updated)
320426

427+
expGraph.Gateway.Source.Generation = gw1Updated.Generation
428+
321429
changed, graphCfg := processor.Process()
322430
Expect(changed).To(BeTrue())
323-
Expect(graphCfg.Gateway).ToNot(BeNil())
431+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
324432
})
325433
})
326434
When("the GatewayClass update without generation change is processed", func() {
@@ -338,9 +446,11 @@ var _ = Describe("ChangeProcessor", func() {
338446
It("returns populated graph", func() {
339447
processor.CaptureUpsertChange(gcUpdated)
340448

449+
expGraph.GatewayClass.Source.Generation = gcUpdated.Generation
450+
341451
changed, graphCfg := processor.Process()
342452
Expect(changed).To(BeTrue())
343-
Expect(graphCfg.GatewayClass).ToNot(BeNil())
453+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
344454
})
345455
})
346456
When("no changes are captured", func() {
@@ -355,18 +465,37 @@ var _ = Describe("ChangeProcessor", func() {
355465
It("returns populated graph using first gateway", func() {
356466
processor.CaptureUpsertChange(gw2)
357467

468+
expGraph.IgnoredGateways = map[types.NamespacedName]*v1beta1.Gateway{
469+
{Namespace: "test", Name: "gateway-2"}: gw2,
470+
}
471+
358472
changed, graphCfg := processor.Process()
359473
Expect(changed).To(BeTrue())
360-
Expect(graphCfg.Gateway.Source.Name).To(Equal(gw1.Name))
474+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
361475
})
362476
})
363477
When("the second HTTPRoute is upserted", func() {
364478
It("returns populated graph", func() {
365479
processor.CaptureUpsertChange(hr2)
366480

481+
hrName := types.NamespacedName{Namespace: "test", Name: "hr-2"}
482+
483+
expGraph.IgnoredGateways = map[types.NamespacedName]*v1beta1.Gateway{
484+
{Namespace: "test", Name: "gateway-2"}: gw2,
485+
}
486+
expGraph.Routes[hrName] = expRouteHR2
487+
expGraph.Routes[hrName].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
488+
AcceptedHostnames: map[string][]string{},
489+
FailedCondition: conditions.NewTODO("Gateway is ignored"),
490+
}
491+
expGraph.Routes[hrName].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
492+
AcceptedHostnames: map[string][]string{},
493+
FailedCondition: conditions.NewTODO("Gateway is ignored"),
494+
}
495+
367496
changed, graphCfg := processor.Process()
368497
Expect(changed).To(BeTrue())
369-
Expect(graphCfg.Routes).To(HaveLen(2))
498+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
370499
})
371500
})
372501
When("the first Gateway is deleted", func() {
@@ -376,10 +505,22 @@ var _ = Describe("ChangeProcessor", func() {
376505
types.NamespacedName{Namespace: "test", Name: "gateway-1"},
377506
)
378507

508+
// gateway 2 takes over;
509+
// route 1 has been replaced by route 2
510+
hr1Name := types.NamespacedName{Namespace: "test", Name: "hr-1"}
511+
hr2Name := types.NamespacedName{Namespace: "test", Name: "hr-2"}
512+
expGraph.Gateway.Source = gw2
513+
delete(expGraph.Gateway.Listeners["listener-80-1"].Routes, hr1Name)
514+
delete(expGraph.Gateway.Listeners["listener-443-1"].Routes, hr1Name)
515+
expGraph.Gateway.Listeners["listener-80-1"].Routes[hr2Name] = expRouteHR2
516+
expGraph.Gateway.Listeners["listener-443-1"].Routes[hr2Name] = expRouteHR2
517+
518+
delete(expGraph.Routes, hr1Name)
519+
expGraph.Routes[hr2Name] = expRouteHR2
520+
379521
changed, graphCfg := processor.Process()
380522
Expect(changed).To(BeTrue())
381-
Expect(graphCfg.Gateway.Source.Name).To(Equal(gw2.Name))
382-
Expect(graphCfg.Routes).To(HaveLen(1))
523+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
383524
})
384525
})
385526
When("the second HTTPRoute is deleted", func() {
@@ -389,9 +530,17 @@ var _ = Describe("ChangeProcessor", func() {
389530
types.NamespacedName{Namespace: "test", Name: "hr-2"},
390531
)
391532

533+
// gateway 2 still in charge;
534+
// no routes remain
535+
hr1Name := types.NamespacedName{Namespace: "test", Name: "hr-1"}
536+
expGraph.Gateway.Source = gw2
537+
delete(expGraph.Gateway.Listeners["listener-80-1"].Routes, hr1Name)
538+
delete(expGraph.Gateway.Listeners["listener-443-1"].Routes, hr1Name)
539+
expGraph.Routes = map[types.NamespacedName]*graph.Route{}
540+
392541
changed, graphCfg := processor.Process()
393542
Expect(changed).To(BeTrue())
394-
Expect(graphCfg.Routes).To(HaveLen(0))
543+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
395544
})
396545
})
397546
When("the GatewayClass is deleted", func() {
@@ -401,9 +550,16 @@ var _ = Describe("ChangeProcessor", func() {
401550
types.NamespacedName{Name: gcName},
402551
)
403552

553+
expGraph.GatewayClass = nil
554+
expGraph.Gateway = &graph.Gateway{
555+
Source: gw2,
556+
Conditions: conditions.NewGatewayInvalid("GatewayClass doesn't exist"),
557+
}
558+
expGraph.Routes = map[types.NamespacedName]*graph.Route{}
559+
404560
changed, graphCfg := processor.Process()
405561
Expect(changed).To(BeTrue())
406-
Expect(graphCfg.GatewayClass).To(BeNil())
562+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
407563
})
408564
})
409565
When("the second Gateway is deleted", func() {
@@ -413,9 +569,11 @@ var _ = Describe("ChangeProcessor", func() {
413569
types.NamespacedName{Namespace: "test", Name: "gateway-2"},
414570
)
415571

572+
expGraph := &graph.Graph{}
573+
416574
changed, graphCfg := processor.Process()
417575
Expect(changed).To(BeTrue())
418-
Expect(graphCfg.Gateway).To(BeNil())
576+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
419577
})
420578
})
421579
When("the first HTTPRoute is deleted", func() {
@@ -425,9 +583,11 @@ var _ = Describe("ChangeProcessor", func() {
425583
types.NamespacedName{Namespace: "test", Name: "hr-1"},
426584
)
427585

586+
expGraph := &graph.Graph{}
587+
428588
changed, graphCfg := processor.Process()
429589
Expect(changed).To(BeTrue())
430-
Expect(graphCfg.Routes).To(BeNil())
590+
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
431591
})
432592
})
433593
})
@@ -766,6 +926,9 @@ var _ = Describe("ChangeProcessor", func() {
766926
})
767927

768928
Describe("Ensuring non-changing changes don't override previously changing changes", func() {
929+
// Note: in these tests, we deliberately don't fully inspect the returned configuration and statuses
930+
// -- this is done in 'Normal cases of processing changes'
931+
769932
var (
770933
processor *state.ChangeProcessorImpl
771934
fakeRelationshipCapturer *relationshipfakes.FakeCapturer

0 commit comments

Comments
 (0)