2016-05-06 15:37:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Matt Martz <matt@sivel.net>
|
|
|
|
# Copyright (C) 2016 Rackspace US, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import imp
|
|
|
|
import sys
|
|
|
|
|
2018-01-11 23:41:53 +00:00
|
|
|
from contextlib import contextmanager
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
import mock
|
|
|
|
|
2018-01-11 23:41:53 +00:00
|
|
|
from ansible.module_utils.six import reraise
|
|
|
|
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
MODULE_CLASSES = [
|
|
|
|
'ansible.module_utils.basic.AnsibleModule',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleModuleCallError(RuntimeError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-11 23:41:53 +00:00
|
|
|
class AnsibleModuleImportError(ImportError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2016-05-06 15:37:41 +00:00
|
|
|
def add_mocks(filename):
|
2018-01-19 22:33:16 +00:00
|
|
|
# Used to clean up imports later
|
|
|
|
pre_sys_modules = list(sys.modules.keys())
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
module_mock = mock.MagicMock()
|
|
|
|
for module_class in MODULE_CLASSES:
|
2018-03-26 17:15:32 +00:00
|
|
|
p = mock.patch('%s.__init__' % module_class, new=module_mock).start()
|
2018-01-11 23:41:53 +00:00
|
|
|
p.side_effect = AnsibleModuleCallError('AnsibleModuleCallError')
|
2018-03-26 17:15:32 +00:00
|
|
|
mock.patch('ansible.module_utils.basic._load_params').start()
|
2018-01-11 23:41:53 +00:00
|
|
|
|
|
|
|
yield module_mock
|
2016-05-06 15:37:41 +00:00
|
|
|
|
2018-03-26 17:15:32 +00:00
|
|
|
mock.patch.stopall()
|
2016-05-06 15:37:41 +00:00
|
|
|
|
2018-01-19 22:33:16 +00:00
|
|
|
# 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]
|
|
|
|
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
def get_argument_spec(filename):
|
2018-01-11 23:41:53 +00:00
|
|
|
with add_mocks(filename) as module_mock:
|
|
|
|
try:
|
|
|
|
mod = imp.load_source('module', filename)
|
|
|
|
if not module_mock.call_args:
|
|
|
|
mod.main()
|
|
|
|
except AnsibleModuleCallError:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
reraise(AnsibleModuleImportError, AnsibleModuleImportError('%s' % e), sys.exc_info()[2])
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
args, kwargs = module_mock.call_args
|
|
|
|
try:
|
2018-01-17 16:11:30 +00:00
|
|
|
return kwargs['argument_spec'], args, kwargs
|
2016-05-06 15:37:41 +00:00
|
|
|
except KeyError:
|
2018-01-17 16:11:30 +00:00
|
|
|
return args[0], args, kwargs
|
2016-05-06 15:37:41 +00:00
|
|
|
except TypeError:
|
2018-01-17 16:11:30 +00:00
|
|
|
return {}, (), {}
|