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):
|
|
|
|
gp = mock.patch('ansible.module_utils.basic.get_platform').start()
|
|
|
|
gp.return_value = 'linux'
|
|
|
|
|
|
|
|
module_mock = mock.MagicMock()
|
|
|
|
mocks = []
|
|
|
|
for module_class in MODULE_CLASSES:
|
|
|
|
mocks.append(
|
2018-01-11 23:41:53 +00:00
|
|
|
mock.patch('%s.__init__' % module_class, new=module_mock)
|
2016-05-06 15:37:41 +00:00
|
|
|
)
|
|
|
|
for m in mocks:
|
|
|
|
p = m.start()
|
2018-01-11 23:41:53 +00:00
|
|
|
p.side_effect = AnsibleModuleCallError('AnsibleModuleCallError')
|
|
|
|
mocks.append(
|
|
|
|
mock.patch('ansible.module_utils.basic._load_params').start()
|
|
|
|
)
|
|
|
|
|
|
|
|
yield module_mock
|
2016-05-06 15:37:41 +00:00
|
|
|
|
|
|
|
for m in mocks:
|
|
|
|
m.stop()
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
return kwargs['argument_spec']
|
|
|
|
except KeyError:
|
|
|
|
return args[0]
|
|
|
|
except TypeError:
|
|
|
|
return {}
|