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_device_ha_group import ( 20 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_v13(self): 51 args = dict( 52 name='foobar', 53 description='baz', 54 active_bonus=20, 55 enable='yes', 56 state='present', 57 pools=[ 58 dict( 59 pool_name='fakepool', 60 attribute='percent-up-members', 61 weight=30, 62 minimum_threshold=2, 63 partition='Common' 64 ) 65 ], 66 trunks=[ 67 dict( 68 trunk_name='faketrunk', 69 attribute='percent-up-members', 70 weight=30, 71 minimum_threshold=2 72 ) 73 ] 74 ) 75 76 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_ha_group.tmos_version') 77 self.m1 = self.p1.start() 78 self.m1.return_value = '13.1.0' 79 80 p = ModuleParameters(params=args) 81 82 assert p.name == 'foobar' 83 assert p.state == 'present' 84 assert p.active_bonus == 20 85 assert p.enabled is True 86 assert p.pools == [{'name': '/Common/fakepool', 'attribute': 'percent-up-members', 87 'weight': 30, 'minimumThreshold': 2}] 88 assert p.trunks == [{'name': 'faketrunk', 'attribute': 'percent-up-members', 89 'weight': 30, 'minimumThreshold': 2}] 90 91 self.p1.stop() 92 93 def test_module_parameters_v12(self): 94 args = dict( 95 name='foobar', 96 description='baz', 97 active_bonus=20, 98 enable='yes', 99 state='present', 100 pools=[ 101 dict( 102 pool_name='fakepool', 103 attribute='percent-up-members', 104 weight=30, 105 minimum_threshold=2, 106 partition='Common' 107 ) 108 ], 109 trunks=[ 110 dict( 111 trunk_name='faketrunk', 112 attribute='percent-up-members', 113 weight=20, 114 minimum_threshold=1 115 ) 116 ] 117 ) 118 119 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_ha_group.tmos_version') 120 self.m1 = self.p1.start() 121 self.m1.return_value = '12.1.0' 122 123 p = ModuleParameters(params=args) 124 125 assert p.name == 'foobar' 126 assert p.state == 'present' 127 assert p.active_bonus == 20 128 assert p.enabled is True 129 assert p.pools == [{'name': '/Common/fakepool', 'attribute': 'percent-up-members', 130 'weight': 30, 'threshold': 2}] 131 assert p.trunks == [{'name': 'faketrunk', 'attribute': 'percent-up-members', 132 'weight': 20, 'threshold': 1}] 133 134 self.p1.stop() 135 136 137class TestManager(unittest.TestCase): 138 def setUp(self): 139 self.spec = ArgumentSpec() 140 141 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_ha_group.tmos_version') 142 self.m1 = self.p1.start() 143 self.m1.return_value = '13.1.0' 144 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_ha_group.send_teem') 145 self.m2 = self.p2.start() 146 self.m2.return_value = True 147 148 def tearDown(self): 149 self.p1.stop() 150 self.p2.stop() 151 152 def test_create_ha_group(self, *args): 153 set_module_args(dict( 154 name='fake_group', 155 state='present', 156 description='baz', 157 active_bonus=20, 158 enable='yes', 159 pools=[ 160 dict( 161 pool_name='fakepool', 162 attribute='percent-up-members', 163 weight=30, 164 minimum_threshold=2, 165 partition='Common' 166 ) 167 ], 168 trunks=[ 169 dict( 170 trunk_name='faketrunk', 171 attribute='percent-up-members', 172 weight=20, 173 minimum_threshold=1 174 ) 175 ], 176 provider=dict( 177 server='localhost', 178 password='password', 179 user='admin' 180 ) 181 )) 182 183 module = AnsibleModule( 184 argument_spec=self.spec.argument_spec, 185 supports_check_mode=self.spec.supports_check_mode 186 ) 187 188 # Override methods to force specific logic in the module to happen 189 mm = ModuleManager(module=module) 190 mm.exists = Mock(return_value=False) 191 mm.create_on_device = Mock(return_value=True) 192 193 results = mm.exec_module() 194 195 assert results['changed'] is True 196 assert results['name'] == 'fake_group' 197 assert results['description'] == 'baz' 198 assert results['active_bonus'] == 20 199 assert results['enable'] == 'yes' 200 assert results['pools'] == [{'pool_name': '/Common/fakepool', 'attribute': 'percent-up-members', 201 'weight': 30, 'minimum_threshold': 2}] 202 assert results['trunks'] == [{'trunk_name': 'faketrunk', 'attribute': 'percent-up-members', 203 'weight': 20, 'minimum_threshold': 1}] 204