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
9aa8676b
Commit
9aa8676b
authored
May 17, 2015
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More template unit tests for v2
parent
23cd3294
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
7 deletions
+33
-7
lib/ansible/plugins/lookup/file.py
+8
-4
lib/ansible/template/__init__.py
+1
-1
test/units/mock/loader.py
+6
-0
test/units/template/test_templar.py
+18
-2
No files found.
lib/ansible/plugins/lookup/file.py
View file @
9aa8676b
...
...
@@ -42,18 +42,22 @@ class LookupModule(LookupBase):
# role/files/ directory, and finally the playbook directory
# itself (which will be relative to the current working dir)
if
'role_path'
in
variables
:
relative_path
=
self
.
_loader
.
path_dwim_relative
(
variables
[
'role_path'
],
'files'
,
term
,
check
=
False
)
# FIXME: the original file stuff still needs to be worked out, but the
# playbook_dir stuff should be able to be removed as it should
# be covered by the fact that the loader contains that info
#if '_original_file' in variables:
# relative_path = self._loader.path_dwim_relative(variables['_original_file'], 'files', term, self.basedir, check=False)
#if 'playbook_dir' in variables:
# playbook_path = os.path.join(variables['playbook_dir'], term)
for
path
in
(
basedir_path
,
relative_path
,
playbook_path
):
if
path
and
os
.
path
.
exists
(
path
):
ret
.
append
(
codecs
.
open
(
path
,
encoding
=
"utf8"
)
.
read
()
.
rstrip
())
try
:
contents
=
self
.
_loader
.
_get_file_contents
(
path
)
ret
.
append
(
contents
.
rstrip
())
break
except
AnsibleParserError
:
continue
else
:
raise
AnsibleError
(
"could not locate file in lookup:
%
s"
%
term
)
...
...
lib/ansible/template/__init__.py
View file @
9aa8676b
...
...
@@ -218,7 +218,7 @@ class Templar:
# safely catch run failures per #5059
try
:
ran
=
instance
.
run
(
*
args
,
variables
=
self
.
_available_variables
,
**
kwargs
)
except
AnsibleUndefinedVariable
:
except
(
AnsibleUndefinedVariable
,
UndefinedError
)
:
raise
except
Exception
,
e
:
if
self
.
_fail_on_lookup_errors
:
...
...
test/units/mock/loader.py
View file @
9aa8676b
...
...
@@ -38,6 +38,12 @@ class DictDataLoader(DataLoader):
return
self
.
load
(
self
.
_file_mapping
[
path
],
path
)
return
None
def
_get_file_contents
(
self
,
path
):
if
path
in
self
.
_file_mapping
:
return
self
.
_file_mapping
[
path
]
else
:
raise
AnsibleParserError
(
"file not found:
%
s"
%
path
)
def
path_exists
(
self
,
path
):
return
path
in
self
.
_file_mapping
or
path
in
self
.
_known_directories
...
...
test/units/template/test_templar.py
View file @
9aa8676b
...
...
@@ -19,10 +19,13 @@
from
__future__
import
(
absolute_import
,
division
,
print_function
)
__metaclass__
=
type
from
jinja2.exceptions
import
UndefinedError
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests.mock
import
patch
,
MagicMock
from
ansible
import
constants
as
C
from
ansible.errors
import
*
from
ansible.plugins
import
filter_loader
,
lookup_loader
,
module_loader
from
ansible.plugins.strategies
import
SharedPluginLoaderObj
from
ansible.template
import
Templar
...
...
@@ -38,9 +41,11 @@ class TestTemplar(unittest.TestCase):
pass
def
test_templar_simple
(
self
):
fake_loader
=
DictDataLoader
({})
fake_loader
=
DictDataLoader
({
"/path/to/my_file.txt"
:
"foo
\n
"
,
})
shared_loader
=
SharedPluginLoaderObj
()
templar
=
Templar
(
loader
=
fake_loader
,
variables
=
dict
(
foo
=
"bar"
,
bam
=
"{{foo}}"
,
num
=
1
,
var_true
=
True
,
var_false
=
False
,
var_dict
=
dict
(
a
=
"b"
),
bad_dict
=
"{a='b'"
,
var_list
=
[
1
]))
templar
=
Templar
(
loader
=
fake_loader
,
variables
=
dict
(
foo
=
"bar"
,
bam
=
"{{foo}}"
,
num
=
1
,
var_true
=
True
,
var_false
=
False
,
var_dict
=
dict
(
a
=
"b"
),
bad_dict
=
"{a='b'"
,
var_list
=
[
1
]
,
recursive
=
"{{recursive}}"
))
# test some basic templating
self
.
assertEqual
(
templar
.
template
(
"{{foo}}"
),
"bar"
)
...
...
@@ -54,6 +59,17 @@ class TestTemplar(unittest.TestCase):
self
.
assertEqual
(
templar
.
template
(
"{{var_dict}}"
),
dict
(
a
=
"b"
))
self
.
assertEqual
(
templar
.
template
(
"{{bad_dict}}"
),
"{a='b'"
)
self
.
assertEqual
(
templar
.
template
(
"{{var_list}}"
),
[
1
])
self
.
assertEqual
(
templar
.
template
(
1
,
convert_bare
=
True
),
1
)
self
.
assertRaises
(
UndefinedError
,
templar
.
template
,
"{{bad_var}}"
)
self
.
assertEqual
(
templar
.
template
(
"{{lookup('file', '/path/to/my_file.txt')}}"
),
"foo"
)
self
.
assertRaises
(
UndefinedError
,
templar
.
template
,
"{{lookup('file', bad_var)}}"
)
self
.
assertRaises
(
AnsibleError
,
templar
.
template
,
"{{lookup('bad_lookup')}}"
)
self
.
assertRaises
(
AnsibleError
,
templar
.
template
,
"{{recursive}}"
)
self
.
assertRaises
(
AnsibleUndefinedVariable
,
templar
.
template
,
"{{foo-bar}}"
)
# test with fail_on_undefined=False
templar
=
Templar
(
loader
=
fake_loader
,
fail_on_undefined
=
False
)
self
.
assertEqual
(
templar
.
template
(
"{{bad_var}}"
),
"{{bad_var}}"
)
# test set_available_variables()
templar
.
set_available_variables
(
variables
=
dict
(
foo
=
"bam"
))
...
...
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