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_snmp_dca import (
20    Parameters, 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            agent_type='UCD',
53            community='public',
54            cpu_coefficient='1.5',
55            cpu_threshold='80',
56            parent='/Common/snmp_dca',
57            disk_coefficient='2.0',
58            disk_threshold='90',
59            interval=10,
60            memory_coefficient='1.0',
61            memory_threshold='70',
62            time_until_up=0,
63            timeout=30,
64            version='v1'
65        )
66
67        p = Parameters(params=args)
68        assert p.agent_type == 'UCD'
69        assert p.community == 'public'
70        assert p.cpu_coefficient == 1.5
71        assert p.cpu_threshold == 80
72        assert p.parent == '/Common/snmp_dca'
73        assert p.disk_coefficient == 2.0
74        assert p.disk_threshold == 90
75        assert p.interval == 10
76        assert p.memory_coefficient == 1.0
77        assert p.memory_threshold == 70
78        assert p.time_until_up == 0
79        assert p.timeout == 30
80        assert p.version == 'v1'
81
82    def test_api_parameters(self):
83        args = dict(
84            agentType='UCD',
85            community='public',
86            cpuCoefficient='1.5',
87            cpuThreshold='80',
88            defaultsFrom='/Common/snmp_dca',
89            diskCoefficient='2.0',
90            diskThreshold='90',
91            interval=10,
92            memoryCoefficient='1.0',
93            memoryThreshold='70',
94            timeUntilUp=0,
95            timeout=30,
96            apiRawValues={
97                "userDefined asdasd": "{ foo }",
98                "userDefined bar": "tim rupp",
99                "user-defined baz-": "nia",
100                "userDefined userDefined": "23234"
101            },
102            version='v1'
103        )
104
105        p = Parameters(params=args)
106        assert p.agent_type == 'UCD'
107        assert p.community == 'public'
108        assert p.cpu_coefficient == 1.5
109        assert p.cpu_threshold == 80
110        assert p.parent == '/Common/snmp_dca'
111        assert p.disk_coefficient == 2.0
112        assert p.disk_threshold == 90
113        assert p.interval == 10
114        assert p.memory_coefficient == 1.0
115        assert p.memory_threshold == 70
116        assert p.time_until_up == 0
117        assert p.timeout == 30
118        assert p.version == 'v1'
119
120
121class TestManager(unittest.TestCase):
122    def setUp(self):
123        self.spec = ArgumentSpec()
124        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_snmp_dca.tmos_version')
125        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_snmp_dca.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.p2.stop()
133        self.p3.stop()
134
135    def test_create(self, *args):
136        set_module_args(dict(
137            name='foo',
138            agent_type='UCD',
139            community='public',
140            cpu_coefficient='1.5',
141            cpu_threshold='80',
142            parent='/Common/snmp_dca',
143            disk_coefficient='2.0',
144            disk_threshold='90',
145            memory_coefficient='1.0',
146            memory_threshold='70',
147            version='v1',
148            interval=20,
149            timeout=30,
150            time_until_up=60,
151            provider=dict(
152                server='localhost',
153                password='password',
154                user='admin'
155            )
156        ))
157
158        module = AnsibleModule(
159            argument_spec=self.spec.argument_spec,
160            supports_check_mode=self.spec.supports_check_mode
161        )
162
163        # Override methods in the specific type of manager
164        mm = ModuleManager(module=module)
165        mm.exists = Mock(side_effect=[False, True])
166        mm.create_on_device = Mock(return_value=True)
167
168        results = mm.exec_module()
169
170        assert results['changed'] is True
171