diff options
author | Milosz Wasilewski <milosz.wasilewski@linaro.org> | 2015-01-12 17:09:07 +0000 |
---|---|---|
committer | Milosz Wasilewski <milosz.wasilewski@linaro.org> | 2015-01-12 17:09:07 +0000 |
commit | 89922ad10a723bd25c646081f276b4bcf96f74d8 (patch) | |
tree | 011ed1d865c4defda9439f3595ac5d0fd023453b | |
parent | fa2bb668666b95e67bfd889953c902d521133137 (diff) |
Example unit test approach
Signed-off-by: Milosz Wasilewski <milosz.wasilewski@linaro.org>
-rw-r--r-- | test-definitions-ci.py | 230 |
1 files changed, 100 insertions, 130 deletions
diff --git a/test-definitions-ci.py b/test-definitions-ci.py index 89aae35..8b8c98b 100644 --- a/test-definitions-ci.py +++ b/test-definitions-ci.py @@ -5,140 +5,110 @@ import os import subprocess import traceback import yaml +import unittest -jenkins_env = ['WORKSPACE', 'BUILD_URL', 'GERRIT_PROJECT', 'GERRIT_REFSPEC', 'GERRIT_PATCHSET_REVISION', 'GERRIT_CHANGE_OWNER_NAME'] -git_server = 'http://git.linaro.org/git' -gerrit_server = 'review.linaro.org' -result_message_list = [] -debug = True +class YamlValidateTest(unittest.TestCase): + def runTest(self): + for root, dirs, files in os.walk('.'): + if not root.startswith("./.git"): + for name in files: + if name.endswith("yaml"): + try: + filename = root + "/" + name + with open(filename, "r") as f: + y = yaml.load(f.read()) + except: + result = [] + exc_type, exc_value, exc_traceback = sys.exc_info() + for line in traceback.format_exception_only(exc_type, exc_value): + result.append(' ' + line) + self.fail('\n'.join(result)) -def dummy_env(): - os.environ['WORKSPACE'] = '/home/milosz/linaro/testcases/test-definitions-ci/tmp' - os.environ['BUILD_URL'] = 'https://ci.linaro.org/jenkins/job/lava-project-pep8-compliance/686/' - os.environ['GERRIT_PROJECT'] = 'people/milosz.wasilewski/test-definitions' - os.environ['GERRIT_REFSPEC'] = 'refs/changes/94/4194/2' - os.environ['GERRIT_PATCHSET_REVISION'] = '742dfae05943f11c260702104e79206334be77dc' - os.environ['GERRIT_CHANGE_OWNER_NAME'] = 'Milosz Wasilewski' -def check_enviroment(): - is_env_set = True - for env_variable in jenkins_env: - if env_variable in os.environ: - print '%s is defined as %s' % (env_variable, os.environ[env_variable]) - else: - print '%s is not defined' % env_variable - is_env_set = False - return is_env_set +#def publish_result(): +# result_message = '\n'.join(result_message_list) +# if debug: +# print result_message +# f = open('build-error.txt', 'w') +# f.write("\n\n") +# f.write(result_message) +# f.write("\n\n") +# f.close() +# sys.stderr.write(result_message) +# sys.stderr.write("\n") +# +#def pep8_check(ignore_options): +# cmd = 'pep8 --ignore %s .' % ignore_options +# try: +# output = subprocess.check_output(cmd, shell=True) +# if debug: +# print output +# message = '* PEP8 CHECK: [PASSED]: %s' % cmd +# result_message_list.append(message) +# except subprocess.CalledProcessError as e: +# message_list = [] +# message_list.append('* PEP8 CHECK: [FAILED]: %s' % cmd) +# message_list.append('* PEP8 CHECK: [OUTPUT]:') +# for line in e.output.splitlines(): +# message_list.append('* ' + line) +# message = '\n\n'.join(message_list) +# result_message_list.append(message) +# cmd_verbose = 'pep8 --ignore %s --show-source --show-pep8 .' % ignore_options +# subprocess.call(cmd_verbose, shell=True) +# publish_result() +# exit(1) +#def validate_yaml(filename): +# with open(filename, "r") as f: +# try: +# y = yaml.load(f.read()) +# message = "* YAMLVALID: [PASSED]: " + filename +# result_message_list.append(message) +# except: +# message = "* YAMLVALID: [FAILED]: " + filename +# result_message_list.append(message) +# result_message_list.append("\n\n") +# exc_type, exc_value, exc_traceback = sys.exc_info() +# for line in traceback.format_exception_only(exc_type, exc_value): +# #result_message_list.append(' ' + line.replace("\"", "'")) +# result_message_list.append(' ' + line) +# publish_result() +# exit(1) -def notify_committer(): - message_list= [] - message_list.append('* Hello %s' % os.environ['GERRIT_CHANGE_OWNER_NAME']) - message_list.append('* Your patch set %s has triggered automated testing.' % os.environ['GERRIT_PATCHSET_REVISION']) - message_list.append('* Please do not merge this commit until after I have reviewed the results with you.') - message_list.append('* %s' % os.environ['BUILD_URL']) - message = '\n'.join(message_list) - if debug: - print message -def publish_result(): - result_message = '\n'.join(result_message_list) - if debug: - print result_message - f = open('build-error.txt', 'w') - f.write("\n\n") - f.write(result_message) - f.write("\n\n") - f.close() - sys.stderr.write(result_message) - sys.stderr.write("\n") +#def validate_shell(filename): +# cmd = 'checkbashisms %s 2>&1' % filename +# try: +# output = subprocess.check_output(cmd, shell=True) +# if debug: +# print output +# message = '* CHECKBASHISMS: [PASSED]: %s' % filename +# result_message_list.append(message) +# except subprocess.CalledProcessError as e: +# result_message_list.append('* CHECKBASHISMS: [WARNING]: %s' % filename) +# result_message_list.append('* CHECKBASHISMS: [OUTPUT]:') +# for line in e.output.splitlines(): +# #result_message_list.append(' ' + line.replace("\"", "'").replace("{","\\{").replace("}","\\}")) +# result_message_list.append(' ' + line) +# publish_result() +# exit(1) +# +#def run_unit_tests(): +# for root, dirs, files in os.walk('.'): +# if not root.startswith("./.git"): +# for name in files: +# if name.endswith("yaml"): +# validate_yaml(root + "/" + name) +# # disable checkbashisms for now +# #if name.endswith("sh"): +# # validate_shell(root + "/" + name) +# +#def main(ignore_options): +# # disable pep8 for now +# #pep8_check(ignore_options) +# run_unit_tests() +# publish_result() +# exit(0) -def pep8_check(ignore_options): - cmd = 'pep8 --ignore %s .' % ignore_options - try: - output = subprocess.check_output(cmd, shell=True) - if debug: - print output - message = '* PEP8 CHECK: [PASSED]: %s' % cmd - result_message_list.append(message) - except subprocess.CalledProcessError as e: - message_list = [] - message_list.append('* PEP8 CHECK: [FAILED]: %s' % cmd) - message_list.append('* PEP8 CHECK: [OUTPUT]:') - for line in e.output.splitlines(): - message_list.append('* ' + line) - message = '\n\n'.join(message_list) - result_message_list.append(message) - cmd_verbose = 'pep8 --ignore %s --show-source --show-pep8 .' % ignore_options - subprocess.call(cmd_verbose, shell=True) - publish_result() - exit(1) - -def validate_yaml(filename): - with open(filename, "r") as f: - try: - y = yaml.load(f.read()) - message = "* YAMLVALID: [PASSED]: " + filename - result_message_list.append(message) - except: - message = "* YAMLVALID: [FAILED]: " + filename - result_message_list.append(message) - result_message_list.append("\n\n") - exc_type, exc_value, exc_traceback = sys.exc_info() - for line in traceback.format_exception_only(exc_type, exc_value): - #result_message_list.append(' ' + line.replace("\"", "'")) - result_message_list.append(' ' + line) - publish_result() - exit(1) - -def validate_shell(filename): - cmd = 'checkbashisms %s 2>&1' % filename - try: - output = subprocess.check_output(cmd, shell=True) - if debug: - print output - message = '* CHECKBASHISMS: [PASSED]: %s' % filename - result_message_list.append(message) - except subprocess.CalledProcessError as e: - result_message_list.append('* CHECKBASHISMS: [WARNING]: %s' % filename) - result_message_list.append('* CHECKBASHISMS: [OUTPUT]:') - for line in e.output.splitlines(): - #result_message_list.append(' ' + line.replace("\"", "'").replace("{","\\{").replace("}","\\}")) - result_message_list.append(' ' + line) - publish_result() - exit(1) - -def run_unit_tests(): - for root, dirs, files in os.walk('.'): - if not root.startswith("./.git"): - for name in files: - if name.endswith("yaml"): - validate_yaml(root + "/" + name) - # disable checkbashisms for now - #if name.endswith("sh"): - # validate_shell(root + "/" + name) - -def init(): - result_message_list.append('* TESTBOT: [RESULTS]: Automated test results for patch set: %s' % os.environ['GERRIT_PATCHSET_REVISION']) - -def main(ignore_options): - print "TESTBOT: STARTING" - # Uncomment the following to run locally - #debug = True - #dummy_env() - if check_enviroment(): - print 'All required environment variables have been set...continuing' - else: - print 'All required environment variables have not been set...exiting' - exit(1) - #notify_committer() - init() - #checkout_and_rebase() - # disable pep8 for now - #pep8_check(ignore_options) - run_unit_tests() - publish_result() - exit(0) - -if __name__ == '__main__': - main(sys.argv[1]) +#if __name__ == '__main__': +# main(sys.argv[1]) |