Show More
@@ -0,0 +1,135 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2016-2016 RhodeCode GmbH | |||
|
4 | # | |||
|
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 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | import pytest | |||
|
22 | ||||
|
23 | from mock import patch | |||
|
24 | ||||
|
25 | from rhodecode.lib.vcs.backends.base import Reference | |||
|
26 | ||||
|
27 | ||||
|
28 | class TestMercurialRemoteRepoInvalidation(object): | |||
|
29 | ref_stub = Reference('branch', 'default', None) | |||
|
30 | ||||
|
31 | @pytest.mark.parametrize('method_name', [ | |||
|
32 | 'bookmark', | |||
|
33 | 'commit', | |||
|
34 | 'pull', | |||
|
35 | 'pull_cmd', | |||
|
36 | 'push', | |||
|
37 | 'rebase', | |||
|
38 | 'strip', | |||
|
39 | 'strip', | |||
|
40 | ]) | |||
|
41 | def test_method_invokes_invalidate_on_remote_repo( | |||
|
42 | self, method_name, backend_hg): | |||
|
43 | """ | |||
|
44 | Check that the listed methods call invalidate_vcs_cache on their remote | |||
|
45 | repo instance. | |||
|
46 | """ | |||
|
47 | from rhodecode.lib.vcs import client_http | |||
|
48 | repo = backend_hg.repo.scm_instance() | |||
|
49 | remote = repo._remote | |||
|
50 | with patch.object(remote, 'invalidate_vcs_cache') as invalidate_cache: | |||
|
51 | with patch.object(client_http, '_remote_call'): | |||
|
52 | method = getattr(remote, method_name) | |||
|
53 | method() | |||
|
54 | assert invalidate_cache.called | |||
|
55 | ||||
|
56 | def _prepare_shadow_repo(self, pull_request): | |||
|
57 | """ | |||
|
58 | Helper that creates a shadow repo that can be used to reproduce the | |||
|
59 | CommitDoesNotExistError when pulling in from target and source | |||
|
60 | references. | |||
|
61 | """ | |||
|
62 | from rhodecode.model.pull_request import PullRequestModel | |||
|
63 | ||||
|
64 | target_vcs = pull_request.target_repo.scm_instance() | |||
|
65 | target_ref = pull_request.target_ref_parts | |||
|
66 | source_ref = pull_request.source_ref_parts | |||
|
67 | ||||
|
68 | # Create shadow repository. | |||
|
69 | pr = PullRequestModel() | |||
|
70 | workspace_id = pr._workspace_id(pull_request) | |||
|
71 | shadow_repository_path = target_vcs._maybe_prepare_merge_workspace( | |||
|
72 | workspace_id, target_ref) | |||
|
73 | shadow_repo = target_vcs._get_shadow_instance(shadow_repository_path) | |||
|
74 | ||||
|
75 | # This will populate the cache of the mercurial repository object | |||
|
76 | # inside of the VCSServer. | |||
|
77 | shadow_repo.get_commit() | |||
|
78 | ||||
|
79 | return shadow_repo, source_ref, target_ref | |||
|
80 | ||||
|
81 | @pytest.mark.backends('hg') | |||
|
82 | def test_commit_does_not_exist_error_happens(self, pr_util): | |||
|
83 | from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError | |||
|
84 | ||||
|
85 | pull_request = pr_util.create_pull_request() | |||
|
86 | target_vcs = pull_request.target_repo.scm_instance() | |||
|
87 | source_vcs = pull_request.source_repo.scm_instance() | |||
|
88 | shadow_repo, source_ref, target_ref = self._prepare_shadow_repo( | |||
|
89 | pull_request) | |||
|
90 | ||||
|
91 | # Pull from target and source references but without invalidation of | |||
|
92 | # RemoteRepo objects and without VCSServer caching of mercurial | |||
|
93 | # repository objects. | |||
|
94 | with patch.object(shadow_repo._remote, 'invalidate_vcs_cache'): | |||
|
95 | # NOTE: Do not use patch.dict() to disable the cache because it | |||
|
96 | # restores the WHOLE dict and not only the patched keys. | |||
|
97 | shadow_repo._remote._wire['cache'] = False | |||
|
98 | shadow_repo._local_pull(target_vcs.path, target_ref) | |||
|
99 | shadow_repo._local_pull(source_vcs.path, source_ref) | |||
|
100 | shadow_repo._remote._wire.pop('cache') | |||
|
101 | ||||
|
102 | # Try to lookup the target_ref in shadow repo. This should work because | |||
|
103 | # the shadow repo is a clone of the target and always contains all off | |||
|
104 | # it's commits in the initial cache. | |||
|
105 | shadow_repo.get_commit(target_ref.commit_id) | |||
|
106 | ||||
|
107 | # If we try to lookup the source_ref it should fail because the shadow | |||
|
108 | # repo commit cache doesn't get invalidated. (Due to patched | |||
|
109 | # invalidation and caching above). | |||
|
110 | with pytest.raises(CommitDoesNotExistError): | |||
|
111 | shadow_repo.get_commit(source_ref.commit_id) | |||
|
112 | ||||
|
113 | @pytest.mark.backends('hg') | |||
|
114 | def test_commit_does_not_exist_error_does_not_happen(self, pr_util): | |||
|
115 | pull_request = pr_util.create_pull_request() | |||
|
116 | target_vcs = pull_request.target_repo.scm_instance() | |||
|
117 | source_vcs = pull_request.source_repo.scm_instance() | |||
|
118 | shadow_repo, source_ref, target_ref = self._prepare_shadow_repo( | |||
|
119 | pull_request) | |||
|
120 | ||||
|
121 | # Pull from target and source references without without VCSServer | |||
|
122 | # caching of mercurial repository objects but with active invalidation | |||
|
123 | # of RemoteRepo objects. | |||
|
124 | # NOTE: Do not use patch.dict() to disable the cache because it | |||
|
125 | # restores the WHOLE dict and not only the patched keys. | |||
|
126 | shadow_repo._remote._wire['cache'] = False | |||
|
127 | shadow_repo._local_pull(target_vcs.path, target_ref) | |||
|
128 | shadow_repo._local_pull(source_vcs.path, source_ref) | |||
|
129 | shadow_repo._remote._wire.pop('cache') | |||
|
130 | ||||
|
131 | # Try to lookup the target and source references in shadow repo. This | |||
|
132 | # should work because the RemoteRepo object gets invalidated during the | |||
|
133 | # above pull operations. | |||
|
134 | shadow_repo.get_commit(target_ref.commit_id) | |||
|
135 | shadow_repo.get_commit(source_ref.commit_id) |
General Comments 0
You need to be logged in to leave comments.
Login now