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