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