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_vxlan 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_vxlan.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_system_vxlan_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_vxlan': { 51 'dstport': '3', 52 'interface': 'test_value_4', 53 'ip_version': 'ipv4-unicast', 54 'multicast_ttl': '6', 55 'name': 'default_name_7', 56 'vni': '8' 57 }, 58 'vdom': 'root'} 59 60 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 61 62 expected_data = { 63 'dstport': '3', 64 'interface': 'test_value_4', 65 'ip-version': 'ipv4-unicast', 66 'multicast-ttl': '6', 67 'name': 'default_name_7', 68 'vni': '8' 69 } 70 71 set_method_mock.assert_called_with('system', 'vxlan', data=expected_data, vdom='root') 72 schema_method_mock.assert_not_called() 73 assert not is_error 74 assert changed 75 assert response['status'] == 'success' 76 assert response['http_status'] == 200 77 78 79def test_system_vxlan_creation_fails(mocker): 80 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 81 82 set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 83 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 84 85 input_data = { 86 'username': 'admin', 87 'state': 'present', 88 'system_vxlan': { 89 'dstport': '3', 90 'interface': 'test_value_4', 91 'ip_version': 'ipv4-unicast', 92 'multicast_ttl': '6', 93 'name': 'default_name_7', 94 'vni': '8' 95 }, 96 'vdom': 'root'} 97 98 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 99 100 expected_data = { 101 'dstport': '3', 102 'interface': 'test_value_4', 103 'ip-version': 'ipv4-unicast', 104 'multicast-ttl': '6', 105 'name': 'default_name_7', 106 'vni': '8' 107 } 108 109 set_method_mock.assert_called_with('system', 'vxlan', data=expected_data, vdom='root') 110 schema_method_mock.assert_not_called() 111 assert is_error 112 assert not changed 113 assert response['status'] == 'error' 114 assert response['http_status'] == 500 115 116 117def test_system_vxlan_removal(mocker): 118 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 119 120 delete_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 121 delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result) 122 123 input_data = { 124 'username': 'admin', 125 'state': 'absent', 126 'system_vxlan': { 127 'dstport': '3', 128 'interface': 'test_value_4', 129 'ip_version': 'ipv4-unicast', 130 'multicast_ttl': '6', 131 'name': 'default_name_7', 132 'vni': '8' 133 }, 134 'vdom': 'root'} 135 136 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 137 138 delete_method_mock.assert_called_with('system', 'vxlan', mkey=ANY, vdom='root') 139 schema_method_mock.assert_not_called() 140 assert not is_error 141 assert changed 142 assert response['status'] == 'success' 143 assert response['http_status'] == 200 144 145 146def test_system_vxlan_deletion_fails(mocker): 147 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 148 149 delete_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 150 delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result) 151 152 input_data = { 153 'username': 'admin', 154 'state': 'absent', 155 'system_vxlan': { 156 'dstport': '3', 157 'interface': 'test_value_4', 158 'ip_version': 'ipv4-unicast', 159 'multicast_ttl': '6', 160 'name': 'default_name_7', 161 'vni': '8' 162 }, 163 'vdom': 'root'} 164 165 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 166 167 delete_method_mock.assert_called_with('system', 'vxlan', mkey=ANY, vdom='root') 168 schema_method_mock.assert_not_called() 169 assert is_error 170 assert not changed 171 assert response['status'] == 'error' 172 assert response['http_status'] == 500 173 174 175def test_system_vxlan_idempotent(mocker): 176 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 177 178 set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404} 179 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 180 181 input_data = { 182 'username': 'admin', 183 'state': 'present', 184 'system_vxlan': { 185 'dstport': '3', 186 'interface': 'test_value_4', 187 'ip_version': 'ipv4-unicast', 188 'multicast_ttl': '6', 189 'name': 'default_name_7', 190 'vni': '8' 191 }, 192 'vdom': 'root'} 193 194 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 195 196 expected_data = { 197 'dstport': '3', 198 'interface': 'test_value_4', 199 'ip-version': 'ipv4-unicast', 200 'multicast-ttl': '6', 201 'name': 'default_name_7', 202 'vni': '8' 203 } 204 205 set_method_mock.assert_called_with('system', 'vxlan', data=expected_data, vdom='root') 206 schema_method_mock.assert_not_called() 207 assert not is_error 208 assert not changed 209 assert response['status'] == 'error' 210 assert response['http_status'] == 404 211 212 213def test_system_vxlan_filter_foreign_attributes(mocker): 214 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 215 216 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 217 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 218 219 input_data = { 220 'username': 'admin', 221 'state': 'present', 222 'system_vxlan': { 223 'random_attribute_not_valid': 'tag', 224 'dstport': '3', 225 'interface': 'test_value_4', 226 'ip_version': 'ipv4-unicast', 227 'multicast_ttl': '6', 228 'name': 'default_name_7', 229 'vni': '8' 230 }, 231 'vdom': 'root'} 232 233 is_error, changed, response = fortios_system_vxlan.fortios_system(input_data, fos_instance) 234 235 expected_data = { 236 'dstport': '3', 237 'interface': 'test_value_4', 238 'ip-version': 'ipv4-unicast', 239 'multicast-ttl': '6', 240 'name': 'default_name_7', 241 'vni': '8' 242 } 243 244 set_method_mock.assert_called_with('system', 'vxlan', data=expected_data, vdom='root') 245 schema_method_mock.assert_not_called() 246 assert not is_error 247 assert changed 248 assert response['status'] == 'success' 249 assert response['http_status'] == 200 250