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_hostname 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 hostname='foo.internal.com' 53 ) 54 p = ModuleParameters(params=args) 55 assert p.hostname == 'foo.internal.com' 56 57 58class TestManager(unittest.TestCase): 59 def setUp(self): 60 self.spec = ArgumentSpec() 61 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_hostname.tmos_version') 62 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_hostname.send_teem') 63 self.m2 = self.p2.start() 64 self.m2.return_value = '14.1.0' 65 self.m3 = self.p3.start() 66 self.m3.return_value = True 67 68 def tearDown(self): 69 self.p2.stop() 70 self.p3.stop() 71 72 def test_update_hostname(self, *args): 73 set_module_args(dict( 74 hostname='foo2.internal.com', 75 provider=dict( 76 server='localhost', 77 password='password', 78 user='admin' 79 ) 80 )) 81 82 # Configure the parameters that would be returned by querying the 83 # remote device 84 current = ApiParameters( 85 params=dict( 86 hostname='foo.internal.com' 87 ) 88 ) 89 module = AnsibleModule( 90 argument_spec=self.spec.argument_spec, 91 supports_check_mode=self.spec.supports_check_mode 92 ) 93 94 # Override methods to force specific logic in the module to happen 95 mm = ModuleManager(module=module) 96 mm.update_on_device = Mock(return_value=True) 97 mm.read_current_from_device = Mock(return_value=current) 98 99 results = mm.exec_module() 100 101 assert results['changed'] is True 102 assert results['hostname'] == 'foo2.internal.com' 103