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