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_profile_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            parent='bar',
54            description='description one',
55            inherit_parent_profile=True,
56            log_profile='bazbar',
57            log_publisher='pub1',
58            translate_extended=True,
59            port=635,
60            security=False,
61            partition='Common'
62        )
63
64        p = ModuleParameters(params=args)
65
66        assert p.name == 'foo'
67        assert p.parent == '/Common/bar'
68        assert p.description == 'description one'
69        assert p.inherit_parent_profile == 'enabled'
70        assert p.log_profile == '/Common/bazbar'
71        assert p.log_publisher == '/Common/pub1'
72        assert p.translate_extended == 'enabled'
73        assert p.port == 635
74        assert p.security == 'disabled'
75
76    def test_api_parameters(self):
77        args = load_fixture('load_ltm_profile_ftp.json')
78        p = ApiParameters(params=args)
79        assert p.name == 'foo'
80        assert p.description is None
81
82
83class TestManager(unittest.TestCase):
84    def setUp(self):
85        self.spec = ArgumentSpec()
86        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_ftp.tmos_version')
87        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_ftp.send_teem')
88        self.m2 = self.p2.start()
89        self.m2.return_value = '14.1.0'
90        self.m3 = self.p3.start()
91        self.m3.return_value = True
92
93    def tearDown(self):
94        self.p2.stop()
95        self.p3.stop()
96
97    def test_create_ftp_profile(self, *args):
98        # Configure the arguments that would be sent to the Ansible module
99        set_module_args(dict(
100            name='foo',
101            parent='bar',
102            description='description one',
103            inherit_parent_profile=True,
104            provider=dict(
105                server='localhost',
106                password='password',
107                user='admin'
108            )
109        ))
110
111        module = AnsibleModule(
112            argument_spec=self.spec.argument_spec,
113            supports_check_mode=self.spec.supports_check_mode
114        )
115        mm = ModuleManager(module=module)
116
117        # Override methods to force specific logic in the module to happen
118        mm.exists = Mock(return_value=False)
119        mm.create_on_device = Mock(return_value=True)
120
121        results = mm.exec_module()
122
123        assert results['changed'] is True
124        assert results['description'] == 'description one'
125        assert results['inherit_parent_profile'] == 'yes'
126        assert results['parent'] == '/Common/bar'
127
128    def test_update_ftp_profile(self, *args):
129        set_module_args(dict(
130            name='foo',
131            description='my description',
132            allow_ftps=False,
133            inherit_parent_profile=False,
134            port=2048,
135            log_profile='alg_profile',
136            log_publisher='baz_publish',
137            provider=dict(
138                server='localhost',
139                password='password',
140                user='admin'
141            )
142        ))
143
144        current = ApiParameters(params=load_fixture('load_ltm_profile_ftp.json'))
145
146        module = AnsibleModule(
147            argument_spec=self.spec.argument_spec,
148            supports_check_mode=self.spec.supports_check_mode,
149        )
150
151        # Override methods in the specific type of manager
152        mm = ModuleManager(module=module)
153        mm.exists = Mock(return_value=True)
154        mm.update_on_device = Mock(return_value=True)
155        mm.read_current_from_device = Mock(return_value=current)
156
157        results = mm.exec_module()
158        assert results['changed'] is True
159        assert results['description'] == 'my description'
160        assert results['allow_ftps'] == 'no'
161        assert results['inherit_parent_profile'] == 'no'
162        assert results['port'] == 2048
163        assert results['log_profile'] == '/Common/alg_profile'
164        assert results['log_publisher'] == '/Common/baz_publish'
165