|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import unittest |
| 16 | +from unittest.mock import MagicMock, patch |
16 | 17 |
|
17 | | -from .ws_client import get_websocket_url |
| 18 | +from . import ws_client as ws_client_module |
| 19 | +from .ws_client import get_websocket_url, WSClient, V5_CHANNEL_PROTOCOL, V4_CHANNEL_PROTOCOL |
18 | 20 | from .ws_client import websocket_proxycare |
19 | 21 | from kubernetes.client.configuration import Configuration |
20 | 22 | import os |
21 | 23 | import socket |
22 | 24 | import threading |
23 | 25 | import pytest |
24 | 26 | from kubernetes import stream, client, config |
| 27 | +import websocket |
25 | 28 |
|
26 | 29 | try: |
27 | 30 | import urllib3 |
@@ -123,6 +126,117 @@ def test_websocket_proxycare(self): |
123 | 126 | assert dictval(connect_opts, 'http_proxy_auth') == expect_auth |
124 | 127 | assert dictval(connect_opts, 'http_no_proxy') == expect_noproxy |
125 | 128 |
|
| 129 | + |
| 130 | +class WSClientProtocolTest(unittest.TestCase): |
| 131 | + """Tests for WSClient V5 protocol handling""" |
| 132 | + |
| 133 | + def setUp(self): |
| 134 | + # Mock configuration to avoid real connections in WSClient.__init__ |
| 135 | + self.config_mock = MagicMock() |
| 136 | + self.config_mock.assert_hostname = False |
| 137 | + self.config_mock.api_key = {} |
| 138 | + self.config_mock.proxy = None |
| 139 | + self.config_mock.ssl_ca_cert = None |
| 140 | + self.config_mock.cert_file = None |
| 141 | + self.config_mock.key_file = None |
| 142 | + self.config_mock.verify_ssl = True |
| 143 | + |
| 144 | + def test_create_websocket_header(self): |
| 145 | + """Verify sec-websocket-protocol header requests v5 first""" |
| 146 | + # Patch WebSocket class in the module |
| 147 | + with patch.object(ws_client_module, 'WebSocket', autospec=True) as mock_ws_cls: |
| 148 | + mock_ws = mock_ws_cls.return_value |
| 149 | + |
| 150 | + WSClient(self.config_mock, "ws://test", headers=None, capture_all=True) |
| 151 | + |
| 152 | + mock_ws.connect.assert_called_once() |
| 153 | + call_args = mock_ws.connect.call_args |
| 154 | + # connect(url, **options) |
| 155 | + # check kwargs for 'header' |
| 156 | + kwargs = call_args[1] |
| 157 | + self.assertIn('header', kwargs) |
| 158 | + expected_header = f"sec-websocket-protocol: {V5_CHANNEL_PROTOCOL},{V4_CHANNEL_PROTOCOL}" |
| 159 | + self.assertIn(expected_header, kwargs['header']) |
| 160 | + |
| 161 | + def test_close_channel_v5(self): |
| 162 | + """Verify close_channel sends correct frame when v5 is negotiated""" |
| 163 | + with patch.object(ws_client_module, 'create_websocket') as mock_create: |
| 164 | + mock_ws = MagicMock() |
| 165 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 166 | + mock_ws.connected = True |
| 167 | + mock_create.return_value = mock_ws |
| 168 | + |
| 169 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True) |
| 170 | + client.close_channel(0) |
| 171 | + |
| 172 | + mock_ws.send.assert_called_with(b'\xff\x00', opcode=websocket.ABNF.OPCODE_BINARY) |
| 173 | + |
| 174 | + def test_close_channel_v4(self): |
| 175 | + """Verify close_channel does nothing when v4 is negotiated""" |
| 176 | + with patch.object(ws_client_module, 'create_websocket') as mock_create: |
| 177 | + mock_ws = MagicMock() |
| 178 | + mock_ws.subprotocol = V4_CHANNEL_PROTOCOL |
| 179 | + mock_ws.connected = True |
| 180 | + mock_create.return_value = mock_ws |
| 181 | + |
| 182 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True) |
| 183 | + client.close_channel(0) |
| 184 | + |
| 185 | + mock_ws.send.assert_not_called() |
| 186 | + |
| 187 | + def test_update_receives_close_v5(self): |
| 188 | + """Verify update processes close signal when v5 is negotiated""" |
| 189 | + with patch.object(ws_client_module, 'create_websocket') as mock_create, \ |
| 190 | + patch('select.select') as mock_select: |
| 191 | + |
| 192 | + mock_ws = MagicMock() |
| 193 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 194 | + mock_ws.connected = True |
| 195 | + mock_ws.sock.fileno.return_value = 10 |
| 196 | + |
| 197 | + # Setup frame with close signal for channel 0 |
| 198 | + frame = MagicMock() |
| 199 | + frame.data = b'\xff\x00' |
| 200 | + mock_ws.recv_data_frame.return_value = (websocket.ABNF.OPCODE_BINARY, frame) |
| 201 | + |
| 202 | + mock_create.return_value = mock_ws |
| 203 | + # Make select return ready |
| 204 | + mock_select.return_value = ([mock_ws.sock], [], []) |
| 205 | + |
| 206 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True) |
| 207 | + client.update(timeout=0) |
| 208 | + |
| 209 | + self.assertIn(0, client._closed_channels) |
| 210 | + |
| 211 | + def test_update_ignores_close_signal_v4(self): |
| 212 | + """Verify update treats 0xFF as regular data (or ignores signal interpretation) when v4""" |
| 213 | + with patch.object(ws_client_module, 'create_websocket') as mock_create, \ |
| 214 | + patch('select.select') as mock_select: |
| 215 | + |
| 216 | + mock_ws = MagicMock() |
| 217 | + mock_ws.subprotocol = V4_CHANNEL_PROTOCOL |
| 218 | + mock_ws.connected = True |
| 219 | + mock_ws.sock.fileno.return_value = 10 |
| 220 | + |
| 221 | + # Setup frame that looks like close signal but should be treated as data |
| 222 | + frame = MagicMock() |
| 223 | + frame.data = b'\xff\x00' |
| 224 | + mock_ws.recv_data_frame.return_value = (websocket.ABNF.OPCODE_BINARY, frame) |
| 225 | + |
| 226 | + mock_create.return_value = mock_ws |
| 227 | + mock_select.return_value = ([mock_ws.sock], [], []) |
| 228 | + |
| 229 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True, binary=True) # binary=True to avoid decode errors |
| 230 | + client.update(timeout=0) |
| 231 | + |
| 232 | + # Should NOT be in closed channels |
| 233 | + self.assertNotIn(0, client._closed_channels) |
| 234 | + # Should be in data channels (channel 255 with data \x00) |
| 235 | + # Code: channel = data[0] (255), data = data[1:] (\x00) |
| 236 | + # if channel (255) not in _channels... |
| 237 | + self.assertIn(255, client._channels) |
| 238 | + self.assertEqual(client._channels[255], b'\x00') |
| 239 | + |
126 | 240 | @pytest.fixture(scope="module") |
127 | 241 | def dummy_proxy(): |
128 | 242 | #Dummy Proxy |
|
0 commit comments