1
16
17 package tests
18
19 import (
20 "bytes"
21 "fmt"
22 "testing"
23 "time"
24
25 "golang.org/x/net/websocket"
26 "k8s.io/apimachinery/pkg/types"
27
28 "sigs.k8s.io/gateway-api/conformance/utils/http"
29 "sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
30 "sigs.k8s.io/gateway-api/conformance/utils/suite"
31 )
32
33 func init() {
34 ConformanceTests = append(ConformanceTests,
35 HTTPRouteBackendProtocolWebSocket,
36 )
37 }
38
39 var HTTPRouteBackendProtocolWebSocket = suite.ConformanceTest{
40 ShortName: "HTTPRouteBackendProtocolWebSocket",
41 Description: "A HTTPRoute with a BackendRef that has an appProtocol kubernetes.io/ws should be functional",
42 Features: []suite.SupportedFeature{
43 suite.SupportGateway,
44 suite.SupportHTTPRoute,
45 suite.SupportHTTPRouteBackendProtocolWebSocket,
46 },
47 Manifests: []string{
48 "tests/httproute-backend-protocol-ws.yaml",
49 },
50 Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
51 ns := "gateway-conformance-infra"
52 routeNN := types.NamespacedName{Name: "backend-protocol-ws", Namespace: ns}
53 gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
54 gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
55
56 threshold := suite.TimeoutConfig.RequiredConsecutiveSuccesses
57 maxTimeToConsistency := suite.TimeoutConfig.MaxTimeToConsistency
58
59 t.Run("websocket connection should reach backend", func(t *testing.T) {
60 http.AwaitConvergence(t, threshold, maxTimeToConsistency, func(elapsed time.Duration) bool {
61 origin := fmt.Sprintf("ws://gateway/%s", t.Name())
62 remote := fmt.Sprintf("ws://%s/ws", gwAddr)
63
64 ws, err := websocket.Dial(remote, "", origin)
65 if err != nil {
66 t.Log("failed to dial", err)
67 return false
68 }
69 defer ws.Close()
70
71
72 var (
73 textMessage = "Websocket Support!"
74 textReply string
75 )
76 if err := websocket.Message.Send(ws, textMessage); err != nil {
77 t.Log("failed to send text frame", err)
78 return false
79 }
80 if err := websocket.Message.Receive(ws, &textReply); err != nil {
81 t.Log("failed to receive text frame", err)
82 return false
83 }
84 if textMessage != textReply {
85 t.Logf("unexpected reply - want: %s got: %s", textMessage, textReply)
86 return false
87 }
88
89
90 var (
91 binaryMessage = []byte{1, 2, 3, 4, 5, 6, 7}
92 binaryReply []byte
93 )
94 if err := websocket.Message.Send(ws, binaryMessage); err != nil {
95 t.Log("failed to send binary frame", err)
96 return false
97 }
98 if err := websocket.Message.Receive(ws, &binaryReply); err != nil {
99 t.Log("failed to receive binary frame", err)
100 }
101 if !bytes.Equal(binaryMessage, binaryReply) {
102 t.Logf("unexpected reply - want: %#v got: %#v", binaryMessage, binaryReply)
103 return false
104 }
105 return true
106 })
107 })
108 },
109 }
110
View as plain text