Skip to content

Commit 6599e4f

Browse files
fix: wss related tests are unstable (#2675) (b70e22e) (#371)
1 parent 0e4aaaf commit 6599e4f

File tree

9 files changed

+87
-80
lines changed

9 files changed

+87
-80
lines changed

internal/controller/gateway_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
120120
}
121121
return ctrl.Result{}, err
122122
}
123+
if !r.checkGatewayClass(gateway) {
124+
return ctrl.Result{}, nil
125+
}
126+
123127
conditionProgrammedStatus, conditionProgrammedMsg := true, "Programmed"
124128

125129
r.Log.Info("gateway has been accepted", "gateway", gateway.GetName())

internal/controller/indexer/ssl_host.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ func GatewayTLSHostIndexFunc(rawObj client.Object) []string {
6969
}
7070
}
7171

72-
tlsHostIndexLogger.Info("GatewayTLSHostIndexFunc", "hosts", hostSetToSlice(hosts), "len", len(hostSetToSlice(hosts)))
72+
hostsSlice := hostSetToSlice(hosts)
7373

74-
return hostSetToSlice(hosts)
74+
tlsHostIndexLogger.V(1).Info("GatewayTLSHostIndexFunc", "hosts", hostsSlice)
75+
76+
return hostsSlice
7577
}
7678

7779
// IngressTLSHostIndexFunc indexes Ingresses by their TLS SNI hosts.

test/e2e/crds/v2/route.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,28 +2323,12 @@ spec:
23232323
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"},
23242324
new(apiv2.ApisixRoute), fmt.Sprintf(apisixRouteWithBackendWSS, s.Namespace()))
23252325

2326-
By("verify wss connection with retry")
2327-
u := url.URL{
2328-
Scheme: "wss",
2329-
Host: s.GetAPISIXHTTPSEndpoint(),
2330-
Path: "/ws",
2331-
}
2332-
headers := http.Header{"Host": []string{"api6.com"}}
2333-
dialer := websocket.Dialer{
2334-
TLSClientConfig: &tls.Config{
2335-
InsecureSkipVerify: true,
2336-
ServerName: "api6.com",
2337-
},
2338-
}
2339-
2340-
var conn *websocket.Conn
2341-
var resp *http.Response
2342-
Eventually(func() error {
2343-
var dialErr error
2344-
conn, resp, dialErr = dialer.Dial(u.String(), headers)
2345-
return dialErr
2346-
}).WithTimeout(30*time.Second).WithPolling(2*time.Second).Should(Succeed(), "WebSocket handshake should succeed")
2347-
Expect(resp.StatusCode).Should(Equal(http.StatusSwitchingProtocols))
2326+
By("verify wss connection")
2327+
hostname := "api6.com"
2328+
conn := s.NewWebsocketClient(&tls.Config{
2329+
InsecureSkipVerify: true,
2330+
ServerName: hostname,
2331+
}, "/ws", http.Header{"Host": []string{hostname}})
23482332

23492333
defer func() {
23502334
_ = conn.Close()

test/e2e/framework/manifests/apisix.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ metadata:
6363
name: apisix
6464
labels:
6565
app.kubernetes.io/name: apisix
66+
app: apisix
6667
spec:
6768
replicas: {{ default 1 .Replicas }}
6869
selector:
6970
matchLabels:
7071
app.kubernetes.io/name: apisix
72+
app: apisix
7173
template:
7274
metadata:
7375
labels:
7476
app.kubernetes.io/name: apisix
77+
app: apisix
7578
spec:
7679
initContainers:
7780
- name: config-setup

test/e2e/framework/manifests/nginx.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ spec:
124124
containers:
125125
- livenessProbe:
126126
failureThreshold: 3
127-
initialDelaySeconds: 1
128-
periodSeconds: 5
127+
initialDelaySeconds: 10
128+
periodSeconds: 15
129129
successThreshold: 1
130130
httpGet:
131131
path: /healthz
132132
port: 80
133133
timeoutSeconds: 2
134+
readinessProbe:
135+
httpGet:
136+
path: /healthz
137+
port: 80
138+
initialDelaySeconds: 5
139+
periodSeconds: 5
134140
image: "openresty/openresty:1.27.1.2-4-bullseye-fat"
135141
imagePullPolicy: IfNotPresent
136142
name: nginx

test/e2e/gatewayapi/httproute.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/tls"
2323
"fmt"
2424
"net/http"
25-
"net/url"
2625
"strings"
2726
"time"
2827

@@ -2507,33 +2506,13 @@ spec:
25072506

25082507
It("WSS backend", func() {
25092508
s.ResourceApplied("HTTPRoute", "nginx-wss", fmt.Sprintf(httprouteWithWSS, s.Namespace()), 1)
2510-
time.Sleep(6 * time.Second)
25112509

25122510
By("verify wss connection")
2513-
u := url.URL{
2514-
Scheme: "wss",
2515-
Host: s.GetAPISIXHTTPSEndpoint(),
2516-
Path: "/ws",
2517-
}
2518-
headers := http.Header{"Host": []string{"api6.com"}}
2519-
25202511
hostname := "api6.com"
2521-
2522-
dialer := websocket.Dialer{
2523-
TLSClientConfig: &tls.Config{
2524-
InsecureSkipVerify: true,
2525-
ServerName: hostname,
2526-
},
2527-
}
2528-
2529-
var conn *websocket.Conn
2530-
var resp *http.Response
2531-
Eventually(func() error {
2532-
var dialErr error
2533-
conn, resp, dialErr = dialer.Dial(u.String(), headers)
2534-
return dialErr
2535-
}).WithTimeout(30*time.Second).WithPolling(2*time.Second).Should(Succeed(), "WebSocket handshake should succeed")
2536-
Expect(resp.StatusCode).Should(Equal(http.StatusSwitchingProtocols))
2512+
conn := s.NewWebsocketClient(&tls.Config{
2513+
InsecureSkipVerify: true,
2514+
ServerName: hostname,
2515+
}, "/ws", http.Header{"Host": []string{hostname}})
25372516

25382517
defer func() {
25392518
_ = conn.Close()

test/e2e/ingress/ingress.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/base64"
2424
"fmt"
2525
"net/http"
26-
"net/url"
2726
"strings"
2827
"time"
2928

@@ -992,14 +991,13 @@ spec:
992991
By("create Ingress")
993992
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(ingress, s.Namespace()), s.Namespace())
994993
Expect(err).NotTo(HaveOccurred(), "creating Ingress")
995-
time.Sleep(5 * time.Second)
996994

997995
By("verify Ingress works")
998-
s.NewAPISIXClient().
999-
GET("/get").
1000-
WithHost("ingress.example.com").
1001-
Expect().
1002-
Status(200)
996+
s.RequestAssert(&scaffold.RequestAssert{
997+
Method: "GET",
998+
Host: "ingress.example.com",
999+
Check: scaffold.WithExpectedStatus(http.StatusOK),
1000+
})
10031001

10041002
By("create additional gateway group to get new admin key")
10051003
additionalGatewayGroupID, _, err = s.Deployer.CreateAdditionalGateway("gateway-proxy-update")
@@ -1121,30 +1119,13 @@ spec:
11211119
createSecret(s, _secretName)
11221120
By("create Ingress")
11231121
Expect(s.CreateResourceFromString(fmt.Sprintf(ingressWithWSS, s.Namespace()))).ShouldNot(HaveOccurred(), "creating Ingress")
1124-
time.Sleep(6 * time.Second)
11251122

11261123
By("verify wss connection")
1127-
u := url.URL{
1128-
Scheme: "wss",
1129-
Host: s.GetAPISIXHTTPSEndpoint(),
1130-
Path: "/ws",
1131-
}
1132-
headers := http.Header{"Host": []string{"api6.com"}}
1133-
dialer := websocket.Dialer{
1134-
TLSClientConfig: &tls.Config{
1135-
InsecureSkipVerify: true,
1136-
ServerName: "api6.com",
1137-
},
1138-
}
1139-
1140-
var conn *websocket.Conn
1141-
var resp *http.Response
1142-
Eventually(func() error {
1143-
var dialErr error
1144-
conn, resp, dialErr = dialer.Dial(u.String(), headers)
1145-
return dialErr
1146-
}).WithTimeout(30*time.Second).WithPolling(2*time.Second).Should(Succeed(), "WebSocket handshake should succeed")
1147-
Expect(resp.StatusCode).Should(Equal(http.StatusSwitchingProtocols))
1124+
hostname := "api6.com"
1125+
conn := s.NewWebsocketClient(&tls.Config{
1126+
InsecureSkipVerify: true,
1127+
ServerName: hostname,
1128+
}, "/ws", http.Header{"Host": []string{hostname}})
11481129

11491130
defer func() {
11501131
_ = conn.Close()

test/e2e/scaffold/apisix_deployer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ func (s *APISIXDeployer) AfterEach() {
111111
if output != "" {
112112
_, _ = fmt.Fprintln(GinkgoWriter, output)
113113
}
114+
115+
_, _ = fmt.Fprintln(GinkgoWriter, "Dumping APISIX logs:")
116+
output = s.GetDeploymentLogs("apisix")
117+
if output != "" {
118+
_, _ = fmt.Fprintln(GinkgoWriter, output)
119+
}
120+
if framework.ProviderType == framework.ProviderTypeAPISIXStandalone && s.adminTunnel != nil {
121+
client := NewClient("http", s.adminTunnel.Endpoint())
122+
reporter := &ErrorReporter{}
123+
body := client.GET("/apisix/admin/configs").WithHeader("X-API-KEY", s.AdminKey()).WithReporter(reporter).Expect().Body().Raw()
124+
_, _ = fmt.Fprintln(GinkgoWriter, "Dumping APISIX configs:")
125+
_, _ = fmt.Fprintln(GinkgoWriter, body)
126+
}
114127
}
115128

116129
// Delete all additional gateways

test/e2e/scaffold/scaffold.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/api7/gopkg/pkg/log"
3131
"github.com/gavv/httpexpect/v2"
32+
"github.com/gorilla/websocket"
3233
"github.com/gruntwork-io/terratest/modules/k8s"
3334
"github.com/gruntwork-io/terratest/modules/testing"
3435
. "github.com/onsi/ginkgo/v2" //nolint:staticcheck
@@ -579,3 +580,37 @@ func (s *Scaffold) GetMetricsEndpoint() string {
579580
s.addFinalizers(tunnel.Close)
580581
return fmt.Sprintf("http://%s/metrics", tunnel.Endpoint())
581582
}
583+
584+
func (s *Scaffold) NewWebsocketClient(tls *tls.Config, path string, headers http.Header) *websocket.Conn {
585+
var host = s.ApisixHTTPEndpoint()
586+
var scheme = "ws"
587+
if tls != nil {
588+
scheme = "wss"
589+
host = s.GetAPISIXHTTPSEndpoint()
590+
}
591+
592+
dialer := websocket.Dialer{
593+
TLSClientConfig: tls,
594+
}
595+
596+
u := url.URL{
597+
Scheme: scheme,
598+
Host: host,
599+
Path: path,
600+
}
601+
var conn *websocket.Conn
602+
603+
s.RetryAssertion(func() error {
604+
c, resp, err := dialer.Dial(u.String(), headers)
605+
if err != nil {
606+
return err
607+
}
608+
if resp == nil || resp.StatusCode != http.StatusSwitchingProtocols {
609+
_ = c.Close()
610+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
611+
}
612+
conn = c
613+
return nil
614+
}).ShouldNot(HaveOccurred(), "establishing websocket connection")
615+
return conn
616+
}

0 commit comments

Comments
 (0)