git-review/git_review/tests/utils.py
Jeremy Stanley 4c6ab44916 Upgrade testing to Gerrit 3.4.4
An addition has been proposed to leverage Gerrit's "Cc" feature, but
in order to test this we need a newer version of the server.

Newer Gerrit versions require HTTP basic auth instead of digest for
the REST API, so switch our tests to use that when uploading SSH
keys.

A newer JDK (11) is required, but this version should still be
available on our configured test platforms.

We update the test Gerrit server config to not try to send email. This
prevents annoying tracebacks from ending up in the Gerrit logs on test
failures.

We update test_cloned_repo and test_multiple_changes to look for strings
that newer Gerrit emits on successful push. Git review passes these
through to users and the tests look for them to determine if pushes were
successful.

Because of an SSH host key negotiation regression in 3.4.5, stick
with 3.4.4 for now:

https://bugs.chromium.org/p/gerrit/issues/detail?id=16215

Finally we remove skipsdist from tox.ini so that `which git-review` can
find git-review installed to the test venv. Tox v4 won't install the
project into the venv if skipsdist is set.

Change-Id: I540950b93356b1efbc34bca976bfb3134f47a599
2023-03-07 15:56:59 -08:00

88 lines
2.4 KiB
Python

# 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
import traceback
GERRIT_CONF_TMPL = """
[gerrit]
basePath = git
canonicalWebUrl = http://nonexistent/
[database]
type = h2
database = db/ReviewDB
[auth]
type = DEVELOPMENT_BECOME_ANY_ACCOUNT
[sshd]
listenAddress = %s:%s
[httpd]
listenUrl = http://%s:%s/
[sendemail]
enable = false
"""
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'])
try:
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')
except Exception:
raise Exception(
"Exception while processing the command:\n%s.\n%s" %
(' '.join(args), traceback.format_exc())
)
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, **kwargs):
"""Run git command with the specified args."""
return run_cmd("git", command, *args, **kwargs)
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)
def get_gerrit_conf(ssh_addr, ssh_port, http_addr, http_port):
return GERRIT_CONF_TMPL % (ssh_addr, ssh_port, http_addr, http_port)