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 18from ansible_collections.f5networks.f5_modules.plugins.modules.bigip_asm_policy_fetch import ( 19 ModuleParameters, ModuleManager, ArgumentSpec 20) 21from ansible_collections.f5networks.f5_modules.tests.unit.modules.utils import set_module_args 22from ansible_collections.f5networks.f5_modules.tests.unit.compat import unittest 23from ansible_collections.f5networks.f5_modules.tests.unit.compat.mock import ( 24 Mock, patch 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 inline='yes', 53 compact='no', 54 base64='yes', 55 dest='/tmp/foo.xml', 56 force='yes', 57 file='foo.xml' 58 ) 59 p = ModuleParameters(params=args) 60 assert p.inline is True 61 assert p.compact is False 62 assert p.base64 is True 63 assert p.file == 'foo.xml' 64 65 66class TestManager(unittest.TestCase): 67 def setUp(self): 68 self.spec = ArgumentSpec() 69 self.patcher1 = patch('time.sleep') 70 self.patcher1.start() 71 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_asm_policy_fetch.module_provisioned') 72 self.m1 = self.p1.start() 73 self.m1.return_value = True 74 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_asm_policy_fetch.tmos_version') 75 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_asm_policy_fetch.send_teem') 76 self.m2 = self.p2.start() 77 self.m2.return_value = '14.1.0' 78 self.m3 = self.p3.start() 79 self.m3.return_value = True 80 81 def tearDown(self): 82 self.patcher1.stop() 83 self.p1.stop() 84 self.p2.stop() 85 self.p3.stop() 86 87 def test_create(self, *args): 88 set_module_args(dict( 89 name='fake_policy', 90 file='foobar.xml', 91 dest='/tmp/foobar.xml', 92 provider=dict( 93 server='localhost', 94 password='password', 95 user='admin' 96 ) 97 )) 98 99 module = AnsibleModule( 100 argument_spec=self.spec.argument_spec, 101 supports_check_mode=self.spec.supports_check_mode, 102 mutually_exclusive=self.spec.mutually_exclusive, 103 ) 104 105 mm = ModuleManager(module=module) 106 mm.want.binary = False 107 mm.exists = Mock(return_value=False) 108 mm.create_on_device = Mock(return_value=True) 109 mm.execute = Mock(return_value=True) 110 111 results = mm.exec_module() 112 113 assert results['changed'] is True 114