1# -*- coding: utf-8 -*- 2# 3# Copyright (c) 2017 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_node import ( 20 Parameters, ApiParameters, 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 host='10.20.30.40', 53 name='10.20.30.40' 54 ) 55 56 p = Parameters(params=args) 57 assert p.host == '10.20.30.40' 58 assert p.name == '10.20.30.40' 59 60 def test_api_parameters(self): 61 args = load_fixture('load_ltm_node_1.json') 62 63 p = Parameters(params=args) 64 assert p.address == '1.2.3.4' 65 66 67class TestManager(unittest.TestCase): 68 def setUp(self): 69 self.spec = ArgumentSpec() 70 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_node.tmos_version') 71 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_node.send_teem') 72 self.m2 = self.p2.start() 73 self.m2.return_value = '14.1.0' 74 self.m3 = self.p3.start() 75 self.m3.return_value = True 76 77 def tearDown(self): 78 self.p2.stop() 79 self.p3.stop() 80 81 def test_create_node(self, *args): 82 set_module_args(dict( 83 host='10.20.30.40', 84 name='mytestserver', 85 monitors=[ 86 '/Common/icmp' 87 ], 88 partition='Common', 89 state='present', 90 provider=dict( 91 server='localhost', 92 password='password', 93 user='admin' 94 ) 95 )) 96 97 module = AnsibleModule( 98 argument_spec=self.spec.argument_spec, 99 supports_check_mode=self.spec.supports_check_mode, 100 ) 101 mm = ModuleManager(module=module) 102 103 # Override methods to force specific logic in the module to happen 104 mm.exists = Mock(side_effect=[False, True]) 105 mm.create_on_device = Mock(return_value=True) 106 107 results = mm.exec_module() 108 109 assert results['changed'] is True 110 111 def test_create_node_idempotent(self, *args): 112 set_module_args(dict( 113 host='10.20.30.40', 114 name='mytestserver', 115 monitors=[ 116 '/Common/icmp' 117 ], 118 partition='Common', 119 state='present', 120 provider=dict( 121 server='localhost', 122 password='password', 123 user='admin' 124 ) 125 )) 126 127 current = ApiParameters(params=load_fixture('load_ltm_node_3.json')) 128 129 module = AnsibleModule( 130 argument_spec=self.spec.argument_spec, 131 supports_check_mode=self.spec.supports_check_mode, 132 ) 133 mm = ModuleManager(module=module) 134 135 # Override methods to force specific logic in the module to happen 136 mm.exists = Mock(side_effect=[True, True]) 137 mm.read_current_from_device = Mock(return_value=current) 138 139 results = mm.exec_module() 140 141 assert results['changed'] is False 142 143 def test_create_node_fqdn(self, *args): 144 set_module_args(dict( 145 fqdn='foo.bar', 146 name='mytestserver', 147 monitors=[ 148 '/Common/icmp' 149 ], 150 partition='Common', 151 state='present', 152 provider=dict( 153 server='localhost', 154 password='password', 155 user='admin' 156 ) 157 )) 158 159 module = AnsibleModule( 160 argument_spec=self.spec.argument_spec, 161 supports_check_mode=self.spec.supports_check_mode, 162 ) 163 mm = ModuleManager(module=module) 164 165 # Override methods to force specific logic in the module to happen 166 mm.exists = Mock(side_effect=[False, True]) 167 mm.create_on_device = Mock(return_value=True) 168 169 results = mm.exec_module() 170 171 assert results['changed'] is True 172 173 def test_update_node_fqdn_up_interval(self, *args): 174 set_module_args(dict( 175 fqdn='foo.bar', 176 fqdn_up_interval=100, 177 name='mytestserver', 178 monitors=[ 179 '/Common/icmp' 180 ], 181 partition='Common', 182 state='present', 183 provider=dict( 184 server='localhost', 185 password='password', 186 user='admin' 187 ) 188 )) 189 190 current = ApiParameters(params=load_fixture('load_ltm_node_2.json')) 191 module = AnsibleModule( 192 argument_spec=self.spec.argument_spec, 193 supports_check_mode=self.spec.supports_check_mode, 194 ) 195 mm = ModuleManager(module=module) 196 197 # Override methods to force specific logic in the module to happen 198 mm.exists = Mock(side_effect=[True, True]) 199 mm.update_on_device = Mock(return_value=True) 200 mm.read_current_from_device = Mock(return_value=current) 201 202 results = mm.exec_module() 203 204 assert results['changed'] is True 205 206 def test_update_node_fqdn_up_interval_idempotent(self, *args): 207 set_module_args(dict( 208 fqdn='google.com', 209 fqdn_up_interval=3600, 210 name='fqdn-foo', 211 monitors=[ 212 'icmp', 213 'tcp_echo' 214 ], 215 partition='Common', 216 state='present', 217 provider=dict( 218 server='localhost', 219 password='password', 220 user='admin' 221 ) 222 )) 223 224 current = ApiParameters(params=load_fixture('load_ltm_node_2.json')) 225 module = AnsibleModule( 226 argument_spec=self.spec.argument_spec, 227 supports_check_mode=self.spec.supports_check_mode, 228 ) 229 mm = ModuleManager(module=module) 230 231 # Override methods to force specific logic in the module to happen 232 mm.exists = Mock(side_effect=[True, True]) 233 mm.update_on_device = Mock(return_value=True) 234 mm.read_current_from_device = Mock(return_value=current) 235 236 results = mm.exec_module() 237 238 assert results['changed'] is not True 239