Skip to content

Commit 46d2bbf

Browse files
committed
Add status updater unit tests
1 parent e726354 commit 46d2bbf

File tree

12 files changed

+308
-17
lines changed

12 files changed

+308
-17
lines changed

conformance/scripts/install-gateway.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ if [ -z $2 ]; then
1010
exit 1
1111
fi
1212

13-
if [ -z $3 ]; then
14-
echo "enable experimental argument not set; exiting"
15-
exit 1
16-
fi
17-
1813
if [ $1 == "main" ]; then
1914
temp_dir=$(mktemp -d)
2015
cd ${temp_dir}

conformance/scripts/uninstall-gateway.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ if [ -z $2 ]; then
1010
exit 1
1111
fi
1212

13-
if [ -z $3 ]; then
14-
echo "enable experimental argument not set; exiting"
15-
exit 1
16-
fi
17-
1813
if [ $1 == "main" ]; then
1914
temp_dir=$(mktemp -d)
2015
cd ${temp_dir}

docs/developer/quickstart.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-
128128
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
129129
```
130130
131+
Alternatively, install Gateway API CRDs from the experimental channel:
132+
133+
```shell
134+
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/experimental-install.yaml
135+
```
136+
131137
4. Install NGF using your custom image and expose NGF with a NodePort Service:
132138
133139
- To install with Helm (where your release name is `my-release`):
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package status
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/gomega"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/types"
9+
v1 "sigs.k8s.io/gateway-api/apis/v1"
10+
"sigs.k8s.io/gateway-api/apis/v1alpha2"
11+
12+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
13+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
14+
)
15+
16+
func TestPrepareBackendTLSPolicyStatus(t *testing.T) {
17+
oldStatus := v1alpha2.PolicyStatus{
18+
Ancestors: []v1alpha2.PolicyAncestorStatus{
19+
{
20+
AncestorRef: v1.ParentReference{
21+
Namespace: helpers.GetPointer((v1.Namespace)("ns1")),
22+
Name: v1alpha2.ObjectName("other-gw"),
23+
},
24+
ControllerName: v1alpha2.GatewayController("otherCtlr"),
25+
Conditions: []metav1.Condition{{Type: "otherType", Status: "otherStatus"}},
26+
},
27+
},
28+
}
29+
30+
newStatus := BackendTLSPolicyStatus{
31+
AncestorStatuses: []AncestorStatus{
32+
{
33+
GatewayNsName: types.NamespacedName{
34+
Namespace: "ns1",
35+
Name: "gw1",
36+
},
37+
Conditions: []conditions.Condition{{Type: "type1", Status: "status1"}},
38+
},
39+
},
40+
ObservedGeneration: 1,
41+
}
42+
43+
transistionTime := metav1.Now()
44+
ctlrName := "nginx-gateway"
45+
46+
policyStatus := prepareBackendTLSPolicyStatus(oldStatus, newStatus, ctlrName, transistionTime)
47+
48+
g := NewWithT(t)
49+
50+
g.Expect(policyStatus.Ancestors).To(HaveLen(2))
51+
}

internal/framework/status/setters_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
. "github.com/onsi/gomega"
88
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
10+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1011

1112
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1213
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
@@ -848,3 +849,71 @@ func TestEqualPointers(t *testing.T) {
848849
})
849850
}
850851
}
852+
853+
func TestBtpStatusEqual(t *testing.T) {
854+
getPolicyStatus := func(ancestorName, ancestorNs, ctlrName string) gatewayv1alpha2.PolicyStatus {
855+
return gatewayv1alpha2.PolicyStatus{
856+
Ancestors: []gatewayv1alpha2.PolicyAncestorStatus{
857+
{
858+
AncestorRef: gatewayv1.ParentReference{
859+
Namespace: helpers.GetPointer[gatewayv1.Namespace]((gatewayv1.Namespace)(ancestorNs)),
860+
Name: gatewayv1alpha2.ObjectName(ancestorName),
861+
},
862+
ControllerName: gatewayv1alpha2.GatewayController(ctlrName),
863+
Conditions: []v1.Condition{{Type: "otherType", Status: "otherStatus"}},
864+
},
865+
},
866+
}
867+
}
868+
tests := []struct {
869+
name string
870+
controllerName string
871+
previous gatewayv1alpha2.PolicyStatus
872+
current gatewayv1alpha2.PolicyStatus
873+
expEqual bool
874+
}{
875+
{
876+
name: "status equal",
877+
previous: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
878+
current: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
879+
controllerName: "ctlr1",
880+
expEqual: true,
881+
},
882+
{
883+
name: "status not equal, different ancestor name",
884+
previous: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
885+
current: getPolicyStatus("ancestor2", "ns1", "ctlr1"),
886+
controllerName: "ctlr1",
887+
expEqual: false,
888+
},
889+
{
890+
name: "status not equal, different ancestor namespace",
891+
previous: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
892+
current: getPolicyStatus("ancestor1", "ns2", "ctlr1"),
893+
controllerName: "ctlr1",
894+
expEqual: false,
895+
},
896+
{
897+
name: "status not equal, different controller name on current",
898+
previous: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
899+
current: getPolicyStatus("ancestor1", "ns1", "ctlr2"),
900+
controllerName: "ctlr1",
901+
expEqual: false,
902+
},
903+
{
904+
name: "status not equal, different controller name on previous",
905+
previous: getPolicyStatus("ancestor1", "ns1", "ctlr2"),
906+
current: getPolicyStatus("ancestor1", "ns1", "ctlr1"),
907+
controllerName: "ctlr1",
908+
expEqual: false,
909+
},
910+
}
911+
912+
for _, test := range tests {
913+
t.Run(test.name, func(t *testing.T) {
914+
g := NewWithT(t)
915+
equal := btpStatusEqual(test.controllerName, test.previous, test.current)
916+
g.Expect(equal).To(Equal(test.expEqual))
917+
})
918+
}
919+
}

internal/framework/status/updater_test.go

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1515
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1616
v1 "sigs.k8s.io/gateway-api/apis/v1"
17+
v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1718

1819
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1920
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
@@ -42,6 +43,7 @@ var _ = Describe("Updater", func() {
4243
scheme := runtime.NewScheme()
4344

4445
Expect(v1.AddToScheme(scheme)).Should(Succeed())
46+
Expect(v1alpha2.AddToScheme(scheme)).Should(Succeed())
4547
Expect(ngfAPI.AddToScheme(scheme)).Should(Succeed())
4648

4749
client = fake.NewClientBuilder().
@@ -51,6 +53,7 @@ var _ = Describe("Updater", func() {
5153
&v1.Gateway{},
5254
&v1.HTTPRoute{},
5355
&ngfAPI.NginxGateway{},
56+
&v1alpha2.BackendTLSPolicy{},
5457
).
5558
Build()
5659

@@ -63,8 +66,9 @@ var _ = Describe("Updater", func() {
6366

6467
Describe("Process status updates", Ordered, func() {
6568
type generations struct {
66-
gatewayClass int64
67-
gateways int64
69+
gatewayClass int64
70+
gateways int64
71+
backendTLSPolicies int64
6872
}
6973

7074
var (
@@ -73,6 +77,7 @@ var _ = Describe("Updater", func() {
7377
gw, ignoredGw *v1.Gateway
7478
hr *v1.HTTPRoute
7579
ng *ngfAPI.NginxGateway
80+
btls *v1alpha2.BackendTLSPolicy
7681
addr = v1.GatewayStatusAddress{
7782
Type: helpers.GetPointer(v1.IPAddressType),
7883
Value: "1.2.3.4",
@@ -118,6 +123,17 @@ var _ = Describe("Updater", func() {
118123
},
119124
},
120125
},
126+
BackendTLSPolicyStatuses: status.BackendTLSPolicyStatuses{
127+
{Namespace: "test", Name: "backend-tls-policy"}: {
128+
ObservedGeneration: gens.backendTLSPolicies,
129+
AncestorStatuses: []status.AncestorStatus{
130+
{
131+
GatewayNsName: types.NamespacedName{Namespace: "test", Name: "gateway"},
132+
Conditions: status.CreateTestConditions("Test"),
133+
},
134+
},
135+
},
136+
},
121137
}
122138
}
123139

@@ -233,6 +249,31 @@ var _ = Describe("Updater", func() {
233249
}
234250
}
235251

252+
createExpectedBtlsWithGeneration = func(gen int64) *v1alpha2.BackendTLSPolicy {
253+
return &v1alpha2.BackendTLSPolicy{
254+
ObjectMeta: metav1.ObjectMeta{
255+
Namespace: "test",
256+
Name: "backend-tls-policy",
257+
},
258+
TypeMeta: metav1.TypeMeta{
259+
Kind: "BackendTLSPolicy",
260+
APIVersion: "gateway.networking.k8s.io/v1alpha2",
261+
},
262+
Status: v1alpha2.PolicyStatus{
263+
Ancestors: []v1alpha2.PolicyAncestorStatus{
264+
{
265+
AncestorRef: v1.ParentReference{
266+
Namespace: (*v1.Namespace)(helpers.GetPointer("test")),
267+
Name: "gateway",
268+
},
269+
ControllerName: v1alpha2.GatewayController(gatewayCtrlName),
270+
Conditions: status.CreateExpectedAPIConditions("Test", gen, fakeClockTime),
271+
},
272+
},
273+
},
274+
}
275+
}
276+
236277
createExpectedNGWithGeneration = func(gen int64) *ngfAPI.NginxGateway {
237278
return &ngfAPI.NginxGateway{
238279
ObjectMeta: metav1.ObjectMeta{
@@ -299,6 +340,16 @@ var _ = Describe("Updater", func() {
299340
APIVersion: "gateway.networking.k8s.io/v1",
300341
},
301342
}
343+
btls = &v1alpha2.BackendTLSPolicy{
344+
ObjectMeta: metav1.ObjectMeta{
345+
Namespace: "test",
346+
Name: "backend-tls-policy",
347+
},
348+
TypeMeta: metav1.TypeMeta{
349+
Kind: "BackendTLSPolicy",
350+
APIVersion: "gateway.networking.k8s.io/v1alpha2",
351+
},
352+
}
302353
ng = &ngfAPI.NginxGateway{
303354
ObjectMeta: metav1.ObjectMeta{
304355
Namespace: "nginx-gateway",
@@ -317,12 +368,14 @@ var _ = Describe("Updater", func() {
317368
Expect(client.Create(context.Background(), ignoredGw)).Should(Succeed())
318369
Expect(client.Create(context.Background(), hr)).Should(Succeed())
319370
Expect(client.Create(context.Background(), ng)).Should(Succeed())
371+
Expect(client.Create(context.Background(), btls)).Should(Succeed())
320372
})
321373

322374
It("should update gateway API statuses", func() {
323375
updater.Update(context.Background(), createGwAPIStatuses(generations{
324-
gatewayClass: 1,
325-
gateways: 1,
376+
gatewayClass: 1,
377+
gateways: 1,
378+
backendTLSPolicies: 1,
326379
}))
327380
})
328381

@@ -378,6 +431,22 @@ var _ = Describe("Updater", func() {
378431
Expect(helpers.Diff(expectedHR, latestHR)).To(BeEmpty())
379432
})
380433

434+
It("should have the updated status of BackendTLSPolicy in the API server", func() {
435+
latestBtls := &v1alpha2.BackendTLSPolicy{}
436+
expectedBtls := createExpectedBtlsWithGeneration(1)
437+
438+
err := client.Get(
439+
context.Background(),
440+
types.NamespacedName{Namespace: "test", Name: "backend-tls-policy"},
441+
latestBtls,
442+
)
443+
Expect(err).ToNot(HaveOccurred())
444+
445+
expectedBtls.ResourceVersion = latestBtls.ResourceVersion
446+
447+
Expect(helpers.Diff(expectedBtls, latestBtls)).To(BeEmpty())
448+
})
449+
381450
It("should update nginx gateway status", func() {
382451
updater.Update(context.Background(), createNGStatus(1))
383452
})

internal/mode/static/build_statuses.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ func buildGatewayStatus(
196196
func buildBackendTLSPolicyStatuses(backendTLSPolicies map[types.NamespacedName]*graph.BackendTLSPolicy,
197197
) status.BackendTLSPolicyStatuses {
198198
statuses := make(status.BackendTLSPolicyStatuses, len(backendTLSPolicies))
199-
ignoreStatus := false
200199

201200
for nsname, backendTLSPolicy := range backendTLSPolicies {
202201
if backendTLSPolicy.IsReferenced {
202+
ignoreStatus := false
203203
if !backendTLSPolicy.Valid {
204204
for i := range backendTLSPolicy.Conditions {
205205
if backendTLSPolicy.Conditions[i].Reason == string(staticConds.BackendTLSPolicyReasonIgnored) {

0 commit comments

Comments
 (0)