Skip to content

Commit 5934655

Browse files
committed
Update tests; code review comments
1 parent 6f463bf commit 5934655

File tree

3 files changed

+103
-25
lines changed

3 files changed

+103
-25
lines changed

internal/events/handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,22 @@ func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatc
7777
}
7878
}
7979

80-
changed, graphCfg := h.cfg.Processor.Process()
80+
changed, graph := h.cfg.Processor.Process()
8181
if !changed {
8282
h.cfg.Logger.Info("Handling events didn't result into NGINX configuration changes")
8383
return
8484
}
8585

8686
var nginxReloadRes status.NginxReloadResult
87-
err := h.updateNginx(ctx, dataplane.BuildConfiguration(ctx, graphCfg, h.cfg.ServiceResolver))
87+
err := h.updateNginx(ctx, dataplane.BuildConfiguration(ctx, graph, h.cfg.ServiceResolver))
8888
if err != nil {
8989
h.cfg.Logger.Error(err, "Failed to update NGINX configuration")
9090
nginxReloadRes.Error = err
9191
} else {
9292
h.cfg.Logger.Info("NGINX configuration was successfully updated")
9393
}
9494

95-
h.cfg.StatusUpdater.Update(ctx, status.BuildStatuses(graphCfg, nginxReloadRes))
95+
h.cfg.StatusUpdater.Update(ctx, status.BuildStatuses(graph, nginxReloadRes))
9696
}
9797

9898
func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error {

internal/state/change_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type ChangeProcessor interface {
4343
// The method panics if the resource is of unsupported type or if the passed Gateway is different from the one
4444
// this ChangeProcessor was created for.
4545
CaptureDeleteChange(resourceType client.Object, nsname types.NamespacedName)
46-
// Process produces an internal representation of the Gateway configuration.
46+
// Process produces a Graph-like representation of GatewayAPI resources.
4747
// If no changes were captured, the changed return argument will be false and graph will be empty.
4848
Process() (changed bool, graphCfg *graph.Graph)
4949
}

internal/state/change_processor_test.go

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
1919
"github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/index"
2020
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
21-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
2221
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship"
2322
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship/relationshipfakes"
2423
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets/secretsfakes"
@@ -221,9 +220,9 @@ var _ = Describe("ChangeProcessor", func() {
221220

222221
Describe("Process gateway resources", Ordered, func() {
223222
var (
224-
gcUpdated *v1beta1.GatewayClass
225-
hr1, hr1Updated *v1beta1.HTTPRoute
226-
gw1, gw1Updated *v1beta1.Gateway
223+
gcUpdated *v1beta1.GatewayClass
224+
hr1, hr1Updated, hr2 *v1beta1.HTTPRoute
225+
gw1, gw1Updated, gw2 *v1beta1.Gateway
227226
)
228227
BeforeAll(func() {
229228
gcUpdated = gc.DeepCopy()
@@ -234,10 +233,14 @@ var _ = Describe("ChangeProcessor", func() {
234233
hr1Updated = hr1.DeepCopy()
235234
hr1Updated.Generation++
236235

236+
hr2 = createRoute("hr-2", "gateway-2", "bar.example.com")
237+
237238
gw1 = createGatewayWithTLSListener("gateway-1")
238239

239240
gw1Updated = gw1.DeepCopy()
240241
gw1Updated.Generation++
242+
243+
gw2 = createGatewayWithTLSListener("gateway-2")
241244
})
242245

243246
When("no upsert has occurred", func() {
@@ -247,15 +250,36 @@ var _ = Describe("ChangeProcessor", func() {
247250
Expect(graphCfg).To(BeNil())
248251
})
249252
})
250-
When("upsert has occurred", func() {
251-
It("returns populated graph", func() {
252-
processor.CaptureUpsertChange(hr1)
253-
processor.CaptureUpsertChange(gw1)
253+
When("GatewayClass doesn't exist", func() {
254+
When("Gateways don't exist", func() {
255+
When("the first HTTPRoute is upserted", func() {
256+
It("returns empty graph", func() {
257+
processor.CaptureUpsertChange(hr1)
258+
259+
changed, graphCfg := processor.Process()
260+
Expect(changed).To(BeTrue())
261+
Expect(graphCfg.Routes).To(BeNil())
262+
})
263+
})
264+
When("the first Gateway is upserted", func() {
265+
It("returns populated graph", func() {
266+
processor.CaptureUpsertChange(gw1)
267+
268+
changed, graphCfg := processor.Process()
269+
Expect(changed).To(BeTrue())
270+
Expect(graphCfg.Gateway).ToNot(BeNil())
271+
Expect(graphCfg.Routes).To(HaveLen(1))
272+
})
273+
})
274+
})
275+
})
276+
When("the GatewayClass is upserted", func() {
277+
It("returns updated graph", func() {
254278
processor.CaptureUpsertChange(gc)
255279

256280
changed, graphCfg := processor.Process()
257281
Expect(changed).To(BeTrue())
258-
Expect(graphCfg).ToNot(BeNil())
282+
Expect(graphCfg.GatewayClass).ToNot(BeNil())
259283
})
260284
})
261285
When("the first HTTPRoute without a generation changed is processed", func() {
@@ -275,7 +299,7 @@ var _ = Describe("ChangeProcessor", func() {
275299

276300
changed, graphCfg := processor.Process()
277301
Expect(changed).To(BeTrue())
278-
Expect(graphCfg).ToNot(BeNil())
302+
Expect(graphCfg.Routes).To(HaveLen(1))
279303
},
280304
)
281305
})
@@ -296,7 +320,7 @@ var _ = Describe("ChangeProcessor", func() {
296320

297321
changed, graphCfg := processor.Process()
298322
Expect(changed).To(BeTrue())
299-
Expect(graphCfg).ToNot(BeNil())
323+
Expect(graphCfg.Gateway).ToNot(BeNil())
300324
})
301325
})
302326
When("the GatewayClass update without generation change is processed", func() {
@@ -316,7 +340,7 @@ var _ = Describe("ChangeProcessor", func() {
316340

317341
changed, graphCfg := processor.Process()
318342
Expect(changed).To(BeTrue())
319-
Expect(graphCfg).ToNot(BeNil())
343+
Expect(graphCfg.GatewayClass).ToNot(BeNil())
320344
})
321345
})
322346
When("no changes are captured", func() {
@@ -327,26 +351,83 @@ var _ = Describe("ChangeProcessor", func() {
327351
Expect(graphCfg).To(BeNil())
328352
})
329353
})
330-
When("resources are deleted", func() {
331-
It("returns empty graph", func() {
354+
When("the second Gateway is upserted", func() {
355+
It("returns populated graph using first gateway", func() {
356+
processor.CaptureUpsertChange(gw2)
357+
358+
changed, graphCfg := processor.Process()
359+
Expect(changed).To(BeTrue())
360+
Expect(graphCfg.Gateway.Source.Name).To(Equal(gw1.Name))
361+
})
362+
})
363+
When("the second HTTPRoute is upserted", func() {
364+
It("returns populated graph", func() {
365+
processor.CaptureUpsertChange(hr2)
366+
367+
changed, graphCfg := processor.Process()
368+
Expect(changed).To(BeTrue())
369+
Expect(graphCfg.Routes).To(HaveLen(2))
370+
})
371+
})
372+
When("the first Gateway is deleted", func() {
373+
It("returns updated graph", func() {
374+
processor.CaptureDeleteChange(
375+
&v1beta1.Gateway{},
376+
types.NamespacedName{Namespace: "test", Name: "gateway-1"},
377+
)
378+
379+
changed, graphCfg := processor.Process()
380+
Expect(changed).To(BeTrue())
381+
Expect(graphCfg.Gateway.Source.Name).To(Equal(gw2.Name))
382+
Expect(graphCfg.Routes).To(HaveLen(1))
383+
})
384+
})
385+
When("the second HTTPRoute is deleted", func() {
386+
It("returns updated graph", func() {
387+
processor.CaptureDeleteChange(
388+
&v1beta1.HTTPRoute{},
389+
types.NamespacedName{Namespace: "test", Name: "hr-2"},
390+
)
391+
392+
changed, graphCfg := processor.Process()
393+
Expect(changed).To(BeTrue())
394+
Expect(graphCfg.Routes).To(HaveLen(0))
395+
})
396+
})
397+
When("the GatewayClass is deleted", func() {
398+
It("returns updated graph", func() {
332399
processor.CaptureDeleteChange(
333400
&v1beta1.GatewayClass{},
334401
types.NamespacedName{Name: gcName},
335402
)
403+
404+
changed, graphCfg := processor.Process()
405+
Expect(changed).To(BeTrue())
406+
Expect(graphCfg.GatewayClass).To(BeNil())
407+
})
408+
})
409+
When("the second Gateway is deleted", func() {
410+
It("returns updated graph", func() {
336411
processor.CaptureDeleteChange(
337412
&v1beta1.Gateway{},
338-
types.NamespacedName{Namespace: "test", Name: "gateway-1"},
413+
types.NamespacedName{Namespace: "test", Name: "gateway-2"},
339414
)
415+
416+
changed, graphCfg := processor.Process()
417+
Expect(changed).To(BeTrue())
418+
Expect(graphCfg.Gateway).To(BeNil())
419+
})
420+
})
421+
When("the first HTTPRoute is deleted", func() {
422+
It("returns updated graph", func() {
340423
processor.CaptureDeleteChange(
341424
&v1beta1.HTTPRoute{},
342425
types.NamespacedName{Namespace: "test", Name: "hr-1"},
343426
)
344427

345-
emptyGraph := &graph.Graph{}
346-
347428
changed, graphCfg := processor.Process()
348429
Expect(changed).To(BeTrue())
349-
Expect(helpers.Diff(emptyGraph, graphCfg)).To(BeEmpty())
430+
Expect(graphCfg.Routes).To(BeNil())
350431
})
351432
})
352433
})
@@ -685,9 +766,6 @@ var _ = Describe("ChangeProcessor", func() {
685766
})
686767

687768
Describe("Ensuring non-changing changes don't override previously changing changes", func() {
688-
// Note: in these tests, we deliberately don't fully inspect the returned configuration and statuses
689-
// -- this is done in 'Normal cases of processing changes'
690-
691769
var (
692770
processor *state.ChangeProcessorImpl
693771
fakeRelationshipCapturer *relationshipfakes.FakeCapturer

0 commit comments

Comments
 (0)