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_wireless_controller_hotspot20_qos_map 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_wireless_controller_hotspot20_qos_map.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_wireless_controller_hotspot20_qos_map_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 'wireless_controller_hotspot20_qos_map': {'name': 'default_name_3' 51 }, 52 'vdom': 'root'} 53 54 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 55 56 expected_data = {'name': 'default_name_3' 57 } 58 59 set_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', data=expected_data, vdom='root') 60 schema_method_mock.assert_not_called() 61 assert not is_error 62 assert changed 63 assert response['status'] == 'success' 64 assert response['http_status'] == 200 65 66 67def test_wireless_controller_hotspot20_qos_map_creation_fails(mocker): 68 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 69 70 set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 71 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 72 73 input_data = { 74 'username': 'admin', 75 'state': 'present', 76 'wireless_controller_hotspot20_qos_map': {'name': 'default_name_3' 77 }, 78 'vdom': 'root'} 79 80 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 81 82 expected_data = {'name': 'default_name_3' 83 } 84 85 set_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', data=expected_data, vdom='root') 86 schema_method_mock.assert_not_called() 87 assert is_error 88 assert not changed 89 assert response['status'] == 'error' 90 assert response['http_status'] == 500 91 92 93def test_wireless_controller_hotspot20_qos_map_removal(mocker): 94 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 95 96 delete_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 97 delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result) 98 99 input_data = { 100 'username': 'admin', 101 'state': 'absent', 102 'wireless_controller_hotspot20_qos_map': {'name': 'default_name_3' 103 }, 104 'vdom': 'root'} 105 106 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 107 108 delete_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', mkey=ANY, vdom='root') 109 schema_method_mock.assert_not_called() 110 assert not is_error 111 assert changed 112 assert response['status'] == 'success' 113 assert response['http_status'] == 200 114 115 116def test_wireless_controller_hotspot20_qos_map_deletion_fails(mocker): 117 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 118 119 delete_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 120 delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result) 121 122 input_data = { 123 'username': 'admin', 124 'state': 'absent', 125 'wireless_controller_hotspot20_qos_map': {'name': 'default_name_3' 126 }, 127 'vdom': 'root'} 128 129 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 130 131 delete_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', mkey=ANY, vdom='root') 132 schema_method_mock.assert_not_called() 133 assert is_error 134 assert not changed 135 assert response['status'] == 'error' 136 assert response['http_status'] == 500 137 138 139def test_wireless_controller_hotspot20_qos_map_idempotent(mocker): 140 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 141 142 set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404} 143 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 144 145 input_data = { 146 'username': 'admin', 147 'state': 'present', 148 'wireless_controller_hotspot20_qos_map': {'name': 'default_name_3' 149 }, 150 'vdom': 'root'} 151 152 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 153 154 expected_data = {'name': 'default_name_3' 155 } 156 157 set_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', data=expected_data, vdom='root') 158 schema_method_mock.assert_not_called() 159 assert not is_error 160 assert not changed 161 assert response['status'] == 'error' 162 assert response['http_status'] == 404 163 164 165def test_wireless_controller_hotspot20_qos_map_filter_foreign_attributes(mocker): 166 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 167 168 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 169 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 170 171 input_data = { 172 'username': 'admin', 173 'state': 'present', 174 'wireless_controller_hotspot20_qos_map': { 175 'random_attribute_not_valid': 'tag', 'name': 'default_name_3' 176 }, 177 'vdom': 'root'} 178 179 is_error, changed, response = fortios_wireless_controller_hotspot20_qos_map.fortios_wireless_controller_hotspot20(input_data, fos_instance) 180 181 expected_data = {'name': 'default_name_3' 182 } 183 184 set_method_mock.assert_called_with('wireless-controller.hotspot20', 'qos-map', data=expected_data, vdom='root') 185 schema_method_mock.assert_not_called() 186 assert not is_error 187 assert changed 188 assert response['status'] == 'success' 189 assert response['http_status'] == 200 190