Skip to content

Commit 6eaaefd

Browse files
committed
add test for controller
1 parent b0f281b commit 6eaaefd

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

pkg/dashboard/cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ func (c *cluster) syncCacheOnce(ctx context.Context) (bool, error) {
260260
}
261261

262262
for _, r := range routes {
263+
log.Debug("syncing route with labels", r.Labels)
263264
if err := c.cache.InsertRoute(r); err != nil {
264265
log.Errorw("failed to insert route to cache",
265266
zap.String("route", r.ID),
@@ -270,6 +271,7 @@ func (c *cluster) syncCacheOnce(ctx context.Context) (bool, error) {
270271
}
271272
}
272273
for _, s := range ssl {
274+
log.Debug("syncing ssl with labels", s.Labels)
273275
if err := c.cache.InsertSSL(s); err != nil {
274276
log.Errorw("failed to insert ssl to cache",
275277
zap.String("ssl", s.ID),
@@ -290,6 +292,7 @@ func (c *cluster) syncCacheOnce(ctx context.Context) (bool, error) {
290292
}
291293
}
292294
for _, consumer := range consumers {
295+
log.Debug("syncing consumer with labels", consumer.Labels)
293296
if err := c.cache.InsertConsumer(consumer); err != nil {
294297
log.Errorw("failed to insert consumer to cache",
295298
zap.Any("consumer", consumer),

test/e2e/gatewayapi/controller.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package gatewayapi
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/api7/api7-ingress-controller/test/e2e/scaffold"
12+
)
13+
14+
var _ = Describe("Check if controller cache gets synced with correct resources", func() {
15+
var defautlGatewayClass = `
16+
apiVersion: gateway.networking.k8s.io/v1
17+
kind: GatewayClass
18+
metadata:
19+
name: %s
20+
spec:
21+
controllerName: %s
22+
`
23+
24+
var defautlGateway = `
25+
apiVersion: gateway.networking.k8s.io/v1
26+
kind: Gateway
27+
metadata:
28+
name: %s
29+
spec:
30+
gatewayClassName: %s
31+
listeners:
32+
- name: http1
33+
protocol: HTTP
34+
port: 80
35+
`
36+
37+
var ResourceApplied = func(s *scaffold.Scaffold, resourType, resourceName, resourceRaw string, observedGeneration int) {
38+
Expect(s.CreateResourceFromString(resourceRaw)).
39+
NotTo(HaveOccurred(), fmt.Sprintf("creating %s", resourType))
40+
41+
Eventually(func() string {
42+
hryaml, err := s.GetResourceYaml(resourType, resourceName)
43+
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting %s yaml", resourType))
44+
return hryaml
45+
}, "8s", "2s").
46+
Should(
47+
SatisfyAll(
48+
ContainSubstring(`status: "True"`),
49+
ContainSubstring(fmt.Sprintf("observedGeneration: %d", observedGeneration)),
50+
),
51+
fmt.Sprintf("checking %s condition status", resourType),
52+
)
53+
time.Sleep(1 * time.Second)
54+
}
55+
var beforeEach = func(s *scaffold.Scaffold, gatewayName string) {
56+
By(fmt.Sprintf("create GatewayClass for controller %s", s.GetControllerName()))
57+
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
58+
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), s.Namespace())
59+
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
60+
time.Sleep(20 * time.Second)
61+
62+
By("check GatewayClass condition")
63+
gcyaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName)
64+
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
65+
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
66+
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
67+
68+
By("create Gateway")
69+
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGateway, gatewayName, gatewayClassName), s.Namespace())
70+
Expect(err).NotTo(HaveOccurred(), "creating Gateway")
71+
time.Sleep(20 * time.Second)
72+
73+
By("check Gateway condition")
74+
gwyaml, err := s.GetResourceYaml("Gateway", gatewayName)
75+
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
76+
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
77+
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"), "checking Gateway condition message")
78+
}
79+
80+
Context("Create resource with first controller", func() {
81+
s1 := scaffold.NewScaffold(&scaffold.Options{
82+
Name: "gateway1",
83+
ControllerName: "gateway.api7.io/api7-ingress-controller-1",
84+
})
85+
s2 := scaffold.NewScaffold(&scaffold.Options{
86+
Name: "gateway2",
87+
ControllerName: "gateway.api7.io/api7-ingress-controller-2",
88+
})
89+
var route1 = `
90+
apiVersion: gateway.networking.k8s.io/v1
91+
kind: HTTPRoute
92+
metadata:
93+
name: httpbin
94+
spec:
95+
parentRefs:
96+
- name: gateway1
97+
hostnames:
98+
- httpbin.example
99+
rules:
100+
- matches:
101+
- path:
102+
type: Exact
103+
value: /get
104+
filters:
105+
- type: RequestMirror
106+
requestMirror:
107+
backendRef:
108+
name: echo-service
109+
port: 80
110+
backendRefs:
111+
- name: httpbin-service-e2e-test
112+
port: 80
113+
weight: 50
114+
- name: nginx
115+
port: 80
116+
weight: 50
117+
`
118+
var route2 = `
119+
apiVersion: gateway.networking.k8s.io/v1
120+
kind: HTTPRoute
121+
metadata:
122+
name: httpbin2
123+
spec:
124+
parentRefs:
125+
- name: gateway2
126+
hostnames:
127+
- httpbin.example
128+
rules:
129+
- matches:
130+
- path:
131+
type: Exact
132+
value: /get
133+
filters:
134+
- type: RequestMirror
135+
requestMirror:
136+
backendRef:
137+
name: echo-service
138+
port: 80
139+
backendRefs:
140+
- name: httpbin-service-e2e-test
141+
port: 80
142+
weight: 50
143+
- name: nginx
144+
port: 80
145+
weight: 50
146+
`
147+
BeforeEach(func() {
148+
beforeEach(s1, "gateway1")
149+
beforeEach(s2, "gateway2")
150+
})
151+
It("Apply resource ", func() {
152+
ResourceApplied(s1, "HTTPRoute", "httpbin", route1, 1)
153+
ResourceApplied(s2, "HTTPRoute", "httpbin2", route2, 1)
154+
routes, err := s1.DefaultDataplaneResource().Route().List(s1.Context)
155+
Expect(err).NotTo(HaveOccurred())
156+
Expect(routes).To(HaveLen(1))
157+
assert.Equal(GinkgoT(), routes[0].Labels["controller_name"], "gateway.api7.io/api7-ingress-controller-1")
158+
159+
routes, err = s2.DefaultDataplaneResource().Route().List(s2.Context)
160+
Expect(err).NotTo(HaveOccurred())
161+
Expect(routes).To(HaveLen(1))
162+
assert.Equal(GinkgoT(), routes[0].Labels["controller_name"], "gateway.api7.io/api7-ingress-controller-2")
163+
})
164+
})
165+
})

0 commit comments

Comments
 (0)