1# -*- coding: utf-8 -*- 2# 3# Copyright: (c) 2019, 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_oracle 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 parent='/Common/oracle', 53 interval=5, 54 timeout=120, 55 time_until_up=20, 56 target_username='foobar', 57 send='SELECT * from v$instance1', 58 database='instance1', 59 recv='OPEN', 60 recv_column='1', 61 recv_row='1', 62 count=10, 63 manual_resume=True, 64 debug=True 65 ) 66 p = ModuleParameters(params=args) 67 assert p.parent == '/Common/oracle' 68 assert p.interval == 5 69 assert p.timeout == 120 70 assert p.time_until_up == 20 71 assert p.target_username == 'foobar' 72 assert p.send == 'SELECT * from v$instance1' 73 assert p.recv == 'OPEN' 74 assert p.count == 10 75 assert p.recv_column == '1' 76 assert p.recv_row == '1' 77 assert p.manual_resume == 'enabled' 78 assert p.debug == 'yes' 79 80 def test_api_parameters(self): 81 args = load_fixture('load_oracle_monitor.json') 82 p = ApiParameters(params=args) 83 assert p.parent == '/Common/oracle' 84 assert p.ip == '*' 85 assert p.port == '*' 86 assert p.time_until_up == 0 87 assert p.up_interval == 0 88 assert p.manual_resume == 'disabled' 89 assert p.target_username == 'user' 90 assert p.timeout == 91 91 assert p.count == 1 92 assert p.send == 'foo' 93 assert p.recv == 'bar' 94 assert p.recv_column == '3' 95 assert p.recv_row == '2' 96 assert p.debug == 'no' 97 98 99class TestManager(unittest.TestCase): 100 def setUp(self): 101 self.spec = ArgumentSpec() 102 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_oracle.tmos_version') 103 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_oracle.send_teem') 104 self.m2 = self.p2.start() 105 self.m2.return_value = '14.1.0' 106 self.m3 = self.p3.start() 107 self.m3.return_value = True 108 109 def tearDown(self): 110 self.p2.stop() 111 self.p3.stop() 112 113 def test_create_monitor(self, *args): 114 set_module_args(dict( 115 name='oracledb', 116 parent='/Common/oracle', 117 interval=10, 118 timeout=30, 119 time_until_up=5, 120 target_username='foobar', 121 send='SELECT * from v$instance1', 122 database='instance1', 123 recv='OPEN', 124 recv_column='1', 125 recv_row='1', 126 count=10, 127 manual_resume=True, 128 debug=True, 129 provider=dict( 130 server='localhost', 131 password='password', 132 user='admin' 133 ) 134 )) 135 136 module = AnsibleModule( 137 argument_spec=self.spec.argument_spec, 138 supports_check_mode=self.spec.supports_check_mode 139 ) 140 141 # Override methods in the specific type of manager 142 mm = ModuleManager(module=module) 143 mm.exists = Mock(side_effect=[False, True]) 144 mm.create_on_device = Mock(return_value=True) 145 146 results = mm.exec_module() 147 148 assert results['changed'] is True 149 assert results['parent'] == '/Common/oracle' 150 assert results['interval'] == 10 151 assert results['timeout'] == 30 152 assert results['time_until_up'] == 5 153 assert results['target_username'] == 'foobar' 154 assert results['send'] == 'SELECT * from v$instance1' 155 assert results['recv'] == 'OPEN' 156 assert results['count'] == 10 157 assert results['recv_column'] == '1' 158 assert results['recv_row'] == '1' 159 assert results['manual_resume'] == 'yes' 160 assert results['debug'] == 'yes' 161