1*d68f33bcSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*d68f33bcSAndroid Build Coastguard Worker# Copyright 2019 The Android Open Source Project 3*d68f33bcSAndroid Build Coastguard Worker# 4*d68f33bcSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*d68f33bcSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*d68f33bcSAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*d68f33bcSAndroid Build Coastguard Worker# 8*d68f33bcSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*d68f33bcSAndroid Build Coastguard Worker# 10*d68f33bcSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*d68f33bcSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*d68f33bcSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*d68f33bcSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*d68f33bcSAndroid Build Coastguard Worker# limitations under the License. 15*d68f33bcSAndroid Build Coastguard Worker 16*d68f33bcSAndroid Build Coastguard Worker"""Integration tests for the config module (via PREUPLOAD.cfg).""" 17*d68f33bcSAndroid Build Coastguard Worker 18*d68f33bcSAndroid Build Coastguard Workerimport argparse 19*d68f33bcSAndroid Build Coastguard Workerimport os 20*d68f33bcSAndroid Build Coastguard Workerimport re 21*d68f33bcSAndroid Build Coastguard Workerimport sys 22*d68f33bcSAndroid Build Coastguard Worker 23*d68f33bcSAndroid Build Coastguard Worker 24*d68f33bcSAndroid Build Coastguard WorkerREPOTOOLS = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 25*d68f33bcSAndroid Build Coastguard WorkerREPO_ROOT = os.path.dirname(os.path.dirname(REPOTOOLS)) 26*d68f33bcSAndroid Build Coastguard Worker 27*d68f33bcSAndroid Build Coastguard Worker 28*d68f33bcSAndroid Build Coastguard Workerdef assertEqual(msg, exp, actual): 29*d68f33bcSAndroid Build Coastguard Worker """Assert |exp| equals |actual|.""" 30*d68f33bcSAndroid Build Coastguard Worker assert exp == actual, f'{msg}: expected "{exp}" but got "{actual}"' 31*d68f33bcSAndroid Build Coastguard Worker 32*d68f33bcSAndroid Build Coastguard Worker 33*d68f33bcSAndroid Build Coastguard Workerdef assertEnv(var, value): 34*d68f33bcSAndroid Build Coastguard Worker """Assert |var| is set in the environment as |value|.""" 35*d68f33bcSAndroid Build Coastguard Worker assert var in os.environ, f'${var} missing in environment' 36*d68f33bcSAndroid Build Coastguard Worker assertEqual(f'env[{var}]', value, os.environ[var]) 37*d68f33bcSAndroid Build Coastguard Worker 38*d68f33bcSAndroid Build Coastguard Worker 39*d68f33bcSAndroid Build Coastguard Workerdef check_commit_id(commit): 40*d68f33bcSAndroid Build Coastguard Worker """Check |commit| looks like a git commit id.""" 41*d68f33bcSAndroid Build Coastguard Worker assert len(commit) == 40, f'commit "{commit}" must be 40 chars' 42*d68f33bcSAndroid Build Coastguard Worker assert re.match(r'^[a-f0-9]+$', commit), \ 43*d68f33bcSAndroid Build Coastguard Worker f'commit "{commit}" must be all hex' 44*d68f33bcSAndroid Build Coastguard Worker 45*d68f33bcSAndroid Build Coastguard Worker 46*d68f33bcSAndroid Build Coastguard Workerdef check_commit_msg(msg): 47*d68f33bcSAndroid Build Coastguard Worker """Check the ${PREUPLOAD_COMMIT_MESSAGE} setting.""" 48*d68f33bcSAndroid Build Coastguard Worker assert len(msg) > 1, f'commit message must be at least 2 bytes: {msg}' 49*d68f33bcSAndroid Build Coastguard Worker 50*d68f33bcSAndroid Build Coastguard Worker 51*d68f33bcSAndroid Build Coastguard Workerdef check_repo_root(root): 52*d68f33bcSAndroid Build Coastguard Worker """Check the ${REPO_ROOT} setting.""" 53*d68f33bcSAndroid Build Coastguard Worker assertEqual('REPO_ROOT', REPO_ROOT, root) 54*d68f33bcSAndroid Build Coastguard Worker 55*d68f33bcSAndroid Build Coastguard Worker 56*d68f33bcSAndroid Build Coastguard Workerdef check_files(files): 57*d68f33bcSAndroid Build Coastguard Worker """Check the ${PREUPLOAD_FILES} setting.""" 58*d68f33bcSAndroid Build Coastguard Worker assert files 59*d68f33bcSAndroid Build Coastguard Worker 60*d68f33bcSAndroid Build Coastguard Worker 61*d68f33bcSAndroid Build Coastguard Workerdef check_env(): 62*d68f33bcSAndroid Build Coastguard Worker """Verify all exported env vars look sane.""" 63*d68f33bcSAndroid Build Coastguard Worker assertEnv('REPO_PROJECT', 'platform/tools/repohooks') 64*d68f33bcSAndroid Build Coastguard Worker assertEnv('REPO_PATH', 'tools/repohooks') 65*d68f33bcSAndroid Build Coastguard Worker assertEnv('REPO_REMOTE', 'aosp') 66*d68f33bcSAndroid Build Coastguard Worker check_commit_id(os.environ['REPO_LREV']) 67*d68f33bcSAndroid Build Coastguard Worker print(os.environ['REPO_RREV']) 68*d68f33bcSAndroid Build Coastguard Worker check_commit_id(os.environ['PREUPLOAD_COMMIT']) 69*d68f33bcSAndroid Build Coastguard Worker 70*d68f33bcSAndroid Build Coastguard Worker 71*d68f33bcSAndroid Build Coastguard Workerdef get_parser(): 72*d68f33bcSAndroid Build Coastguard Worker """Return a command line parser.""" 73*d68f33bcSAndroid Build Coastguard Worker parser = argparse.ArgumentParser(description=__doc__) 74*d68f33bcSAndroid Build Coastguard Worker parser.add_argument('--check-env', action='store_true', 75*d68f33bcSAndroid Build Coastguard Worker help='Check all exported env vars.') 76*d68f33bcSAndroid Build Coastguard Worker parser.add_argument('--commit-id', 77*d68f33bcSAndroid Build Coastguard Worker help='${PREUPLOAD_COMMIT} setting.') 78*d68f33bcSAndroid Build Coastguard Worker parser.add_argument('--commit-msg', 79*d68f33bcSAndroid Build Coastguard Worker help='${PREUPLOAD_COMMIT_MESSAGE} setting.') 80*d68f33bcSAndroid Build Coastguard Worker parser.add_argument('--repo-root', 81*d68f33bcSAndroid Build Coastguard Worker help='${REPO_ROOT} setting.') 82*d68f33bcSAndroid Build Coastguard Worker parser.add_argument('files', nargs='+', 83*d68f33bcSAndroid Build Coastguard Worker help='${PREUPLOAD_FILES} paths.') 84*d68f33bcSAndroid Build Coastguard Worker return parser 85*d68f33bcSAndroid Build Coastguard Worker 86*d68f33bcSAndroid Build Coastguard Worker 87*d68f33bcSAndroid Build Coastguard Workerdef main(argv): 88*d68f33bcSAndroid Build Coastguard Worker """The main entry.""" 89*d68f33bcSAndroid Build Coastguard Worker parser = get_parser() 90*d68f33bcSAndroid Build Coastguard Worker opts = parser.parse_args(argv) 91*d68f33bcSAndroid Build Coastguard Worker 92*d68f33bcSAndroid Build Coastguard Worker try: 93*d68f33bcSAndroid Build Coastguard Worker if opts.check_env: 94*d68f33bcSAndroid Build Coastguard Worker check_env() 95*d68f33bcSAndroid Build Coastguard Worker if opts.commit_id is not None: 96*d68f33bcSAndroid Build Coastguard Worker check_commit_id(opts.commit_id) 97*d68f33bcSAndroid Build Coastguard Worker if opts.commit_msg is not None: 98*d68f33bcSAndroid Build Coastguard Worker check_commit_msg(opts.commit_msg) 99*d68f33bcSAndroid Build Coastguard Worker if opts.repo_root is not None: 100*d68f33bcSAndroid Build Coastguard Worker check_repo_root(opts.repo_root) 101*d68f33bcSAndroid Build Coastguard Worker check_files(opts.files) 102*d68f33bcSAndroid Build Coastguard Worker except AssertionError as e: 103*d68f33bcSAndroid Build Coastguard Worker print(f'error: {e}', file=sys.stderr) 104*d68f33bcSAndroid Build Coastguard Worker return 1 105*d68f33bcSAndroid Build Coastguard Worker 106*d68f33bcSAndroid Build Coastguard Worker return 0 107*d68f33bcSAndroid Build Coastguard Worker 108*d68f33bcSAndroid Build Coastguard Worker 109*d68f33bcSAndroid Build Coastguard Workerif __name__ == '__main__': 110*d68f33bcSAndroid Build Coastguard Worker sys.exit(main(sys.argv[1:])) 111