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_monitor_dns 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 26fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') 27fixture_data = {} 28 29 30def load_fixture(name): 31 path = os.path.join(fixture_path, name) 32 33 if path in fixture_data: 34 return fixture_data[path] 35 36 with open(path) as f: 37 data = f.read() 38 39 try: 40 data = json.loads(data) 41 except Exception: 42 pass 43 44 fixture_data[path] = data 45 return data 46 47 48class TestParameters(unittest.TestCase): 49 def test_module_parameters(self): 50 args = dict( 51 parent='/Common/dns', 52 interval=10, 53 time_until_up=0, 54 timeout=30, 55 ) 56 57 p = ModuleParameters(params=args) 58 assert p.parent == '/Common/dns' 59 assert p.interval == 10 60 assert p.time_until_up == 0 61 assert p.timeout == 30 62 63 def test_api_parameters(self): 64 args = dict( 65 defaultsFrom='/Common/dns', 66 interval=10, 67 timeUntilUp=0, 68 timeout=30, 69 ) 70 71 p = ApiParameters(params=args) 72 assert p.parent == '/Common/dns' 73 assert p.interval == 10 74 assert p.time_until_up == 0 75 assert p.timeout == 30 76 77 78class TestManager(unittest.TestCase): 79 def setUp(self): 80 self.spec = ArgumentSpec() 81 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_dns.tmos_version') 82 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_dns.send_teem') 83 self.m2 = self.p2.start() 84 self.m2.return_value = '14.1.0' 85 self.m3 = self.p3.start() 86 self.m3.return_value = True 87 88 def tearDown(self): 89 self.p2.stop() 90 self.p3.stop() 91 92 def test_create(self, *args): 93 set_module_args(dict( 94 name='foo', 95 parent='/Common/dns', 96 query_name='foo', 97 interval=20, 98 timeout=30, 99 time_until_up=60, 100 provider=dict( 101 server='localhost', 102 password='password', 103 user='admin' 104 ) 105 )) 106 107 module = AnsibleModule( 108 argument_spec=self.spec.argument_spec, 109 supports_check_mode=self.spec.supports_check_mode 110 ) 111 112 # Override methods in the specific type of manager 113 mm = ModuleManager(module=module) 114 mm.exists = Mock(side_effect=[False, True]) 115 mm.create_on_device = Mock(return_value=True) 116 117 results = mm.exec_module() 118 119 assert results['changed'] is True 120 121 def test_create_with_description(self, *args): 122 set_module_args(dict( 123 name='foo', 124 parent='/Common/dns', 125 query_name='foo', 126 interval=20, 127 timeout=30, 128 time_until_up=60, 129 description='Important Description', 130 provider=dict( 131 server='localhost', 132 password='password', 133 user='admin' 134 ) 135 )) 136 137 module = AnsibleModule( 138 argument_spec=self.spec.argument_spec, 139 supports_check_mode=self.spec.supports_check_mode 140 ) 141 142 # Override methods in the specific type of manager 143 mm = ModuleManager(module=module) 144 mm.exists = Mock(side_effect=[False, True]) 145 mm.create_on_device = Mock(return_value=True) 146 147 results = mm.exec_module() 148 149 assert results['changed'] is True 150