##// END OF EJS Templates
tests: fixed some flacky tests on docker.
milka -
r4654:930a1cf8 default
parent child Browse files
Show More
@@ -1,152 +1,156 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2020 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import json
22 import os
23
22 24 import mock
23 25 import pytest
24 26
25 27 from rhodecode.apps.ssh_support.lib.backends.git import GitServer
26 28 from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user
27 29
28 30
29 31 class GitServerCreator(object):
30 32 root = '/tmp/repo/path/'
31 33 git_path = '/usr/local/bin/git'
32 34 config_data = {
33 35 'app:main': {
34 36 'ssh.executable.git': git_path,
35 37 'vcs.hooks.protocol': 'http',
36 38 }
37 39 }
38 40 repo_name = 'test_git'
39 41 repo_mode = 'receive-pack'
40 42 user = plain_dummy_user()
41 43
42 44 def __init__(self):
43 45 def config_get(part, key):
44 46 return self.config_data.get(part, {}).get(key)
45 47 self.config_mock = mock.Mock()
46 48 self.config_mock.get = mock.Mock(side_effect=config_get)
47 49
48 50 def create(self, **kwargs):
49 51 parameters = {
50 52 'store': self.root,
51 53 'ini_path': '',
52 54 'user': self.user,
53 55 'repo_name': self.repo_name,
54 56 'repo_mode': self.repo_mode,
55 57 'user_permissions': {
56 58 self.repo_name: 'repository.admin'
57 59 },
58 60 'config': self.config_mock,
59 61 'env': plain_dummy_env()
60 62 }
61 63 parameters.update(kwargs)
62 64 server = GitServer(**parameters)
63 65 return server
64 66
65 67
66 68 @pytest.fixture()
67 69 def git_server(app):
68 70 return GitServerCreator()
69 71
70 72
71 73 class TestGitServer(object):
72 74
73 75 def test_command(self, git_server):
74 76 server = git_server.create()
75 77 expected_command = (
76 78 'cd {root}; {git_path} {repo_mode} \'{root}{repo_name}\''.format(
77 79 root=git_server.root, git_path=git_server.git_path,
78 80 repo_mode=git_server.repo_mode, repo_name=git_server.repo_name)
79 81 )
80 82 assert expected_command == server.tunnel.command()
81 83
82 84 @pytest.mark.parametrize('permissions, action, code', [
83 85 ({}, 'pull', -2),
84 86 ({'test_git': 'repository.read'}, 'pull', 0),
85 87 ({'test_git': 'repository.read'}, 'push', -2),
86 88 ({'test_git': 'repository.write'}, 'push', 0),
87 89 ({'test_git': 'repository.admin'}, 'push', 0),
88 90
89 91 ])
90 92 def test_permission_checks(self, git_server, permissions, action, code):
91 93 server = git_server.create(user_permissions=permissions)
92 94 result = server._check_permissions(action)
93 95 assert result is code
94 96
95 97 @pytest.mark.parametrize('permissions, value', [
96 98 ({}, False),
97 99 ({'test_git': 'repository.read'}, False),
98 100 ({'test_git': 'repository.write'}, True),
99 101 ({'test_git': 'repository.admin'}, True),
100 102
101 103 ])
102 104 def test_has_write_permissions(self, git_server, permissions, value):
103 105 server = git_server.create(user_permissions=permissions)
104 106 result = server.has_write_perm()
105 107 assert result is value
106 108
107 109 def test_run_returns_executes_command(self, git_server):
108 110 server = git_server.create()
109 111 from rhodecode.apps.ssh_support.lib.backends.git import GitTunnelWrapper
112
113 os.environ['SSH_CLIENT'] = '127.0.0.1'
110 114 with mock.patch.object(GitTunnelWrapper, 'create_hooks_env') as _patch:
111 115 _patch.return_value = 0
112 116 with mock.patch.object(GitTunnelWrapper, 'command', return_value='date'):
113 117 exit_code = server.run()
114 118
115 119 assert exit_code == (0, False)
116 120
117 121 @pytest.mark.parametrize(
118 122 'repo_mode, action', [
119 123 ['receive-pack', 'push'],
120 124 ['upload-pack', 'pull']
121 125 ])
122 126 def test_update_environment(self, git_server, repo_mode, action):
123 127 server = git_server.create(repo_mode=repo_mode)
124 128 store = server.store
125 129
126 130 with mock.patch('os.environ', {'SSH_CLIENT': '10.10.10.10 b'}):
127 131 with mock.patch('os.putenv') as putenv_mock:
128 132 server.update_environment(action)
129 133
130 134 expected_data = {
131 135 'username': git_server.user.username,
132 136 'user_id': git_server.user.user_id,
133 137 'scm': 'git',
134 138 'repository': git_server.repo_name,
135 139 'make_lock': None,
136 140 'action': action,
137 141 'ip': '10.10.10.10',
138 142 'locked_by': [None, None],
139 143 'config': '',
140 144 'repo_store': store,
141 145 'server_url': None,
142 146 'hooks': ['push', 'pull'],
143 147 'is_shadow_repo': False,
144 148 'hooks_module': 'rhodecode.lib.hooks_daemon',
145 149 'check_branch_perms': False,
146 150 'detect_force_push': False,
147 151 'user_agent': u'ssh-user-agent',
148 152 'SSH': True,
149 153 'SSH_PERMISSIONS': 'repository.admin',
150 154 }
151 155 args, kwargs = putenv_mock.call_args
152 156 assert json.loads(args[1]) == expected_data
@@ -1,119 +1,120 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2020 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import os
22 22 import mock
23 23 import pytest
24 24
25 25 from rhodecode.apps.ssh_support.lib.backends.hg import MercurialServer
26 26 from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user
27 27
28 28
29 29 class MercurialServerCreator(object):
30 30 root = '/tmp/repo/path/'
31 31 hg_path = '/usr/local/bin/hg'
32 32
33 33 config_data = {
34 34 'app:main': {
35 35 'ssh.executable.hg': hg_path,
36 36 'vcs.hooks.protocol': 'http',
37 37 }
38 38 }
39 39 repo_name = 'test_hg'
40 40 user = plain_dummy_user()
41 41
42 42 def __init__(self):
43 43 def config_get(part, key):
44 44 return self.config_data.get(part, {}).get(key)
45 45 self.config_mock = mock.Mock()
46 46 self.config_mock.get = mock.Mock(side_effect=config_get)
47 47
48 48 def create(self, **kwargs):
49 49 parameters = {
50 50 'store': self.root,
51 51 'ini_path': '',
52 52 'user': self.user,
53 53 'repo_name': self.repo_name,
54 54 'user_permissions': {
55 55 'test_hg': 'repository.admin'
56 56 },
57 57 'config': self.config_mock,
58 58 'env': plain_dummy_env()
59 59 }
60 60 parameters.update(kwargs)
61 61 server = MercurialServer(**parameters)
62 62 return server
63 63
64 64
65 65 @pytest.fixture()
66 66 def hg_server(app):
67 67 return MercurialServerCreator()
68 68
69 69
70 70 class TestMercurialServer(object):
71 71
72 72 def test_command(self, hg_server, tmpdir):
73 73 server = hg_server.create()
74 74 custom_hgrc = os.path.join(str(tmpdir), 'hgrc')
75 75 expected_command = (
76 76 'cd {root}; HGRCPATH={custom_hgrc} {hg_path} -R {root}{repo_name} serve --stdio'.format(
77 77 root=hg_server.root, custom_hgrc=custom_hgrc, hg_path=hg_server.hg_path,
78 78 repo_name=hg_server.repo_name)
79 79 )
80 80 server_command = server.tunnel.command(custom_hgrc)
81 81 assert expected_command == server_command
82 82
83 83 @pytest.mark.parametrize('permissions, action, code', [
84 84 ({}, 'pull', -2),
85 85 ({'test_hg': 'repository.read'}, 'pull', 0),
86 86 ({'test_hg': 'repository.read'}, 'push', -2),
87 87 ({'test_hg': 'repository.write'}, 'push', 0),
88 88 ({'test_hg': 'repository.admin'}, 'push', 0),
89 89
90 90 ])
91 91 def test_permission_checks(self, hg_server, permissions, action, code):
92 92 server = hg_server.create(user_permissions=permissions)
93 93 result = server._check_permissions(action)
94 94 assert result is code
95 95
96 96 @pytest.mark.parametrize('permissions, value', [
97 97 ({}, False),
98 98 ({'test_hg': 'repository.read'}, False),
99 99 ({'test_hg': 'repository.write'}, True),
100 100 ({'test_hg': 'repository.admin'}, True),
101 101
102 102 ])
103 103 def test_has_write_permissions(self, hg_server, permissions, value):
104 104 server = hg_server.create(user_permissions=permissions)
105 105 result = server.has_write_perm()
106 106 assert result is value
107 107
108 108 def test_run_returns_executes_command(self, hg_server):
109 109 server = hg_server.create()
110 110 from rhodecode.apps.ssh_support.lib.backends.hg import MercurialTunnelWrapper
111 os.environ['SSH_CLIENT'] = '127.0.0.1'
111 112 with mock.patch.object(MercurialTunnelWrapper, 'create_hooks_env') as _patch:
112 113 _patch.return_value = 0
113 114 with mock.patch.object(MercurialTunnelWrapper, 'command', return_value='date'):
114 115 exit_code = server.run()
115 116
116 117 assert exit_code == (0, False)
117 118
118 119
119 120
@@ -1,206 +1,207 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2020 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20 import os
21 21 import mock
22 22 import pytest
23 23
24 24 from rhodecode.apps.ssh_support.lib.backends.svn import SubversionServer
25 25 from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user
26 26
27 27
28 28 class SubversionServerCreator(object):
29 29 root = '/tmp/repo/path/'
30 30 svn_path = '/usr/local/bin/svnserve'
31 31 config_data = {
32 32 'app:main': {
33 33 'ssh.executable.svn': svn_path,
34 34 'vcs.hooks.protocol': 'http',
35 35 }
36 36 }
37 37 repo_name = 'test-svn'
38 38 user = plain_dummy_user()
39 39
40 40 def __init__(self):
41 41 def config_get(part, key):
42 42 return self.config_data.get(part, {}).get(key)
43 43 self.config_mock = mock.Mock()
44 44 self.config_mock.get = mock.Mock(side_effect=config_get)
45 45
46 46 def create(self, **kwargs):
47 47 parameters = {
48 48 'store': self.root,
49 49 'repo_name': self.repo_name,
50 50 'ini_path': '',
51 51 'user': self.user,
52 52 'user_permissions': {
53 53 self.repo_name: 'repository.admin'
54 54 },
55 55 'config': self.config_mock,
56 56 'env': plain_dummy_env()
57 57 }
58 58
59 59 parameters.update(kwargs)
60 60 server = SubversionServer(**parameters)
61 61 return server
62 62
63 63
64 64 @pytest.fixture()
65 65 def svn_server(app):
66 66 return SubversionServerCreator()
67 67
68 68
69 69 class TestSubversionServer(object):
70 70 def test_command(self, svn_server):
71 71 server = svn_server.create()
72 72 expected_command = [
73 73 svn_server.svn_path, '-t',
74 74 '--config-file', server.tunnel.svn_conf_path,
75 75 '--tunnel-user', svn_server.user.username,
76 76 '-r', svn_server.root
77 77 ]
78 78
79 79 assert expected_command == server.tunnel.command()
80 80
81 81 @pytest.mark.parametrize('permissions, action, code', [
82 82 ({}, 'pull', -2),
83 83 ({'test-svn': 'repository.read'}, 'pull', 0),
84 84 ({'test-svn': 'repository.read'}, 'push', -2),
85 85 ({'test-svn': 'repository.write'}, 'push', 0),
86 86 ({'test-svn': 'repository.admin'}, 'push', 0),
87 87
88 88 ])
89 89 def test_permission_checks(self, svn_server, permissions, action, code):
90 90 server = svn_server.create(user_permissions=permissions)
91 91 result = server._check_permissions(action)
92 92 assert result is code
93 93
94 94 @pytest.mark.parametrize('permissions, access_paths, expected_match', [
95 95 # not matched repository name
96 96 ({
97 97 'test-svn': ''
98 98 }, ['test-svn-1', 'test-svn-1/subpath'],
99 99 None),
100 100
101 101 # exact match
102 102 ({
103 103 'test-svn': ''
104 104 },
105 105 ['test-svn'],
106 106 'test-svn'),
107 107
108 108 # subdir commits
109 109 ({
110 110 'test-svn': ''
111 111 },
112 112 ['test-svn/foo',
113 113 'test-svn/foo/test-svn',
114 114 'test-svn/trunk/development.txt',
115 115 ],
116 116 'test-svn'),
117 117
118 118 # subgroups + similar patterns
119 119 ({
120 120 'test-svn': '',
121 121 'test-svn-1': '',
122 122 'test-svn-subgroup/test-svn': '',
123 123
124 124 },
125 125 ['test-svn-1',
126 126 'test-svn-1/foo/test-svn',
127 127 'test-svn-1/test-svn',
128 128 ],
129 129 'test-svn-1'),
130 130
131 131 # subgroups + similar patterns
132 132 ({
133 133 'test-svn-1': '',
134 134 'test-svn-10': '',
135 135 'test-svn-100': '',
136 136 },
137 137 ['test-svn-10',
138 138 'test-svn-10/foo/test-svn',
139 139 'test-svn-10/test-svn',
140 140 ],
141 141 'test-svn-10'),
142 142
143 143 # subgroups + similar patterns
144 144 ({
145 145 'name': '',
146 146 'nameContains': '',
147 147 'nameContainsThis': '',
148 148 },
149 149 ['nameContains',
150 150 'nameContains/This',
151 151 'nameContains/This/test-svn',
152 152 ],
153 153 'nameContains'),
154 154
155 155 # subgroups + similar patterns
156 156 ({
157 157 'test-svn': '',
158 158 'test-svn-1': '',
159 159 'test-svn-subgroup/test-svn': '',
160 160
161 161 },
162 162 ['test-svn-subgroup/test-svn',
163 163 'test-svn-subgroup/test-svn/foo/test-svn',
164 164 'test-svn-subgroup/test-svn/trunk/example.txt',
165 165 ],
166 166 'test-svn-subgroup/test-svn'),
167 167 ])
168 168 def test_repo_extraction_on_subdir(self, svn_server, permissions, access_paths, expected_match):
169 169 server = svn_server.create(user_permissions=permissions)
170 170 for path in access_paths:
171 171 repo_name = server.tunnel._match_repo_name(path)
172 172 assert repo_name == expected_match
173 173
174 174 def test_run_returns_executes_command(self, svn_server):
175 175 server = svn_server.create()
176 176 from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper
177 os.environ['SSH_CLIENT'] = '127.0.0.1'
177 178 with mock.patch.object(
178 179 SubversionTunnelWrapper, 'get_first_client_response',
179 180 return_value={'url': 'http://server/test-svn'}):
180 181 with mock.patch.object(
181 182 SubversionTunnelWrapper, 'patch_first_client_response',
182 183 return_value=0):
183 184 with mock.patch.object(
184 185 SubversionTunnelWrapper, 'sync',
185 186 return_value=0):
186 187 with mock.patch.object(
187 188 SubversionTunnelWrapper, 'command',
188 189 return_value=['date']):
189 190
190 191 exit_code = server.run()
191 192 # SVN has this differently configured, and we get in our mock env
192 193 # None as return code
193 194 assert exit_code == (None, False)
194 195
195 196 def test_run_returns_executes_command_that_cannot_extract_repo_name(self, svn_server):
196 197 server = svn_server.create()
197 198 from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper
198 199 with mock.patch.object(
199 200 SubversionTunnelWrapper, 'command',
200 201 return_value=['date']):
201 202 with mock.patch.object(
202 203 SubversionTunnelWrapper, 'get_first_client_response',
203 204 return_value=None):
204 205 exit_code = server.run()
205 206
206 207 assert exit_code == (1, False)
General Comments 0
You need to be logged in to leave comments. Login now