1# -*- coding: utf-8 -*- 2# 3# Copyright: (c) 2018, F5 Networks Inc. 4# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 5 6from __future__ import (absolute_import, division, print_function) 7__metaclass__ = type 8 9import os 10import json 11import pytest 12import sys 13 14if sys.version_info < (2, 7): 15 pytestmark = pytest.mark.skip("F5 Ansible modules require Python >= 2.7") 16 17from ansible.module_utils.basic import AnsibleModule 18 19from ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_fastl4 import ( 20 ApiParameters, ModuleParameters, ModuleManager, ArgumentSpec 21) 22from ansible_collections.f5networks.f5_modules.tests.unit.compat import unittest 23from ansible_collections.f5networks.f5_modules.tests.unit.compat.mock import Mock, patch 24from ansible_collections.f5networks.f5_modules.tests.unit.modules.utils import set_module_args 25 26 27fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') 28fixture_data = {} 29 30 31def load_fixture(name): 32 path = os.path.join(fixture_path, name) 33 34 if path in fixture_data: 35 return fixture_data[path] 36 37 with open(path) as f: 38 data = f.read() 39 40 try: 41 data = json.loads(data) 42 except Exception: 43 pass 44 45 fixture_data[path] = data 46 return data 47 48 49class TestParameters(unittest.TestCase): 50 def test_module_parameters(self): 51 args = dict( 52 name='foo', 53 parent='bar', 54 idle_timeout=100, 55 client_timeout=101, 56 description='description one', 57 explicit_flow_migration=False, 58 ip_df_mode='pmtu', 59 ip_tos_to_client=102, 60 ip_tos_to_server=103, 61 ip_ttl_v4=104, 62 ip_ttl_v6=105, 63 ip_ttl_mode='proxy', 64 keep_alive_interval=106, 65 late_binding=True, 66 link_qos_to_client=7, 67 link_qos_to_server=6, 68 loose_close=False, 69 loose_initialization=True, 70 mss_override=4, 71 reassemble_fragments=True, 72 receive_window_size=109, 73 reset_on_timeout=False, 74 rtt_from_client=True, 75 rtt_from_server=False, 76 server_sack=True, 77 server_timestamp=False, 78 syn_cookie_mss=110, 79 tcp_close_timeout=111, 80 tcp_generate_isn=True, 81 tcp_handshake_timeout=112, 82 tcp_strip_sack=False, 83 tcp_time_wait_timeout=113, 84 tcp_timestamp_mode='rewrite', 85 tcp_wscale_mode='strip', 86 timeout_recovery='fallback', 87 hardware_syn_cookie=True, 88 ) 89 90 p = ModuleParameters(params=args) 91 assert p.name == 'foo' 92 assert p.parent == '/Common/bar' 93 assert p.description == 'description one' 94 assert p.idle_timeout == 100 95 assert p.client_timeout == 101 96 assert p.explicit_flow_migration == 'no' 97 assert p.ip_df_mode == 'pmtu' 98 assert p.ip_tos_to_client == 102 99 assert p.ip_tos_to_server == 103 100 assert p.ip_ttl_v4 == 104 101 assert p.ip_ttl_v6 == 105 102 assert p.ip_ttl_mode == 'proxy' 103 assert p.keep_alive_interval == 106 104 assert p.late_binding == 'yes' 105 assert p.link_qos_to_client == 7 106 assert p.link_qos_to_server == 6 107 assert p.loose_close == 'no' 108 assert p.loose_initialization == 'yes' 109 assert p.mss_override == 4 110 assert p.reassemble_fragments == 'yes' 111 assert p.receive_window_size == 109 112 assert p.reset_on_timeout == 'no' 113 assert p.rtt_from_client == 'yes' 114 assert p.rtt_from_server == 'no' 115 assert p.server_sack == 'yes' 116 assert p.server_timestamp == 'no' 117 assert p.syn_cookie_mss == 110 118 assert p.tcp_close_timeout == 111 119 assert p.tcp_generate_isn == 'yes' 120 assert p.tcp_handshake_timeout == 112 121 assert p.tcp_strip_sack == 'no' 122 assert p.tcp_time_wait_timeout == 113 123 assert p.tcp_timestamp_mode == 'rewrite' 124 assert p.tcp_wscale_mode == 'strip' 125 assert p.timeout_recovery == 'fallback' 126 assert p.hardware_syn_cookie == 'enabled' 127 128 def test_api_parameters(self): 129 args = load_fixture('load_ltm_fastl4_profile_1.json') 130 p = ApiParameters(params=args) 131 assert p.name == 'fastL4' 132 assert p.description is None 133 134 135class TestManager(unittest.TestCase): 136 def setUp(self): 137 self.spec = ArgumentSpec() 138 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_fastl4.tmos_version') 139 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_fastl4.send_teem') 140 self.m2 = self.p2.start() 141 self.m2.return_value = '14.1.0' 142 self.m3 = self.p3.start() 143 self.m3.return_value = True 144 145 def tearDown(self): 146 self.p2.stop() 147 self.p3.stop() 148 149 def test_create(self, *args): 150 # Configure the arguments that would be sent to the Ansible module 151 set_module_args(dict( 152 name='foo', 153 parent='bar', 154 provider=dict( 155 server='localhost', 156 password='password', 157 user='admin' 158 ) 159 )) 160 161 module = AnsibleModule( 162 argument_spec=self.spec.argument_spec, 163 supports_check_mode=self.spec.supports_check_mode 164 ) 165 mm = ModuleManager(module=module) 166 167 # Override methods to force specific logic in the module to happen 168 mm.exists = Mock(return_value=False) 169 mm.create_on_device = Mock(return_value=True) 170 171 results = mm.exec_module() 172 173 assert results['changed'] is True 174