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
44afa7fa
Commit
44afa7fa
authored
Oct 25, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updating Role class for new DataLoader stuff
parent
7cb489ec
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
13 deletions
+110
-13
v2/ansible/playbook/base.py
+1
-1
v2/ansible/playbook/role.py
+6
-7
v2/test/playbook/test_role.py
+103
-5
No files found.
v2/ansible/playbook/base.py
View file @
44afa7fa
...
...
@@ -35,7 +35,7 @@ class Base:
def
__init__
(
self
,
loader
=
DataLoader
):
# the data loader class is used to parse data from strings and files
self
.
_loader
=
loader
self
.
_loader
=
loader
()
# each class knows attributes set upon it, see Task.py for example
self
.
_attributes
=
dict
()
...
...
v2/ansible/playbook/role.py
View file @
44afa7fa
...
...
@@ -38,16 +38,15 @@ class Role(Base):
_src
=
FieldAttribute
(
isa
=
'string'
)
_scm
=
FieldAttribute
(
isa
=
'string'
)
_version
=
FieldAttribute
(
isa
=
'string'
)
_params
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
_metadata
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
_task_blocks
=
FieldAttribute
(
isa
=
'list'
,
default
=
[])
_handler_blocks
=
FieldAttribute
(
isa
=
'list'
,
default
=
[])
_params
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
_metadata
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
_default_vars
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
_role_vars
=
FieldAttribute
(
isa
=
'dict'
,
default
=
dict
())
def
__init__
(
self
,
vault_password
=
None
,
loader
=
DataLoader
):
def
__init__
(
self
,
loader
=
DataLoader
):
self
.
_role_path
=
None
self
.
_vault_password
=
vault_password
super
(
Role
,
self
)
.
__init__
(
loader
=
loader
)
def
__repr__
(
self
):
...
...
@@ -57,9 +56,9 @@ class Role(Base):
return
self
.
_attributes
[
'role_name'
]
@staticmethod
def
load
(
data
,
vault_password
=
None
):
def
load
(
data
):
assert
isinstance
(
data
,
string_types
)
or
isinstance
(
data
,
dict
)
r
=
Role
(
vault_password
=
vault_password
)
r
=
Role
()
r
.
load_data
(
data
)
return
r
...
...
@@ -116,7 +115,7 @@ class Role(Base):
if
os
.
path
.
exists
(
file_path
)
and
os
.
path
.
isdir
(
file_path
):
main_file
=
self
.
_resolve_main
(
file_path
)
if
os
.
path
.
exists
(
main_file
):
return
load_data_from_file
(
main_file
,
self
.
_vault_password
)
return
self
.
_loader
.
load_from_file
(
main_file
)
return
None
def
_resolve_main
(
self
,
basepath
):
...
...
v2/test/playbook/test_role.py
View file @
44afa7fa
...
...
@@ -19,10 +19,14 @@
from
__future__
import
(
absolute_import
,
division
,
print_function
)
__metaclass__
=
type
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests.mock
import
patch
,
MagicMock
from
ansible.playbook.block
import
Block
from
ansible.playbook.role
import
Role
from
ansible.playbook.task
import
Task
from
ansible.compat.tests
import
unittest
from
ansible.parsing.yaml
import
DataLoader
class
TestRole
(
unittest
.
TestCase
):
...
...
@@ -35,6 +39,15 @@ class TestRole(unittest.TestCase):
def
test_construct_empty_block
(
self
):
r
=
Role
()
@patch.object
(
DataLoader
,
'load_from_file'
)
def
test__load_role_yaml
(
self
,
_load_from_file
):
_load_from_file
.
return_value
=
dict
(
foo
=
'bar'
)
r
=
Role
()
with
patch
(
'os.path.exists'
,
return_value
=
True
):
with
patch
(
'os.path.isdir'
,
return_value
=
True
):
res
=
r
.
_load_role_yaml
(
'/fake/path'
,
'some_subdir'
)
self
.
assertEqual
(
res
,
dict
(
foo
=
'bar'
))
def
test_role__load_list_of_blocks
(
self
):
task
=
dict
(
action
=
'test'
)
r
=
Role
()
...
...
@@ -45,8 +58,93 @@ class TestRole(unittest.TestCase):
res
=
r
.
_load_list_of_blocks
([
task
,
task
,
task
])
self
.
assertEqual
(
len
(
res
),
3
)
def
test_load_role_simple
(
self
):
pass
@patch.object
(
Role
,
'_get_role_path'
)
@patch.object
(
Role
,
'_load_role_yaml'
)
def
test_load_role_with_tasks
(
self
,
_load_role_yaml
,
_get_role_path
):
_get_role_path
.
return_value
=
(
'foo'
,
'/etc/ansible/roles/foo'
)
def
fake_load_role_yaml
(
role_path
,
subdir
):
if
role_path
==
'/etc/ansible/roles/foo'
:
if
subdir
==
'tasks'
:
return
[
dict
(
shell
=
'echo "hello world"'
)]
return
None
_load_role_yaml
.
side_effect
=
fake_load_role_yaml
r
=
Role
.
load
(
'foo'
)
self
.
assertEqual
(
len
(
r
.
task_blocks
),
1
)
assert
isinstance
(
r
.
task_blocks
[
0
],
Block
)
@patch.object
(
Role
,
'_get_role_path'
)
@patch.object
(
Role
,
'_load_role_yaml'
)
def
test_load_role_with_handlers
(
self
,
_load_role_yaml
,
_get_role_path
):
_get_role_path
.
return_value
=
(
'foo'
,
'/etc/ansible/roles/foo'
)
def
fake_load_role_yaml
(
role_path
,
subdir
):
if
role_path
==
'/etc/ansible/roles/foo'
:
if
subdir
==
'handlers'
:
return
[
dict
(
name
=
'test handler'
,
shell
=
'echo "hello world"'
)]
return
None
_load_role_yaml
.
side_effect
=
fake_load_role_yaml
r
=
Role
.
load
(
'foo'
)
self
.
assertEqual
(
len
(
r
.
handler_blocks
),
1
)
assert
isinstance
(
r
.
handler_blocks
[
0
],
Block
)
@patch.object
(
Role
,
'_get_role_path'
)
@patch.object
(
Role
,
'_load_role_yaml'
)
def
test_load_role_with_vars
(
self
,
_load_role_yaml
,
_get_role_path
):
_get_role_path
.
return_value
=
(
'foo'
,
'/etc/ansible/roles/foo'
)
def
fake_load_role_yaml
(
role_path
,
subdir
):
if
role_path
==
'/etc/ansible/roles/foo'
:
if
subdir
==
'defaults'
:
return
dict
(
foo
=
'bar'
)
elif
subdir
==
'vars'
:
return
dict
(
foo
=
'bam'
)
return
None
_load_role_yaml
.
side_effect
=
fake_load_role_yaml
r
=
Role
.
load
(
'foo'
)
self
.
assertEqual
(
r
.
default_vars
,
dict
(
foo
=
'bar'
))
self
.
assertEqual
(
r
.
role_vars
,
dict
(
foo
=
'bam'
))
@patch.object
(
Role
,
'_get_role_path'
)
@patch.object
(
Role
,
'_load_role_yaml'
)
def
test_load_role_with_metadata
(
self
,
_load_role_yaml
,
_get_role_path
):
_get_role_path
.
return_value
=
(
'foo'
,
'/etc/ansible/roles/foo'
)
def
fake_load_role_yaml
(
role_path
,
subdir
):
if
role_path
==
'/etc/ansible/roles/foo'
:
if
subdir
==
'meta'
:
return
dict
(
dependencies
=
[],
allow_duplicates
=
False
)
return
None
_load_role_yaml
.
side_effect
=
fake_load_role_yaml
r
=
Role
.
load
(
'foo'
)
self
.
assertEqual
(
r
.
metadata
,
dict
(
dependencies
=
[],
allow_duplicates
=
False
))
@patch.object
(
Role
,
'_get_role_path'
)
@patch.object
(
Role
,
'_load_role_yaml'
)
def
test_load_role_complex
(
self
,
_load_role_yaml
,
_get_role_path
):
_get_role_path
.
return_value
=
(
'foo'
,
'/etc/ansible/roles/foo'
)
def
fake_load_role_yaml
(
role_path
,
subdir
):
if
role_path
==
'/etc/ansible/roles/foo'
:
if
subdir
==
'tasks'
:
return
[
dict
(
shell
=
'echo "hello world"'
)]
return
None
_load_role_yaml
.
side_effect
=
fake_load_role_yaml
r
=
Role
.
load
(
dict
(
role
=
'foo'
))
def
test_load_role_complex
(
self
):
pass
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