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_fastl4_tcp 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
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            description='my description',
54            service_environment='bar',
55            servers=[
56                dict(
57                    address='1.2.3.4',
58                    port=8080
59                ),
60                dict(
61                    address='5.6.7.8',
62                    port=8000
63                )
64            ],
65            inbound_virtual=dict(
66                address='2.2.2.2',
67                netmask='255.255.255.255',
68                port=80
69            )
70        )
71
72        p = ModuleParameters(params=args)
73        assert p.name == 'foo'
74        assert p.config_set_name == 'foo'
75        assert p.sub_path == 'foo'
76        assert p.http_profile == 'profile_http'
77        assert p.service_environment == 'bar'
78        assert len(p.servers) == 2
79        assert 'address' in p.servers[0]
80        assert 'port' in p.servers[0]
81        assert 'address' in p.servers[1]
82        assert 'port' in p.servers[1]
83        assert p.servers[0]['address'] == '1.2.3.4'
84        assert p.servers[0]['port'] == 8080
85        assert p.servers[1]['address'] == '5.6.7.8'
86        assert p.servers[1]['port'] == 8000
87        assert 'address' in p.inbound_virtual
88        assert 'netmask' in p.inbound_virtual
89        assert 'port' in p.inbound_virtual
90        assert p.inbound_virtual['address'] == '2.2.2.2'
91        assert p.inbound_virtual['netmask'] == '255.255.255.255'
92        assert p.inbound_virtual['port'] == 80
93
94
95class TestManager(unittest.TestCase):
96    def setUp(self):
97        self.spec = ArgumentSpec()
98        self.patcher1 = patch('time.sleep')
99        self.patcher1.start()
100        self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_fastl4_tcp.bigiq_version')
101        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_fastl4_tcp.ModuleParameters.template_reference')
102        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_fastl4_tcp.ModuleParameters.default_device_reference')
103
104        self.m1 = self.p1.start()
105        self.m2 = self.p2.start()
106        self.m3 = self.p3.start()
107
108        self.m1.return_value = '6.1.0'
109        self.m2.return_value = Mock(return_value='https://localhost/mgmt/foobar1')
110        self.m3.return_value = Mock(return_value='https://localhost/mgmt/foobar3')
111        self.p5 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigiq_application_fastl4_tcp.send_teem')
112        self.m5 = self.p5.start()
113        self.m5.return_value = True
114
115    def tearDown(self):
116        self.patcher1.stop()
117        self.p1.stop()
118        self.p2.stop()
119        self.p3.stop()
120        self.p5.stop()
121
122    def test_create(self, *args):
123        set_module_args(dict(
124            name='foo',
125            description='my description',
126            service_environment='bar',
127            servers=[
128                dict(
129                    address='1.2.3.4',
130                    port=8080
131                ),
132                dict(
133                    address='5.6.7.8',
134                    port=8000
135                )
136            ],
137            inbound_virtual=dict(
138                address='2.2.2.2',
139                netmask='255.255.255.255',
140                port=80
141            ),
142            provider=dict(
143                server='localhost',
144                password='password',
145                user='admin'
146            )
147        ))
148
149        module = AnsibleModule(
150            argument_spec=self.spec.argument_spec,
151            supports_check_mode=self.spec.supports_check_mode
152        )
153
154        # Override methods to force specific logic in the module to happen
155        mm = ModuleManager(module=module)
156        mm.check_bigiq_version = Mock(return_value=True)
157        mm.has_no_service_environment = Mock(return_value=False)
158        mm.wait_for_apply_template_task = Mock(return_value=True)
159
160        mm.create_on_device = Mock(return_value=True)
161        mm.exists = Mock(side_effect=[False, True])
162
163        results = mm.exec_module()
164
165        assert results['changed'] is True
166        assert results['description'] == 'my description'
167
168    def test_bigiq_version_raises(self):
169        set_module_args(dict(
170            name='foo',
171            description='my description',
172            service_environment='bar',
173            servers=[
174                dict(
175                    address='1.2.3.4',
176                    port=8080
177                ),
178                dict(
179                    address='5.6.7.8',
180                    port=8000
181                )
182            ],
183            inbound_virtual=dict(
184                address='2.2.2.2',
185                netmask='255.255.255.255',
186                port=80
187            ),
188            provider=dict(
189                server='localhost',
190                password='password',
191                user='admin'
192            )
193        ))
194
195        module = AnsibleModule(
196            argument_spec=self.spec.argument_spec,
197            supports_check_mode=self.spec.supports_check_mode
198        )
199
200        msg = 'Module supports only BIGIQ version 6.0.x or lower.'
201        # Override methods to force specific logic in the module to happen
202        mm = ModuleManager(module=module)
203
204        with pytest.raises(F5ModuleError) as err:
205            mm.exec_module()
206        assert str(err.value) == msg
207