mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-03 02:36:55 +00:00
Move modules and module_utils unit tests to correct place (#81)
* Move modules and module_utils unit tests to correct place. * Update ignore.txt * Fix imports. * Fix typos. * Fix more typos.
This commit is contained in:
parent
ab3c2120fb
commit
be191cce6c
1170 changed files with 732 additions and 751 deletions
0
tests/unit/plugins/modules/network/radware/__init__.py
Normal file
0
tests/unit/plugins/modules/network/radware/__init__.py
Normal file
32
tests/unit/plugins/modules/network/radware/ct.vm
Normal file
32
tests/unit/plugins/modules/network/radware/ct.vm
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
##Copyright 2019 Radware
|
||||
##
|
||||
##Licensed under the Apache License, Version 2.0 (the "License");
|
||||
##you may not use this file except in compliance with the License.
|
||||
##You may obtain a copy of the License at
|
||||
##
|
||||
## http://www.apache.org/licenses/LICENSE-2.0
|
||||
##
|
||||
##Unless required by applicable law or agreed to in writing, software
|
||||
##distributed under the License is distributed on an "AS IS" BASIS,
|
||||
##WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
##See the License for the specific language governing permissions and
|
||||
##limitations under the License.
|
||||
|
||||
#property('description', 'Ansible Test mock')
|
||||
|
||||
#param($p1, 'int', 'in')
|
||||
#param($p2, 'int[]', 'out')
|
||||
|
||||
#set($p2 = [])
|
||||
#set($start = 2)
|
||||
#set($end = 1024)
|
||||
#set($range = [$start..$end])
|
||||
|
||||
#foreach($i in $range)
|
||||
#set($j = $adc.readBean('MOCK', $i))
|
||||
#if ($adc.isEmpty($j))
|
||||
#set($dummy = $p2.add($i))
|
||||
#if ($p2.size() == $p1)
|
||||
#break
|
||||
#end
|
||||
#end
|
||||
|
|
@ -0,0 +1,199 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2017 Radware LTD.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible 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.
|
||||
#
|
||||
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch, MagicMock
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
||||
|
||||
BASE_PARAMS = {'vdirect_ip': None, 'vdirect_user': None, 'vdirect_password': None,
|
||||
'vdirect_wait': None, 'vdirect_secondary_ip': None,
|
||||
'vdirect_https_port': None, 'vdirect_http_port': None,
|
||||
'vdirect_timeout': None, 'vdirect_use_ssl': None, 'validate_certs': None}
|
||||
|
||||
COMMIT_PARAMS = {'devices': ['adc', 'defensepro', 'vx', 'appwall'], 'apply': True, 'save': True, 'sync': True}
|
||||
|
||||
COMMIT_GET_DEVICE_200_RESULT = [200, '', '', {'type': 'AlteonPartitioned'}]
|
||||
COMMIT_GET_DEVICE_404_RESULT = [404, '', '', '']
|
||||
|
||||
COMMIT_RESULT_200 = [200, '', '', '']
|
||||
COMMIT_RESULT_204 = [204, '', '', '']
|
||||
|
||||
MODULE_RESULT = {"msg": "Requested actions were successfully performed on all devices.",
|
||||
"details": [{'device_name': 'adc', 'device_type': 'Adc',
|
||||
'apply': 'succeeded', 'save': 'succeeded', 'sync': 'succeeded'},
|
||||
{'device_name': 'defensepro', 'device_type': 'DefensePro',
|
||||
'commit': 'succeeded'},
|
||||
{'device_name': 'vx', 'device_type': 'Container',
|
||||
'apply': 'succeeded', 'save': 'succeeded'},
|
||||
{'device_name': 'appwall', 'device_type': 'AppWall',
|
||||
'commit': 'succeeded'}]}
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.RestClient')
|
||||
class RestClient:
|
||||
def __init__(self, vdirect_ip=None, vdirect_user=None, vdirect_password=None, wait=None,
|
||||
secondary_vdirect_ip=None, https_port=None, http_port=None,
|
||||
timeout=None, https=None, strict_http_results=None,
|
||||
verify=None):
|
||||
pass
|
||||
|
||||
|
||||
class DeviceMock:
|
||||
|
||||
def __init__(self, name, client):
|
||||
self.name = name
|
||||
self.client = client
|
||||
self.get_throw = False
|
||||
self.control_throw = False
|
||||
self.exception = Exception('exception message')
|
||||
self.control_result = COMMIT_RESULT_200
|
||||
|
||||
def set_control_result(self, result):
|
||||
self.control_result = result
|
||||
|
||||
def throw_exception(self, get_throw=False, control_throw=False):
|
||||
self.get_throw = get_throw
|
||||
self.control_throw = control_throw
|
||||
|
||||
def get(self, name):
|
||||
if self.get_throw:
|
||||
raise self.exception # pylint: disable=E0702
|
||||
if name == self.name:
|
||||
return COMMIT_GET_DEVICE_200_RESULT
|
||||
else:
|
||||
return COMMIT_GET_DEVICE_404_RESULT
|
||||
|
||||
def control_device(self, name, action):
|
||||
if self.control_throw:
|
||||
raise self.exception # pylint: disable=E0702
|
||||
return self.control_result
|
||||
|
||||
def control(self, name, action):
|
||||
return self.control_device(name, action)
|
||||
|
||||
|
||||
class TestManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.module_mock = MagicMock()
|
||||
self.module_mock.rest_client.RESP_STATUS = 0
|
||||
self.module_mock.rest_client.RESP_REASON = 1
|
||||
self.module_mock.rest_client.RESP_STR = 2
|
||||
self.module_mock.rest_client.RESP_DATA = 3
|
||||
|
||||
def test_missing_parameter(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client.RestClient': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_commit
|
||||
|
||||
try:
|
||||
params = BASE_PARAMS.copy()
|
||||
vdirect_commit.VdirectCommit(params)
|
||||
self.fail("KeyError was not thrown for missing parameter")
|
||||
except KeyError:
|
||||
assert True
|
||||
|
||||
def test_validate_devices(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client.RestClient': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_commit
|
||||
|
||||
BASE_PARAMS.update(COMMIT_PARAMS)
|
||||
vdirectcommit = vdirect_commit.VdirectCommit(BASE_PARAMS)
|
||||
vdirectcommit.client.adc = DeviceMock('adc', vdirectcommit.client)
|
||||
vdirectcommit.client.container = DeviceMock('vx', vdirectcommit.client)
|
||||
vdirectcommit.client.appWall = DeviceMock('appwall', vdirectcommit.client)
|
||||
vdirectcommit.client.defensePro = DeviceMock('defensepro', vdirectcommit.client)
|
||||
|
||||
vdirectcommit._validate_devices()
|
||||
assert True
|
||||
|
||||
vdirectcommit.client.adc.throw_exception(True)
|
||||
try:
|
||||
vdirectcommit._validate_devices()
|
||||
self.fail("CommitException was not thrown for device communication failure")
|
||||
except vdirect_commit.CommitException:
|
||||
assert True
|
||||
|
||||
vdirectcommit.client.adc.throw_exception(False)
|
||||
vdirectcommit.client.defensePro.throw_exception(True)
|
||||
try:
|
||||
vdirectcommit._validate_devices()
|
||||
self.fail("CommitException was not thrown for device communication failure")
|
||||
except vdirect_commit.CommitException:
|
||||
assert True
|
||||
|
||||
vdirectcommit.client.defensePro.throw_exception(False)
|
||||
|
||||
vdirectcommit.client.adc.name = 'wrong'
|
||||
try:
|
||||
vdirectcommit._validate_devices()
|
||||
self.fail("MissingDeviceException was not thrown for missing device")
|
||||
except vdirect_commit.MissingDeviceException:
|
||||
assert True
|
||||
|
||||
def test_commit(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client.RestClient': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_commit
|
||||
|
||||
BASE_PARAMS.update(COMMIT_PARAMS)
|
||||
vdirectcommit = vdirect_commit.VdirectCommit(BASE_PARAMS)
|
||||
vdirectcommit.client.adc = DeviceMock('adc', vdirectcommit.client)
|
||||
vdirectcommit.client.container = DeviceMock('vx', vdirectcommit.client)
|
||||
vdirectcommit.client.appWall = DeviceMock('appwall', vdirectcommit.client)
|
||||
vdirectcommit.client.defensePro = DeviceMock('defensepro', vdirectcommit.client)
|
||||
|
||||
res = vdirectcommit.commit()
|
||||
assert res == MODULE_RESULT
|
||||
|
||||
vdirectcommit.sync = False
|
||||
for detail in MODULE_RESULT['details']:
|
||||
if 'sync' in detail:
|
||||
detail['sync'] = vdirect_commit.NOT_PERFORMED
|
||||
res = vdirectcommit.commit()
|
||||
assert res == MODULE_RESULT
|
||||
|
||||
vdirectcommit.client.adc.control_result = COMMIT_RESULT_204
|
||||
vdirectcommit.client.adc.control_result[self.module_mock.rest_client.RESP_STATUS] = 500
|
||||
vdirectcommit.client.adc.control_result[self.module_mock.rest_client.RESP_STR] = 'Some Failure'
|
||||
MODULE_RESULT['msg'] = 'Failure occurred while performing requested actions on devices. See details'
|
||||
for detail in MODULE_RESULT['details']:
|
||||
if detail['device_name'] == 'adc':
|
||||
detail['apply'] = vdirect_commit.FAILED
|
||||
detail['failure_description'] = 'Some Failure'
|
||||
detail['save'] = vdirect_commit.NOT_PERFORMED
|
||||
detail['sync'] = vdirect_commit.NOT_PERFORMED
|
||||
res = vdirectcommit.commit()
|
||||
assert res == MODULE_RESULT
|
||||
|
||||
vdirectcommit.client.adc.throw_exception(control_throw=True)
|
||||
for detail in MODULE_RESULT['details']:
|
||||
if detail['device_name'] == 'adc':
|
||||
detail['failure_description'] = 'Exception occurred while performing apply action. ' \
|
||||
'Exception: exception message'
|
||||
res = vdirectcommit.commit()
|
||||
assert res == MODULE_RESULT
|
||||
241
tests/unit/plugins/modules/network/radware/test_vdirect_file.py
Normal file
241
tests/unit/plugins/modules/network/radware/test_vdirect_file.py
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2017 Radware LTD.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible 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.
|
||||
#
|
||||
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
from mock import patch, MagicMock
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
||||
|
||||
RESP_STATUS = 0
|
||||
RESP_REASON = 1
|
||||
RESP_STR = 2
|
||||
RESP_DATA = 3
|
||||
|
||||
NONE_PARAMS = {'vdirect_ip': None, 'vdirect_user': None, 'vdirect_password': None,
|
||||
'vdirect_wait': None, 'vdirect_secondary_ip': None,
|
||||
'vdirect_https_port': None, 'vdirect_http_port': None,
|
||||
'vdirect_timeout': None, 'vdirect_use_ssl': None, 'validate_certs': None}
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.RestClient')
|
||||
class RestClient:
|
||||
def __init__(self, vdirect_ip=None, vdirect_user=None, vdirect_password=None, wait=None,
|
||||
secondary_vdirect_ip=None, https_port=None, http_port=None,
|
||||
timeout=None, https=None, strict_http_results=None,
|
||||
verify=None):
|
||||
pass
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.Template')
|
||||
class Template:
|
||||
create_from_source_result = None
|
||||
upload_source_result = None
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_create_from_source_result(cls, result):
|
||||
Template.create_from_source_result = result
|
||||
|
||||
@classmethod
|
||||
def set_upload_source_result(cls, result):
|
||||
Template.upload_source_result = result
|
||||
|
||||
def create_from_source(self, data, name=None, tenant=None, fail_if_invalid=False):
|
||||
return Template.create_from_source_result
|
||||
|
||||
def upload_source(self, data, name=None, tenant=None, fail_if_invalid=False):
|
||||
return Template.upload_source_result
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.WorkflowTemplate')
|
||||
class WorkflowTemplate:
|
||||
create_template_from_archive_result = None
|
||||
update_archive_result = None
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_create_template_from_archive_result(cls, result):
|
||||
WorkflowTemplate.create_template_from_archive_result = result
|
||||
|
||||
@classmethod
|
||||
def set_update_archive_result(cls, result):
|
||||
WorkflowTemplate.update_archive_result = result
|
||||
|
||||
def create_template_from_archive(self, data, validate=False, fail_if_invalid=False, tenant=None):
|
||||
return WorkflowTemplate.create_template_from_archive_result
|
||||
|
||||
def update_archive(self, data, workflow_template_name):
|
||||
return WorkflowTemplate.update_archive_result
|
||||
|
||||
|
||||
class TestManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_missing_parameter(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
|
||||
try:
|
||||
params = NONE_PARAMS.copy()
|
||||
del params['vdirect_ip']
|
||||
vdirect_file.VdirectFile(params)
|
||||
self.fail("KeyError was not thrown for missing parameter")
|
||||
except KeyError:
|
||||
assert True
|
||||
|
||||
def test_wrong_file_extension(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
|
||||
module_mock.RESP_STATUS = 0
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
result = file.upload("file.??")
|
||||
assert result == vdirect_file.WRONG_EXTENSION_ERROR
|
||||
|
||||
def test_missing_file(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
try:
|
||||
file.upload("missing_file.vm")
|
||||
self.fail("IOException was not thrown for missing file")
|
||||
except IOError:
|
||||
assert True
|
||||
|
||||
def test_template_upload_create(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.Template = Template
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
Template.set_create_from_source_result([201])
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.assertEqual(result, vdirect_file.CONFIGURATION_TEMPLATE_CREATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
Template.set_create_from_source_result([400, "", "Parsing error", ""])
|
||||
try:
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.fail("InvalidSourceException was not thrown")
|
||||
except vdirect_file.InvalidSourceException:
|
||||
assert True
|
||||
|
||||
def test_template_upload_update(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.Template = Template
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
Template.set_create_from_source_result([409])
|
||||
Template.set_upload_source_result([201])
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.assertEqual(result, vdirect_file.CONFIGURATION_TEMPLATE_UPDATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
Template.set_upload_source_result([400, "", "Parsing error", ""])
|
||||
try:
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.fail("InvalidSourceException was not thrown")
|
||||
except vdirect_file.InvalidSourceException:
|
||||
assert True
|
||||
|
||||
def test_workflow_upload_create(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.WorkflowTemplate = WorkflowTemplate
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
WorkflowTemplate.set_create_template_from_archive_result([201])
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.assertEqual(result, vdirect_file.WORKFLOW_TEMPLATE_CREATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
WorkflowTemplate.set_create_template_from_archive_result([400, "", "Parsing error", ""])
|
||||
try:
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.fail("InvalidSourceException was not thrown")
|
||||
except vdirect_file.InvalidSourceException:
|
||||
assert True
|
||||
|
||||
def test_workflow_upload_update(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.WorkflowTemplate = WorkflowTemplate
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
WorkflowTemplate.set_create_template_from_archive_result([409])
|
||||
WorkflowTemplate.set_update_archive_result([201])
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.assertEqual(result, vdirect_file.WORKFLOW_TEMPLATE_UPDATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
WorkflowTemplate.set_update_archive_result([400, "", "Parsing error", ""])
|
||||
try:
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.fail("InvalidSourceException was not thrown")
|
||||
except vdirect_file.InvalidSourceException:
|
||||
assert True
|
||||
|
|
@ -0,0 +1,435 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2017 Radware LTD.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible 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.
|
||||
#
|
||||
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch, MagicMock
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
||||
|
||||
BASE_PARAMS = {'vdirect_ip': None, 'vdirect_user': None, 'vdirect_password': None,
|
||||
'vdirect_wait': None, 'vdirect_secondary_ip': None,
|
||||
'vdirect_https_port': None, 'vdirect_http_port': None,
|
||||
'vdirect_timeout': None, 'vdirect_use_ssl': None, 'validate_certs': None}
|
||||
|
||||
CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS = {
|
||||
'runnable_type': 'ConfigurationTemplate', 'runnable_name': 'runnable',
|
||||
'action_name': None, 'parameters': None}
|
||||
|
||||
WORKFLOW_TEMPLATE_RUNNABLE_PARAMS = {
|
||||
'runnable_type': 'WorkflowTemplate', 'runnable_name': 'runnable',
|
||||
'action_name': None, 'parameters': None}
|
||||
|
||||
WORKFLOW_RUNNABLE_PARAMS = {
|
||||
'runnable_type': 'Workflow', 'runnable_name': 'runnable',
|
||||
'action_name': 'one', 'parameters': None}
|
||||
|
||||
PLUGIN_RUNNABLE_PARAMS = {
|
||||
'runnable_type': 'Plugin', 'runnable_name': 'runnable',
|
||||
'action_name': 'two', 'parameters': None}
|
||||
|
||||
|
||||
WORKFLOW_RUNNABLE_OBJECT_RESULT = [200, '', '', {'names': ['runnable']}]
|
||||
ACTIONS_RESULT = [200, '', '', {'names': ['one', 'two']}]
|
||||
|
||||
|
||||
ACTIONS_PARAMS_RESULT_BASIC = [200, '', '',
|
||||
{'parameters': [
|
||||
{'name': 'pin', 'type': 'in', 'direction': 'in'},
|
||||
{'name': 'pout', 'type': 'out', 'direction': 'out'},
|
||||
{'name': 'alteon', 'type': 'alteon'}
|
||||
]}]
|
||||
ACTIONS_PARAMS_RESULT_FULL = [200, '', '',
|
||||
{'parameters': [
|
||||
{'name': 'pin', 'type': 'in', 'direction': 'in'},
|
||||
{'name': 'pout', 'type': 'out', 'direction': 'out'},
|
||||
{'name': 'alteon', 'type': 'alteon'},
|
||||
{'name': 'alteon_array', 'type': 'alteon[]'},
|
||||
{'name': 'dp', 'type': 'defensePro'},
|
||||
{'name': 'dp_array', 'type': 'defensePro[]'},
|
||||
{'name': 'appWall', 'type': 'appWall'},
|
||||
{'name': 'appWall_array', 'type': 'appWall[]'}
|
||||
]}]
|
||||
|
||||
RUN_RESULT = [200, '', '', {
|
||||
"uri": "https://10.11.12.13:2189/api/status?token=Workflow%5Ca%5Capply%5Cc4b533a8-8764-4cbf-a19c-63b11b9ccc09",
|
||||
"targetUri": "https://10.11.12.13:2189/api/workflow/a",
|
||||
"complete": True, "status": 200, "success": True, "messages": [], "action": "apply", "parameters": {},
|
||||
}]
|
||||
|
||||
MODULE_RESULT = {"msg": "Configuration template run completed."}
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.RestClient')
|
||||
class RestClient:
|
||||
def __init__(self, vdirect_ip=None, vdirect_user=None, vdirect_password=None, wait=None,
|
||||
secondary_vdirect_ip=None, https_port=None, http_port=None,
|
||||
timeout=None, https=None, strict_http_results=None,
|
||||
verify=None):
|
||||
pass
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.Runnable')
|
||||
class Runnable:
|
||||
runnable_objects_result = None
|
||||
available_actions_result = None
|
||||
action_info_result = None
|
||||
run_result = None
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_runnable_objects_result(cls, result):
|
||||
Runnable.runnable_objects_result = result
|
||||
|
||||
@classmethod
|
||||
def set_available_actions_result(cls, result):
|
||||
Runnable.available_actions_result = result
|
||||
|
||||
@classmethod
|
||||
def set_action_info_result(cls, result):
|
||||
Runnable.action_info_result = result
|
||||
|
||||
@classmethod
|
||||
def set_run_result(cls, result):
|
||||
Runnable.run_result = result
|
||||
|
||||
def get_runnable_objects(self, type):
|
||||
return Runnable.runnable_objects_result
|
||||
|
||||
def get_available_actions(self, type=None, name=None):
|
||||
return Runnable.available_actions_result
|
||||
|
||||
def get_action_info(self, type, name, action_name):
|
||||
return Runnable.action_info_result
|
||||
|
||||
def run(self, data, type, name, action_name):
|
||||
return Runnable.run_result
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.Catalog')
|
||||
class Catalog:
|
||||
_404 = False
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_catalog_item_200(cls):
|
||||
Catalog._404 = False
|
||||
|
||||
@classmethod
|
||||
def set_catalog_item_404(cls):
|
||||
Catalog._404 = True
|
||||
|
||||
@classmethod
|
||||
def get_catalog_item(cls, type=None, name=None):
|
||||
if Catalog._404:
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
raise vdirect_runnable.MissingRunnableException(name)
|
||||
|
||||
|
||||
class TestManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.module_mock = MagicMock()
|
||||
self.module_mock.rest_client.RESP_STATUS = 0
|
||||
self.module_mock.rest_client.RESP_REASON = 1
|
||||
self.module_mock.rest_client.RESP_STR = 2
|
||||
self.module_mock.rest_client.RESP_DATA = 3
|
||||
|
||||
def test_missing_parameter(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
try:
|
||||
params = BASE_PARAMS.copy()
|
||||
vdirect_runnable.VdirectRunnable(params)
|
||||
self.fail("KeyError was not thrown for missing parameter")
|
||||
except KeyError:
|
||||
assert True
|
||||
|
||||
def test_validate_configuration_template_exists(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Catalog.set_catalog_item_200()
|
||||
BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
assert True
|
||||
|
||||
def test_validate_workflow_template_exists(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Catalog.set_catalog_item_200()
|
||||
BASE_PARAMS.update(WORKFLOW_TEMPLATE_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
assert True
|
||||
|
||||
def test_validate_workflow_exists(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Catalog.set_catalog_item_200()
|
||||
BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS)
|
||||
Runnable.set_runnable_objects_result(WORKFLOW_RUNNABLE_OBJECT_RESULT)
|
||||
BASE_PARAMS.update(WORKFLOW_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
assert True
|
||||
|
||||
def test_validate_plugin_exists(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_runnable_objects_result(WORKFLOW_RUNNABLE_OBJECT_RESULT)
|
||||
BASE_PARAMS.update(WORKFLOW_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
assert True
|
||||
|
||||
BASE_PARAMS['runnable_name'] = 'missing'
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
try:
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
self.fail("MissingRunnableException was not thrown for missing runnable name")
|
||||
except vdirect_runnable.MissingRunnableException:
|
||||
assert True
|
||||
|
||||
def test_validate_configuration_template_action_name(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Catalog.set_catalog_item_200()
|
||||
BASE_PARAMS.update(PLUGIN_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
assert True
|
||||
|
||||
Catalog.set_catalog_item_404()
|
||||
try:
|
||||
vdirectRunnable._validate_runnable_exists()
|
||||
self.fail("MissingRunnableException was not thrown for missing runnable name")
|
||||
except vdirect_runnable.MissingRunnableException:
|
||||
assert True
|
||||
|
||||
def test_validate_configuration_template_action_name(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_available_actions_result(ACTIONS_RESULT)
|
||||
BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable._validate_action_name()
|
||||
assert vdirectRunnable.action_name == vdirect_runnable.VdirectRunnable.RUN_ACTION
|
||||
|
||||
def test_validate_workflow_template_action_name(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_available_actions_result(ACTIONS_RESULT)
|
||||
BASE_PARAMS.update(WORKFLOW_TEMPLATE_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable._validate_action_name()
|
||||
assert vdirectRunnable.action_name == vdirect_runnable.VdirectRunnable.CREATE_WORKFLOW_ACTION
|
||||
|
||||
def test_validate_workflow_action_name(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_available_actions_result(ACTIONS_RESULT)
|
||||
BASE_PARAMS.update(WORKFLOW_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_action_name()
|
||||
assert vdirectRunnable.action_name == 'one'
|
||||
|
||||
BASE_PARAMS['action_name'] = 'three'
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
try:
|
||||
vdirectRunnable._validate_action_name()
|
||||
self.fail("WrongActionNameException was not thrown for wrong action name")
|
||||
except vdirect_runnable.WrongActionNameException:
|
||||
assert True
|
||||
|
||||
def test_validate_plugin_action_name(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_available_actions_result(ACTIONS_RESULT)
|
||||
BASE_PARAMS.update(PLUGIN_RUNNABLE_PARAMS)
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
vdirectRunnable._validate_action_name()
|
||||
assert vdirectRunnable.action_name == 'two'
|
||||
|
||||
BASE_PARAMS['action_name'] = 'three'
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
try:
|
||||
vdirectRunnable._validate_action_name()
|
||||
self.fail("WrongActionNameException was not thrown for wrong action name")
|
||||
except vdirect_runnable.WrongActionNameException:
|
||||
assert True
|
||||
|
||||
def test_validate_required_action_params(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_BASIC)
|
||||
BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS)
|
||||
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
try:
|
||||
vdirectRunnable._validate_required_action_params()
|
||||
self.fail("MissingActionParametersException was not thrown for missing parameters")
|
||||
except vdirect_runnable.MissingActionParametersException:
|
||||
assert True
|
||||
|
||||
BASE_PARAMS['parameters'] = {"alteon": "x"}
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
try:
|
||||
vdirectRunnable._validate_required_action_params()
|
||||
self.fail("MissingActionParametersException was not thrown for missing parameters")
|
||||
except vdirect_runnable.MissingActionParametersException:
|
||||
assert True
|
||||
|
||||
BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "a1"}
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable._validate_action_name()
|
||||
vdirectRunnable._validate_required_action_params()
|
||||
assert True
|
||||
|
||||
Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_FULL)
|
||||
vdirectRunnable._validate_action_name()
|
||||
try:
|
||||
vdirectRunnable._validate_required_action_params()
|
||||
self.fail("MissingActionParametersException was not thrown for missing parameters")
|
||||
except vdirect_runnable.MissingActionParametersException:
|
||||
assert True
|
||||
|
||||
BASE_PARAMS['parameters'].update(
|
||||
{"alteon_array": "[a1, a2]",
|
||||
"dp": "dp1", "dp_array": "[dp1, dp2]",
|
||||
"appWall": "appWall1",
|
||||
"appWall_array": "[appWall1, appWall2]"
|
||||
})
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable._validate_action_name()
|
||||
vdirectRunnable._validate_required_action_params()
|
||||
assert True
|
||||
|
||||
def test_run(self, *args):
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': self.module_mock,
|
||||
'vdirect_client.rest_client': self.module_mock,
|
||||
}):
|
||||
from ansible_collections.community.general.plugins.modules.network.radware import vdirect_runnable
|
||||
|
||||
Catalog.set_catalog_item_200()
|
||||
BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS)
|
||||
Runnable.set_available_actions_result(ACTIONS_RESULT)
|
||||
Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_BASIC)
|
||||
BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "x"}
|
||||
|
||||
vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS)
|
||||
vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client)
|
||||
Runnable.set_run_result(RUN_RESULT)
|
||||
res = vdirectRunnable.run()
|
||||
assert res['msg'] == MODULE_RESULT['msg']
|
||||
|
||||
result_parameters = {"param1": "value1", "param2": "value2"}
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_DATA]['parameters'] = result_parameters
|
||||
MODULE_RESULT['parameters'] = result_parameters
|
||||
res = vdirectRunnable.run()
|
||||
assert res['msg'] == MODULE_RESULT['msg']
|
||||
assert res['output']['parameters'] == result_parameters
|
||||
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_DATA]['status'] = 404
|
||||
vdirectRunnable.run()
|
||||
assert res['msg'] == MODULE_RESULT['msg']
|
||||
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 400
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_REASON] = "Reason"
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_STR] = "Details"
|
||||
try:
|
||||
vdirectRunnable.run()
|
||||
self.fail("RunnableException was not thrown for failed run.")
|
||||
except vdirect_runnable.RunnableException as e:
|
||||
assert str(e) == "Reason: Reason. Details:Details."
|
||||
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 200
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["status"] = 400
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["success"] = False
|
||||
RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["exception"] = {"message": "exception message"}
|
||||
try:
|
||||
vdirectRunnable.run()
|
||||
self.fail("RunnableException was not thrown for failed run.")
|
||||
except vdirect_runnable.RunnableException as e:
|
||||
assert str(e) == "Reason: exception message. Details:Details."
|
||||
0
tests/unit/plugins/modules/network/radware/wt.zip
Normal file
0
tests/unit/plugins/modules/network/radware/wt.zip
Normal file
Loading…
Add table
Add a link
Reference in a new issue