##// END OF EJS Templates
tests: Adapt tests to recent changes.
Martin Bornhold -
r612:47f28cf8 default
parent child Browse files
Show More
@@ -1,130 +1,130 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import mock
21 import mock
22 import Pyro4
22 import Pyro4
23 import pytest
23 import pytest
24 import webtest
24 import webtest
25
25
26 from rhodecode.lib.middleware.utils import scm_app_http, scm_app
26 from rhodecode.lib.middleware.utils import scm_app_http, scm_app
27 from rhodecode.lib.vcs.conf import settings
27 from rhodecode.lib.vcs.conf import settings
28
28
29
29
30 def vcs_http_app(vcsserver_http_echo_app):
30 def vcs_http_app(vcsserver_http_echo_app):
31 """
31 """
32 VcsHttpProxy wrapped in WebTest.
32 VcsHttpProxy wrapped in WebTest.
33 """
33 """
34 git_url = vcsserver_http_echo_app.http_url + 'stream/git/'
34 git_url = vcsserver_http_echo_app.http_url + 'stream/git/'
35 vcs_http_proxy = scm_app_http.VcsHttpProxy(
35 vcs_http_proxy = scm_app_http.VcsHttpProxy(
36 git_url, 'stub_path', 'stub_name', None)
36 git_url, 'stub_path', 'stub_name', None, 'stub_backend')
37 app = webtest.TestApp(vcs_http_proxy)
37 app = webtest.TestApp(vcs_http_proxy)
38 return app
38 return app
39
39
40
40
41 @pytest.fixture(scope='module')
41 @pytest.fixture(scope='module')
42 def vcsserver_http_echo_app(request, vcsserver_factory):
42 def vcsserver_http_echo_app(request, vcsserver_factory):
43 """
43 """
44 A running VCSServer with the EchoApp activated via HTTP.
44 A running VCSServer with the EchoApp activated via HTTP.
45 """
45 """
46 vcsserver = vcsserver_factory(
46 vcsserver = vcsserver_factory(
47 request=request,
47 request=request,
48 use_http=True,
48 use_http=True,
49 overrides=[{'app:main': {'dev.use_echo_app': 'true'}}])
49 overrides=[{'app:main': {'dev.use_echo_app': 'true'}}])
50 return vcsserver
50 return vcsserver
51
51
52
52
53 @pytest.fixture(scope='session')
53 @pytest.fixture(scope='session')
54 def data():
54 def data():
55 one_kb = "x" * 1024
55 one_kb = "x" * 1024
56 return one_kb * 1024 * 10
56 return one_kb * 1024 * 10
57
57
58
58
59 def test_reuse_app_no_data(repeat, vcsserver_http_echo_app):
59 def test_reuse_app_no_data(repeat, vcsserver_http_echo_app):
60 app = vcs_http_app(vcsserver_http_echo_app)
60 app = vcs_http_app(vcsserver_http_echo_app)
61 for x in xrange(repeat / 10):
61 for x in xrange(repeat / 10):
62 response = app.post('/')
62 response = app.post('/')
63 assert response.status_code == 200
63 assert response.status_code == 200
64
64
65
65
66 def test_reuse_app_with_data(data, repeat, vcsserver_http_echo_app):
66 def test_reuse_app_with_data(data, repeat, vcsserver_http_echo_app):
67 app = vcs_http_app(vcsserver_http_echo_app)
67 app = vcs_http_app(vcsserver_http_echo_app)
68 for x in xrange(repeat / 10):
68 for x in xrange(repeat / 10):
69 response = app.post('/', params=data)
69 response = app.post('/', params=data)
70 assert response.status_code == 200
70 assert response.status_code == 200
71
71
72
72
73 def test_create_app_per_request_no_data(repeat, vcsserver_http_echo_app):
73 def test_create_app_per_request_no_data(repeat, vcsserver_http_echo_app):
74 for x in xrange(repeat / 10):
74 for x in xrange(repeat / 10):
75 app = vcs_http_app(vcsserver_http_echo_app)
75 app = vcs_http_app(vcsserver_http_echo_app)
76 response = app.post('/')
76 response = app.post('/')
77 assert response.status_code == 200
77 assert response.status_code == 200
78
78
79
79
80 def test_create_app_per_request_with_data(
80 def test_create_app_per_request_with_data(
81 data, repeat, vcsserver_http_echo_app):
81 data, repeat, vcsserver_http_echo_app):
82 for x in xrange(repeat / 10):
82 for x in xrange(repeat / 10):
83 app = vcs_http_app(vcsserver_http_echo_app)
83 app = vcs_http_app(vcsserver_http_echo_app)
84 response = app.post('/', params=data)
84 response = app.post('/', params=data)
85 assert response.status_code == 200
85 assert response.status_code == 200
86
86
87
87
88 @pytest.fixture(scope='module')
88 @pytest.fixture(scope='module')
89 def vcsserver_pyro_echo_app(request, vcsserver_factory):
89 def vcsserver_pyro_echo_app(request, vcsserver_factory):
90 """
90 """
91 A running VCSServer with the EchoApp activated via Pyro4.
91 A running VCSServer with the EchoApp activated via Pyro4.
92 """
92 """
93 vcsserver = vcsserver_factory(
93 vcsserver = vcsserver_factory(
94 request=request,
94 request=request,
95 use_http=False,
95 use_http=False,
96 overrides=[{'DEFAULT': {'dev.use_echo_app': 'true'}}])
96 overrides=[{'DEFAULT': {'dev.use_echo_app': 'true'}}])
97 return vcsserver
97 return vcsserver
98
98
99
99
100 def vcs_pyro4_app(vcsserver_pyro_echo_app):
100 def vcs_pyro4_app(vcsserver_pyro_echo_app):
101 """
101 """
102 Pyro4 based Vcs proxy wrapped in WebTest
102 Pyro4 based Vcs proxy wrapped in WebTest
103 """
103 """
104 stub_config = {
104 stub_config = {
105 'git_update_server_info': 'stub',
105 'git_update_server_info': 'stub',
106 }
106 }
107 server_and_port = vcsserver_pyro_echo_app.server_and_port
107 server_and_port = vcsserver_pyro_echo_app.server_and_port
108 GIT_REMOTE_WSGI = Pyro4.Proxy(
108 GIT_REMOTE_WSGI = Pyro4.Proxy(
109 settings.pyro_remote(
109 settings.pyro_remote(
110 settings.PYRO_GIT_REMOTE_WSGI, server_and_port))
110 settings.PYRO_GIT_REMOTE_WSGI, server_and_port))
111 with mock.patch('rhodecode.lib.middleware.utils.scm_app.GIT_REMOTE_WSGI',
111 with mock.patch('rhodecode.lib.middleware.utils.scm_app.GIT_REMOTE_WSGI',
112 GIT_REMOTE_WSGI):
112 GIT_REMOTE_WSGI):
113 pyro4_app = scm_app.create_git_wsgi_app(
113 pyro4_app = scm_app.create_git_wsgi_app(
114 'stub_path', 'stub_name', stub_config)
114 'stub_path', 'stub_name', stub_config)
115 app = webtest.TestApp(pyro4_app)
115 app = webtest.TestApp(pyro4_app)
116 return app
116 return app
117
117
118
118
119 def test_pyro4_no_data(repeat, pylonsapp, vcsserver_pyro_echo_app):
119 def test_pyro4_no_data(repeat, pylonsapp, vcsserver_pyro_echo_app):
120 for x in xrange(repeat / 10):
120 for x in xrange(repeat / 10):
121 app = vcs_pyro4_app(vcsserver_pyro_echo_app)
121 app = vcs_pyro4_app(vcsserver_pyro_echo_app)
122 response = app.post('/')
122 response = app.post('/')
123 assert response.status_code == 200
123 assert response.status_code == 200
124
124
125
125
126 def test_pyro4_with_data(repeat, pylonsapp, vcsserver_pyro_echo_app, data):
126 def test_pyro4_with_data(repeat, pylonsapp, vcsserver_pyro_echo_app, data):
127 for x in xrange(repeat / 10):
127 for x in xrange(repeat / 10):
128 app = vcs_pyro4_app(vcsserver_pyro_echo_app)
128 app = vcs_pyro4_app(vcsserver_pyro_echo_app)
129 response = app.post('/', params=data)
129 response = app.post('/', params=data)
130 assert response.status_code == 200
130 assert response.status_code == 200
@@ -1,136 +1,136 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 """
21 """
22 Checking the chunked data transfer via HTTP
22 Checking the chunked data transfer via HTTP
23 """
23 """
24
24
25 import os
25 import os
26 import time
26 import time
27 import subprocess
27 import subprocess
28
28
29 import pytest
29 import pytest
30 import requests
30 import requests
31
31
32 from rhodecode.lib.middleware.utils import scm_app_http
32 from rhodecode.lib.middleware.utils import scm_app_http
33 from rhodecode.tests.utils import wait_for_url
33 from rhodecode.tests.utils import wait_for_url
34
34
35
35
36 def test_does_chunked_end_to_end_transfer(scm_app):
36 def test_does_chunked_end_to_end_transfer(scm_app):
37 response = requests.post(scm_app, data='', stream=True)
37 response = requests.post(scm_app, data='', stream=True)
38 assert response.headers['Transfer-Encoding'] == 'chunked'
38 assert response.headers['Transfer-Encoding'] == 'chunked'
39 times = [time.time() for chunk in response.raw.read_chunked()]
39 times = [time.time() for chunk in response.raw.read_chunked()]
40 assert times[1] - times[0] > 0.1, "Chunks arrived at the same time"
40 assert times[1] - times[0] > 0.1, "Chunks arrived at the same time"
41
41
42
42
43 @pytest.fixture
43 @pytest.fixture
44 def echo_app_chunking(request, available_port_factory):
44 def echo_app_chunking(request, available_port_factory):
45 """
45 """
46 Run the EchoApp via Waitress in a subprocess.
46 Run the EchoApp via Waitress in a subprocess.
47
47
48 Return the URL endpoint to reach the app.
48 Return the URL endpoint to reach the app.
49 """
49 """
50 port = available_port_factory()
50 port = available_port_factory()
51 command = (
51 command = (
52 'waitress-serve --send-bytes 1 --port {port} --call '
52 'waitress-serve --send-bytes 1 --port {port} --call '
53 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
53 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
54 ':create_echo_app')
54 ':create_echo_app')
55 command = command.format(port=port)
55 command = command.format(port=port)
56 proc = subprocess.Popen(command.split(' '), bufsize=0)
56 proc = subprocess.Popen(command.split(' '), bufsize=0)
57 echo_app_url = 'http://localhost:' + str(port)
57 echo_app_url = 'http://localhost:' + str(port)
58
58
59 @request.addfinalizer
59 @request.addfinalizer
60 def stop_echo_app():
60 def stop_echo_app():
61 proc.kill()
61 proc.kill()
62
62
63 return echo_app_url
63 return echo_app_url
64
64
65
65
66 @pytest.fixture
66 @pytest.fixture
67 def scm_app(request, available_port_factory, echo_app_chunking):
67 def scm_app(request, available_port_factory, echo_app_chunking):
68 """
68 """
69 Run the scm_app in Waitress.
69 Run the scm_app in Waitress.
70
70
71 Returns the URL endpoint where this app can be reached.
71 Returns the URL endpoint where this app can be reached.
72 """
72 """
73 port = available_port_factory()
73 port = available_port_factory()
74 command = (
74 command = (
75 'waitress-serve --send-bytes 1 --port {port} --call '
75 'waitress-serve --send-bytes 1 --port {port} --call '
76 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
76 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
77 ':create_scm_app')
77 ':create_scm_app')
78 command = command.format(port=port)
78 command = command.format(port=port)
79 env = os.environ.copy()
79 env = os.environ.copy()
80 env["RC_ECHO_URL"] = echo_app_chunking
80 env["RC_ECHO_URL"] = echo_app_chunking
81 proc = subprocess.Popen(command.split(' '), bufsize=0, env=env)
81 proc = subprocess.Popen(command.split(' '), bufsize=0, env=env)
82 scm_app_url = 'http://localhost:' + str(port)
82 scm_app_url = 'http://localhost:' + str(port)
83 wait_for_url(scm_app_url)
83 wait_for_url(scm_app_url)
84
84
85 @request.addfinalizer
85 @request.addfinalizer
86 def stop_echo_app():
86 def stop_echo_app():
87 proc.kill()
87 proc.kill()
88
88
89 return scm_app_url
89 return scm_app_url
90
90
91
91
92 class EchoApp(object):
92 class EchoApp(object):
93 """
93 """
94 Stub WSGI application which returns a chunked response to every request.
94 Stub WSGI application which returns a chunked response to every request.
95 """
95 """
96
96
97 def __init__(self, repo_path, repo_name, config):
97 def __init__(self, repo_path, repo_name, config):
98 self._repo_path = repo_path
98 self._repo_path = repo_path
99
99
100 def __call__(self, environ, start_response):
100 def __call__(self, environ, start_response):
101 environ['wsgi.input'].read()
101 environ['wsgi.input'].read()
102 status = '200 OK'
102 status = '200 OK'
103 headers = []
103 headers = []
104 start_response(status, headers)
104 start_response(status, headers)
105 return result_generator()
105 return result_generator()
106
106
107
107
108 def result_generator():
108 def result_generator():
109 """
109 """
110 Simulate chunked results.
110 Simulate chunked results.
111
111
112 The intended usage is to simulate a chunked response as we would get it
112 The intended usage is to simulate a chunked response as we would get it
113 out of a vcs operation during a call to "hg clone".
113 out of a vcs operation during a call to "hg clone".
114 """
114 """
115 yield 'waiting 2 seconds'
115 yield 'waiting 2 seconds'
116 # Wait long enough so that the first chunk can go out
116 # Wait long enough so that the first chunk can go out
117 time.sleep(2)
117 time.sleep(2)
118 yield 'final chunk'
118 yield 'final chunk'
119 # Another small wait, otherwise they go together
119 # Another small wait, otherwise they go together
120 time.sleep(0.1)
120 time.sleep(0.1)
121
121
122
122
123 def create_echo_app():
123 def create_echo_app():
124 """
124 """
125 Create EchoApp filled with stub data.
125 Create EchoApp filled with stub data.
126 """
126 """
127 return EchoApp('stub_path', 'repo_name', {})
127 return EchoApp('stub_path', 'repo_name', {})
128
128
129
129
130 def create_scm_app():
130 def create_scm_app():
131 """
131 """
132 Create a scm_app hooked up to speak to EchoApp.
132 Create a scm_app hooked up to speak to EchoApp.
133 """
133 """
134 echo_app_url = os.environ["RC_ECHO_URL"]
134 echo_app_url = os.environ["RC_ECHO_URL"]
135 return scm_app_http.VcsHttpProxy(
135 return scm_app_http.VcsHttpProxy(
136 echo_app_url, 'stub_path', 'stub_name', None)
136 echo_app_url, 'stub_path', 'stub_name', None, 'stub_backend')
General Comments 0
You need to be logged in to leave comments. Login now