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_snmp_trap import (
20    V2Parameters, V1Parameters, ModuleManager, V2Manager, V1Manager, 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, DEFAULT
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_networked_parameters(self):
51        args = dict(
52            name='foo',
53            snmp_version='1',
54            community='public',
55            destination='10.10.10.10',
56            port=1000,
57            network='other',
58        )
59        p = V2Parameters(params=args)
60        assert p.name == 'foo'
61        assert p.snmp_version == '1'
62        assert p.community == 'public'
63        assert p.destination == '10.10.10.10'
64        assert p.port == 1000
65        assert p.network == 'other'
66
67    def test_module_non_networked_parameters(self):
68        args = dict(
69            name='foo',
70            snmp_version='1',
71            community='public',
72            destination='10.10.10.10',
73            port=1000,
74            network='other',
75        )
76        p = V1Parameters(params=args)
77        assert p.name == 'foo'
78        assert p.snmp_version == '1'
79        assert p.community == 'public'
80        assert p.destination == '10.10.10.10'
81        assert p.port == 1000
82        assert p.network is None
83
84    def test_api_parameters(self):
85        args = dict(
86            name='foo',
87            community='public',
88            host='10.10.10.10',
89            network='other',
90            version=1,
91            port=1000
92        )
93        p = V2Parameters(params=args)
94        assert p.name == 'foo'
95        assert p.snmp_version == '1'
96        assert p.community == 'public'
97        assert p.destination == '10.10.10.10'
98        assert p.port == 1000
99        assert p.network == 'other'
100
101
102class TestManager(unittest.TestCase):
103    def setUp(self):
104        self.spec = ArgumentSpec()
105        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_snmp_trap.tmos_version')
106        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_snmp_trap.send_teem')
107        self.m2 = self.p2.start()
108        self.m2.return_value = '14.1.0'
109        self.m3 = self.p3.start()
110        self.m3.return_value = True
111
112    def tearDown(self):
113        self.p2.stop()
114        self.p3.stop()
115
116    def test_create_trap(self, *args):
117        set_module_args(dict(
118            name='foo',
119            snmp_version='1',
120            community='public',
121            destination='10.10.10.10',
122            port=1000,
123            network='other',
124            provider=dict(
125                server='localhost',
126                password='password',
127                user='admin'
128            )
129        ))
130
131        module = AnsibleModule(
132            argument_spec=self.spec.argument_spec,
133            supports_check_mode=self.spec.supports_check_mode
134        )
135
136        # Override methods to force specific logic in the module to happen
137        m0 = ModuleManager(module=module)
138        m0.is_version_without_network = Mock(return_value=False)
139        m0.is_version_with_default_network = Mock(return_value=True)
140
141        patches = dict(
142            create_on_device=DEFAULT,
143            exists=DEFAULT
144        )
145        with patch.multiple(V2Manager, **patches) as mo:
146            mo['create_on_device'].side_effect = Mock(return_value=True)
147            mo['exists'].side_effect = Mock(return_value=False)
148            results = m0.exec_module()
149
150        assert results['changed'] is True
151        assert results['port'] == 1000
152        assert results['snmp_version'] == '1'
153
154    def test_create_trap_non_network(self, *args):
155        set_module_args(dict(
156            name='foo',
157            snmp_version='1',
158            community='public',
159            destination='10.10.10.10',
160            port=1000,
161            provider=dict(
162                server='localhost',
163                password='password',
164                user='admin'
165            )
166        ))
167
168        module = AnsibleModule(
169            argument_spec=self.spec.argument_spec,
170            supports_check_mode=self.spec.supports_check_mode
171        )
172
173        # Override methods to force specific logic in the module to happen
174        m0 = ModuleManager(module=module)
175        m0.is_version_without_network = Mock(return_value=True)
176
177        patches = dict(
178            create_on_device=DEFAULT,
179            exists=DEFAULT
180        )
181        with patch.multiple(V1Manager, **patches) as mo:
182            mo['create_on_device'].side_effect = Mock(return_value=True)
183            mo['exists'].side_effect = Mock(return_value=False)
184            results = m0.exec_module()
185
186        assert results['changed'] is True
187        assert results['port'] == 1000
188        assert results['snmp_version'] == '1'
189