2012-04-04 17:11:31 +02:00
|
|
|
|
|
|
|
# tests are fairly 'live' (but safe to run)
|
|
|
|
# setup authorized_keys for logged in user such
|
|
|
|
# that the user can log in as themselves before running tests
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import getpass
|
|
|
|
import ansible.runner
|
|
|
|
import os
|
|
|
|
import shutil
|
2012-04-04 17:24:22 +02:00
|
|
|
import time
|
2012-07-30 07:40:12 +02:00
|
|
|
import tempfile
|
2012-04-04 17:11:31 +02:00
|
|
|
|
2012-04-17 13:30:30 +02:00
|
|
|
from nose.plugins.skip import SkipTest
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2012-04-17 13:30:30 +02:00
|
|
|
def get_binary(name):
|
|
|
|
for directory in os.environ["PATH"].split(os.pathsep):
|
|
|
|
path = os.path.join(directory, name)
|
|
|
|
if os.path.isfile(path) and os.access(path, os.X_OK):
|
|
|
|
return path
|
|
|
|
return None
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2012-04-04 17:11:31 +02:00
|
|
|
class TestRunner(unittest.TestCase):
|
|
|
|
|
2012-05-11 17:38:15 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.user = getpass.getuser()
|
|
|
|
self.runner = ansible.runner.Runner(
|
2013-05-25 00:05:28 +02:00
|
|
|
basedir='test/',
|
2012-05-11 17:38:15 +02:00
|
|
|
module_name='ping',
|
|
|
|
module_path='library/',
|
|
|
|
module_args='',
|
|
|
|
remote_user=self.user,
|
|
|
|
remote_pass=None,
|
|
|
|
host_list='test/ansible_hosts',
|
|
|
|
timeout=5,
|
|
|
|
forks=1,
|
|
|
|
background=0,
|
|
|
|
pattern='all',
|
2012-11-19 16:30:30 +01:00
|
|
|
transport='local',
|
2012-05-11 17:38:15 +02:00
|
|
|
)
|
|
|
|
self.cwd = os.getcwd()
|
|
|
|
self.test_dir = os.path.join(self.cwd, 'test')
|
|
|
|
self.stage_dir = self._prepare_stage_dir()
|
|
|
|
|
|
|
|
def _prepare_stage_dir(self):
|
|
|
|
stage_path = os.path.join(self.test_dir, 'test_data')
|
|
|
|
if os.path.exists(stage_path):
|
|
|
|
shutil.rmtree(stage_path, ignore_errors=False)
|
|
|
|
assert not os.path.exists(stage_path)
|
|
|
|
os.makedirs(stage_path)
|
|
|
|
assert os.path.exists(stage_path)
|
|
|
|
return stage_path
|
|
|
|
|
|
|
|
def _get_test_file(self, filename):
|
|
|
|
# get a file inside the test input directory
|
|
|
|
filename = os.path.join(self.test_dir, filename)
|
|
|
|
assert os.path.exists(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
|
|
|
|
|
2013-04-07 04:20:10 +02:00
|
|
|
def _run(self, module_name, module_args, background=0, check_mode=False):
|
2012-05-11 17:38:15 +02:00
|
|
|
''' run a module and get the localhost results '''
|
|
|
|
self.runner.module_name = module_name
|
|
|
|
args = ' '.join(module_args)
|
|
|
|
self.runner.module_args = args
|
|
|
|
self.runner.background = background
|
2013-04-07 04:20:10 +02:00
|
|
|
self.runner.check = check_mode
|
2012-05-11 17:38:15 +02:00
|
|
|
results = self.runner.run()
|
|
|
|
# when using nosetests this will only show up on failure
|
|
|
|
# which is pretty useful
|
2012-08-28 05:06:39 +02:00
|
|
|
assert "localhost" in results['contacted']
|
|
|
|
return results['contacted']['localhost']
|
2012-05-11 17:38:15 +02:00
|
|
|
|
2013-05-25 00:05:28 +02:00
|
|
|
def test_action_plugins(self):
|
|
|
|
result = self._run("uncategorized_plugin", [])
|
|
|
|
assert result.get("msg") == "uncategorized"
|
|
|
|
result = self._run("categorized_plugin", [])
|
|
|
|
assert result.get("msg") == "categorized"
|
|
|
|
|
2012-05-11 17:38:15 +02:00
|
|
|
def test_ping(self):
|
|
|
|
result = self._run('ping', [])
|
|
|
|
assert "ping" in result
|
|
|
|
|
|
|
|
def test_facter(self):
|
|
|
|
if not get_binary("facter"):
|
|
|
|
raise SkipTest
|
|
|
|
result = self._run('facter', [])
|
|
|
|
assert "hostname" in result
|
|
|
|
|
|
|
|
# temporarily disbabled since it occasionally hangs
|
|
|
|
# ohai's fault, setup module doesn't actually run this
|
|
|
|
# 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_command(self):
|
|
|
|
# test command module, change trigger, etc
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["/bin/echo", "hi"])
|
2012-05-11 17:38:15 +02:00
|
|
|
assert "failed" not in result
|
|
|
|
assert "msg" not in result
|
|
|
|
assert result['rc'] == 0
|
|
|
|
assert result['stdout'] == 'hi'
|
|
|
|
assert result['stderr'] == ''
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["false"])
|
2012-05-11 17:38:15 +02:00
|
|
|
assert result['rc'] == 1
|
|
|
|
assert 'failed' not in result
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["/usr/bin/this_does_not_exist", "splat"])
|
2012-05-11 17:38:15 +02:00
|
|
|
assert 'msg' in result
|
2013-12-03 02:59:32 +01:00
|
|
|
assert result.get('failed')
|
2012-05-11 17:38:15 +02:00
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('shell', ["/bin/echo", "$HOME"])
|
2012-05-11 17:38:15 +02:00
|
|
|
assert 'failed' not in result
|
|
|
|
assert result['rc'] == 0
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["creates='/tmp/ansible command test'", "chdir=/tmp", "touch", "'ansible command test'"])
|
2013-12-03 02:59:32 +01:00
|
|
|
assert result.get('changed')
|
2012-10-01 08:30:53 +02:00
|
|
|
assert result['rc'] == 0
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["creates='/tmp/ansible command test'", "false"])
|
2013-12-03 02:59:32 +01:00
|
|
|
assert result.get('skipped')
|
2012-10-01 08:30:53 +02:00
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('shell', ["removes=/tmp/ansible\\ command\\ test", "chdir=/tmp", "rm -f 'ansible command test'; echo $?"])
|
2013-12-03 02:59:32 +01:00
|
|
|
assert result.get('changed')
|
2012-10-01 08:30:53 +02:00
|
|
|
assert result['rc'] == 0
|
|
|
|
assert result['stdout'] == '0'
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('shell', ["removes=/tmp/ansible\\ command\\ test", "false"])
|
2013-12-03 02:59:32 +01:00
|
|
|
assert result.get('skipped')
|
2012-10-01 08:30:53 +02:00
|
|
|
|
2012-08-23 06:08:38 +02:00
|
|
|
def test_git(self):
|
2014-02-06 06:11:23 +01:00
|
|
|
pid = os.getpid()
|
|
|
|
git_demo = "gitdemo%d" % pid
|
|
|
|
git_dm = "gitdm%d" % pid
|
|
|
|
git_bare = "gdbare%d" % pid
|
|
|
|
git_ref = "gdref%d" % pid
|
|
|
|
git_reftest = "gdreftest%d" % pid
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_demo, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_dm, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_bare, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_ref, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_reftest, 'state=absent'])
|
|
|
|
self._run('command', ['git init %s' % git_demo, 'chdir=/tmp'])
|
|
|
|
self._run('command', ['touch a', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git add *', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git commit -m "test commit 1"', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['touch b', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git add *', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git commit -m "test commit 2"', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_dm])
|
2012-08-23 06:08:38 +02:00
|
|
|
assert result['changed']
|
|
|
|
# test the force option not set
|
2014-02-06 06:11:23 +01:00
|
|
|
self._run('file', ['path=/tmp/%s/a' % git_dm, 'state=absent'])
|
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_dm, "force=no"])
|
2012-08-23 06:08:38 +02:00
|
|
|
assert result['failed']
|
|
|
|
# test the force option when set
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_dm, "force=yes"])
|
2012-08-23 06:08:38 +02:00
|
|
|
assert result['changed']
|
2013-10-28 02:22:10 +01:00
|
|
|
# test the bare option
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_bare, "bare=yes", "remote=test"])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['changed']
|
2013-11-14 20:42:07 +01:00
|
|
|
# test a no-op fetch, add origin for el6 versions of git
|
2014-02-11 18:41:30 +01:00
|
|
|
self._run('command', ['git', 'remote', 'add', 'origin', 'file:///tmp/%s' % git_demo, 'chdir=/tmp/%s' % git_bare])
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_bare, "bare=yes"])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert not result['changed']
|
|
|
|
# test whether fetch is working for bare repos
|
2014-02-06 06:11:23 +01:00
|
|
|
self._run('command', ['touch c', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git add *', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
self._run('command', ['git commit -m "test commit 3"', 'chdir=/tmp/%s' % git_demo])
|
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_bare, "bare=yes"])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['changed']
|
|
|
|
# test reference repos
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_bare, "dest=/tmp/%s" % git_ref, "bare=yes"])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['changed']
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('git', ["repo=\"file:///tmp/%s\"" % git_demo, "dest=/tmp/%s" % git_reftest, "reference=/tmp/%s/" % git_ref])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['changed']
|
2014-02-06 06:11:23 +01:00
|
|
|
assert os.path.isfile('/tmp/%s/a' % git_reftest)
|
|
|
|
result = self._run('command', ['ls', 'chdir=/tmp/%s/objects/pack' % git_ref])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['stdout'] != ''
|
2014-02-06 06:11:23 +01:00
|
|
|
result = self._run('command', ['ls', 'chdir=/tmp/%s/.git/objects/pack' % git_reftest])
|
2013-10-28 02:22:10 +01:00
|
|
|
assert result['stdout'] == ''
|
2014-02-06 06:11:23 +01:00
|
|
|
# cleanup
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_demo, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_dm, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_bare, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_ref, 'state=absent'])
|
|
|
|
self._run('file', ['path=/tmp/%s' % git_reftest, 'state=absent'])
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2012-05-11 17:38:15 +02:00
|
|
|
def test_large_output(self):
|
2012-05-15 14:26:43 +02:00
|
|
|
large_path = "/usr/share/dict/words"
|
|
|
|
if not os.path.exists(large_path):
|
|
|
|
large_path = "/usr/share/dict/cracklib-small"
|
|
|
|
if not os.path.exists(large_path):
|
|
|
|
raise SkipTest
|
2012-05-11 17:38:15 +02:00
|
|
|
# Ensure reading a large amount of output from a command doesn't hang.
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', ["/bin/cat", large_path])
|
2012-05-11 17:38:15 +02:00
|
|
|
assert "failed" not in result
|
|
|
|
assert "msg" not in result
|
|
|
|
assert result['rc'] == 0
|
|
|
|
assert len(result['stdout']) > 100000
|
|
|
|
assert result['stderr'] == ''
|
|
|
|
|
|
|
|
def test_async(self):
|
|
|
|
# test async launch and job status
|
|
|
|
# of any particular module
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('command', [get_binary("sleep"), "3"], background=20)
|
2012-05-11 17:38:15 +02:00
|
|
|
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
|
2013-03-26 00:22:33 +01:00
|
|
|
result = self._run('async_status', ["jid=%s" % jid])
|
2012-05-11 17:38:15 +02:00
|
|
|
# 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
|
|
|
|
|
2012-06-12 08:09:54 +02:00
|
|
|
def test_assemble(self):
|
|
|
|
input = self._get_test_file('assemble.d')
|
|
|
|
output = self._get_stage_file('sample.out')
|
|
|
|
result = self._run('assemble', [
|
|
|
|
"src=%s" % input,
|
|
|
|
"dest=%s" % output,
|
|
|
|
])
|
|
|
|
assert os.path.exists(output)
|
|
|
|
out = file(output).read()
|
|
|
|
assert out.find("first") != -1
|
|
|
|
assert out.find("second") != -1
|
|
|
|
assert out.find("third") != -1
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed'] is True
|
2012-06-12 08:09:54 +02:00
|
|
|
assert 'md5sum' in result
|
|
|
|
assert 'failed' not in result
|
|
|
|
result = self._run('assemble', [
|
|
|
|
"src=%s" % input,
|
|
|
|
"dest=%s" % output,
|
|
|
|
])
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed'] is False
|
2013-01-03 23:09:29 +01:00
|
|
|
|
2013-02-21 07:05:06 +01:00
|
|
|
def test_lineinfile(self):
|
2013-04-05 19:13:33 +02:00
|
|
|
# Unit tests for the lineinfile module, without backref features.
|
2013-02-23 18:20:38 +01:00
|
|
|
sampleroot = 'rocannon'
|
|
|
|
sample_origin = self._get_test_file(sampleroot + '.txt')
|
|
|
|
sample = self._get_stage_file(sampleroot + '.out' + '.txt')
|
2013-03-26 00:22:33 +01:00
|
|
|
shutil.copy(sample_origin, sample)
|
2013-02-21 07:05:06 +01:00
|
|
|
# The order of the test cases is important
|
|
|
|
|
|
|
|
# defaults to insertafter at the end of the file
|
|
|
|
testline = 'First: Line added by default at the end of the file.'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^First: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line added'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact[-1] == testline
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
# run a second time, verify only one line has been added
|
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert not result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == ''
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
# insertafter with EOF
|
|
|
|
testline = 'Second: Line added with insertafter=EOF'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter=EOF",
|
|
|
|
"regexp='^Second: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line added'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact[-1] == testline
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
# with invalid insertafter regex
|
2013-03-26 00:22:33 +01:00
|
|
|
# If the regexp doesn't match and the insertafter doesn't match,
|
|
|
|
# do nothing.
|
2013-02-21 07:05:06 +01:00
|
|
|
testline = 'Third: Line added with an invalid insertafter regex'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='^abcdefgh'",
|
|
|
|
"regexp='^Third: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert not result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
|
|
|
|
# with an insertafter regex
|
2013-03-26 00:22:33 +01:00
|
|
|
# The regexp doesn't match, but the insertafter is specified and does,
|
|
|
|
# so insert after insertafter.
|
2013-02-21 07:05:06 +01:00
|
|
|
testline = 'Fourth: Line added with a valid insertafter regex'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='^receive messages to '",
|
|
|
|
"regexp='^Fourth: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line added'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
idx = artifact.index('receive messages to and from a corresponding device over any distance')
|
|
|
|
assert artifact[idx + 1] == testline
|
|
|
|
|
|
|
|
# replacement of a line from a regex
|
|
|
|
# we replace the line, so we need to get its idx before the run
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
target_line = 'combination of microphone, speaker, keyboard and display. It can send and'
|
|
|
|
idx = artifact.index(target_line)
|
|
|
|
|
|
|
|
testline = 'Fith: replacement of a line: combination of microphone'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='combination of microphone'",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line replaced'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
assert artifact.index(testline) == idx
|
|
|
|
assert target_line not in artifact
|
|
|
|
|
|
|
|
# removal of a line
|
|
|
|
# we replace the line, so we need to get its idx before the run
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
target_line = 'receive messages to and from a corresponding device over any distance'
|
|
|
|
idx = artifact.index(target_line)
|
|
|
|
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^receive messages to and from '",
|
|
|
|
"state=absent"
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert target_line not in artifact
|
|
|
|
|
|
|
|
# with both insertafter and insertbefore (should fail)
|
|
|
|
testline = 'Seventh: this line should not be there'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='BOF'",
|
|
|
|
"insertbefore='BOF'",
|
|
|
|
"regexp='^communication. '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['failed']
|
2013-02-21 07:05:06 +01:00
|
|
|
|
|
|
|
# insertbefore with BOF
|
|
|
|
testline = 'Eighth: insertbefore BOF'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertbefore=BOF",
|
|
|
|
"regexp='^Eighth: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line added'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
assert artifact[0] == testline
|
|
|
|
|
|
|
|
# insertbefore with regex
|
|
|
|
testline = 'Ninth: insertbefore with a regex'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertbefore='^communication. Typically '",
|
|
|
|
"regexp='^Ninth: '",
|
|
|
|
"line='%s'" % testline
|
2013-03-26 00:22:33 +01:00
|
|
|
])
|
2013-02-21 07:05:06 +01:00
|
|
|
result = self._run(*testcase)
|
2013-03-26 00:22:33 +01:00
|
|
|
assert result['changed']
|
2013-02-21 07:05:06 +01:00
|
|
|
assert result['msg'] == 'line added'
|
2013-03-26 00:22:33 +01:00
|
|
|
artifact = [x.strip() for x in open(sample)]
|
2013-02-21 07:05:06 +01:00
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
idx = artifact.index('communication. Typically it is depicted as a lunch-box sized object with some')
|
|
|
|
assert artifact[idx - 1] == testline
|
|
|
|
|
2013-10-06 19:43:16 +02:00
|
|
|
# Testing validate
|
|
|
|
testline = 'Tenth: Testing with validate'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^Tenth: '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"validate='grep -q Tenth %s'",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert result['changed'], "File wasn't changed when it should have been"
|
|
|
|
assert result['msg'] == 'line added', "msg was incorrect"
|
|
|
|
artifact = [ x.strip() for x in open(sample) ]
|
|
|
|
assert artifact[-1] == testline
|
|
|
|
|
|
|
|
|
|
|
|
# Testing validate
|
|
|
|
testline = '#11: Testing with validate'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^#11: '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"validate='grep -q #12# %s'",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert result['failed']
|
|
|
|
|
2014-01-19 02:24:26 +01:00
|
|
|
# insert multiline at the end of the file
|
|
|
|
testline1 = '#12: The \\n character replaced with'
|
|
|
|
testline2 = 'an actual newline.'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^#12: '",
|
|
|
|
"line='%s\n%s'" % (testline1, testline2)
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert result['changed']
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert artifact[-2] == testline1
|
|
|
|
assert artifact[-1] == testline2
|
|
|
|
assert artifact.count(testline1) == 1
|
|
|
|
assert artifact.count(testline2) == 1
|
|
|
|
|
2013-02-21 07:05:06 +01:00
|
|
|
# cleanup
|
|
|
|
os.unlink(sample)
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
def test_lineinfile_backrefs(self):
|
2013-04-05 19:13:33 +02:00
|
|
|
# Unit tests for the lineinfile module, with backref features.
|
2013-03-26 00:22:33 +01:00
|
|
|
sampleroot = 'rocannon'
|
|
|
|
sample_origin = self._get_test_file(sampleroot + '.txt')
|
|
|
|
origin_lines = [line.strip() for line in open(sample_origin)]
|
|
|
|
sample = self._get_stage_file(sampleroot + '.out' + '.txt')
|
|
|
|
shutil.copy(sample_origin, sample)
|
|
|
|
# The order of the test cases is important
|
|
|
|
|
|
|
|
# The regexp doesn't match, so the line will not be added anywhere.
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1: Line added by default at the end of the file.'
|
2013-03-26 00:22:33 +01:00
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='^(First): '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['msg'] == ''
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert artifact == origin_lines
|
|
|
|
|
|
|
|
# insertafter with EOF
|
|
|
|
# The regexp doesn't match, so the line will not be added anywhere.
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1: Line added with insertafter=EOF'
|
2013-03-26 00:22:33 +01:00
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter=EOF",
|
|
|
|
"regexp='^(Second): '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['msg'] == ''
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert artifact == origin_lines
|
|
|
|
|
|
|
|
# with invalid insertafter regex
|
|
|
|
# The regexp doesn't match, so do nothing.
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1: Line added with an invalid insertafter regex'
|
2013-03-26 00:22:33 +01:00
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='^abcdefgh'",
|
|
|
|
"regexp='^(Third): '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert not result['changed']
|
|
|
|
assert artifact == origin_lines
|
|
|
|
|
|
|
|
# with an insertafter regex
|
|
|
|
# The regexp doesn't match, so do nothing.
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1: Line added with a valid insertafter regex'
|
2013-03-26 00:22:33 +01:00
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='^receive messages to '",
|
|
|
|
"regexp='^(Fourth): '",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['msg'] == ''
|
|
|
|
assert artifact == origin_lines
|
|
|
|
|
|
|
|
# replacement of a line from a regex
|
|
|
|
# we replace the line, so we need to get its idx before the run
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
target_line = 'combination of microphone, speaker, keyboard and display. It can send and'
|
|
|
|
idx = artifact.index(target_line)
|
2013-02-21 07:05:06 +01:00
|
|
|
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1 of megaphone'
|
2013-03-26 22:00:34 +01:00
|
|
|
testline_after = 'combination of megaphone'
|
2013-03-26 00:22:33 +01:00
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='(combination) of microphone'",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert result['changed']
|
|
|
|
assert result['msg'] == 'line replaced'
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert artifact.count(testline_after) == 1
|
|
|
|
assert artifact.index(testline_after) == idx
|
|
|
|
assert target_line not in artifact
|
|
|
|
|
2013-03-26 22:00:34 +01:00
|
|
|
# Go again, should be unchanged now.
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r'\1 of megaphone'
|
2013-03-26 22:00:34 +01:00
|
|
|
testline_after = 'combination of megaphone'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"regexp='(combination) of megaphone'",
|
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['msg'] == ''
|
|
|
|
|
|
|
|
# Try a numeric, named capture group example.
|
|
|
|
f = open(sample, 'a+')
|
|
|
|
f.write("1 + 1 = 3" + os.linesep)
|
|
|
|
f.close()
|
2013-05-24 00:59:10 +02:00
|
|
|
testline = r"2 + \g<num> = 3"
|
2013-03-26 22:00:34 +01:00
|
|
|
testline_after = "2 + 1 = 3"
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
2013-05-24 00:59:10 +02:00
|
|
|
r"regexp='1 \+ (?P<num>\d) = 3'",
|
2013-03-26 22:00:34 +01:00
|
|
|
"line='%s'" % testline,
|
|
|
|
"backrefs=yes",
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert result['changed']
|
|
|
|
assert result['msg'] == 'line replaced'
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
assert '1 + 1 = 3' not in artifact
|
|
|
|
assert testline_after == artifact[-1]
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
# with both insertafter and insertbefore (should fail)
|
|
|
|
testline = 'Seventh: this line should not be there'
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
"dest=%s" % sample,
|
|
|
|
"insertafter='BOF'",
|
|
|
|
"insertbefore='BOF'",
|
|
|
|
"regexp='^communication. '",
|
|
|
|
"line='%s'" % testline
|
|
|
|
])
|
|
|
|
result = self._run(*testcase)
|
|
|
|
assert result['failed']
|
|
|
|
|
|
|
|
os.unlink(sample)
|