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_partition 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 description='my description', 54 route_domain=0 55 ) 56 57 p = ModuleParameters(params=args) 58 assert p.name == 'foo' 59 assert p.description == 'my description' 60 assert p.route_domain == 0 61 62 def test_module_parameters_string_domain(self): 63 args = dict( 64 name='foo', 65 route_domain='0' 66 ) 67 68 p = ModuleParameters(params=args) 69 assert p.name == 'foo' 70 assert p.route_domain == 0 71 72 def test_api_parameters(self): 73 args = dict( 74 name='foo', 75 description='my description', 76 defaultRouteDomain=1 77 ) 78 79 p = ApiParameters(params=args) 80 assert p.name == 'foo' 81 assert p.description == 'my description' 82 assert p.route_domain == 1 83 84 85class TestManagerEcho(unittest.TestCase): 86 def setUp(self): 87 self.spec = ArgumentSpec() 88 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_partition.tmos_version') 89 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_partition.send_teem') 90 self.m2 = self.p2.start() 91 self.m2.return_value = '14.1.0' 92 self.m3 = self.p3.start() 93 self.m3.return_value = True 94 95 def tearDown(self): 96 self.p2.stop() 97 self.p3.stop() 98 99 def test_create_partition(self, *args): 100 set_module_args(dict( 101 name='foo', 102 description='my description', 103 provider=dict( 104 server='localhost', 105 password='password', 106 user='admin' 107 ) 108 )) 109 110 module = AnsibleModule( 111 argument_spec=self.spec.argument_spec, 112 supports_check_mode=self.spec.supports_check_mode 113 ) 114 115 # Override methods in the specific type of manager 116 mm = ModuleManager(module=module) 117 mm.exists = Mock(side_effect=[False, True]) 118 mm.create_on_device = Mock(return_value=True) 119 mm.update_folder_on_device = Mock(return_value=True) 120 121 results = mm.exec_module() 122 123 assert results['changed'] is True 124 125 def test_create_partition_idempotent(self, *args): 126 set_module_args(dict( 127 name='foo', 128 description='my description', 129 provider=dict( 130 server='localhost', 131 password='password', 132 user='admin' 133 ) 134 )) 135 136 current = ApiParameters(params=load_fixture('load_tm_auth_partition.json')) 137 current.update({'folder_description': 'my description'}) 138 module = AnsibleModule( 139 argument_spec=self.spec.argument_spec, 140 supports_check_mode=self.spec.supports_check_mode 141 ) 142 143 # Override methods in the specific type of manager 144 mm = ModuleManager(module=module) 145 mm.exists = Mock(return_value=True) 146 mm.read_current_from_device = Mock(return_value=current) 147 148 results = mm.exec_module() 149 150 assert results['changed'] is False 151 152 def test_update_description(self, *args): 153 set_module_args(dict( 154 name='foo', 155 description='another description', 156 provider=dict( 157 server='localhost', 158 password='password', 159 user='admin' 160 ) 161 )) 162 163 current = ApiParameters(params=load_fixture('load_tm_auth_partition.json')) 164 module = AnsibleModule( 165 argument_spec=self.spec.argument_spec, 166 supports_check_mode=self.spec.supports_check_mode 167 ) 168 169 # Override methods in the specific type of manager 170 mm = ModuleManager(module=module) 171 mm.exists = Mock(return_value=True) 172 mm.read_current_from_device = Mock(return_value=current) 173 mm.update_on_device = Mock(return_value=True) 174 mm.update_folder_on_device = Mock(return_value=True) 175 176 results = mm.exec_module() 177 178 assert results['changed'] is True 179 assert results['description'] == 'another description' 180 181 def test_update_route_domain(self, *args): 182 set_module_args(dict( 183 name='foo', 184 route_domain=1, 185 provider=dict( 186 server='localhost', 187 password='password', 188 user='admin' 189 ) 190 )) 191 192 current = ApiParameters(params=load_fixture('load_tm_auth_partition.json')) 193 module = AnsibleModule( 194 argument_spec=self.spec.argument_spec, 195 supports_check_mode=self.spec.supports_check_mode 196 ) 197 198 # Override methods in the specific type of manager 199 mm = ModuleManager(module=module) 200 mm.exists = Mock(return_value=True) 201 mm.read_current_from_device = Mock(return_value=current) 202 mm.update_on_device = Mock(return_value=True) 203 204 results = mm.exec_module() 205 206 assert results['changed'] is True 207 assert results['route_domain'] == 1 208