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_sflow 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_sflow.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_system_vdom_sflow_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_sflow': { 51 'collector_ip': 'test_value_3', 52 'collector_port': '4', 53 'source_ip': '84.230.14.5', 54 'vdom_sflow': 'enable' 55 }, 56 'vdom': 'root'} 57 58 is_error, changed, response = fortios_system_vdom_sflow.fortios_system(input_data, fos_instance) 59 60 expected_data = { 61 'collector-ip': 'test_value_3', 62 'collector-port': '4', 63 'source-ip': '84.230.14.5', 64 'vdom-sflow': 'enable' 65 } 66 67 set_method_mock.assert_called_with('system', 'vdom-sflow', data=expected_data, vdom='root') 68 schema_method_mock.assert_not_called() 69 assert not is_error 70 assert changed 71 assert response['status'] == 'success' 72 assert response['http_status'] == 200 73 74 75def test_system_vdom_sflow_creation_fails(mocker): 76 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 77 78 set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500} 79 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 80 81 input_data = { 82 'username': 'admin', 83 'state': 'present', 84 'system_vdom_sflow': { 85 'collector_ip': 'test_value_3', 86 'collector_port': '4', 87 'source_ip': '84.230.14.5', 88 'vdom_sflow': 'enable' 89 }, 90 'vdom': 'root'} 91 92 is_error, changed, response = fortios_system_vdom_sflow.fortios_system(input_data, fos_instance) 93 94 expected_data = { 95 'collector-ip': 'test_value_3', 96 'collector-port': '4', 97 'source-ip': '84.230.14.5', 98 'vdom-sflow': 'enable' 99 } 100 101 set_method_mock.assert_called_with('system', 'vdom-sflow', data=expected_data, vdom='root') 102 schema_method_mock.assert_not_called() 103 assert is_error 104 assert not changed 105 assert response['status'] == 'error' 106 assert response['http_status'] == 500 107 108 109def test_system_vdom_sflow_idempotent(mocker): 110 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 111 112 set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404} 113 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 114 115 input_data = { 116 'username': 'admin', 117 'state': 'present', 118 'system_vdom_sflow': { 119 'collector_ip': 'test_value_3', 120 'collector_port': '4', 121 'source_ip': '84.230.14.5', 122 'vdom_sflow': 'enable' 123 }, 124 'vdom': 'root'} 125 126 is_error, changed, response = fortios_system_vdom_sflow.fortios_system(input_data, fos_instance) 127 128 expected_data = { 129 'collector-ip': 'test_value_3', 130 'collector-port': '4', 131 'source-ip': '84.230.14.5', 132 'vdom-sflow': 'enable' 133 } 134 135 set_method_mock.assert_called_with('system', 'vdom-sflow', data=expected_data, vdom='root') 136 schema_method_mock.assert_not_called() 137 assert not is_error 138 assert not changed 139 assert response['status'] == 'error' 140 assert response['http_status'] == 404 141 142 143def test_system_vdom_sflow_filter_foreign_attributes(mocker): 144 schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema') 145 146 set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200} 147 set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result) 148 149 input_data = { 150 'username': 'admin', 151 'state': 'present', 152 'system_vdom_sflow': { 153 'random_attribute_not_valid': 'tag', 154 'collector_ip': 'test_value_3', 155 'collector_port': '4', 156 'source_ip': '84.230.14.5', 157 'vdom_sflow': 'enable' 158 }, 159 'vdom': 'root'} 160 161 is_error, changed, response = fortios_system_vdom_sflow.fortios_system(input_data, fos_instance) 162 163 expected_data = { 164 'collector-ip': 'test_value_3', 165 'collector-port': '4', 166 'source-ip': '84.230.14.5', 167 'vdom-sflow': 'enable' 168 } 169 170 set_method_mock.assert_called_with('system', 'vdom-sflow', data=expected_data, vdom='root') 171 schema_method_mock.assert_not_called() 172 assert not is_error 173 assert changed 174 assert response['status'] == 'success' 175 assert response['http_status'] == 200 176