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_device_group 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 save_on_auto_sync=True, 53 full_sync=False, 54 description="my description", 55 type="sync-failover", 56 auto_sync=True 57 ) 58 59 p = ModuleParameters(params=args) 60 assert p.save_on_auto_sync is True 61 assert p.full_sync is False 62 assert p.description == "my description" 63 assert p.type == "sync-failover" 64 assert p.auto_sync is True 65 66 def test_api_parameters(self): 67 args = dict( 68 asmSync="disabled", 69 autoSync="enabled", 70 fullLoadOnSync="false", 71 incrementalConfigSyncSizeMax=1024, 72 networkFailover="disabled", 73 saveOnAutoSync="false", 74 type="sync-only" 75 ) 76 77 p = ApiParameters(params=args) 78 assert p.auto_sync is True 79 assert p.full_sync is False 80 assert p.max_incremental_sync_size == 1024 81 assert p.save_on_auto_sync is False 82 assert p.type == 'sync-only' 83 84 85class TestModuleManager(unittest.TestCase): 86 87 def setUp(self): 88 self.spec = ArgumentSpec() 89 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_group.tmos_version') 90 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_group.send_teem') 91 self.m2 = self.p2.start() 92 self.m2.return_value = '14.1.0' 93 self.m3 = self.p3.start() 94 self.m3.return_value = True 95 96 def tearDown(self): 97 self.p2.stop() 98 self.p3.stop() 99 100 def test_create_default_device_group(self, *args): 101 set_module_args( 102 dict( 103 name="foo-group", 104 state="present", 105 provider=dict( 106 server='localhost', 107 password='password', 108 user='admin' 109 ) 110 ) 111 ) 112 113 module = AnsibleModule( 114 argument_spec=self.spec.argument_spec, 115 supports_check_mode=self.spec.supports_check_mode 116 ) 117 mm = ModuleManager(module=module) 118 119 # Override methods to force specific logic in the module to happen 120 mm.create_on_device = Mock(return_value=True) 121 mm.exists = Mock(return_value=False) 122 123 results = mm.exec_module() 124 assert results['changed'] is True 125 126 def test_update_device_group(self, *args): 127 set_module_args( 128 dict( 129 full_sync=True, 130 name="foo-group", 131 state="present", 132 provider=dict( 133 server='localhost', 134 password='password', 135 user='admin' 136 ) 137 ) 138 ) 139 140 current = ApiParameters(params=load_fixture('load_tm_cm_device_group.json')) 141 module = AnsibleModule( 142 argument_spec=self.spec.argument_spec, 143 supports_check_mode=self.spec.supports_check_mode 144 ) 145 mm = ModuleManager(module=module) 146 147 # Override methods to force specific logic in the module to happen 148 mm.update_on_device = Mock(return_value=True) 149 mm.exists = Mock(return_value=True) 150 mm.read_current_from_device = Mock(return_value=current) 151 152 results = mm.exec_module() 153 assert results['changed'] is True 154 155 def test_delete_device_group(self, *args): 156 set_module_args( 157 dict( 158 name="foo-group", 159 state="absent", 160 provider=dict( 161 server='localhost', 162 password='password', 163 user='admin' 164 ) 165 ) 166 ) 167 168 module = AnsibleModule( 169 argument_spec=self.spec.argument_spec, 170 supports_check_mode=self.spec.supports_check_mode 171 ) 172 mm = ModuleManager(module=module) 173 174 # Override methods to force specific logic in the module to happen 175 mm.exists = Mock(side_effect=[True, False]) 176 mm.remove_from_device = Mock(return_value=True) 177 mm.remove_members_in_group_from_device = Mock(return_value=True) 178 179 results = mm.exec_module() 180 assert results['changed'] is True 181