|
|
|
@ -10,10 +10,10 @@ import os
|
|
|
|
|
import shutil
|
|
|
|
|
import time
|
|
|
|
|
import tempfile
|
|
|
|
|
import urllib2
|
|
|
|
|
|
|
|
|
|
from nose.plugins.skip import SkipTest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_binary(name):
|
|
|
|
|
for directory in os.environ["PATH"].split(os.pathsep):
|
|
|
|
|
path = os.path.join(directory, name)
|
|
|
|
@ -21,6 +21,7 @@ def get_binary(name):
|
|
|
|
|
return path
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRunner(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
@ -110,13 +111,13 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
data_out = file(output).read()
|
|
|
|
|
assert data_in == data_out
|
|
|
|
|
assert 'failed' not in result
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed'] is True
|
|
|
|
|
assert 'md5sum' in result
|
|
|
|
|
result = self._run('copy', [
|
|
|
|
|
"src=%s" % input_,
|
|
|
|
|
"dest=%s" % output,
|
|
|
|
|
])
|
|
|
|
|
assert result['changed'] == False
|
|
|
|
|
assert result['changed'] is False
|
|
|
|
|
|
|
|
|
|
def test_command(self):
|
|
|
|
|
# test command module, change trigger, etc
|
|
|
|
@ -191,7 +192,6 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
assert not os.path.exists(filedemo)
|
|
|
|
|
assert not self._run('file', ['dest=' + filedemo, 'state=absent'])['changed']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filedemo = tempfile.mkdtemp()
|
|
|
|
|
assert self._run('file', ['dest=' + filedemo, 'state=file'])['failed']
|
|
|
|
|
assert os.path.isdir(filedemo)
|
|
|
|
@ -209,7 +209,6 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
assert not os.path.exists(filedemo)
|
|
|
|
|
assert not self._run('file', ['dest=' + filedemo, 'state=absent'])['changed']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tmp_dir = tempfile.mkdtemp()
|
|
|
|
|
filedemo = os.path.join(tmp_dir, 'link')
|
|
|
|
|
os.symlink('/dev/zero', filedemo)
|
|
|
|
@ -263,7 +262,7 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
def test_fetch(self):
|
|
|
|
|
input_ = self._get_test_file('sample.j2')
|
|
|
|
|
output = os.path.join(self.stage_dir, 'localhost', input_)
|
|
|
|
|
result = self._run('fetch', [ "src=%s" % input_, "dest=%s" % self.stage_dir ])
|
|
|
|
|
self._run('fetch', ["src=%s" % input_, "dest=%s" % self.stage_dir])
|
|
|
|
|
assert os.path.exists(output)
|
|
|
|
|
assert open(input_).read() == open(output).read()
|
|
|
|
|
|
|
|
|
@ -280,7 +279,7 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
assert out.find("first") != -1
|
|
|
|
|
assert out.find("second") != -1
|
|
|
|
|
assert out.find("third") != -1
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed'] is True
|
|
|
|
|
assert 'md5sum' in result
|
|
|
|
|
assert 'failed' not in result
|
|
|
|
|
result = self._run('assemble', [
|
|
|
|
@ -288,9 +287,10 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"dest=%s" % output,
|
|
|
|
|
])
|
|
|
|
|
print result
|
|
|
|
|
assert result['changed'] == False
|
|
|
|
|
assert result['changed'] is False
|
|
|
|
|
|
|
|
|
|
def test_lineinfile(self):
|
|
|
|
|
"""Unit tests for the lineinfile module, without backref features."""
|
|
|
|
|
sampleroot = 'rocannon'
|
|
|
|
|
sample_origin = self._get_test_file(sampleroot + '.txt')
|
|
|
|
|
sample = self._get_stage_file(sampleroot + '.out' + '.txt')
|
|
|
|
@ -305,17 +305,17 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
assert artifact[-1] == testline
|
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
|
|
# run a second time, verify only one line has been added
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == False
|
|
|
|
|
assert not result['changed']
|
|
|
|
|
assert result['msg'] == ''
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
|
|
# insertafter with EOF
|
|
|
|
@ -327,13 +327,15 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
assert artifact[-1] == testline
|
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
|
|
|
|
|
# with invalid insertafter regex
|
|
|
|
|
# If the regexp doesn't match and the insertafter doesn't match,
|
|
|
|
|
# do nothing.
|
|
|
|
|
testline = 'Third: Line added with an invalid insertafter regex'
|
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
|
"dest=%s" % sample,
|
|
|
|
@ -342,13 +344,11 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
assert artifact[-1] == testline
|
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
assert not result['changed']
|
|
|
|
|
|
|
|
|
|
# with an insertafter regex
|
|
|
|
|
# The regexp doesn't match, but the insertafter is specified and does,
|
|
|
|
|
# so insert after insertafter.
|
|
|
|
|
testline = 'Fourth: Line added with a valid insertafter regex'
|
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
|
"dest=%s" % sample,
|
|
|
|
@ -357,16 +357,16 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
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
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
@ -377,16 +377,16 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line replaced'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
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
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
target_line = 'receive messages to and from a corresponding device over any distance'
|
|
|
|
|
idx = artifact.index(target_line)
|
|
|
|
|
|
|
|
|
@ -396,11 +396,10 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"state=absent"
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
assert result['changed']
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
assert target_line not in artifact
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# with both insertafter and insertbefore (should fail)
|
|
|
|
|
testline = 'Seventh: this line should not be there'
|
|
|
|
|
testcase = ('lineinfile', [
|
|
|
|
@ -411,7 +410,7 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['failed'] == True
|
|
|
|
|
assert result['failed']
|
|
|
|
|
|
|
|
|
|
# insertbefore with BOF
|
|
|
|
|
testline = 'Eighth: insertbefore BOF'
|
|
|
|
@ -422,9 +421,9 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
assert artifact.count(testline) == 1
|
|
|
|
|
assert artifact[0] == testline
|
|
|
|
|
|
|
|
|
@ -437,9 +436,9 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
"line='%s'" % testline
|
|
|
|
|
])
|
|
|
|
|
result = self._run(*testcase)
|
|
|
|
|
assert result['changed'] == True
|
|
|
|
|
assert result['changed']
|
|
|
|
|
assert result['msg'] == 'line added'
|
|
|
|
|
artifact = [ x.strip() for x in open(sample).readlines() ]
|
|
|
|
|
artifact = [x.strip() for x in open(sample)]
|
|
|
|
|
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
|
|
|
|
@ -447,4 +446,106 @@ class TestRunner(unittest.TestCase):
|
|
|
|
|
# cleanup
|
|
|
|
|
os.unlink(sample)
|
|
|
|
|
|
|
|
|
|
def test_lineinfile_backrefs(self):
|
|
|
|
|
"""Unit tests for the lineinfile module, with backref features."""
|
|
|
|
|
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.
|
|
|
|
|
testline = '\\1: Line added by default at the end of the file.'
|
|
|
|
|
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.
|
|
|
|
|
testline = '\\1: Line added with insertafter=EOF'
|
|
|
|
|
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.
|
|
|
|
|
testline = '\\1: Line added with an invalid insertafter regex'
|
|
|
|
|
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.
|
|
|
|
|
testline = '\\1: Line added with a valid insertafter regex'
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
testline = '\\\\1 of microphone'
|
|
|
|
|
testline_after = 'combination of microphone'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|