Clean up imports to prevent issues with mutable data being used in modules (#35112)

* Clean up imports to prevent issues with mutable data being used in modules

* Remove un-needed mock
This commit is contained in:
Matt Martz
2018-01-19 16:33:16 -06:00
committed by GitHub
parent bf1580a333
commit f9c2c9570d
2 changed files with 9 additions and 37 deletions

View File

@@ -41,8 +41,8 @@ class AnsibleModuleImportError(ImportError):
@contextmanager
def add_mocks(filename):
gp = mock.patch('ansible.module_utils.basic.get_platform').start()
gp.return_value = 'linux'
# Used to clean up imports later
pre_sys_modules = list(sys.modules.keys())
module_mock = mock.MagicMock()
mocks = []
@@ -62,6 +62,13 @@ def add_mocks(filename):
for m in mocks:
m.stop()
# Clean up imports to prevent issues with mutable data being used in modules
for k in list(sys.modules.keys()):
# It's faster if we limit to items in ansible.module_utils
# But if this causes problems later, we should remove it
if k not in pre_sys_modules and k.startswith('ansible.module_utils.'):
del sys.modules[k]
def get_argument_spec(filename):
with add_mocks(filename) as module_mock: