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_bigip 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='parent',
54            ip='10.10.10.10',
55            port=80,
56            interval=20,
57            timeout=30,
58            aggregate_dynamic_ratios='average-members',
59            partition='Common'
60        )
61
62        p = ModuleParameters(params=args)
63        assert p.name == 'foo'
64        assert p.parent == '/Common/parent'
65        assert p.ip == '10.10.10.10'
66        assert p.port == 80
67        assert p.type == 'bigip'
68        assert p.destination == '10.10.10.10:80'
69        assert p.interval == 20
70        assert p.timeout == 30
71        assert p.aggregate_dynamic_ratios == 'average-members'
72
73    def test_module_parameters_ints_as_strings(self):
74        args = dict(
75            name='foo',
76            parent='parent',
77            ip='10.10.10.10',
78            port='80',
79            interval='20',
80            timeout='30',
81            partition='Common'
82        )
83
84        p = ModuleParameters(params=args)
85        assert p.name == 'foo'
86        assert p.parent == '/Common/parent'
87        assert p.ip == '10.10.10.10'
88        assert p.port == 80
89        assert p.type == 'bigip'
90        assert p.destination == '10.10.10.10:80'
91        assert p.interval == 20
92        assert p.timeout == 30
93
94    def test_api_parameters(self):
95        args = dict(
96            name='foo',
97            defaultsFrom='/Common/parent',
98            destination='10.10.10.10:80',
99            interval=20,
100            timeout=30,
101            ignoreDownResponse='disabled',
102            aggregateDynamicRatios='none',
103        )
104
105        p = ApiParameters(params=args)
106        assert p.name == 'foo'
107        assert p.parent == '/Common/parent'
108        assert p.ip == '10.10.10.10'
109        assert p.port == 80
110        assert p.type == 'bigip'
111        assert p.destination == '10.10.10.10:80'
112        assert p.interval == 20
113        assert p.timeout == 30
114        assert p.aggregate_dynamic_ratios == 'none'
115
116
117class TestManager(unittest.TestCase):
118
119    def setUp(self):
120        self.spec = ArgumentSpec()
121        self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_bigip.module_provisioned')
122        self.m1 = self.p1.start()
123        self.m1.return_value = True
124        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_bigip.tmos_version')
125        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_monitor_bigip.send_teem')
126        self.m2 = self.p2.start()
127        self.m2.return_value = '14.1.0'
128        self.m3 = self.p3.start()
129        self.m3.return_value = True
130
131    def tearDown(self):
132        self.p1.stop()
133        self.p2.stop()
134        self.p3.stop()
135
136    def test_create_monitor(self, *args):
137        set_module_args(dict(
138            name='foo',
139            ip='10.10.10.10',
140            port=80,
141            interval=20,
142            timeout=30,
143            provider=dict(
144                server='localhost',
145                password='password',
146                user='admin'
147            )
148        ))
149
150        module = AnsibleModule(
151            argument_spec=self.spec.argument_spec,
152            supports_check_mode=self.spec.supports_check_mode
153        )
154
155        # Override methods in the specific type of manager
156        mm = ModuleManager(module=module)
157        mm.exists = Mock(side_effect=[False, True])
158        mm.create_on_device = Mock(return_value=True)
159        mm.module_provisioned = Mock(return_value=True)
160
161        results = mm.exec_module()
162
163        assert results['changed'] is True
164