eCTS: Rewrite run_ects.sh in Python

Since the script is expected to upload results and probably send
alerts by e-mail, it's better to be written in Python so that we
can utilize chromite library. It will no longer run outside chroot
because of imports from chromite.lib.

BUG=chromium:735546
BRANCH=none
TEST=Run run_ects.py and verify all tests pass.

Change-Id: I6c79b061cdaef4d30305cf531f37b6734d18b7d9
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/546579
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Daisuke Nojiri
2017-06-23 12:42:06 -07:00
committed by chrome-bot
parent 7771c52368
commit 681b6fdf1f
2 changed files with 92 additions and 118 deletions

92
util/run_ects.py Normal file
View File

@@ -0,0 +1,92 @@
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Run all eCTS tests and publish results."""
import argparse
import logging
import os
import subprocess
import sys
# List of tests to run.
TESTS = ['meta', 'gpio', 'hook', 'interrupt', 'mutex', 'task', 'timer']
class CtsRunner(object):
"""Class running eCTS tests."""
def __init__(self, ec_dir, dryrun):
self.ec_dir = ec_dir
self.cts_py = []
if dryrun:
self.cts_py += ['echo']
self.cts_py += [os.path.join(ec_dir, 'cts/cts.py')]
def run_cmd(self, cmd):
try:
rc = subprocess.call(cmd)
if rc != 0:
return False
except OSError:
return False
return True
def run_test(self, test):
cmd = self.cts_py + ['-m', test]
self.run_cmd(cmd)
def run(self, tests):
for test in tests:
logging.info('Running', test, 'test.')
self.run_test(test)
def sync(self):
logging.info('Syncing tree...')
os.chdir(self.ec_dir)
cmd = ['repo', 'sync', '.']
return self.run_cmd(cmd)
def upload(self):
logging.info('Uploading results...')
def main():
if not os.path.exists('/etc/cros_chroot_version'):
logging.error('This script has to run inside chroot.')
sys.exit(-1)
ec_dir = os.path.realpath(os.path.dirname(__file__) + '/..')
parser = argparse.ArgumentParser(description='Run eCTS and report results.')
parser.add_argument('-d',
'--dryrun',
action='store_true',
help='Echo commands to be executed without running them.')
parser.add_argument('-s',
'--sync',
action='store_true',
help='Sync tree before running tests.')
parser.add_argument('-u',
'--upload',
action='store_true',
help='Upload test results.')
args = parser.parse_args()
runner = CtsRunner(ec_dir, args.dryrun)
if args.sync:
if not runner.sync():
logging.error('Failed to sync.')
sys.exit(-1)
runner.run(TESTS)
if args.upload:
runner.upload()
if __name__ == '__main__':
main()

View File

@@ -1,118 +0,0 @@
#!/bin/bash
#
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Run all eCTS tests and publish results
set -e
NOC='\033[0m'
RED='\033[0;31m'
GRN='\033[0;32m'
# List of tests to run.
TESTS=(meta gpio hook interrupt mutex task timer)
usage() {
cat <<END
${SCRIPT_NAME} - Run all eCTS tests and publish results.
Usage: ${SCRIPT_NAME} [options]
Options:
-d: Dry run tests.
-h: Print this message.
-s: Sync tree before running tests.
-u: Upload results.
-v: Enable verbose output.
END
}
error() {
printf "%b[%s] %s%b\n" "${RED}" "${SCRIPT_NAME}" "$*" "${NOC}" 1>&2
}
info() {
printf "%b[%s] %s%b\n" "${GRN}" "${SCRIPT_NAME}" "$*" "${NOC}"
}
get_script_name() {
local name=$(basename "$1")
printf "${name%.*}"
}
get_ec_dir() {
readlink -f "$(dirname $1)/.."
}
get_cros_sdk() {
if [[ -e "/etc/cros_chroot_version" ]]; then
printf ""
else
printf "cros_sdk --"
fi
}
sync_src() {
info "Syncing tree..."
if ! repo sync .; then
error "Failed to sync source"
exit 1
fi
}
run_test() {
${CROS_SDK} ${DRY_RUN} "/mnt/host/source/src/platform/ec/cts/cts.py" -m "$1"
}
run() {
local t
for t in "${TESTS[@]}"; do
info "Running ${t} test"
run_test "${t}"
done
}
upload_results() {
info "Uploading results... (Not implemented)"
}
main() {
local do_sync
local do_upload
SCRIPT_NAME=$(get_script_name "$0")
CROS_SDK=$(get_cros_sdk)
EC_DIR=$(get_ec_dir "$0")
DRY_RUN=""
VERBOSITY=""
# Need to cd to SDK directory to run tools (cros_sdk, repo sync).
cd "${EC_DIR}"
while getopts ":dhsuv" opt; do
case "${opt}" in
d) DRY_RUN="echo" ;;
h)
usage
exit 0
;;
s) do_sync="y" ;;
u) do_upload="y" ;;
v) VERBOSITY="y" ;;
\?)
error "invalid option: -${OPTARG}"
exit 1
;;
esac
done
shift $((OPTIND-1))
[[ "${do_sync}" == "y" ]] && sync_src
run
[[ "${do_upload}" == "y" ]] && upload_results
}
main "$@"