1# Copyright 2019 Fortinet, Inc. 2# 3# This program is free software: you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation, either version 3 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with Ansible. If not, see <https://www.gnu.org/licenses/>. 15 16# Make coding more python3-ish 17from __future__ import (absolute_import, division, print_function) 18__metaclass__ = type 19 20import os 21import json 22import pytest 23from mock import ANY 24from ansible.module_utils.network.fortios.fortios import FortiOSHandler 25 26try: 27 from ansible.modules.network.fortios import fortios_system_ftm_push 28except ImportError: 29 pytest.skip("Could not load required modules for testing", allow_module_level=True) 30 31 32@pytest.fixture(autouse=True) 33def connection_mock(mocker): 34 connection_class_mock = mocker.patch('ansible.modules.network.fortios.fortios_system_ftm_push.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_system_ftm_push_creation(mocker): 42 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 43 44 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 45 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 46 47 input_data = { 48 'username': 'admin', 49 'state': 'present', 50 'system_ftm_push': { 51 'server_ip': 'test_value_3', 52 'server_port': '4', 53 'status': 'enable' 54 }, 55 'vdom': 'root'} 56 57 is_error, changed, response = fortios_system_ftm_push.fortios_system(input_data, fos_instance) 58 59 expected_data = { 60 'server-ip': 'test_value_3', 61 'server-port': '4', 62 'status': 'enable' 63 } 64 65 set_method_mock.assert_called_with('system', 'ftm-push', data=expected_data, vdom='root') 66 schema_method_mock.assert_not_called() 67 assert not is_error 68 assert changed 69 assert response['status'] == 'success' 70 assert response['http_status'] == 200 71 72 73def test_system_ftm_push_creation_fails(mocker): 74 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 75 76 set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 77 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 78 79 input_data = { 80 'username': 'admin', 81 'state': 'present', 82 'system_ftm_push': { 83 'server_ip': 'test_value_3', 84 'server_port': '4', 85 'status': 'enable' 86 }, 87 'vdom': 'root'} 88 89 is_error, changed, response = fortios_system_ftm_push.fortios_system(input_data, fos_instance) 90 91 expected_data = { 92 'server-ip': 'test_value_3', 93 'server-port': '4', 94 'status': 'enable' 95 } 96 97 set_method_mock.assert_called_with('system', 'ftm-push', data=expected_data, vdom='root') 98 schema_method_mock.assert_not_called() 99 assert is_error 100 assert not changed 101 assert response['status'] == 'error' 102 assert response['http_status'] == 500 103 104 105def test_system_ftm_push_idempotent(mocker): 106 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 107 108 set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404} 109 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 110 111 input_data = { 112 'username': 'admin', 113 'state': 'present', 114 'system_ftm_push': { 115 'server_ip': 'test_value_3', 116 'server_port': '4', 117 'status': 'enable' 118 }, 119 'vdom': 'root'} 120 121 is_error, changed, response = fortios_system_ftm_push.fortios_system(input_data, fos_instance) 122 123 expected_data = { 124 'server-ip': 'test_value_3', 125 'server-port': '4', 126 'status': 'enable' 127 } 128 129 set_method_mock.assert_called_with('system', 'ftm-push', data=expected_data, vdom='root') 130 schema_method_mock.assert_not_called() 131 assert not is_error 132 assert not changed 133 assert response['status'] == 'error' 134 assert response['http_status'] == 404 135 136 137def test_system_ftm_push_filter_foreign_attributes(mocker): 138 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 139 140 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 141 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 142 143 input_data = { 144 'username': 'admin', 145 'state': 'present', 146 'system_ftm_push': { 147 'random_attribute_not_valid': 'tag', 148 'server_ip': 'test_value_3', 149 'server_port': '4', 150 'status': 'enable' 151 }, 152 'vdom': 'root'} 153 154 is_error, changed, response = fortios_system_ftm_push.fortios_system(input_data, fos_instance) 155 156 expected_data = { 157 'server-ip': 'test_value_3', 158 'server-port': '4', 159 'status': 'enable' 160 } 161 162 set_method_mock.assert_called_with('system', 'ftm-push', data=expected_data, vdom='root') 163 schema_method_mock.assert_not_called() 164 assert not is_error 165 assert changed 166 assert response['status'] == 'success' 167 assert response['http_status'] == 200 168