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_gtm_monitor_http 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='/Common/my-http', 54 send='the send string', 55 receive='the receive string', 56 ip='1.1.1.1', 57 port='80', 58 interval='10', 59 timeout='20', 60 ignore_down_response=True, 61 transparent=False, 62 probe_timeout='30', 63 reverse=True 64 ) 65 66 p = ModuleParameters(params=args) 67 assert p.name == 'foo' 68 assert p.parent == '/Common/my-http' 69 assert p.send == 'the send string' 70 assert p.receive == 'the receive string' 71 assert p.destination == '1.1.1.1:80' 72 assert p.ip == '1.1.1.1' 73 assert p.port == 80 74 assert p.interval == 10 75 assert p.timeout == 20 76 assert p.ignore_down_response is True 77 assert p.transparent is False 78 assert p.probe_timeout == 30 79 assert p.reverse is True 80 81 def test_api_parameters(self): 82 args = load_fixture('load_gtm_monitor_http_1.json') 83 84 p = ApiParameters(params=args) 85 assert p.name == 'foo' 86 assert p.parent == '/Common/http' 87 assert p.send == 'GET /' 88 assert p.receive == 'the receive string' 89 assert p.destination == '3.3.3.3:8080' 90 assert p.ip == '3.3.3.3' 91 assert p.port == 8080 92 assert p.interval == 30 93 assert p.timeout == 120 94 assert p.ignore_down_response is False 95 assert p.transparent is True 96 assert p.probe_timeout == 5 97 assert p.reverse is True 98 99 100class TestManager(unittest.TestCase): 101 102 def setUp(self): 103 self.spec = ArgumentSpec() 104 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_http.module_provisioned') 105 self.m1 = self.p1.start() 106 self.m1.return_value = True 107 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_http.tmos_version') 108 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_http.send_teem') 109 self.m2 = self.p2.start() 110 self.m2.return_value = '14.1.0' 111 self.m3 = self.p3.start() 112 self.m3.return_value = True 113 114 def tearDown(self): 115 self.p1.stop() 116 self.p2.stop() 117 self.p3.stop() 118 119 def test_create_monitor(self, *args): 120 set_module_args(dict( 121 name='foo', 122 ip='10.10.10.10', 123 port=80, 124 interval=20, 125 timeout=30, 126 provider=dict( 127 server='localhost', 128 password='password', 129 user='admin' 130 ) 131 )) 132 133 module = AnsibleModule( 134 argument_spec=self.spec.argument_spec, 135 supports_check_mode=self.spec.supports_check_mode 136 ) 137 138 # Override methods in the specific type of manager 139 mm = ModuleManager(module=module) 140 mm.exists = Mock(side_effect=[False, True]) 141 mm.create_on_device = Mock(return_value=True) 142 mm.module_provisioned = Mock(return_value=True) 143 144 results = mm.exec_module() 145 146 assert results['changed'] is True 147