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
68e13126
Commit
68e13126
authored
Jun 05, 2015
by
Brian Coca
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11187 from sivel/fix-tests
Get tests passing
parents
ed57f073
49d19e82
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
58 deletions
+78
-58
test/units/module_utils/test_basic.py
+73
-53
test/units/plugins/strategies/test_strategy_base.py
+5
-5
No files found.
test/units/module_utils/test_basic.py
View file @
68e13126
...
@@ -26,7 +26,7 @@ import errno
...
@@ -26,7 +26,7 @@ import errno
from
nose.tools
import
timed
from
nose.tools
import
timed
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests.mock
import
patch
,
MagicMock
,
mock_open
from
ansible.compat.tests.mock
import
patch
,
MagicMock
,
mock_open
,
Mock
class
TestModuleUtilsBasic
(
unittest
.
TestCase
):
class
TestModuleUtilsBasic
(
unittest
.
TestCase
):
...
@@ -71,7 +71,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -71,7 +71,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
return
(
""
,
""
,
""
)
return
(
""
,
""
,
""
)
with
patch
(
'platform.linux_distribution'
,
side_effect
=
_dist
):
with
patch
(
'platform.linux_distribution'
,
side_effect
=
_dist
):
self
.
assertEqual
(
get_distribution
(),
"Amazon"
)
self
.
assertEqual
(
get_distribution
(),
"Amazon
foobar
"
)
def
_dist
(
distname
=
''
,
version
=
''
,
id
=
''
,
supported_dists
=
(),
full_distribution_name
=
1
):
def
_dist
(
distname
=
''
,
version
=
''
,
id
=
''
,
supported_dists
=
(),
full_distribution_name
=
1
):
if
supported_dists
!=
():
if
supported_dists
!=
():
...
@@ -80,7 +80,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -80,7 +80,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
return
(
""
,
""
,
""
)
return
(
""
,
""
,
""
)
with
patch
(
'platform.linux_distribution'
,
side_effect
=
_dist
):
with
patch
(
'platform.linux_distribution'
,
side_effect
=
_dist
):
self
.
assertEqual
(
get_distribution
(),
"
OtherLinux
"
)
self
.
assertEqual
(
get_distribution
(),
"
Bar
"
)
with
patch
(
'platform.linux_distribution'
,
side_effect
=
Exception
(
"boo"
)):
with
patch
(
'platform.linux_distribution'
,
side_effect
=
Exception
(
"boo"
)):
with
patch
(
'platform.dist'
,
return_value
=
(
"bar"
,
"2"
,
"Two"
)):
with
patch
(
'platform.dist'
,
return_value
=
(
"bar"
,
"2"
,
"Two"
)):
...
@@ -356,10 +356,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -356,10 +356,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
False
)
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
False
)
basic
.
HAVE_SELINUX
=
True
basic
.
HAVE_SELINUX
=
True
with
patch
(
'selinux.is_selinux_mls_enabled'
,
return_value
=
0
):
basic
.
selinux
=
Mock
()
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
False
)
with
patch
.
dict
(
'sys.modules'
,
{
'selinux'
:
basic
.
selinux
}):
with
patch
(
'selinux.is_selinux_mls_enabled'
,
return_value
=
1
):
with
patch
(
'selinux.is_selinux_mls_enabled'
,
return_value
=
0
):
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
True
)
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
False
)
with
patch
(
'selinux.is_selinux_mls_enabled'
,
return_value
=
1
):
self
.
assertEqual
(
am
.
selinux_mls_enabled
(),
True
)
delattr
(
basic
,
'selinux'
)
def
test_module_utils_basic_ansible_module_selinux_initial_context
(
self
):
def
test_module_utils_basic_ansible_module_selinux_initial_context
(
self
):
from
ansible.module_utils
import
basic
from
ansible.module_utils
import
basic
...
@@ -399,10 +402,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -399,10 +402,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
# finally we test the case where the python selinux lib is installed,
# finally we test the case where the python selinux lib is installed,
# and both possibilities there (enabled vs. disabled)
# and both possibilities there (enabled vs. disabled)
basic
.
HAVE_SELINUX
=
True
basic
.
HAVE_SELINUX
=
True
with
patch
(
'selinux.is_selinux_enabled'
,
return_value
=
0
):
basic
.
selinux
=
Mock
()
self
.
assertEqual
(
am
.
selinux_enabled
(),
False
)
with
patch
.
dict
(
'sys.modules'
,
{
'selinux'
:
basic
.
selinux
}):
with
patch
(
'selinux.is_selinux_enabled'
,
return_value
=
1
):
with
patch
(
'selinux.is_selinux_enabled'
,
return_value
=
0
):
self
.
assertEqual
(
am
.
selinux_enabled
(),
True
)
self
.
assertEqual
(
am
.
selinux_enabled
(),
False
)
with
patch
(
'selinux.is_selinux_enabled'
,
return_value
=
1
):
self
.
assertEqual
(
am
.
selinux_enabled
(),
True
)
delattr
(
basic
,
'selinux'
)
def
test_module_utils_basic_ansible_module_selinux_default_context
(
self
):
def
test_module_utils_basic_ansible_module_selinux_default_context
(
self
):
from
ansible.module_utils
import
basic
from
ansible.module_utils
import
basic
...
@@ -422,18 +428,23 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -422,18 +428,23 @@ class TestModuleUtilsBasic(unittest.TestCase):
# all following tests assume the python selinux bindings are installed
# all following tests assume the python selinux bindings are installed
basic
.
HAVE_SELINUX
=
True
basic
.
HAVE_SELINUX
=
True
# next, we test with a mocked implementation of selinux.matchpathcon to simulate
basic
.
selinux
=
Mock
()
# an actual context being found
with
patch
(
'selinux.matchpathcon'
,
return_value
=
[
0
,
'unconfined_u:object_r:default_t:s0'
]):
with
patch
.
dict
(
'sys.modules'
,
{
'selinux'
:
basic
.
selinux
}):
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
'unconfined_u'
,
'object_r'
,
'default_t'
,
's0'
])
# next, we test with a mocked implementation of selinux.matchpathcon to simulate
# an actual context being found
with
patch
(
'selinux.matchpathcon'
,
return_value
=
[
0
,
'unconfined_u:object_r:default_t:s0'
]):
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
'unconfined_u'
,
'object_r'
,
'default_t'
,
's0'
])
# we also test the case where matchpathcon returned a failure
# we also test the case where matchpathcon returned a failure
with
patch
(
'selinux.matchpathcon'
,
return_value
=
[
-
1
,
''
]):
with
patch
(
'selinux.matchpathcon'
,
return_value
=
[
-
1
,
''
]):
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
# finally, we test where an OSError occurred during matchpathcon's call
# finally, we test where an OSError occurred during matchpathcon's call
with
patch
(
'selinux.matchpathcon'
,
side_effect
=
OSError
):
with
patch
(
'selinux.matchpathcon'
,
side_effect
=
OSError
):
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
self
.
assertEqual
(
am
.
selinux_default_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
delattr
(
basic
,
'selinux'
)
def
test_module_utils_basic_ansible_module_selinux_context
(
self
):
def
test_module_utils_basic_ansible_module_selinux_context
(
self
):
from
ansible.module_utils
import
basic
from
ansible.module_utils
import
basic
...
@@ -453,24 +464,29 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -453,24 +464,29 @@ class TestModuleUtilsBasic(unittest.TestCase):
# all following tests assume the python selinux bindings are installed
# all following tests assume the python selinux bindings are installed
basic
.
HAVE_SELINUX
=
True
basic
.
HAVE_SELINUX
=
True
# next, we test with a mocked implementation of selinux.lgetfilecon_raw to simulate
basic
.
selinux
=
Mock
()
# an actual context being found
with
patch
(
'selinux.lgetfilecon_raw'
,
return_value
=
[
0
,
'unconfined_u:object_r:default_t:s0'
]):
with
patch
.
dict
(
'sys.modules'
,
{
'selinux'
:
basic
.
selinux
}):
self
.
assertEqual
(
am
.
selinux_context
(
path
=
'/foo/bar'
),
[
'unconfined_u'
,
'object_r'
,
'default_t'
,
's0'
])
# next, we test with a mocked implementation of selinux.lgetfilecon_raw to simulate
# an actual context being found
with
patch
(
'selinux.lgetfilecon_raw'
,
return_value
=
[
0
,
'unconfined_u:object_r:default_t:s0'
]):
self
.
assertEqual
(
am
.
selinux_context
(
path
=
'/foo/bar'
),
[
'unconfined_u'
,
'object_r'
,
'default_t'
,
's0'
])
# we also test the case where matchpathcon returned a failure
# we also test the case where matchpathcon returned a failure
with
patch
(
'selinux.lgetfilecon_raw'
,
return_value
=
[
-
1
,
''
]):
with
patch
(
'selinux.lgetfilecon_raw'
,
return_value
=
[
-
1
,
''
]):
self
.
assertEqual
(
am
.
selinux_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
self
.
assertEqual
(
am
.
selinux_context
(
path
=
'/foo/bar'
),
[
None
,
None
,
None
,
None
])
# finally, we test where an OSError occurred during matchpathcon's call
# finally, we test where an OSError occurred during matchpathcon's call
e
=
OSError
()
e
=
OSError
()
e
.
errno
=
errno
.
ENOENT
e
.
errno
=
errno
.
ENOENT
with
patch
(
'selinux.lgetfilecon_raw'
,
side_effect
=
e
):
with
patch
(
'selinux.lgetfilecon_raw'
,
side_effect
=
e
):
self
.
assertRaises
(
SystemExit
,
am
.
selinux_context
,
path
=
'/foo/bar'
)
self
.
assertRaises
(
SystemExit
,
am
.
selinux_context
,
path
=
'/foo/bar'
)
e
=
OSError
()
e
=
OSError
()
with
patch
(
'selinux.lgetfilecon_raw'
,
side_effect
=
e
):
with
patch
(
'selinux.lgetfilecon_raw'
,
side_effect
=
e
):
self
.
assertRaises
(
SystemExit
,
am
.
selinux_context
,
path
=
'/foo/bar'
)
self
.
assertRaises
(
SystemExit
,
am
.
selinux_context
,
path
=
'/foo/bar'
)
delattr
(
basic
,
'selinux'
)
def
test_module_utils_basic_ansible_module_is_special_selinux_path
(
self
):
def
test_module_utils_basic_ansible_module_is_special_selinux_path
(
self
):
from
ansible.module_utils
import
basic
from
ansible.module_utils
import
basic
...
@@ -583,26 +599,30 @@ class TestModuleUtilsBasic(unittest.TestCase):
...
@@ -583,26 +599,30 @@ class TestModuleUtilsBasic(unittest.TestCase):
am
.
selinux_context
=
MagicMock
(
return_value
=
[
'bar_u'
,
'bar_r'
,
None
,
None
])
am
.
selinux_context
=
MagicMock
(
return_value
=
[
'bar_u'
,
'bar_r'
,
None
,
None
])
am
.
is_special_selinux_path
=
MagicMock
(
return_value
=
(
False
,
None
))
am
.
is_special_selinux_path
=
MagicMock
(
return_value
=
(
False
,
None
))
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
0
)
as
m
:
basic
.
selinux
=
Mock
()
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
with
patch
.
dict
(
'sys.modules'
,
{
'selinux'
:
basic
.
selinux
}):
m
.
assert_called_with
(
'/path/to/file'
,
'foo_u:foo_r:foo_t:s0'
)
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
0
)
as
m
:
m
.
reset_mock
()
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
am
.
check_mode
=
True
m
.
assert_called_with
(
'/path/to/file'
,
'foo_u:foo_r:foo_t:s0'
)
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
m
.
reset_mock
()
self
.
assertEqual
(
m
.
called
,
False
)
am
.
check_mode
=
True
am
.
check_mode
=
False
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
self
.
assertEqual
(
m
.
called
,
False
)
am
.
check_mode
=
False
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
1
)
as
m
:
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
1
)
as
m
:
self
.
assertRaises
(
SystemExit
,
am
.
set_context_if_different
,
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
True
)
self
.
assertRaises
(
SystemExit
,
am
.
set_context_if_different
,
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
True
)
with
patch
(
'selinux.lsetfilecon'
,
side_effect
=
OSError
)
as
m
:
with
patch
(
'selinux.lsetfilecon'
,
side_effect
=
OSError
)
as
m
:
self
.
assertRaises
(
SystemExit
,
am
.
set_context_if_different
,
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
True
)
self
.
assertRaises
(
SystemExit
,
am
.
set_context_if_different
,
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
True
)
am
.
is_special_selinux_path
=
MagicMock
(
return_value
=
(
True
,
[
'sp_u'
,
'sp_r'
,
'sp_t'
,
's0'
]))
am
.
is_special_selinux_path
=
MagicMock
(
return_value
=
(
True
,
[
'sp_u'
,
'sp_r'
,
'sp_t'
,
's0'
]))
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
0
)
as
m
:
with
patch
(
'selinux.lsetfilecon'
,
return_value
=
0
)
as
m
:
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
self
.
assertEqual
(
am
.
set_context_if_different
(
'/path/to/file'
,
[
'foo_u'
,
'foo_r'
,
'foo_t'
,
's0'
],
False
),
True
)
m
.
assert_called_with
(
'/path/to/file'
,
'sp_u:sp_r:sp_t:s0'
)
m
.
assert_called_with
(
'/path/to/file'
,
'sp_u:sp_r:sp_t:s0'
)
delattr
(
basic
,
'selinux'
)
def
test_module_utils_basic_ansible_module_set_owner_if_different
(
self
):
def
test_module_utils_basic_ansible_module_set_owner_if_different
(
self
):
from
ansible.module_utils
import
basic
from
ansible.module_utils
import
basic
...
...
test/units/plugins/strategies/test_strategy_base.py
View file @
68e13126
...
@@ -55,15 +55,15 @@ class TestStrategyBase(unittest.TestCase):
...
@@ -55,15 +55,15 @@ class TestStrategyBase(unittest.TestCase):
mock_conn_info
=
MagicMock
()
mock_conn_info
=
MagicMock
()
mock_tqm
.
_failed_hosts
=
[]
mock_tqm
.
_failed_hosts
=
dict
()
mock_tqm
.
_unreachable_hosts
=
[]
mock_tqm
.
_unreachable_hosts
=
dict
()
strategy_base
=
StrategyBase
(
tqm
=
mock_tqm
)
strategy_base
=
StrategyBase
(
tqm
=
mock_tqm
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
),
0
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
),
0
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
1
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
1
)
mock_tqm
.
_failed_hosts
=
[
"host1"
]
mock_tqm
.
_failed_hosts
=
dict
(
host1
=
True
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
2
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
2
)
mock_tqm
.
_unreachable_hosts
=
[
"host1"
]
mock_tqm
.
_unreachable_hosts
=
dict
(
host1
=
True
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
3
)
self
.
assertEqual
(
strategy_base
.
run
(
iterator
=
mock_iterator
,
connection_info
=
mock_conn_info
,
result
=
False
),
3
)
def
test_strategy_base_get_hosts
(
self
):
def
test_strategy_base_get_hosts
(
self
):
...
...
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