Last active
August 29, 2015 14:17
-
-
Save tychoish/14395d5f14c109a4a55d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import logging | |
import collections | |
import distutils.spawn | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger('mci-packer-test') | |
TestCase = collections.namedtuple("TestCase", ['name', 'callable', 'args', 'flunk']) | |
# Add a TestCase() object (a collections.namedtuple) to the cases list. The name | |
# should be a unique string to identify the case, a callable of a function that | |
# takes no arguments and returns a boolean, and a "flunk" boolean that, when | |
# True will cause the script to exit with a non-zero status. | |
# | |
# All tests run, each time the script even after the first failure. All | |
# unhandled exceptions in test cases cause the script to exit with a non-zero | |
# status. | |
## Test Cases | |
def tool_exists(name): | |
path = distutils.spawn.find_executable(name) | |
if path is None: | |
return False | |
else: | |
return True | |
def verify_pymongo(): | |
try: | |
import pymongo | |
logger.info('pymongo installed') | |
if pymongo.version >= "2.8": | |
return True | |
else: | |
logger.error('{0} is not a correct version of pymongo'.format(pymongo.version)) | |
return False | |
except: | |
logger.error('pymongo not installed.') | |
return False | |
def verify_scons(): | |
try: | |
scons_version = subprocess.check_output(['scons', '--version']).split('\n')[1].split()[1][1:-1] | |
logger.info('scons installed') | |
if scons_version >= '2.3.0': | |
return True | |
else: | |
logger.error('{0} is not a correct version of scons'.format(scons_version)) | |
return False | |
except Exception as e: | |
print(e) | |
logger.error('scons is not installed') | |
return False | |
def verify_golang(): | |
try: | |
go_version = subprocess.check_output(['go', 'version']).split()[2][2:] | |
if go_version >= '1.3.1': | |
return True | |
else: | |
logger.error('golang version "{0}" is insufficient'.format(go_version)) | |
return False | |
except subprocess.CalledProcessError: | |
logger.error('problem with go installation') | |
return False | |
except: | |
logger.error('problem checking go version') | |
return False | |
def verify_gcc(): | |
try: | |
gcc_version = subprocess.check_output(['gcc', '--version']).split('\n')[0].split()[2] | |
if gcc_version >= '4.8.2': | |
return True | |
else: | |
logger.error('gcc version "{0}" is incorrect'.format(gcc_version)) | |
return False | |
except subprocess.CalledProcessError: | |
logger.error('problem with gcc installation') | |
return False | |
except: | |
logger.error('problem checking gcc version') | |
return False | |
## Test Registry | |
cases = [TestCase(name='git_exits', callable=tool_exists, args=dict(name='git'), flunk=True), | |
TestCase(name='scons_exits', callable=tool_exists, args=dict(name='scons'), flunk=True), | |
TestCase(name='gcc_exists', callable=tool_exists, args=dict(name='gcc'), flunk=True), | |
TestCase(name='go_exists', callable=tool_exists, args=dict(name='go'), flunk=True), | |
TestCase(name='verify_golang', callable=verify_golang, args=None, flunk=True), | |
TestCase(name='verify_gcc', callable=verify_gcc, args=None, flunk=True), | |
TestCase(name='verify_pymongo', callable=verify_pymongo, args=None, flunk=True), | |
TestCase(name='verify_scons', callable=verify_scons, args=None, flunk=True)] | |
## Integration and Execution | |
def main(): | |
results = [] | |
passing = 0 | |
for case in cases: | |
logger.info('running test: ' + case.name) | |
try: | |
if case.args is None: | |
result = case.callable() | |
elif isinstance(case.args, (list, tuple)): | |
result = case.callable(*case.args) | |
elif isinstance(case.args, (dict)): | |
result = case.callable(**case.args) | |
else: | |
logger.error("case '{0}' specifies invalid arguments".format(case.name)) | |
result = False | |
case.flunk = True | |
except Exception as e: | |
logger.error(e) | |
logger.warning("encountered unhandeled exception in: " + case.name) | |
result = False | |
case.flunk = True | |
if result is True: | |
logger.info('test succeeded: ' + case.name) | |
passing += 1 | |
else: | |
if case.flunk is True: | |
results.append(result) | |
logger.critical('test failed: ' + case.name) | |
else: | |
logger.warning('test failed: ' + case.name) | |
msg = '{0} passing tests and {1} flunk/failing test out of {2} tests.' | |
msg = msg.format(passing, len(results), len(cases)) | |
if False in results: | |
logger.critical(msg) | |
raise SystemExit(1) | |
else: | |
logger.info(msg) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment