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.bigiq_regkey_license import ( 20 ModuleParameters, ApiParameters, 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 regkey_pool='foo', 53 license_key='XXXX-XXXX-XXXX-XXXX-XXXX', 54 accept_eula=True, 55 description='this is a description' 56 ) 57 58 p = ModuleParameters(params=args) 59 assert p.regkey_pool == 'foo' 60 assert p.license_key == 'XXXX-XXXX-XXXX-XXXX-XXXX' 61 assert p.accept_eula is True 62 assert p.description == 'this is a description' 63 64 def test_api_parameters(self): 65 args = load_fixture('load_regkey_license_key.json') 66 67 p = ApiParameters(params=args) 68 assert p.description == 'foo bar baz' 69 70 71class TestManager(unittest.TestCase): 72 def setUp(self): 73 self.spec = ArgumentSpec() 74 self.patcher1 = patch('time.sleep') 75 self.patcher1.start() 76 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_regkey_license.bigiq_version') 77 self.m1 = self.p1.start() 78 self.m1.return_value = '6.1.0' 79 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_regkey_license.send_teem') 80 self.m2 = self.p2.start() 81 self.m2.return_value = True 82 83 def tearDown(self): 84 self.patcher1.stop() 85 self.p1.stop() 86 self.p2.stop() 87 88 def test_create(self, *args): 89 set_module_args(dict( 90 regkey_pool='foo', 91 license_key='XXXX-XXXX-XXXX-XXXX-XXXX', 92 accept_eula=True, 93 description='this is a description', 94 provider=dict( 95 server='localhost', 96 password='password', 97 user='admin' 98 ) 99 )) 100 101 module = AnsibleModule( 102 argument_spec=self.spec.argument_spec, 103 supports_check_mode=self.spec.supports_check_mode 104 ) 105 mm = ModuleManager(module=module) 106 107 # Override methods to force specific logic in the module to happen 108 mm.exists = Mock(side_effect=[False, True]) 109 mm.create_on_device = Mock(return_value=True) 110 111 results = mm.exec_module() 112 113 assert results['changed'] is True 114 assert results['description'] == 'this is a description' 115