Add test for large output; fix indentation.

pull/4420/head
John Kleint 2012-05-11 11:38:15 -04:00
parent fba2bdcf0c
commit 83b9a43e60
1 changed files with 186 additions and 182 deletions

View File

@ -10,9 +10,9 @@ import os
import shutil import shutil
import time import time
try: try:
import json import json
except: except:
import simplejson as json import simplejson as json
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
@ -25,202 +25,206 @@ def get_binary(name):
class TestRunner(unittest.TestCase): class TestRunner(unittest.TestCase):
def setUp(self): def setUp(self):
self.user = getpass.getuser() self.user = getpass.getuser()
self.runner = ansible.runner.Runner( self.runner = ansible.runner.Runner(
module_name='ping', module_name='ping',
module_path='library/', module_path='library/',
module_args='', module_args='',
remote_user=self.user, remote_user=self.user,
remote_pass=None, remote_pass=None,
host_list='test/ansible_hosts', host_list='test/ansible_hosts',
timeout=5, timeout=5,
forks=1, forks=1,
background=0, background=0,
pattern='all', pattern='all',
) )
self.cwd = os.getcwd() self.cwd = os.getcwd()
self.test_dir = os.path.join(self.cwd, 'test') self.test_dir = os.path.join(self.cwd, 'test')
self.stage_dir = self._prepare_stage_dir() self.stage_dir = self._prepare_stage_dir()
def _prepare_stage_dir(self): def _prepare_stage_dir(self):
stage_path = os.path.join(self.test_dir, 'test_data') stage_path = os.path.join(self.test_dir, 'test_data')
if os.path.exists(stage_path): if os.path.exists(stage_path):
shutil.rmtree(stage_path, ignore_errors=False) shutil.rmtree(stage_path, ignore_errors=False)
assert not os.path.exists(stage_path) assert not os.path.exists(stage_path)
os.makedirs(stage_path) os.makedirs(stage_path)
assert os.path.exists(stage_path) assert os.path.exists(stage_path)
return stage_path return stage_path
def _get_test_file(self, filename): def _get_test_file(self, filename):
# get a file inside the test input directory # get a file inside the test input directory
filename = os.path.join(self.test_dir, filename) filename = os.path.join(self.test_dir, filename)
assert os.path.exists(filename) assert os.path.exists(filename)
return filename return filename
def _get_stage_file(self, filename):
# get a file inside the test output directory
filename = os.path.join(self.stage_dir, filename)
return filename
def _run(self, module_name, module_args, background=0): def _get_stage_file(self, filename):
''' run a module and get the localhost results ''' # get a file inside the test output directory
self.runner.module_name = module_name filename = os.path.join(self.stage_dir, filename)
args = ' '.join(module_args) return filename
print "DEBUG: using args=%s" % args
self.runner.module_args = args
self.runner.background = background
results = self.runner.run()
# when using nosetests this will only show up on failure
# which is pretty useful
print "RESULTS=%s" % results
assert "127.0.0.2" in results['contacted']
return results['contacted']['127.0.0.2']
def test_ping(self): def _run(self, module_name, module_args, background=0):
result = self._run('ping',[]) ''' run a module and get the localhost results '''
assert "ping" in result self.runner.module_name = module_name
args = ' '.join(module_args)
print "DEBUG: using args=%s" % args
self.runner.module_args = args
self.runner.background = background
results = self.runner.run()
# when using nosetests this will only show up on failure
# which is pretty useful
print "RESULTS=%s" % results
assert "127.0.0.2" in results['contacted']
return results['contacted']['127.0.0.2']
def test_facter(self): def test_ping(self):
if not get_binary("facter"): result = self._run('ping', [])
raise SkipTest assert "ping" in result
result = self._run('facter',[])
assert "hostname" in result
# temporarily disbabled since it occasionally hangs def test_facter(self):
# ohai's fault, setup module doesn't actually run this if not get_binary("facter"):
# to get ohai's "facts" anyway raise SkipTest
# result = self._run('facter', [])
#def test_ohai(self): assert "hostname" in result
# if not get_binary("facter"):
# raise SkipTest
# result = self._run('ohai',[])
# assert "hostname" in result
def test_copy(self): # temporarily disbabled since it occasionally hangs
# test copy module, change trigger, etc # ohai's fault, setup module doesn't actually run this
pass # to get ohai's "facts" anyway
#
#def test_ohai(self):
# if not get_binary("facter"):
# raise SkipTest
# result = self._run('ohai',[])
# assert "hostname" in result
def test_copy(self): def test_copy(self):
input = self._get_test_file('sample.j2') # test copy module, change trigger, etc
output = self._get_stage_file('sample.out') input_ = self._get_test_file('sample.j2')
assert not os.path.exists(output) output = self._get_stage_file('sample.out')
result = self._run('copy', [ assert not os.path.exists(output)
"src=%s" % input, result = self._run('copy', [
"dest=%s" % output, "src=%s" % input_,
]) "dest=%s" % output,
assert os.path.exists(output) ])
data_in = file(input).read() assert os.path.exists(output)
data_out = file(output).read() data_in = file(input_).read()
assert data_in == data_out data_out = file(output).read()
assert 'failed' not in result assert data_in == data_out
assert result['changed'] == True assert 'failed' not in result
assert 'md5sum' in result assert result['changed'] == True
result = self._run('copy', [ assert 'md5sum' in result
"src=%s" % input, result = self._run('copy', [
"dest=%s" % output, "src=%s" % input_,
]) "dest=%s" % output,
assert result['changed'] == False ])
assert result['changed'] == False
def test_template(self): def test_template(self):
input = self._get_test_file('sample.j2') input_ = self._get_test_file('sample.j2')
metadata = self._get_test_file('metadata.json') metadata = self._get_test_file('metadata.json')
output = self._get_stage_file('sample.out') output = self._get_stage_file('sample.out')
result = self._run('template', [ result = self._run('template', [
"src=%s" % input, "src=%s" % input_,
"dest=%s" % output, "dest=%s" % output,
"metadata=%s" % metadata "metadata=%s" % metadata
]) ])
assert os.path.exists(output) assert os.path.exists(output)
out = file(output).read() out = file(output).read()
assert out.find("duck") != -1 assert out.find("duck") != -1
assert result['changed'] == True assert result['changed'] == True
assert 'md5sum' in result assert 'md5sum' in result
assert 'failed' not in result assert 'failed' not in result
result = self._run('template', [ result = self._run('template', [
"src=%s" % input, "src=%s" % input_,
"dest=%s" % output, "dest=%s" % output,
"metadata=%s" % metadata "metadata=%s" % metadata
]) ])
assert result['changed'] == False assert result['changed'] == False
def test_command(self): def test_command(self):
# test command module, change trigger, etc
# test command module, change trigger, etc result = self._run('command', [ "/bin/echo", "hi" ])
result = self._run('command', [ "/bin/echo", "hi" ]) assert "failed" not in result
assert "failed" not in result assert "msg" not in result
assert "msg" not in result assert result['rc'] == 0
assert result['rc'] == 0 assert result['stdout'] == 'hi'
assert result['stdout'] == 'hi' assert result['stderr'] == ''
assert result['stderr'] == ''
result = self._run('command', [ "/bin/false" ])
assert result['rc'] == 1
assert 'failed' not in result
result = self._run('command', [ "/usr/bin/this_does_not_exist", "splat" ])
assert 'msg' in result
assert 'failed' in result
assert 'rc' not in result
result = self._run('shell', [ "/bin/echo", "$HOME" ]) result = self._run('command', [ "/bin/false" ])
assert 'failed' not in result assert result['rc'] == 1
assert result['rc'] == 0 assert 'failed' not in result
def test_setup(self): result = self._run('command', [ "/usr/bin/this_does_not_exist", "splat" ])
output = self._get_stage_file('output.json') assert 'msg' in result
result = self._run('setup', [ "metadata=%s" % output, "a=2", "b=3", "c=4" ]) assert 'failed' in result
assert 'failed' not in result assert 'rc' not in result
assert 'md5sum' in result
assert result['changed'] == True
outds = json.loads(file(output).read())
assert outds['c'] == '4'
# not bothering to test change hooks here since ohai/facter results change
# almost every time so changed is always true, this just tests that
# rewriting the file is ok
result = self._run('setup', [ "metadata=%s" % output, "a=2", "b=3", "c=4" ])
print "RAW RESULT=%s" % result
assert 'md5sum' in result
def test_async(self): result = self._run('shell', [ "/bin/echo", "$HOME" ])
# test async launch and job status assert 'failed' not in result
# of any particular module assert result['rc'] == 0
result = self._run('command', [ get_binary("sleep"), "3" ], background=20)
assert 'ansible_job_id' in result
assert 'started' in result
jid = result['ansible_job_id']
# no real chance of this op taking a while, but whatever
time.sleep(5)
# CLI will abstract this (when polling), but this is how it works internally
result = self._run('async_status', [ "jid=%s" % jid ])
# TODO: would be nice to have tests for supervisory process
# killing job after X seconds
assert 'finished' in result
assert 'failed' not in result
assert 'rc' in result
assert 'stdout' in result
assert result['ansible_job_id'] == jid
def test_fetch(self): def test_large_output(self):
input = self._get_test_file('sample.j2') # Ensure reading a large amount of output from a command doesn't hang.
output = os.path.join(self.stage_dir, '127.0.0.2', input) result = self._run('command', [ "/bin/cat", "/usr/share/dict/words" ])
result = self._run('fetch', [ "src=%s" % input, "dest=%s" % self.stage_dir ]) assert "failed" not in result
assert os.path.exists(output) assert "msg" not in result
assert open(input).read() == open(output).read() assert result['rc'] == 0
assert len(result['stdout']) > 100000
assert result['stderr'] == ''
def test_yum(self): def test_setup(self):
if not get_binary("yum"): output = self._get_stage_file('output.json')
raise SkipTest result = self._run('setup', [ "metadata=%s" % output, "a=2", "b=3", "c=4" ])
result = self._run('yum', [ "list=repos" ]) assert 'failed' not in result
assert 'failed' not in result assert 'md5sum' in result
assert result['changed'] == True
outds = json.loads(file(output).read())
assert outds['c'] == '4'
# not bothering to test change hooks here since ohai/facter results change
# almost every time so changed is always true, this just tests that
# rewriting the file is ok
result = self._run('setup', [ "metadata=%s" % output, "a=2", "b=3", "c=4" ])
print "RAW RESULT=%s" % result
assert 'md5sum' in result
def test_git(self): def test_async(self):
# TODO: tests for the git module # test async launch and job status
pass # of any particular module
result = self._run('command', [ get_binary("sleep"), "3" ], background=20)
assert 'ansible_job_id' in result
assert 'started' in result
jid = result['ansible_job_id']
# no real chance of this op taking a while, but whatever
time.sleep(5)
# CLI will abstract this (when polling), but this is how it works internally
result = self._run('async_status', [ "jid=%s" % jid ])
# TODO: would be nice to have tests for supervisory process
# killing job after X seconds
assert 'finished' in result
assert 'failed' not in result
assert 'rc' in result
assert 'stdout' in result
assert result['ansible_job_id'] == jid
def test_service(self): def test_fetch(self):
# TODO: tests for the service module input_ = self._get_test_file('sample.j2')
pass output = os.path.join(self.stage_dir, '127.0.0.2', input_)
result = self._run('fetch', [ "src=%s" % input_, "dest=%s" % self.stage_dir ])
assert os.path.exists(output)
assert open(input_).read() == open(output).read()
def test_yum(self):
if not get_binary("yum"):
raise SkipTest
result = self._run('yum', [ "list=repos" ])
assert 'failed' not in result
def test_git(self):
# TODO: tests for the git module
pass
def test_service(self):
# TODO: tests for the service module
pass