Skip to content

Commit 4f32b4a

Browse files
committed
fix tests
1 parent 6eaaefd commit 4f32b4a

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

test/e2e/gatewayapi/controller.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ apiVersion: gateway.networking.k8s.io/v1
1717
kind: GatewayClass
1818
metadata:
1919
name: %s
20+
namespace: %s
2021
spec:
2122
controllerName: %s
2223
`
@@ -26,6 +27,7 @@ apiVersion: gateway.networking.k8s.io/v1
2627
kind: Gateway
2728
metadata:
2829
name: %s
30+
namespace: %s
2931
spec:
3032
gatewayClassName: %s
3133
listeners:
@@ -34,12 +36,12 @@ spec:
3436
port: 80
3537
`
3638

37-
var ResourceApplied = func(s *scaffold.Scaffold, resourType, resourceName, resourceRaw string, observedGeneration int) {
38-
Expect(s.CreateResourceFromString(resourceRaw)).
39+
var ResourceApplied = func(s *scaffold.Scaffold, resourType, resourceName, ns, resourceRaw string, observedGeneration int) {
40+
Expect(s.CreateResourceFromStringWithNamespace(resourceRaw, ns)).
3941
NotTo(HaveOccurred(), fmt.Sprintf("creating %s", resourType))
4042

4143
Eventually(func() string {
42-
hryaml, err := s.GetResourceYaml(resourType, resourceName)
44+
hryaml, err := s.GetResourceYamlFromNamespace(resourType, resourceName, ns)
4345
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting %s yaml", resourType))
4446
return hryaml
4547
}, "8s", "2s").
@@ -53,25 +55,31 @@ spec:
5355
time.Sleep(1 * time.Second)
5456
}
5557
var beforeEach = func(s *scaffold.Scaffold, gatewayName string) {
58+
s.CreateResourceFromString(fmt.Sprintf(`
59+
kind: Namespace
60+
apiVersion: v1
61+
metadata:
62+
name: %s
63+
`, gatewayName))
5664
By(fmt.Sprintf("create GatewayClass for controller %s", s.GetControllerName()))
5765
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
58-
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), s.Namespace())
66+
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, gatewayName, s.GetControllerName()), gatewayName)
5967
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
60-
time.Sleep(20 * time.Second)
68+
time.Sleep(10 * time.Second)
6169

6270
By("check GatewayClass condition")
63-
gcyaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName)
71+
gcyaml, err := s.GetResourceYamlFromNamespace("GatewayClass", gatewayClassName, gatewayName)
6472
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
6573
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
6674
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
6775

6876
By("create Gateway")
69-
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGateway, gatewayName, gatewayClassName), s.Namespace())
77+
err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGateway, gatewayName, gatewayName, gatewayClassName), gatewayName)
7078
Expect(err).NotTo(HaveOccurred(), "creating Gateway")
71-
time.Sleep(20 * time.Second)
79+
time.Sleep(10 * time.Second)
7280

7381
By("check Gateway condition")
74-
gwyaml, err := s.GetResourceYaml("Gateway", gatewayName)
82+
gwyaml, err := s.GetResourceYamlFromNamespace("Gateway", gatewayName, gatewayName)
7583
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
7684
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
7785
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"), "checking Gateway condition message")
@@ -82,15 +90,12 @@ spec:
8290
Name: "gateway1",
8391
ControllerName: "gateway.api7.io/api7-ingress-controller-1",
8492
})
85-
s2 := scaffold.NewScaffold(&scaffold.Options{
86-
Name: "gateway2",
87-
ControllerName: "gateway.api7.io/api7-ingress-controller-2",
88-
})
8993
var route1 = `
9094
apiVersion: gateway.networking.k8s.io/v1
9195
kind: HTTPRoute
9296
metadata:
9397
name: httpbin
98+
namespace: gateway1
9499
spec:
95100
parentRefs:
96101
- name: gateway1
@@ -115,11 +120,28 @@ spec:
115120
port: 80
116121
weight: 50
117122
`
123+
BeforeEach(func() {
124+
beforeEach(s1, "gateway1")
125+
})
126+
It("Apply resource ", func() {
127+
ResourceApplied(s1, "HTTPRoute", "httpbin", "gateway1", route1, 1)
128+
routes, err := s1.DefaultDataplaneResource().Route().List(s1.Context)
129+
Expect(err).NotTo(HaveOccurred())
130+
Expect(routes).To(HaveLen(1))
131+
assert.Equal(GinkgoT(), routes[0].Labels["controller_name"], "gateway.api7.io/api7-ingress-controller-1")
132+
})
133+
})
134+
Context("Create resource with second controller", func() {
135+
s2 := scaffold.NewScaffold(&scaffold.Options{
136+
Name: "gateway2",
137+
ControllerName: "gateway.api7.io/api7-ingress-controller-2",
138+
})
118139
var route2 = `
119140
apiVersion: gateway.networking.k8s.io/v1
120141
kind: HTTPRoute
121142
metadata:
122143
name: httpbin2
144+
namespace: gateway2
123145
spec:
124146
parentRefs:
125147
- name: gateway2
@@ -145,18 +167,11 @@ spec:
145167
weight: 50
146168
`
147169
BeforeEach(func() {
148-
beforeEach(s1, "gateway1")
149170
beforeEach(s2, "gateway2")
150171
})
151172
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)
173+
ResourceApplied(s2, "HTTPRoute", "httpbin2", "gateway2", route2, 1)
174+
routes, err := s2.DefaultDataplaneResource().Route().List(s2.Context)
160175
Expect(err).NotTo(HaveOccurred())
161176
Expect(routes).To(HaveLen(1))
162177
assert.Equal(GinkgoT(), routes[0].Labels["controller_name"], "gateway.api7.io/api7-ingress-controller-2")

test/e2e/gatewayapi/gateway.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ spec:
288288
assert.Nil(GinkgoT(), err, "list tls error")
289289
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
290290
assert.Equal(GinkgoT(), Cert, tls[0].Cert, "tls cert not expect")
291+
assert.Equal(GinkgoT(), tls[0].Labels["controller_name"], "gateway.api7.io/api7-ingress-controller")
291292
})
292293
})
293294
})

test/e2e/scaffold/k8s.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (s *Scaffold) GetOutputFromString(shell ...string) (string, error) {
8484
return output, err
8585
}
8686

87+
func (s *Scaffold) GetResourceYamlFromNamespace(resourceType, resourceName, namespace string) (string, error) {
88+
return s.GetOutputFromString(resourceType, resourceName, "-n", namespace, "-o", "yaml")
89+
}
90+
8791
func (s *Scaffold) GetResourceYaml(resourceType, resourceName string) (string, error) {
8892
return s.GetOutputFromString(resourceType, resourceName, "-o", "yaml")
8993
}

0 commit comments

Comments
 (0)