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_vdom_dns 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_vdom_dns.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_system_vdom_dns_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_vdom_dns': { 51 'ip6_primary': 'test_value_3', 52 'ip6_secondary': 'test_value_4', 53 'primary': 'test_value_5', 54 'secondary': 'test_value_6', 55 'source_ip': '84.230.14.7', 56 'vdom_dns': 'enable' 57 }, 58 'vdom': 'root'} 59 60 is_error, changed, response = fortios_system_vdom_dns.fortios_system(input_data, fos_instance) 61 62 expected_data = { 63 'ip6-primary': 'test_value_3', 64 'ip6-secondary': 'test_value_4', 65 'primary': 'test_value_5', 66 'secondary': 'test_value_6', 67 'source-ip': '84.230.14.7', 68 'vdom-dns': 'enable' 69 } 70 71 set_method_mock.assert_called_with('system', 'vdom-dns', 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_vdom_dns_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_vdom_dns': { 89 'ip6_primary': 'test_value_3', 90 'ip6_secondary': 'test_value_4', 91 'primary': 'test_value_5', 92 'secondary': 'test_value_6', 93 'source_ip': '84.230.14.7', 94 'vdom_dns': 'enable' 95 }, 96 'vdom': 'root'} 97 98 is_error, changed, response = fortios_system_vdom_dns.fortios_system(input_data, fos_instance) 99 100 expected_data = { 101 'ip6-primary': 'test_value_3', 102 'ip6-secondary': 'test_value_4', 103 'primary': 'test_value_5', 104 'secondary': 'test_value_6', 105 'source-ip': '84.230.14.7', 106 'vdom-dns': 'enable' 107 } 108 109 set_method_mock.assert_called_with('system', 'vdom-dns', 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_vdom_dns_idempotent(mocker): 118 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 119 120 set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404} 121 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 122 123 input_data = { 124 'username': 'admin', 125 'state': 'present', 126 'system_vdom_dns': { 127 'ip6_primary': 'test_value_3', 128 'ip6_secondary': 'test_value_4', 129 'primary': 'test_value_5', 130 'secondary': 'test_value_6', 131 'source_ip': '84.230.14.7', 132 'vdom_dns': 'enable' 133 }, 134 'vdom': 'root'} 135 136 is_error, changed, response = fortios_system_vdom_dns.fortios_system(input_data, fos_instance) 137 138 expected_data = { 139 'ip6-primary': 'test_value_3', 140 'ip6-secondary': 'test_value_4', 141 'primary': 'test_value_5', 142 'secondary': 'test_value_6', 143 'source-ip': '84.230.14.7', 144 'vdom-dns': 'enable' 145 } 146 147 set_method_mock.assert_called_with('system', 'vdom-dns', data=expected_data, vdom='root') 148 schema_method_mock.assert_not_called() 149 assert not is_error 150 assert not changed 151 assert response['status'] == 'error' 152 assert response['http_status'] == 404 153 154 155def test_system_vdom_dns_filter_foreign_attributes(mocker): 156 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 157 158 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 159 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 160 161 input_data = { 162 'username': 'admin', 163 'state': 'present', 164 'system_vdom_dns': { 165 'random_attribute_not_valid': 'tag', 166 'ip6_primary': 'test_value_3', 167 'ip6_secondary': 'test_value_4', 168 'primary': 'test_value_5', 169 'secondary': 'test_value_6', 170 'source_ip': '84.230.14.7', 171 'vdom_dns': 'enable' 172 }, 173 'vdom': 'root'} 174 175 is_error, changed, response = fortios_system_vdom_dns.fortios_system(input_data, fos_instance) 176 177 expected_data = { 178 'ip6-primary': 'test_value_3', 179 'ip6-secondary': 'test_value_4', 180 'primary': 'test_value_5', 181 'secondary': 'test_value_6', 182 'source-ip': '84.230.14.7', 183 'vdom-dns': 'enable' 184 } 185 186 set_method_mock.assert_called_with('system', 'vdom-dns', data=expected_data, vdom='root') 187 schema_method_mock.assert_not_called() 188 assert not is_error 189 assert changed 190 assert response['status'] == 'success' 191 assert response['http_status'] == 200 192