@ -31,8 +31,7 @@ try:
except ImportError :
except ImportError :
import __builtin__ as builtins
import __builtin__ as builtins
from ansible . compat . six import PY3
from units . mock . procenv import swap_stdin_and_argv
from ansible . utils . unicode import to_bytes
from ansible . compat . tests import unittest
from ansible . compat . tests import unittest
from ansible . compat . tests . mock import patch , MagicMock , mock_open , Mock , call
from ansible . compat . tests . mock import patch , MagicMock , mock_open , Mock , call
@ -42,10 +41,14 @@ realimport = builtins.__import__
class TestModuleUtilsBasic ( unittest . TestCase ) :
class TestModuleUtilsBasic ( unittest . TestCase ) :
def setUp ( self ) :
def setUp ( self ) :
self . real_stdin = sys . stdin
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
# unittest doesn't have a clean place to use a context manager, so we have to enter/exit manually
self . stdin_swap = swap_stdin_and_argv ( stdin_data = args )
self . stdin_swap . __enter__ ( )
def tearDown ( self ) :
def tearDown ( self ) :
sys . stdin = self . real_stdin
# unittest doesn't have a clean place to use a context manager, so we have to enter/exit manually
self . stdin_swap . __exit__ ( None , None , None )
def clear_modules ( self , mods ) :
def clear_modules ( self , mods ) :
for mod in mods :
for mod in mods :
@ -271,13 +274,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_creation ( self ) :
def test_module_utils_basic_ansible_module_creation ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -293,94 +289,71 @@ class TestModuleUtilsBasic(unittest.TestCase):
# should test ok
# should test ok
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " foo " : " hello " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " foo " : " hello " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
with swap_stdin_and_argv ( stdin_data = args ) :
argument_spec = arg_spec ,
am = basic . AnsibleModule (
mutually_exclusive = mut_ex ,
argument_spec = arg_spec ,
required_together = req_to ,
mutually_exclusive = mut_ex ,
no_log = True ,
required_together = req_to ,
check_invalid_arguments = False ,
no_log = True ,
add_file_common_args = True ,
check_invalid_arguments = False ,
supports_check_mode = True ,
add_file_common_args = True ,
)
supports_check_mode = True ,
)
# FIXME: add asserts here to verify the basic config
# FIXME: add asserts here to verify the basic config
# fail, because a required param was not specified
# fail, because a required param was not specified
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
with swap_stdin_and_argv ( stdin_data = args ) :
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
self . assertRaises (
else :
SystemExit ,
sys . stdin = BytesIO ( to_bytes ( args ) )
basic . AnsibleModule ,
argument_spec = arg_spec ,
self . assertRaises (
mutually_exclusive = mut_ex ,
SystemExit ,
required_together = req_to ,
basic . AnsibleModule ,
no_log = True ,
argument_spec = arg_spec ,
check_invalid_arguments = False ,
mutually_exclusive = mut_ex ,
add_file_common_args = True ,
required_together = req_to ,
supports_check_mode = True ,
no_log = True ,
)
check_invalid_arguments = False ,
add_file_common_args = True ,
supports_check_mode = True ,
)
# fail because of mutually exclusive parameters
# fail because of mutually exclusive parameters
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " foo " : " hello " , " bar " : " bad " , " bam " : " bad " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " foo " : " hello " , " bar " : " bad " , " bam " : " bad " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
with swap_stdin_and_argv ( stdin_data = args ) :
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
self . assertRaises (
else :
SystemExit ,
sys . stdin = BytesIO ( to_bytes ( args ) )
basic . AnsibleModule ,
argument_spec = arg_spec ,
self . assertRaises (
mutually_exclusive = mut_ex ,
SystemExit ,
required_together = req_to ,
basic . AnsibleModule ,
no_log = True ,
argument_spec = arg_spec ,
check_invalid_arguments = False ,
mutually_exclusive = mut_ex ,
add_file_common_args = True ,
required_together = req_to ,
supports_check_mode = True ,
no_log = True ,
)
check_invalid_arguments = False ,
add_file_common_args = True ,
supports_check_mode = True ,
)
# fail because a param required due to another param was not specified
# fail because a param required due to another param was not specified
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " bam " : " bad " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { " bam " : " bad " } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
with swap_stdin_and_argv ( stdin_data = args ) :
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
self . assertRaises (
else :
SystemExit ,
sys . stdin = BytesIO ( to_bytes ( args ) )
basic . AnsibleModule ,
argument_spec = arg_spec ,
self . assertRaises (
mutually_exclusive = mut_ex ,
SystemExit ,
required_together = req_to ,
basic . AnsibleModule ,
no_log = True ,
argument_spec = arg_spec ,
check_invalid_arguments = False ,
mutually_exclusive = mut_ex ,
add_file_common_args = True ,
required_together = req_to ,
supports_check_mode = True ,
no_log = True ,
)
check_invalid_arguments = False ,
add_file_common_args = True ,
supports_check_mode = True ,
)
def test_module_utils_basic_ansible_module_load_file_common_arguments ( self ) :
def test_module_utils_basic_ansible_module_load_file_common_arguments ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -429,13 +402,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_selinux_mls_enabled ( self ) :
def test_module_utils_basic_ansible_module_selinux_mls_enabled ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -455,13 +421,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
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
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -475,13 +434,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_selinux_enabled ( self ) :
def test_module_utils_basic_ansible_module_selinux_enabled ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -513,13 +465,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
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
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -555,13 +500,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
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
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -604,59 +542,49 @@ class TestModuleUtilsBasic(unittest.TestCase):
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { " SELINUX_SPECIAL_FS " : " nfs,nfsd,foos " } ) )
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { " SELINUX_SPECIAL_FS " : " nfs,nfsd,foos " } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
with swap_stdin_and_argv ( stdin_data = args ) :
argument_spec = dict ( ) ,
)
print ( am . constants )
def _mock_find_mount_point ( path ) :
am = basic . AnsibleModule (
if path . startswith ( ' /some/path ' ) :
argument_spec = dict ( ) ,
return ' /some/path '
)
elif path . startswith ( ' /weird/random/fstype ' ) :
print ( am . constants )
return ' /weird/random/fstype '
return ' / '
am . find_mount_point = MagicMock ( side_effect = _mock_find_mount_point )
def _mock_find_mount_point ( path ) :
am . selinux_context = MagicMock ( return_value = [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] )
if path . startswith ( ' /some/path ' ) :
return ' /some/path '
elif path . startswith ( ' /weird/random/fstype ' ) :
return ' /weird/random/fstype '
return ' / '
m = mock_open ( )
am . find_mount_point = MagicMock ( side_effect = _mock_find_mount_point )
m . side_effect = OSError
am . selinux_context = MagicMock ( return_value = [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] )
with patch . object ( builtins , ' open ' , m , create = True ) :
m = mock_open ( )
self . assertEqual ( am . is_special_selinux_path ( ' /some/path/that/should/be/nfs ' ) , ( False , None ) )
m . side_effect = OSError
mount_data = [
with patch . object ( builtins , ' open ' , m , create = True ) :
' /dev/disk1 / ext4 rw,seclabel,relatime,data=ordered 0 0 \n ' ,
self . assertEqual ( am . is_special_selinux_path ( ' /some/path/that/should/be/nfs ' ) , ( False , None ) )
' 1.1.1.1:/path/to/nfs /some/path nfs ro 0 0 \n ' ,
' whatever /weird/random/fstype foos rw 0 0 \n ' ,
]
# mock_open has a broken readlines() implementation apparently...
mount_data = [
# this should work by default but doesn't, so we fix it
' /dev/disk1 / ext4 rw,seclabel,relatime,data=ordered 0 0 \n ' ,
m = mock_open ( read_data = ' ' . join ( mount_data ) )
' 1.1.1.1:/path/to/nfs /some/path nfs ro 0 0 \n ' ,
m . return_value . readlines . return_value = mount_data
' whatever /weird/random/fstype foos rw 0 0 \n ' ,
]
with patch . object ( builtins , ' open ' , m , create = True ) :
# mock_open has a broken readlines() implementation apparently...
self . assertEqual ( am . is_special_selinux_path ( ' /some/random/path ' ) , ( False , None ) )
# this should work by default but doesn't, so we fix it
self . assertEqual ( am . is_special_selinux_path ( ' /some/path/that/should/be/nfs ' ) , ( True , [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] ) )
m = mock_open ( read_data = ' ' . join ( mount_data ) )
self . assertEqual ( am . is_special_selinux_path ( ' /weird/random/fstype/path ' ) , ( True , [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] ) )
m . return_value . readlines . return_value = mount_data
with patch . object ( builtins , ' open ' , m , create = True ) :
self . assertEqual ( am . is_special_selinux_path ( ' /some/random/path ' ) , ( False , None ) )
self . assertEqual ( am . is_special_selinux_path ( ' /some/path/that/should/be/nfs ' ) , ( True , [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] ) )
self . assertEqual ( am . is_special_selinux_path ( ' /weird/random/fstype/path ' ) , ( True , [ ' foo_u ' , ' foo_r ' , ' foo_t ' , ' s0 ' ] ) )
def test_module_utils_basic_ansible_module_to_filesystem_str ( self ) :
def test_module_utils_basic_ansible_module_to_filesystem_str ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -667,13 +595,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_user_and_group ( self ) :
def test_module_utils_basic_ansible_module_user_and_group ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -688,13 +609,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_find_mount_point ( self ) :
def test_module_utils_basic_ansible_module_find_mount_point ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -718,13 +632,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_set_context_if_different ( self ) :
def test_module_utils_basic_ansible_module_set_context_if_different ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -769,13 +676,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
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
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -814,13 +714,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_set_group_if_different ( self ) :
def test_module_utils_basic_ansible_module_set_group_if_different ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -859,13 +752,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
def test_module_utils_basic_ansible_module_set_mode_if_different ( self ) :
def test_module_utils_basic_ansible_module_set_mode_if_different ( self ) :
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -953,13 +839,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)
@ -1137,13 +1016,6 @@ class TestModuleUtilsBasic(unittest.TestCase):
from ansible . module_utils import basic
from ansible . module_utils import basic
args = json . dumps ( dict ( ANSIBLE_MODULE_ARGS = { } , ANSIBLE_MODULE_CONSTANTS = { } ) )
if PY3 :
sys . stdin = StringIO ( args )
sys . stdin . buffer = BytesIO ( to_bytes ( args ) )
else :
sys . stdin = BytesIO ( to_bytes ( args ) )
am = basic . AnsibleModule (
am = basic . AnsibleModule (
argument_spec = dict ( ) ,
argument_spec = dict ( ) ,
)
)