FortiManager Plugin Module Conversion: fmgr_script (#52786)

* Auto Commit for: fmgr_script

* Auto Commit for: fmgr_script

* Auto Commit for: fmgr_script
This commit is contained in:
ftntcorecse
2019-02-27 01:39:27 -05:00
committed by Nilashish Chakraborty
parent bf162e258b
commit fa585151c2
3 changed files with 305 additions and 150 deletions

View File

@@ -0,0 +1,92 @@
{
"delete_script": [
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/dvmdb/adom/ansible/script/TestScript"
},
"datagram_sent": {
"name": "TestScript"
},
"paramgram_used": {
"vdom": "root",
"script_target": null,
"script_content": null,
"adom": "ansible",
"script_description": null,
"script_package": null,
"mode": "delete",
"script_scope": null,
"script_name": "TestScript",
"script_type": null
},
"post_method": "delete"
}
],
"set_script": [
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/dvmdb/adom/ansible/script/"
},
"datagram_sent": {
"content": "get system status",
"type": "cli",
"target": "remote_device",
"name": "TestScript",
"desc": "Create by Ansible"
},
"paramgram_used": {
"script_content": "get system status",
"adom": "ansible",
"script_scope": null,
"script_name": "TestScript",
"script_target": "remote_device",
"mode": "add",
"script_description": "Create by Ansible",
"script_package": null,
"vdom": "root",
"script_type": "cli"
},
"post_method": "set"
}
],
"execute_script": [
{
"url": "/dvmdb/adom/ansible/script/execute",
"paramgram_used": {
"script_content": null,
"adom": "ansible",
"script_scope": "FGT1",
"script_name": "TestScript",
"script_target": null,
"mode": "execute",
"script_description": null,
"script_package": null,
"vdom": "root",
"script_type": null
},
"datagram_sent": {
"scope": [
{
"name": "FGT1",
"vdom": "root"
}
],
"adom": "ansible",
"script": "TestScript",
"package": null
},
"raw_response": {
"task": 277
},
"post_method": "exec"
}
]
}

View File

@@ -1,61 +1,129 @@
# (c) 2016 Red Hat Inc.
# Copyright 2018 Fortinet, Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# 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.
#
# Ansible is distributed in the hope that it will be useful,
# 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
# along with Ansible. If not, see <https://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import json
from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler
import pytest
pytestmark = []
try:
from ansible.modules.network.fortimanager import fmgr_script
from .fortimanager_module import TestFortimanagerModule
from units.modules.utils import set_module_args
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load required modules for testing"))
try:
from pyFMG.fortimgr import FortiManager
except ImportError:
pytestmark.append(pytest.mark.skip("FortiManager tests require pyFMG package"))
pytest.skip("Could not load required modules for testing", allow_module_level=True)
class TestFmgrScriptModule(TestFortimanagerModule):
def load_fixtures():
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') + "/{filename}.json".format(
filename=os.path.splitext(os.path.basename(__file__))[0])
try:
with open(fixture_path, "r") as fixture_file:
fixture_data = json.load(fixture_file)
except IOError:
return []
return [fixture_data]
module = fmgr_script
def test_fmg_script_fail_connect(self):
set_module_args(dict(host='1.1.1.1', username='admin', password='admin', adom='root', script_name='TestScript',
script_type='cli', script_target='remote_device', script_description='AnsibleTest',
script_content='get system status'))
result = self.execute_module(failed=True)
self.assertEqual(result['msg'], 'Connection to FortiManager Failed')
@pytest.fixture(autouse=True)
def module_mock(mocker):
connection_class_mock = mocker.patch('ansible.module_utils.basic.AnsibleModule')
return connection_class_mock
def test_fmg_script_login_fail_host(self):
set_module_args(dict(username='admin', password='admin', adom='root', script_name='TestScript',
script_type='cli', script_target='remote_device', script_description='AnsibleTest',
script_content='get system status'))
result = self.execute_module(failed=True)
self.assertEqual(result['msg'], 'missing required arguments: host')
def test_fmg_script_login_fail_username(self):
set_module_args(dict(host='1.1.1.1', password='admin', adom='root', script_name='TestScript',
script_type='cli', script_target='remote_device', script_description='AnsibleTest',
script_content='get system status'))
result = self.execute_module(failed=True)
self.assertEqual(result['msg'], 'Host and username are required for connection')
@pytest.fixture(autouse=True)
def connection_mock(mocker):
connection_class_mock = mocker.patch('ansible.modules.network.fortimanager.fmgr_script.Connection')
return connection_class_mock
@pytest.fixture(scope="function", params=load_fixtures())
def fixture_data(request):
func_name = request.function.__name__.replace("test_", "")
return request.param.get(func_name, None)
fmg_instance = FortiManagerHandler(connection_mock, module_mock)
def test_set_script(fixture_data, mocker):
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
side_effect=fixture_data)
# Fixture sets used:###########################
##################################################
# script_content: get system status
# adom: ansible
# script_scope: None
# script_name: TestScript
# script_target: remote_device
# mode: set
# script_description: Create by Ansible
# script_package: None
# vdom: root
# script_type: cli
##################################################
# Test using fixture 1 #
output = fmgr_script.set_script(fmg_instance, fixture_data[0]['paramgram_used'])
assert output['raw_response']['status']['code'] == 0
def test_delete_script(fixture_data, mocker):
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
side_effect=fixture_data)
# Fixture sets used:###########################
##################################################
# vdom: root
# script_target: None
# script_content: None
# adom: ansible
# script_description: None
# script_package: None
# mode: delete
# script_scope: None
# script_name: TestScript
# script_type: None
##################################################
# Test using fixture 1 #
output = fmgr_script.delete_script(fmg_instance, fixture_data[0]['paramgram_used'])
assert output['raw_response']['status']['code'] == 0
def test_execute_script(fixture_data, mocker):
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
side_effect=fixture_data)
# Fixture sets used:###########################
##################################################
# script_content: None
# adom: ansible
# script_scope: FGT1
# script_name: TestScript
# script_target: None
# mode: exec
# script_description: None
# script_package: None
# vdom: root
# script_type: None
##################################################
# Test using fixture 1 #
output = fmgr_script.execute_script(fmg_instance, fixture_data[0]['paramgram_used'])
assert isinstance(output['raw_response'], dict) is True