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.bigiq_application_http import ( 20 ModuleParameters, ModuleManager, ArgumentSpec 21) 22from ansible_collections.f5networks.f5_modules.plugins.module_utils.common import F5ModuleError 23from ansible_collections.f5networks.f5_modules.tests.unit.compat import unittest 24from ansible_collections.f5networks.f5_modules.tests.unit.compat.mock import Mock, patch 25from ansible_collections.f5networks.f5_modules.tests.unit.modules.utils import set_module_args 26 27 28fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') 29fixture_data = {} 30 31 32def load_fixture(name): 33 path = os.path.join(fixture_path, name) 34 35 if path in fixture_data: 36 return fixture_data[path] 37 38 with open(path) as f: 39 data = f.read() 40 41 try: 42 data = json.loads(data) 43 except Exception: 44 pass 45 46 fixture_data[path] = data 47 return data 48 49 50class TestParameters(unittest.TestCase): 51 def test_module_parameters(self): 52 args = dict( 53 name='foo', 54 description='my description', 55 service_environment='bar', 56 servers=[ 57 dict( 58 address='1.2.3.4', 59 port=8080 60 ), 61 dict( 62 address='5.6.7.8', 63 port=8000 64 ) 65 ], 66 inbound_virtual=dict( 67 address='2.2.2.2', 68 netmask='255.255.255.255', 69 port=80 70 ) 71 ) 72 73 p = ModuleParameters(params=args) 74 assert p.name == 'foo' 75 assert p.config_set_name == 'foo' 76 assert p.sub_path == 'foo' 77 assert p.http_profile == 'profile_http' 78 assert p.service_environment == 'bar' 79 assert len(p.servers) == 2 80 assert 'address' in p.servers[0] 81 assert 'port' in p.servers[0] 82 assert 'address' in p.servers[1] 83 assert 'port' in p.servers[1] 84 assert p.servers[0]['address'] == '1.2.3.4' 85 assert p.servers[0]['port'] == 8080 86 assert p.servers[1]['address'] == '5.6.7.8' 87 assert p.servers[1]['port'] == 8000 88 assert 'address' in p.inbound_virtual 89 assert 'netmask' in p.inbound_virtual 90 assert 'port' in p.inbound_virtual 91 assert p.inbound_virtual['address'] == '2.2.2.2' 92 assert p.inbound_virtual['netmask'] == '255.255.255.255' 93 assert p.inbound_virtual['port'] == 80 94 95 96class TestManager(unittest.TestCase): 97 def setUp(self): 98 self.spec = ArgumentSpec() 99 self.patcher1 = patch('time.sleep') 100 self.patcher1.start() 101 102 self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_http.bigiq_version') 103 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_http.ModuleParameters.template_reference') 104 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_http.ModuleParameters.ssg_reference') 105 self.p4 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_http.ModuleParameters.default_device_reference') 106 107 self.m1 = self.p1.start() 108 self.m2 = self.p2.start() 109 self.m3 = self.p3.start() 110 self.m4 = self.p4.start() 111 112 self.m1.return_value = '6.1.0' 113 self.m2.return_value = Mock(return_value='https://localhost/mgmt/foobar1') 114 self.m3.return_value = Mock(return_value='https://localhost/mgmt/foobar2') 115 self.m4.return_value = Mock(return_value='https://localhost/mgmt/foobar3') 116 117 self.p5 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_http.send_teem') 118 self.m5 = self.p5.start() 119 self.m5.return_value = True 120 121 def tearDown(self): 122 self.patcher1.stop() 123 self.p1.stop() 124 self.p2.stop() 125 self.p3.stop() 126 self.p4.stop() 127 self.p5.stop() 128 129 def test_create(self, *args): 130 set_module_args(dict( 131 name='foo', 132 description='my description', 133 service_environment='bar', 134 servers=[ 135 dict( 136 address='1.2.3.4', 137 port=8080 138 ), 139 dict( 140 address='5.6.7.8', 141 port=8000 142 ) 143 ], 144 inbound_virtual=dict( 145 address='2.2.2.2', 146 netmask='255.255.255.255', 147 port=80 148 ), 149 provider=dict( 150 server='localhost', 151 password='password', 152 user='admin' 153 ) 154 )) 155 156 module = AnsibleModule( 157 argument_spec=self.spec.argument_spec, 158 supports_check_mode=self.spec.supports_check_mode 159 ) 160 161 # Override methods to force specific logic in the module to happen 162 mm = ModuleManager(module=module) 163 mm.check_bigiq_version = Mock(return_value=True) 164 mm.has_no_service_environment = Mock(return_value=False) 165 mm.wait_for_apply_template_task = Mock(return_value=True) 166 167 mm.create_on_device = Mock(return_value=True) 168 mm.exists = Mock(side_effect=[False, True]) 169 170 results = mm.exec_module() 171 172 assert results['changed'] is True 173 assert results['description'] == 'my description' 174 175 def test_bigiq_version_raises(self): 176 set_module_args(dict( 177 name='foo', 178 description='my description', 179 service_environment='bar', 180 servers=[ 181 dict( 182 address='1.2.3.4', 183 port=8080 184 ), 185 dict( 186 address='5.6.7.8', 187 port=8000 188 ) 189 ], 190 inbound_virtual=dict( 191 address='2.2.2.2', 192 netmask='255.255.255.255', 193 port=80 194 ), 195 provider=dict( 196 server='localhost', 197 password='password', 198 user='admin' 199 ) 200 )) 201 202 module = AnsibleModule( 203 argument_spec=self.spec.argument_spec, 204 supports_check_mode=self.spec.supports_check_mode 205 ) 206 207 msg = 'Module supports only BIGIQ version 6.0.x or lower.' 208 # Override methods to force specific logic in the module to happen 209 mm = ModuleManager(module=module) 210 211 with pytest.raises(F5ModuleError) as err: 212 mm.exec_module() 213 assert str(err.value) == msg 214