|
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, CLOSE_CHANNEL, STDIN_CHANNEL |
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,224 @@ 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(bytes([CLOSE_CHANNEL, STDIN_CHANNEL]), 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 = bytes([CLOSE_CHANNEL, STDIN_CHANNEL]) |
| 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 = bytes([CLOSE_CHANNEL, STDIN_CHANNEL]) |
| 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 | + |
| 240 | + def test_readline_channel_closed_with_leftover_data(self): |
| 241 | + """Verify readline_channel flushes remaining buffer when channel is closed""" |
| 242 | + with patch.object(ws_client_module, 'create_websocket') as mock_create: |
| 243 | + mock_ws = MagicMock() |
| 244 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 245 | + mock_ws.connected = True |
| 246 | + mock_create.return_value = mock_ws |
| 247 | + |
| 248 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True, binary=False) |
| 249 | + |
| 250 | + # Simulate some data in the channel buffer, and then close it |
| 251 | + client._channels[1] = "hello" |
| 252 | + client._closed_channels.add(1) |
| 253 | + |
| 254 | + # First call to readline should flush "hello" even though there is no newline |
| 255 | + line1 = client.readline_channel(1) |
| 256 | + self.assertEqual(line1, "hello") |
| 257 | + |
| 258 | + # Subsequent call should return empty string |
| 259 | + line2 = client.readline_channel(1) |
| 260 | + self.assertEqual(line2, "") |
| 261 | + |
| 262 | + def test_readline_channel_closed_with_leftover_data_binary(self): |
| 263 | + """Verify readline_channel flushes remaining buffer when channel is closed in binary mode""" |
| 264 | + with patch.object(ws_client_module, 'create_websocket') as mock_create: |
| 265 | + mock_ws = MagicMock() |
| 266 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 267 | + mock_ws.connected = True |
| 268 | + mock_create.return_value = mock_ws |
| 269 | + |
| 270 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True, binary=True) |
| 271 | + |
| 272 | + # Simulate some bytes in the channel buffer, and then close it |
| 273 | + client._channels[1] = b"hello-binary" |
| 274 | + client._closed_channels.add(1) |
| 275 | + |
| 276 | + # First call to readline should flush leftover bytes |
| 277 | + line1 = client.readline_channel(1) |
| 278 | + self.assertEqual(line1, b"hello-binary") |
| 279 | + |
| 280 | + # Subsequent call should return empty bytes |
| 281 | + line2 = client.readline_channel(1) |
| 282 | + self.assertEqual(line2, b"") |
| 283 | + |
| 284 | + def test_read_channel_closed_with_leftover_data(self): |
| 285 | + """Verify read_channel drains leftover data and then short-circuits on closed channel""" |
| 286 | + with patch.object(ws_client_module, 'create_websocket') as mock_create: |
| 287 | + mock_ws = MagicMock() |
| 288 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 289 | + mock_ws.connected = True |
| 290 | + mock_ws.sock.fileno.return_value = 10 |
| 291 | + mock_create.return_value = mock_ws |
| 292 | + |
| 293 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True, binary=False) |
| 294 | + |
| 295 | + # Simulate leftover data and closed channel |
| 296 | + client._channels[1] = "hello" |
| 297 | + client._closed_channels.add(1) |
| 298 | + |
| 299 | + # First call should drain data |
| 300 | + data1 = client.read_channel(1) |
| 301 | + self.assertEqual(data1, "hello") |
| 302 | + |
| 303 | + # Subsequent call should short-circuit and return empty string |
| 304 | + # Patch `update` to assert it is NOT called (short-circuit) |
| 305 | + with patch.object(client, 'update') as mock_update: |
| 306 | + data2 = client.read_channel(1) |
| 307 | + self.assertEqual(data2, "") |
| 308 | + mock_update.assert_not_called() |
| 309 | + |
| 310 | + def test_peek_channel_closed_with_leftover_data(self): |
| 311 | + """Verify peek_channel allows peeking leftover data and then short-circuits after draining""" |
| 312 | + with patch.object(ws_client_module, 'create_websocket') as mock_create, \ |
| 313 | + patch('select.poll') as mock_poll: |
| 314 | + mock_poll.return_value.poll.return_value = [] |
| 315 | + mock_ws = MagicMock() |
| 316 | + mock_ws.subprotocol = V5_CHANNEL_PROTOCOL |
| 317 | + mock_ws.connected = True |
| 318 | + mock_ws.sock.fileno.return_value = 10 |
| 319 | + mock_create.return_value = mock_ws |
| 320 | + |
| 321 | + client = WSClient(self.config_mock, "ws://test", headers=None, capture_all=True, binary=False) |
| 322 | + |
| 323 | + # Simulate leftover data and closed channel |
| 324 | + client._channels[1] = "hello" |
| 325 | + client._closed_channels.add(1) |
| 326 | + |
| 327 | + # First peek should return data without draining |
| 328 | + data1 = client.peek_channel(1) |
| 329 | + self.assertEqual(data1, "hello") |
| 330 | + |
| 331 | + # Second peek should still return data |
| 332 | + data2 = client.peek_channel(1) |
| 333 | + self.assertEqual(data2, "hello") |
| 334 | + |
| 335 | + # Drain it |
| 336 | + client.read_channel(1) |
| 337 | + |
| 338 | + # Now peek should short-circuit and return empty string |
| 339 | + # Patch `update` to assert it is NOT called (short-circuit) |
| 340 | + with patch.object(client, 'update') as mock_update: |
| 341 | + data3 = client.peek_channel(1) |
| 342 | + self.assertEqual(data3, "") |
| 343 | + mock_update.assert_not_called() |
| 344 | + |
| 345 | + |
| 346 | + |
126 | 347 | @pytest.fixture(scope="module") |
127 | 348 | def dummy_proxy(): |
128 | 349 | #Dummy Proxy |
|
0 commit comments