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_gtm_global 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 synchronization=True, 53 synchronization_group_name='foo', 54 synchronize_zone_files=True 55 ) 56 57 p = ModuleParameters(params=args) 58 assert p.synchronization is True 59 assert p.synchronization_group_name == 'foo' 60 assert p.synchronize_zone_files is True 61 62 def test_api_parameters(self): 63 args = load_fixture('load_gtm_global_settings_general_1.json') 64 65 p = ApiParameters(params=args) 66 assert p.synchronization is False 67 assert p.synchronization_group_name == 'default' 68 assert p.synchronize_zone_files is False 69 70 71class TestManager(unittest.TestCase): 72 73 def setUp(self): 74 self.spec = ArgumentSpec() 75 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_global.tmos_version') 76 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_global.send_teem') 77 self.m2 = self.p2.start() 78 self.m2.return_value = '14.1.0' 79 self.m3 = self.p3.start() 80 self.m3.return_value = True 81 82 def tearDown(self): 83 self.p2.stop() 84 self.p3.stop() 85 86 def update(self, *args): 87 set_module_args(dict( 88 synchronization="yes", 89 synchronization_group_name='foo', 90 synchronize_zone_files="yes", 91 provider=dict( 92 server='localhost', 93 password='password', 94 user='admin' 95 ) 96 )) 97 98 current = ApiParameters(params=load_fixture('load_gtm_global_settings_general_1.json')) 99 module = AnsibleModule( 100 argument_spec=self.spec.argument_spec, 101 supports_check_mode=self.spec.supports_check_mode 102 ) 103 104 # Override methods in the specific type of manager 105 mm = ModuleManager(module=module) 106 mm.update_on_device = Mock(return_value=True) 107 mm.read_current_from_device = Mock(return_value=current) 108 109 results = mm.exec_module() 110 111 assert results['changed'] is True 112 assert results['synchronization'] == 'yes' 113 assert results['synchronization_group_name'] == 'foo' 114 assert results['synchronize_zone_files'] == 'yes' 115