salt/tests/jenkins.py

87 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
'''
This script is used to test Salt from a Jenkins server, specifically
jenkins.saltstack.com.
This script is intended to be shell-centric!!
'''
2013-08-10 07:23:32 +00:00
# Import python libs
import sys
import subprocess
import hashlib
import random
import optparse
2013-08-10 08:04:45 +00:00
def run(platform, provider, commit, clean):
'''
RUN!
'''
htag = hashlib.md5(str(random.randint(1, 100000000))).hexdigest()[:6]
vm_name = 'ZZZ{0}{1}'.format(platform, htag)
cmd = 'salt-cloud --script-args "git {0}" -p {1}_{2} {3}'.format(
commit, provider, platform, vm_name)
2013-08-10 07:53:28 +00:00
print('Running CMD: {0}'.format(cmd))
subprocess.call(
cmd,
shell=True)
# Run tests here
cmd = 'salt {0} state.sls testrun pillar="{{git_commit: {1}}}" --no-color'.format(
vm_name,
commit),
2013-08-10 07:53:28 +00:00
print('Running CMD: {0}'.format(cmd))
out = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0]
print(out)
if 'Result: False' in out:
2013-08-11 18:22:30 +00:00
retcode = 1
else:
retcode = 0
# Clean up the vm
2013-08-10 08:04:45 +00:00
if clean:
cmd = 'salt-cloud -d {0} -y'.format(vm_name),
2013-08-10 08:04:45 +00:00
print('Running CMD: {0}'.format(cmd))
subprocess.call(
cmd,
shell=True)
return retcode
2013-08-10 07:23:32 +00:00
def parse():
'''
Parse the CLI options
2013-08-10 07:23:32 +00:00
'''
parser = optparse.OptionParser()
parser.add_option('--platform',
dest='platform',
help='The target platform, choose from:\ncent6\ncent5\nubuntu12.04')
parser.add_option('--provider',
dest='provider',
help='The vm provider')
parser.add_option('--commit',
dest='commit',
help='The git commit to track')
2013-08-10 08:06:03 +00:00
parser.add_option('--no-clean',
2013-08-10 08:04:45 +00:00
dest='clean',
default=True,
action='store_false',
help='Clean up the built vm')
2013-08-10 07:23:32 +00:00
options, args = parser.parse_args()
if not options.platform:
parser.exit('--platform is required')
if not options.provider:
parser.exit('--provider is required')
if not options.commit:
parser.exit('--commit is required')
2013-08-10 07:43:40 +00:00
return options.__dict__
2013-08-10 07:23:32 +00:00
if __name__ == '__main__':
opts = parse()
2013-08-10 08:04:45 +00:00
exit_code = run(**opts)
print('Exit Code: {0}'.format(exit_code))
2013-08-10 07:23:32 +00:00
sys.exit(exit_code)