blob: c108be981ea941bdfa8f9490fd549c46e983164b [file] [log] [blame]
jianghua wangd22514a2017-05-08 08:05:04 +01001# Copyright 2017 Citrix Systems
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from six.moves.urllib import parse as urlparse
17
18import mock
19
20from tempest.common import compute
21from tempest.tests import base
22
23
24class TestCompute(base.TestCase):
25 def setUp(self):
26 super(TestCompute, self).setUp()
27 self.client_sock = mock.Mock()
28 self.url = urlparse.urlparse("http://www.fake.com:80")
29
30 def test_rfp_frame_not_cached(self):
31 # rfp negotiation frame arrived separately after upgrade
32 # response, so it's not cached.
33 RFP_VERSION = b'RFB.003.003\x0a'
34 rfp_frame_header = b'\x82\x0c'
35
36 self.client_sock.recv.side_effect = [
37 b'fake response start\r\n',
38 b'fake response end\r\n\r\n',
39 rfp_frame_header,
40 RFP_VERSION]
41 expect_response = b'fake response start\r\nfake response end\r\n\r\n'
42
43 webSocket = compute._WebSocket(self.client_sock, self.url)
44
45 self.assertEqual(webSocket.response, expect_response)
46 # no cache
47 self.assertEqual(webSocket.cached_stream, b'')
48 self.client_sock.recv.assert_has_calls([mock.call(4096),
49 mock.call(4096)])
50
51 self.client_sock.recv.reset_mock()
52 recv_version = webSocket.receive_frame()
53
54 self.assertEqual(recv_version, RFP_VERSION)
55 self.client_sock.recv.assert_has_calls([mock.call(2),
56 mock.call(12)])
57
58 def test_rfp_frame_fully_cached(self):
59 RFP_VERSION = b'RFB.003.003\x0a'
60 rfp_version_frame = b'\x82\x0c%s' % RFP_VERSION
61
62 self.client_sock.recv.side_effect = [
63 b'fake response start\r\n',
64 b'fake response end\r\n\r\n%s' % rfp_version_frame]
65 expect_response = b'fake response start\r\nfake response end\r\n\r\n'
66 webSocket = compute._WebSocket(self.client_sock, self.url)
67
68 self.client_sock.recv.assert_has_calls([mock.call(4096),
69 mock.call(4096)])
70 self.assertEqual(webSocket.response, expect_response)
71 self.assertEqual(webSocket.cached_stream, rfp_version_frame)
72
73 self.client_sock.recv.reset_mock()
74 recv_version = webSocket.receive_frame()
75
76 self.client_sock.recv.assert_not_called()
77 self.assertEqual(recv_version, RFP_VERSION)
78 # cached_stream should be empty in the end.
79 self.assertEqual(webSocket.cached_stream, b'')
80
81 def test_rfp_frame_partially_cached(self):
82 RFP_VERSION = b'RFB.003.003\x0a'
83 rfp_version_frame = b'\x82\x0c%s' % RFP_VERSION
84 frame_part1 = rfp_version_frame[:6]
85 frame_part2 = rfp_version_frame[6:]
86
87 self.client_sock.recv.side_effect = [
88 b'fake response start\r\n',
89 b'fake response end\r\n\r\n%s' % frame_part1,
90 frame_part2]
91 expect_response = b'fake response start\r\nfake response end\r\n\r\n'
92 webSocket = compute._WebSocket(self.client_sock, self.url)
93
94 self.client_sock.recv.assert_has_calls([mock.call(4096),
95 mock.call(4096)])
96 self.assertEqual(webSocket.response, expect_response)
97 self.assertEqual(webSocket.cached_stream, frame_part1)
98
99 self.client_sock.recv.reset_mock()
100
101 recv_version = webSocket.receive_frame()
102
103 self.client_sock.recv.assert_called_once_with(len(frame_part2))
104 self.assertEqual(recv_version, RFP_VERSION)
105 # cached_stream should be empty in the end.
106 self.assertEqual(webSocket.cached_stream, b'')