sigs.k8s.io/gateway-api@v1.0.0/conformance/tests/httproute-backend-protocol-ws.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 // Send text frame 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 // Send byte frame 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 }