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