1# -*- coding: utf-8 -*- 2# 3# Copyright: (c) 2018, 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_profile_analytics 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 parent='bar', 54 description='foo', 55 collect_geo=True, 56 collect_ip=True, 57 ) 58 59 p = ModuleParameters(params=args) 60 assert p.name == 'foo' 61 assert p.parent == '/Common/bar' 62 assert p.description == 'foo' 63 assert p.collect_geo == 'yes' 64 assert p.collect_ip == 'yes' 65 66 def test_api_parameters(self): 67 args = load_fixture('load_ltm_profile_analytics_1.json') 68 p = ApiParameters(params=args) 69 assert p.name == 'foo' 70 assert p.collect_geo == 'no' 71 72 73class TestManager(unittest.TestCase): 74 def setUp(self): 75 self.spec = ArgumentSpec() 76 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_analytics.tmos_version') 77 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_analytics.send_teem') 78 self.m2 = self.p2.start() 79 self.m2.return_value = '14.1.0' 80 self.m3 = self.p3.start() 81 self.m3.return_value = True 82 83 def tearDown(self): 84 self.p2.stop() 85 self.p3.stop() 86 87 def test_create(self, *args): 88 # Configure the arguments that would be sent to the Ansible module 89 set_module_args(dict( 90 name='foo', 91 parent='bar', 92 description='foo', 93 collect_geo=True, 94 collect_ip=True, 95 provider=dict( 96 server='localhost', 97 password='password', 98 user='admin' 99 ) 100 )) 101 102 module = AnsibleModule( 103 argument_spec=self.spec.argument_spec, 104 supports_check_mode=self.spec.supports_check_mode 105 ) 106 mm = ModuleManager(module=module) 107 108 # Override methods to force specific logic in the module to happen 109 mm.exists = Mock(return_value=False) 110 mm.create_on_device = Mock(return_value=True) 111 112 results = mm.exec_module() 113 114 assert results['changed'] is True 115