Skip to content
Snippets Groups Projects
Commit 02c16a74 authored by Georges Racinet's avatar Georges Racinet
Browse files

Gitaly Comparison tests: RpcHelper class for do_rpc pattern

This should help reducing the boilerplate in comparison tests,
while also making clear that the focus of them is the comparison,
not the actual responses. For instance, we should not need to
update expected values in the tests if Gitaly fixes a bug and we
follow suit.

We're using `test_find_ref_name()` to demonstrate.

A possible next step would be to include commit id conversions
in the `RpcHelper` class, or rather the fixture.

Closes #56
parent ee43254c
No related branches found
No related tags found
1 merge request!68more helpers for comparison tests
......@@ -29,6 +29,9 @@
def git_repo(self):
return self.gitlab_mirror.git_repo
def rpc_helper(self, **kw):
return RpcHelper(self, **kw)
@contextlib.contextmanager
def gitaly_comparison_fixture(server_repos_root,
......@@ -66,3 +69,28 @@
gitaly_repo=gitaly_repo,
gitlab_mirror=mirror,
)
class RpcHelper:
def __init__(self, comparison, stub_cls, method_name, request_cls,
repository_arg=True,
streaming=False):
self.comparison = comparison
self.stubs = dict(git=stub_cls(comparison.gitaly_channel),
hg=stub_cls(comparison.hgitaly_channel))
self.stub_cls = stub_cls
self.method_name = method_name
self.request_cls = request_cls
self.streaming = streaming
self.repository_arg = repository_arg
def rpc(self, vcs, **kwargs):
if self.repository_arg:
kwargs.setdefault('repository', self.comparison.gitaly_repo)
request = self.request_cls(**kwargs)
meth = getattr(self.stubs[vcs], self.method_name)
if self.streaming:
return [resp for resp in meth(request)]
return meth(request)
......@@ -189,8 +189,6 @@
def test_find_ref_name(gitaly_comparison):
fixture = gitaly_comparison
hgitaly_repo = fixture.hgitaly_repo
gitaly_repo = fixture.gitaly_repo
git_repo = fixture.git_repo
wrapper = fixture.hg_repo_wrapper
......@@ -212,6 +210,12 @@
parent=base_hg_ctx).hex()
other_git_sha = git_repo.branches()[gl_other]['sha']
gitaly_ref_stub = RefServiceStub(fixture.gitaly_channel)
hgitaly_ref_stub = RefServiceStub(fixture.hgitaly_channel)
hg2git = {base_hg_sha: git_sha0,
default_hg_sha: git_sha1,
other_hg_sha: other_git_sha,
}
rpc_helper = fixture.rpc_helper(stub_cls=RefServiceStub,
method_name='FindRefName',
request_cls=FindRefNameRequest)
......@@ -217,8 +221,6 @@
def do_git(commit_id, prefix):
return gitaly_ref_stub.FindRefName(
FindRefNameRequest(repository=gitaly_repo,
commit_id=commit_id,
prefix=prefix
))
def assert_compare(commit_hg_id, prefix):
if len(commit_hg_id) == 40:
# defaulting useful for tests of unknown revs
commit_git_id = hg2git.get(commit_hg_id, commit_hg_id)
......@@ -224,10 +226,7 @@
def do_hg(commit_id, prefix):
return hgitaly_ref_stub.FindRefName(
FindRefNameRequest(repository=hgitaly_repo,
commit_id=commit_id,
prefix=prefix
))
assert (rpc_helper.rpc('hg', commit_id=commit_hg_id, prefix=prefix)
==
rpc_helper.rpc('git', commit_id=commit_git_id, prefix=prefix))
# Git returns the first ref in alphabetic order, hence not branch/default
# for the base commit because 'default' < 'other'
......@@ -238,12 +237,8 @@
b'refs/heads/branch/default',
):
assert do_git(git_sha0, prefix).name == b'refs/heads/branch/default'
assert do_hg(base_hg_sha, prefix).name == b'refs/heads/branch/default'
assert do_git(git_sha1, prefix).name == b'refs/heads/branch/default'
assert do_hg(default_hg_sha, prefix).name == (b'refs/heads/'
b'branch/default')
assert_compare(base_hg_sha, prefix)
assert_compare(default_hg_sha, prefix)
for prefix in (b'refs/heads',
b'refs/heads/',
......@@ -251,4 +246,5 @@
b'refs/heads/branch/',
b'refs/heads/branch/other',
):
assert_compare(other_hg_sha, prefix)
......@@ -254,7 +250,5 @@
assert do_git(other_git_sha, prefix).name == b'refs/heads/branch/other'
assert do_hg(other_hg_sha, prefix).name == b'refs/heads/branch/other'
# cases where response should be empty
for prefix in (b'refs/heads/bra',
b'refs/heads/branch/def',
):
......@@ -258,8 +252,5 @@
for prefix in (b'refs/heads/bra',
b'refs/heads/branch/def',
):
assert not do_git(git_sha0, prefix).name
assert not do_hg(base_hg_sha, prefix).name
assert not do_git(git_sha1, prefix).name
assert not do_hg(default_hg_sha, prefix).name
assert_compare(base_hg_sha, prefix)
assert_compare(default_hg_sha, prefix)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment