git-review/git_review/tests/check_test_id_hashes.py
Yuriy Taraday 33081ce1d6 Use hash of test ID to pick Gerrit ports in tests
This eliminates need for 127.* IPs mapped to loopback interface (which
is true only for Linux) and makes test site generation even more
predictable (name of site dir for a test is the same on any machine).

Hash function used is md5(test_id) % 10000 which is enough for now. This
function is checked for collisions before every run in tox.ini, so if
someone happens to add test that will cause a collision, it'll be
immediately visible. Hash function can be changed to anything else that
maps test ID string to a number in [0,10000].

Change-Id: Ib05d9b489a80e4f55c84db2f8bea20b88e959649
2016-08-15 17:41:30 +01:00

58 lines
1.6 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.
from __future__ import print_function
import sys
from git_review import tests
from git_review.tests import utils
def list_test_ids(argv):
res = utils.run_cmd(sys.executable, '-m', 'testtools.run', *argv[1:])
return res.split('\n')
def find_collisions(test_ids):
hashes = {}
for test_id in test_ids:
hash_ = tests._hash_test_id(test_id)
if hash_ in hashes:
return (hashes[hash_], test_id)
hashes[hash_] = test_id
return None
def main(argv):
test_ids = list_test_ids(argv)
if not test_ids:
print("No tests found, check command line arguments", file=sys.stderr)
return 1
collision = find_collisions(test_ids)
if collision is None:
return 0
print(
"Found a collision for test ids hash function: %s and %s\n"
"You should change _hash_test_id function in"
" git_review/tests/__init__.py module to fit new set of test ids."
% collision,
file=sys.stderr,
)
return 2
if __name__ == "__main__":
sys.exit(main(sys.argv))