Show More
@@ -1,134 +1,132 b'' | |||
|
1 | 1 | |
|
2 | 2 | # Copyright (C) 2010-2023 RhodeCode GmbH |
|
3 | 3 | # |
|
4 | 4 | # This program is free software: you can redistribute it and/or modify |
|
5 | 5 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | 6 | # (only), as published by the Free Software Foundation. |
|
7 | 7 | # |
|
8 | 8 | # This program is distributed in the hope that it will be useful, |
|
9 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | 11 | # GNU General Public License for more details. |
|
12 | 12 | # |
|
13 | 13 | # You should have received a copy of the GNU Affero General Public License |
|
14 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | 15 | # |
|
16 | 16 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | 17 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | 18 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | 19 | |
|
20 | 20 | import logging |
|
21 | 21 | |
|
22 | 22 | import mock |
|
23 | 23 | import msgpack |
|
24 | 24 | import pytest |
|
25 | 25 | |
|
26 | 26 | from rhodecode.lib import vcs |
|
27 | 27 | from rhodecode.lib.vcs import client_http, exceptions |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | def is_new_connection(logger, level, message): |
|
31 | 31 | return ( |
|
32 | 32 | logger == 'requests.packages.urllib3.connectionpool' and |
|
33 | 33 | message.startswith('Starting new HTTP')) |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | @pytest.fixture() |
|
37 | 37 | def stub_session(): |
|
38 | 38 | """ |
|
39 | 39 | Stub of `requests.Session()`. |
|
40 | 40 | """ |
|
41 | 41 | session = mock.Mock() |
|
42 | 42 | post = session.post() |
|
43 | 43 | post.content = msgpack.packb({}) |
|
44 | 44 | post.status_code = 200 |
|
45 | 45 | |
|
46 | 46 | session.reset_mock() |
|
47 | 47 | return session |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | @pytest.fixture() |
|
51 | 51 | def stub_fail_session(): |
|
52 | 52 | """ |
|
53 | 53 | Stub of `requests.Session()`. |
|
54 | 54 | """ |
|
55 | 55 | session = mock.Mock() |
|
56 | 56 | post = session.post() |
|
57 | 57 | post.content = msgpack.packb({'error': '500'}) |
|
58 | 58 | post.status_code = 500 |
|
59 | 59 | |
|
60 | 60 | session.reset_mock() |
|
61 | 61 | return session |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | @pytest.fixture() |
|
65 | 65 | def stub_session_factory(stub_session): |
|
66 | 66 | """ |
|
67 | 67 | Stub of `rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory`. |
|
68 | 68 | """ |
|
69 | 69 | session_factory = mock.Mock() |
|
70 | 70 | session_factory.return_value = stub_session |
|
71 | 71 | return session_factory |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | @pytest.fixture() |
|
75 | 75 | def stub_session_failing_factory(stub_fail_session): |
|
76 | 76 | """ |
|
77 | 77 | Stub of `rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory`. |
|
78 | 78 | """ |
|
79 | 79 | session_factory = mock.Mock() |
|
80 | 80 | session_factory.return_value = stub_fail_session |
|
81 | 81 | return session_factory |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | def test_uses_persistent_http_connections(caplog, vcsbackend_hg): |
|
85 | 85 | repo = vcsbackend_hg.repo |
|
86 | 86 | remote_call = repo._remote.branches |
|
87 | 87 | |
|
88 | 88 | with caplog.at_level(logging.INFO): |
|
89 | 89 | for x in range(5): |
|
90 | 90 | remote_call(normal=True, closed=False) |
|
91 | 91 | |
|
92 | 92 | new_connections = [ |
|
93 | 93 | r for r in caplog.record_tuples if is_new_connection(*r)] |
|
94 | 94 | assert len(new_connections) <= 1 |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | def test_repo_maker_uses_session_for_classmethods(stub_session_factory): |
|
98 | 98 | repo_maker = client_http.RemoteVCSMaker( |
|
99 | 99 | 'server_and_port', 'endpoint', 'test_dummy_scm', stub_session_factory) |
|
100 | 100 | repo_maker.example_call() |
|
101 | 101 | stub_session_factory().post.assert_called_with( |
|
102 |
'http://server_and_port/endpoint', data=mock.ANY |
|
|
103 | headers={'X-RC-Method': 'example_call', 'X-RC-Repo-Name': None}) | |
|
102 | 'http://server_and_port/endpoint', data=mock.ANY) | |
|
104 | 103 | |
|
105 | 104 | |
|
106 | 105 | def test_repo_maker_uses_session_for_instance_methods( |
|
107 | 106 | stub_session_factory, config): |
|
108 | 107 | repo_maker = client_http.RemoteVCSMaker( |
|
109 | 108 | 'server_and_port', 'endpoint', 'test_dummy_scm', stub_session_factory) |
|
110 | 109 | repo = repo_maker('stub_path', 'stub_repo_id', config) |
|
111 | 110 | repo.example_call() |
|
112 | 111 | stub_session_factory().post.assert_called_with( |
|
113 |
'http://server_and_port/endpoint', data=mock.ANY |
|
|
114 | headers={'X-RC-Method': 'example_call', 'X-RC-Repo-Name': 'stub_path'}) | |
|
112 | 'http://server_and_port/endpoint', data=mock.ANY) | |
|
115 | 113 | |
|
116 | 114 | |
|
117 | 115 | @mock.patch('rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory') |
|
118 | 116 | @mock.patch('rhodecode.lib.vcs.connection') |
|
119 | 117 | def test_connect_passes_in_the_same_session( |
|
120 | 118 | connection, session_factory_class, stub_session): |
|
121 | 119 | session_factory = session_factory_class.return_value |
|
122 | 120 | session_factory.return_value = stub_session |
|
123 | 121 | |
|
124 | 122 | vcs.connect_http('server_and_port') |
|
125 | 123 | |
|
126 | 124 | |
|
127 | 125 | def test_repo_maker_uses_session_that_throws_error( |
|
128 | 126 | stub_session_failing_factory, config): |
|
129 | 127 | repo_maker = client_http.RemoteVCSMaker( |
|
130 | 128 | 'server_and_port', 'endpoint', 'test_dummy_scm', stub_session_failing_factory) |
|
131 | 129 | repo = repo_maker('stub_path', 'stub_repo_id', config) |
|
132 | 130 | |
|
133 | 131 | with pytest.raises(exceptions.HttpVCSCommunicationError): |
|
134 | 132 | repo.example_call() |
General Comments 0
You need to be logged in to leave comments.
Login now