Implement integration tests.

Provide intergation testing for the git-review utility. Requires
Gerrit war file (like 2.6.1-gerrit.war) in the .gerrit directory in
the git-review project (like git-review/.gerrit/2.6.1-gerrit.war).

Tests start local Gerrit and create test user and project there to
be used.

Closes-Bug: 1048724
Change-Id: I3242479dcbcf230085178004540992680f3f8e30
This commit is contained in:
Dina Belova 2013-07-01 12:49:24 +04:00 committed by Jeremy Stanley
parent d73c473b19
commit e99aa8b16d
8 changed files with 493 additions and 1 deletions

2
.gitignore vendored
View File

@ -4,6 +4,8 @@ git_review.egg-info
MANIFEST
AUTHORS
ChangeLog
.gerrit
.testrepository
.tox
.venv
*.egg

8
.testr.conf Normal file
View File

@ -0,0 +1,8 @@
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
${PYTHON:-python} -m subunit.run discover -t ./ ./git_review/tests $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

View File

@ -127,6 +127,29 @@ Install with pip install git-review
For installation from source simply add git-review to your $PATH
Running tests
-------------
Running tests for git-review means running a local copy of Gerrit to
check that git-review interacts correctly with it. This requires the
following
:
* a Java Runtime Environment on the machine to run tests on
* Internet access to download the gerrit.war file, or a locally
cached copy (it needs to be located in a .gerrit directory at the
top level of the git-review project)
To run git-review integration tests the following commands may by run::
tox -e py27
tox -e py26
tox -e py32
tox -e py33
depending on what Python interpreter would you like to use.
Contributing
------------

View File

@ -0,0 +1,206 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import shutil
import stat
import sys
if sys.version < '3':
import urllib
urlopen = urllib.urlopen
else:
import urllib.request
urlopen = urllib.request.urlopen
import testtools
from git_review.tests import utils
class GerritHelpers(object):
def _dir(self, base, *args):
"""Creates directory name from base name and other parameters."""
return os.path.join(getattr(self, base + '_dir'), *args)
def init_dirs(self):
self.primary_dir = os.path.abspath(os.path.curdir)
self.gerrit_dir = self._dir('primary', '.gerrit')
self.gsite_dir = self._dir('gerrit', 'golden_site')
def ensure_gerrit_war(self):
# check if gerrit.war file exists in .gerrit directory
if not os.path.exists(self.gerrit_dir):
os.mkdir(self.gerrit_dir)
if not os.path.exists(self._dir('gerrit', 'gerrit.war')):
resp = urlopen(
'http://gerrit-releases.storage.googleapis.com/'
'gerrit-2.6.1.war'
)
utils.write_to_file(self._dir('gerrit', 'gerrit.war'),
resp.read())
def init_gerrit(self):
"""Run Gerrit from the war file and configure it."""
if os.path.exists(self.gsite_dir):
return
# initialize Gerrit
utils.run_cmd('java', '-jar', self._dir('gerrit', 'gerrit.war'),
'init', '-d', self.gsite_dir,
'--batch', '--no-auto-start')
# create SSH public key
key_file = self._dir('gsite', 'test_ssh_key')
utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096',
'-f', key_file, '-N', '')
with open(key_file + '.pub', 'rb') as pub_key_file:
pub_key = pub_key_file.read()
# create admin user in Gerrit database
sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW());
INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \
VALUES (0, 1);
INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) \
VALUES (0, 'username:test_user');
INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \
VALUES ('%s', 'Y')""" % pub_key.decode()
utils.run_cmd('java', '-jar',
self._dir('gsite', 'bin', 'gerrit.war'),
'gsql', '-d', self.gsite_dir, '-c', sql_query)
def _run_gerrit_cli(self, command, *args):
"""SSH to gerrit Gerrit server and run command there."""
return utils.run_cmd('ssh', '-p', str(self.gerrit_port),
'test_user@localhost', 'gerrit', command, *args)
def _run_git_review(self, *args, **kwargs):
"""Run git-review utility from source."""
git_review = utils.run_cmd('which', 'git-review')
return utils.run_cmd(git_review, *args,
chdir=self.test_dir, **kwargs)
class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
"""Base class for the git-review tests."""
def setUp(self):
"""Configure testing environment.
Prepare directory for the testing and clone test Git repository.
Require Gerrit war file in the .gerrit directory to run Gerrit local.
"""
super(BaseGitReviewTestCase, self).setUp()
self.init_dirs()
for i in range(11):
if i == 10:
raise Exception("Failed to select free port for Gerrit")
self.gerrit_port = random.randint(20000, 21000)
self.site_dir = self._dir('gerrit', 'site-%04x' % self.gerrit_port)
if not os.path.exists(self.site_dir):
break
self.test_dir = self._dir('site', 'tmp', 'test_project')
self.ssh_dir = self._dir('site', 'tmp', 'ssh')
self.project_uri = 'ssh://test_user@localhost:%s/' \
'test/test_project.git' % self.gerrit_port
self._run_gerrit()
self._configure_ssh()
# create Gerrit empty project
self._run_gerrit_cli('create-project', '--empty-commit',
'--name', 'test/test_project')
# prepare repository for the testing
self._run_git('clone', self.project_uri)
utils.write_to_file(self._dir('test', 'test_file.txt'),
'test file created'.encode())
cfg = ('[gerrit]\n'
'host=localhost\n'
'port=%s\n'
'project=test/test_project.git' % self.gerrit_port)
utils.write_to_file(self._dir('test', '.gitreview'), cfg.encode())
# push changes to the Gerrit
self._run_git('add', '--all')
self._run_git('commit', '-m', 'Test file and .gitreview added.')
self._run_git('push', 'origin', 'master')
shutil.rmtree(self.test_dir)
# go to the just cloned test Git repository
self._run_git('clone', self.project_uri)
self._run_git('remote', 'add', 'gerrit', self.project_uri)
self.addCleanup(shutil.rmtree, self.test_dir)
def _run_git(self, command, *args):
"""Run git command using test git directory."""
if command == 'clone':
return utils.run_git(command, args[0], self._dir('test'))
return utils.run_git('--git-dir=' + self._dir('test', '.git'),
'--work-tree=' + self._dir('test'),
command, *args)
def _run_gerrit(self):
# create a copy of site dir
shutil.copytree(self.gsite_dir, self.site_dir)
self.addCleanup(shutil.rmtree, self.site_dir)
# write config
with open(self._dir('site', 'etc', 'gerrit.config'), 'w') as _conf:
new_conf = utils.get_gerrit_conf(self.gerrit_port,
self.gerrit_port + 1000)
_conf.write(new_conf)
# start Gerrit
gerrit_sh = self._dir('site', 'bin', 'gerrit.sh')
utils.run_cmd(gerrit_sh, 'start')
self.addCleanup(utils.run_cmd, gerrit_sh, 'stop')
def _simple_change(self, change_text, commit_message,
file_=None):
"""Helper method to create small changes and commit them."""
if file_ is None:
file_ = self._dir('test', 'test_file.txt')
utils.write_to_file(file_, change_text.encode())
self._run_git('add', file_)
self._run_git('commit', '-m', commit_message)
def _configure_ssh(self):
"""Setup ssh and scp to run with special options."""
os.mkdir(self.ssh_dir)
ssh_key = utils.run_cmd('ssh-keyscan', '-p', str(self.gerrit_port),
'localhost')
utils.write_to_file(self._dir('ssh', 'known_hosts'), ssh_key.encode())
self.addCleanup(os.remove, self._dir('ssh', 'known_hosts'))
for cmd in ('ssh', 'scp'):
cmd_file = self._dir('ssh', cmd)
s = '#!/bin/sh\n' \
'/usr/bin/%s -i %s -o UserKnownHostsFile=%s $@' % \
(cmd,
self._dir('gsite', 'test_ssh_key'),
self._dir('ssh', 'known_hosts'))
utils.write_to_file(cmd_file, s.encode())
os.chmod(cmd_file, os.stat(cmd_file).st_mode | stat.S_IEXEC)
os.environ['PATH'] = self.ssh_dir + os.pathsep + os.environ['PATH']
os.environ['GIT_SSH'] = self._dir('ssh', 'ssh')

View File

@ -0,0 +1,26 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from git_review import tests
def main():
helpers = tests.GerritHelpers()
helpers.init_dirs()
helpers.ensure_gerrit_war()
helpers.init_gerrit()
if __name__ == "__main__":
main()

View File

@ -0,0 +1,146 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import shutil
from git_review import tests
class GitReviewTestCase(tests.BaseGitReviewTestCase):
"""Class for the git-review tests."""
def test_cloned_repo(self):
"""Test git-review on the just cloned repository."""
self._simple_change('test file modified', 'test commit message')
self.assertNotIn('Change-Id:', self._run_git('log', '-1'))
self.assertIn('remote: New Changes:', self._run_git_review())
self.assertIn('Change-Id:', self._run_git('log', '-1'))
def test_git_review_s(self):
"""Test git-review -s."""
self._run_git_review('-s')
self._simple_change('test file modified', 'test commit message')
self.assertIn('Change-Id:', self._run_git('log', '-1'))
def test_git_review_d(self):
"""Test git-review -d."""
self._run_git_review('-s')
# create new review to be downloaded
self._simple_change('test file modified', 'test commit message')
self._run_git_review()
change_id = self._run_git('log', '-1').split()[-1]
shutil.rmtree(self.test_dir)
# download clean Git repository and fresh change from Gerrit to it
self._run_git('clone', self.project_uri)
self._run_git('remote', 'add', 'gerrit', self.project_uri)
self._run_git_review('-d', change_id)
self.assertIn('test commit message', self._run_git('log', '-1'))
# second download should also work correct
self._run_git_review('-d', change_id)
self.assertIn('test commit message', self._run_git('show', 'HEAD'))
self.assertNotIn('test commit message',
self._run_git('show', 'HEAD^1'))
def test_multiple_changes(self):
"""Test git-review asks about multiple changes.
Should register user's wish to send two change requests by interactive
'yes' message and by the -y option.
"""
self._run_git_review('-s')
# 'yes' message
self._simple_change('test file modified 1st time',
'test commit message 1')
self._simple_change('test file modified 2nd time',
'test commit message 2')
review_res = self._run_git_review(confirm=True)
self.assertIn("Type 'yes' to confirm", review_res)
self.assertIn("Processing changes: new: 2", review_res)
# abandon changes sent to the Gerrit
head = self._run_git('rev-parse', 'HEAD')
head_1 = self._run_git('rev-parse', 'HEAD^1')
self._run_gerrit_cli('review', '--abandon', head)
self._run_gerrit_cli('review', '--abandon', head_1)
# -y option
self._simple_change('test file modified 3rd time',
'test commit message 3')
self._simple_change('test file modified 4th time',
'test commit message 4')
review_res = self._run_git_review('-y')
self.assertIn("Processing changes: new: 2", review_res)
def test_need_rebase_no_upload(self):
"""Test change needing a rebase does not upload."""
self._run_git_review('-s')
head_1 = self._run_git('rev-parse', 'HEAD^1')
self._run_git('checkout', '-b', 'test_branch', head_1)
self._simple_change('some other message',
'create conflict with master')
exc = self.assertRaises(Exception, self._run_git_review)
self.assertIn("Errors running git rebase -i remotes/gerrit/master",
exc.args[0])
def test_upload_without_rebase(self):
"""Test change not needing a rebase can upload without rebasing."""
self._run_git_review('-s')
head_1 = self._run_git('rev-parse', 'HEAD^1')
self._run_git('checkout', '-b', 'test_branch', head_1)
self._simple_change('some new message',
'just another file (no conflict)',
self._dir('test', 'new_test_file.txt'))
review_res = self._run_git_review('-v')
self.assertIn("Running: git rebase -i remotes/gerrit/master",
review_res)
self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head_1)
def test_no_rebase_check(self):
"""Test -R causes a change to be uploaded without rebase checking."""
self._run_git_review('-s')
head_1 = self._run_git('rev-parse', 'HEAD^1')
self._run_git('checkout', '-b', 'test_branch', head_1)
self._simple_change('some new message', 'just another file',
self._dir('test', 'new_test_file.txt'))
review_res = self._run_git_review('-v', '-R')
self.assertNotIn('rebase', review_res)
self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head_1)
def test_rebase_anyway(self):
"""Test -F causes a change to be rebased regardless."""
self._run_git_review('-s')
head = self._run_git('rev-parse', 'HEAD')
head_1 = self._run_git('rev-parse', 'HEAD^1')
self._run_git('checkout', '-b', 'test_branch', head_1)
self._simple_change('some new message', 'just another file',
self._dir('test', 'new_test_file.txt'))
review_res = self._run_git_review('-v', '-F')
self.assertIn('rebase', review_res)
self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head)

77
git_review/tests/utils.py Normal file
View File

@ -0,0 +1,77 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import subprocess
def run_cmd(*args, **kwargs):
"""Run command and check the return code."""
preexec_fn = None
if 'chdir' in kwargs:
def preexec_fn():
return os.chdir(kwargs['chdir'])
proc = subprocess.Popen(args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=os.environ,
preexec_fn=preexec_fn)
if 'confirm' in kwargs and kwargs['confirm']:
proc.stdin.write('yes'.encode())
proc.stdin.flush()
out, err = proc.communicate()
out = out.decode('utf-8')
if proc.returncode != 0:
raise Exception(
"Error occurred while processing the command:\n%s.\n"
"Stdout: %s\nStderr: %s" %
(' '.join(args), out.strip(), err)
)
return out.strip()
def run_git(command, *args):
"""Run git command with the specified args."""
return run_cmd("git", command, *args)
def write_to_file(path, content):
"""Create (if does not exist) and write to the file."""
with open(path, 'wb') as file_:
file_.write(content)
GERRIT_CONF_TMPL = """
[gerrit]
basePath = git
canonicalWebUrl = http://nonexistent/
[database]
type = h2
database = db/ReviewDB
[auth]
type = DEVELOPMENT_BECOME_ANY_ACCOUNT
[sshd]
listenAddress = *:%s
[httpd]
listenUrl = http://*:%s/
"""
def get_gerrit_conf(port, http_port):
return GERRIT_CONF_TMPL % (port, http_port)

View File

@ -1,5 +1,5 @@
[tox]
envlist = py26,py27,pep8
envlist = py26,py27,py32,py33,pep8
[testenv]
setenv =
@ -8,6 +8,10 @@ setenv =
LANGUAGE=en_US:en
LC_ALL=C
commands =
python -m git_review.tests.prepare
python setup.py testr --slowest --testr-args='{posargs}'
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt