Skip to content

Commit cb2f3f3

Browse files
committed
add test
1 parent 3b92681 commit cb2f3f3

File tree

8 files changed

+181
-10
lines changed

8 files changed

+181
-10
lines changed

api/v2/shared_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const (
101101
SchemeTCP = "tcp"
102102
// SchemeUDP represents the UDP protocol.
103103
SchemeUDP = "udp"
104+
// SchemeTLS represents the TLS protocol.
105+
SchemeTLS = "tls"
104106
)
105107

106108
const (

internal/controller/indexer/tlsroute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func setupTLSRouteIndexer(mgr ctrl.Manager) error {
4040
context.Background(),
4141
&gatewayv1alpha2.TLSRoute{},
4242
ServiceIndexRef,
43-
TCPPRouteServiceIndexFunc,
43+
TLSPRouteServiceIndexFunc,
4444
); err != nil {
4545
return err
4646
}
4747
return nil
4848
}
4949

5050
func TLSRouteParentRefsIndexFunc(rawObj client.Object) []string {
51-
tr := rawObj.(*gatewayv1alpha2.TCPRoute)
51+
tr := rawObj.(*gatewayv1alpha2.TLSRoute)
5252
keys := make([]string, 0, len(tr.Spec.ParentRefs))
5353
for _, ref := range tr.Spec.ParentRefs {
5454
ns := tr.GetNamespace()

internal/controller/tlsroute_controller..go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ func (r *TLSRouteReconciler) listTLSRoutesForBackendTrafficPolicy(ctx context.Co
124124
}
125125
continue
126126
}
127-
tcprList := &gatewayv1alpha2.TLSRouteList{}
128-
if err := r.List(ctx, tcprList, client.MatchingFields{
127+
trList := &gatewayv1alpha2.TLSRouteList{}
128+
if err := r.List(ctx, trList, client.MatchingFields{
129129
indexer.ServiceIndexRef: indexer.GenIndexKey(policy.Namespace, string(targetRef.Name)),
130130
}); err != nil {
131131
r.Log.Error(err, "failed to list tlsroutes by service reference", "service", targetRef.Name)
132132
return nil
133133
}
134-
tlsrouteList = append(tlsrouteList, tcprList.Items...)
134+
tlsrouteList = append(tlsrouteList, trList.Items...)
135135
}
136136
var namespacedNameMap = make(map[k8stypes.NamespacedName]struct{})
137137
requests := make([]reconcile.Request, 0, len(tlsrouteList))
@@ -158,16 +158,16 @@ func (r *TLSRouteReconciler) listTLSRoutesForGateway(ctx context.Context, obj cl
158158
if !ok {
159159
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Gateway")
160160
}
161-
tcprList := &gatewayv1alpha2.TLSRouteList{}
162-
if err := r.List(ctx, tcprList, client.MatchingFields{
161+
trList := &gatewayv1alpha2.TLSRouteList{}
162+
if err := r.List(ctx, trList, client.MatchingFields{
163163
indexer.ParentRefs: indexer.GenIndexKey(gateway.Namespace, gateway.Name),
164164
}); err != nil {
165165
r.Log.Error(err, "failed to list tlsroutes by gateway", "gateway", gateway.Name)
166166
return nil
167167
}
168168

169-
requests := make([]reconcile.Request, 0, len(tcprList.Items))
170-
for _, tcr := range tcprList.Items {
169+
requests := make([]reconcile.Request, 0, len(trList.Items))
170+
for _, tcr := range trList.Items {
171171
requests = append(requests, reconcile.Request{
172172
NamespacedName: client.ObjectKey{
173173
Namespace: tcr.Namespace,

test/conformance/conformance_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
var skippedTestsForSSL = []string{
3535
tests.HTTPRouteHTTPSListener.ShortName,
3636
tests.HTTPRouteRedirectPortAndScheme.ShortName,
37+
38+
// TODO: apisix does not support TLSRoute passthrough.
39+
tests.TLSRouteSimpleSameNamespace.ShortName,
3740
}
3841

3942
// TODO: HTTPRoute hostname intersection and listener hostname matching

test/e2e/framework/manifests/apisix-standalone.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ data:
4040
stream_proxy: # TCP/UDP proxy
4141
tcp: # TCP proxy port list
4242
- 9100
43+
- addr: 9110
44+
tls: true
4345
udp: # UDP proxy port list
4446
- 9200
4547
discovery:
@@ -101,6 +103,9 @@ spec:
101103
- name: udp
102104
containerPort: 9200
103105
protocol: UDP
106+
- name: tls
107+
containerPort: 9110
108+
protocol: TCP
104109
volumeMounts:
105110
- name: config-writable
106111
mountPath: /usr/local/apisix/conf
@@ -139,6 +144,10 @@ spec:
139144
port: 9200
140145
protocol: UDP
141146
targetPort: 9200
147+
- name: tls
148+
port: 9110
149+
protocol: TCP
150+
targetPort: 9110
142151
selector:
143152
app.kubernetes.io/name: apisix
144153
type: {{ .ServiceType | default "NodePort" }}

test/e2e/framework/manifests/apisix.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ data:
4747
stream_proxy: # TCP/UDP proxy
4848
tcp: # TCP proxy port list
4949
- 9100
50+
- addr: 9110
51+
tls: true
5052
udp: # UDP proxy port list
5153
- 9200
5254
discovery:
@@ -111,6 +113,9 @@ spec:
111113
- name: udp
112114
containerPort: 9200
113115
protocol: UDP
116+
- name: tls
117+
containerPort: 9110
118+
protocol: TCP
114119
volumeMounts:
115120
- name: config-writable
116121
mountPath: /usr/local/apisix/conf
@@ -156,6 +161,10 @@ spec:
156161
port: 9200
157162
protocol: UDP
158163
targetPort: 9200
164+
- name: tls
165+
port: 9110
166+
protocol: TCP
167+
targetPort: 9110
159168
selector:
160169
app.kubernetes.io/name: apisix
161170
type: {{ .ServiceType | default "NodePort" }}

test/e2e/gatewayapi/tlsroute.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package gatewayapi
19+
20+
import (
21+
"fmt"
22+
"net/http"
23+
"time"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
28+
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
29+
)
30+
31+
var _ = Describe("suite-gateway: TLSRoute", func() {
32+
s := scaffold.NewDefaultScaffold()
33+
34+
Context("TLSRoute Base", func() {
35+
var (
36+
host = "api6.com"
37+
secretName = _secretName
38+
tlsGateway = `
39+
apiVersion: gateway.networking.k8s.io/v1
40+
kind: Gateway
41+
metadata:
42+
name: tls-gateway
43+
spec:
44+
gatewayClassName: %s
45+
listeners:
46+
- name: https
47+
protocol: TLS
48+
port: 443
49+
hostname: api6.com
50+
tls:
51+
certificateRefs:
52+
- kind: Secret
53+
group: ""
54+
name: %s
55+
infrastructure:
56+
parametersRef:
57+
group: apisix.apache.org
58+
kind: GatewayProxy
59+
name: apisix-proxy-config
60+
`
61+
tlsRoute = `
62+
apiVersion: gateway.networking.k8s.io/v1alpha2
63+
kind: TLSRoute
64+
metadata:
65+
name: tls-route
66+
spec:
67+
parentRefs:
68+
- name: tls-gateway
69+
hostnames: ["api6.com"]
70+
rules:
71+
- backendRefs:
72+
- name: httpbin-service-e2e-test
73+
port: 80
74+
`
75+
)
76+
BeforeEach(func() {
77+
createSecret(s, secretName)
78+
By("create GatewayProxy")
79+
Expect(s.CreateResourceFromString(s.GetGatewayProxySpec())).NotTo(HaveOccurred(), "creating GatewayProxy")
80+
81+
By("create GatewayClass")
82+
Expect(s.CreateResourceFromString(s.GetGatewayClassYaml())).NotTo(HaveOccurred(), "creating GatewayClass")
83+
84+
// Create Gateway with TCP listener
85+
By("create Gateway")
86+
Expect(s.CreateResourceFromString(fmt.Sprintf(tlsGateway, s.Namespace(), secretName))).NotTo(HaveOccurred(), "creating Gateway")
87+
})
88+
It("Basic", func() {
89+
s.ResourceApplied("TLSRoute", "tls-route", tlsRoute, 1)
90+
91+
time.Sleep(10 * time.Second)
92+
client := s.NewAPISIXClientWithTLSProxy(host)
93+
client.GET("/ip").
94+
Expect().
95+
Status(http.StatusOK)
96+
client.GET("/notfound").
97+
Expect().
98+
Status(http.StatusNotFound)
99+
100+
s.DeleteResourceFromString(tlsRoute)
101+
time.Sleep(5 * time.Second)
102+
103+
reporter := &scaffold.ErrorReporter{}
104+
_ = client.GET("/ip").WithReporter(reporter).Expect()
105+
Expect(reporter.Err().Error()).Should(ContainSubstring("EOF"), "should get EOF after deleting TLSRoute")
106+
})
107+
})
108+
})

test/e2e/scaffold/scaffold.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import (
3131
"github.com/gruntwork-io/terratest/modules/k8s"
3232
"github.com/gruntwork-io/terratest/modules/testing"
3333
. "github.com/onsi/ginkgo/v2" //nolint:staticcheck
34-
. "github.com/onsi/gomega" //nolint:staticcheck
34+
ginkgo "github.com/onsi/ginkgo/v2"
35+
. "github.com/onsi/gomega" //nolint:staticcheck
3536
corev1 "k8s.io/api/core/v1"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3738

@@ -80,6 +81,7 @@ type Tunnels struct {
8081
HTTP *k8s.Tunnel
8182
HTTPS *k8s.Tunnel
8283
TCP *k8s.Tunnel
84+
TLS *k8s.Tunnel
8385
}
8486

8587
func (t *Tunnels) Close() {
@@ -95,6 +97,10 @@ func (t *Tunnels) Close() {
9597
t.safeClose(t.TCP.Close)
9698
t.TCP = nil
9799
}
100+
if t.TLS != nil {
101+
t.safeClose(t.TLS.Close)
102+
t.TLS = nil
103+
}
98104
}
99105

100106
func (t *Tunnels) safeClose(close func()) {
@@ -274,6 +280,31 @@ func (s *Scaffold) NewAPISIXClientWithTCPProxy() *httpexpect.Expect {
274280
})
275281
}
276282

283+
func (s *Scaffold) NewAPISIXClientWithTLSProxy(host string) *httpexpect.Expect {
284+
u := url.URL{
285+
Scheme: apiv2.SchemeHTTPS,
286+
Host: s.apisixTunnels.TLS.Endpoint(),
287+
}
288+
return httpexpect.WithConfig(httpexpect.Config{
289+
BaseURL: u.String(),
290+
Client: &http.Client{
291+
Transport: &http.Transport{
292+
TLSClientConfig: &tls.Config{
293+
// accept any certificate; for testing only!
294+
InsecureSkipVerify: true,
295+
ServerName: host,
296+
},
297+
},
298+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
299+
return http.ErrUseLastResponse
300+
},
301+
},
302+
Reporter: httpexpect.NewAssertReporter(
303+
httpexpect.NewAssertReporter(ginkgo.GinkgoT()),
304+
),
305+
})
306+
}
307+
277308
func (s *Scaffold) DefaultDataplaneResource() DataplaneResource {
278309
return s.Deployer.DefaultDataplaneResource()
279310
}
@@ -359,6 +390,7 @@ func (s *Scaffold) createDataplaneTunnels(
359390
httpPort int
360391
httpsPort int
361392
tcpPort int
393+
tlsPort int
362394
)
363395

364396
for _, port := range svc.Spec.Ports {
@@ -369,6 +401,8 @@ func (s *Scaffold) createDataplaneTunnels(
369401
httpsPort = int(port.Port)
370402
case apiv2.SchemeTCP:
371403
tcpPort = int(port.Port)
404+
case apiv2.SchemeTLS:
405+
tlsPort = int(port.Port)
372406
}
373407
}
374408

@@ -381,6 +415,8 @@ func (s *Scaffold) createDataplaneTunnels(
381415
0, httpsPort)
382416
tcpTunnel := k8s.NewTunnel(kubectlOpts, k8s.ResourceTypeService, serviceName,
383417
0, tcpPort)
418+
tlsTunnel := k8s.NewTunnel(kubectlOpts, k8s.ResourceTypeService, serviceName,
419+
0, tlsPort)
384420

385421
if err := httpTunnel.ForwardPortE(s.t); err != nil {
386422
return nil, err
@@ -396,6 +432,10 @@ func (s *Scaffold) createDataplaneTunnels(
396432
return nil, err
397433
}
398434
tunnels.TCP = tcpTunnel
435+
if err := tlsTunnel.ForwardPortE(s.t); err != nil {
436+
return nil, err
437+
}
438+
tunnels.TLS = tlsTunnel
399439

400440
return tunnels, nil
401441
}

0 commit comments

Comments
 (0)