Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
943829c9
Commit
943829c9
authored
Feb 20, 2013
by
Yves Dorfsman
Committed by
Michael DeHaan
Feb 23, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test cases for lineinfile.
parent
9b32ab7e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
163 additions
and
0 deletions
+163
-0
test/TestRunner.py
+157
-0
test/rocannon.txt
+6
-0
No files found.
test/TestRunner.py
View file @
943829c9
...
...
@@ -290,3 +290,160 @@ class TestRunner(unittest.TestCase):
print
result
assert
result
[
'changed'
]
==
False
def
test_lineinfile
(
self
):
samplefn
=
'rocannon.txt'
sample
=
os
.
path
.
join
(
'test'
,
'artifact'
+
samplefn
)
shutil
.
copy
(
os
.
path
.
join
(
'test'
,
samplefn
),
sample
)
# 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
])
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
# run a second time, verify only one line has been added
result
=
self
.
_run
(
*
testcase
)
assert
result
[
'changed'
]
==
False
assert
result
[
'msg'
]
==
''
artifact
=
[
x
.
strip
()
for
x
in
open
(
sample
)
.
readlines
()
]
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
])
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
# with invalid insertafter regex
testline
=
'Third: Line added with an invalid insertafter regex'
testcase
=
(
'lineinfile'
,
[
"dest=
%
s"
%
sample
,
"insertafter='^abcdefgh'"
,
"regexp='^Third: '"
,
"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
# with an insertafter regex
testline
=
'Fourth: Line added with a valid insertafter regex'
testcase
=
(
'lineinfile'
,
[
"dest=
%
s"
%
sample
,
"insertafter='^receive messages to '"
,
"regexp='^Fourth: '"
,
"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
.
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
()
]
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
])
result
=
self
.
_run
(
*
testcase
)
assert
result
[
'changed'
]
==
True
assert
result
[
'msg'
]
==
'line replaced'
artifact
=
[
x
.
strip
()
for
x
in
open
(
sample
)
.
readlines
()
]
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
()
]
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"
])
result
=
self
.
_run
(
*
testcase
)
assert
result
[
'changed'
]
==
True
artifact
=
[
x
.
strip
()
for
x
in
open
(
sample
)
.
readlines
()
]
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'
]
==
True
# insertbefore with BOF
testline
=
'Eighth: insertbefore BOF'
testcase
=
(
'lineinfile'
,
[
"dest=
%
s"
%
sample
,
"insertbefore=BOF"
,
"regexp='^Eighth: '"
,
"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
.
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
])
result
=
self
.
_run
(
*
testcase
)
assert
result
[
'changed'
]
==
True
assert
result
[
'msg'
]
==
'line added'
artifact
=
[
x
.
strip
()
for
x
in
open
(
sample
)
.
readlines
()
]
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
# cleanup
os
.
unlink
(
sample
)
test/rocannon.txt
0 → 100644
View file @
943829c9
An ansible is a fictitious machine capable of instantaneous or superluminal
communication. Typically it is depicted as a lunch-box sized object with some
combination of microphone, speaker, keyboard and display. It can send and
receive messages to and from a corresponding device over any distance
whatsoever with no delay. Ansibles occur as plot devices in science fiction
literature.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment