1# -*- coding: utf-8 -*-
2#
3# Copyright: (c) 2019, 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_ftp 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            app_service='my.app.foo',
54            parent='parent',
55            description='my descr',
56            debug=True,
57            mode='port',
58            filename='/ftp/var/health.txt',
59            target_username='admin',
60            target_password='sekrit',
61            ip='10.10.10.10',
62            port=80,
63            interval=20,
64            timeout=30,
65            time_until_up=60,
66            up_interval=15,
67            manual_resume=True,
68            partition='Common'
69        )
70
71        p = ModuleParameters(params=args)
72        assert p.name == 'foo'
73        assert p.parent == '/Common/parent'
74        assert p.app_service == 'my.app.foo'
75        assert p.description == 'my descr'
76        assert p.debug == 'enabled'
77        assert p.ip == '10.10.10.10'
78        assert p.target_username == 'admin'
79        assert p.target_password == 'sekrit'
80        assert p.port == 80
81        assert p.destination == '10.10.10.10:80'
82        assert p.interval == 20
83        assert p.timeout == 30
84        assert p.time_until_up == 60
85        assert p.up_interval == 15
86        assert p.manual_resume == 'enabled'
87
88    def test_api_parameters(self):
89        args = load_fixture('load_ltm_monitor_ftp.json')
90        p = ApiParameters(params=args)
91        assert p.name == 'foo_ftp'
92        assert p.destination == '*:*'
93        assert p.ip == '*'
94        assert p.port == '*'
95
96
97class TestManager(unittest.TestCase):
98    def setUp(self):
99        self.spec = ArgumentSpec()
100        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_ftp.tmos_version')
101        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_monitor_ftp.send_teem')
102        self.m2 = self.p2.start()
103        self.m2.return_value = '14.1.0'
104        self.m3 = self.p3.start()
105        self.m3.return_value = True
106
107    def tearDown(self):
108        self.p2.stop()
109        self.p3.stop()
110
111    def test_create_ftp_monitor(self, *args):
112        # Configure the arguments that would be sent to the Ansible module
113        set_module_args(dict(
114            name='foo_ftp',
115            parent='ftp_parent',
116            description='description one',
117            debug='yes',
118            mode='port',
119            filename='/ftp/var/health.txt',
120            target_username='admin',
121            target_password='sekrit',
122            ip='10.10.10.10',
123            port=80,
124            manual_resume='no',
125            provider=dict(
126                server='localhost',
127                password='password',
128                user='admin'
129            )
130        ))
131
132        module = AnsibleModule(
133            argument_spec=self.spec.argument_spec,
134            supports_check_mode=self.spec.supports_check_mode
135        )
136        mm = ModuleManager(module=module)
137
138        # Override methods to force specific logic in the module to happen
139        mm.exists = Mock(return_value=False)
140        mm.create_on_device = Mock(return_value=True)
141
142        results = mm.exec_module()
143
144        assert results['changed'] is True
145        assert results['description'] == 'description one'
146        assert results['debug'] == 'yes'
147        assert results['parent'] == '/Common/ftp_parent'
148        assert results['mode'] == 'port'
149        assert results['manual_resume'] == 'no'
150
151    def test_update_ftp_monitor(self, *args):
152        set_module_args(dict(
153            name='foo_ftp',
154            debug='no',
155            mode='passive',
156            ip='15.15.15.1',
157            port=8080,
158            provider=dict(
159                server='localhost',
160                password='password',
161                user='admin'
162            )
163        ))
164
165        current = ApiParameters(params=load_fixture('load_ltm_profile_ftp.json'))
166
167        module = AnsibleModule(
168            argument_spec=self.spec.argument_spec,
169            supports_check_mode=self.spec.supports_check_mode,
170        )
171
172        # Override methods in the specific type of manager
173        mm = ModuleManager(module=module)
174        mm.exists = Mock(return_value=True)
175        mm.update_on_device = Mock(return_value=True)
176        mm.read_current_from_device = Mock(return_value=current)
177
178        results = mm.exec_module()
179        assert results['changed'] is True
180        assert results['debug'] == 'no'
181        assert results['mode'] == 'passive'
182        assert results['ip'] == '15.15.15.1'
183        assert results['port'] == 8080
184