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_device_sshd 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
26fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
27fixture_data = {}
28
29
30def load_fixture(name):
31    path = os.path.join(fixture_path, name)
32
33    if path in fixture_data:
34        return fixture_data[path]
35
36    with open(path) as f:
37        data = f.read()
38
39    try:
40        data = json.loads(data)
41    except Exception:
42        pass
43
44    fixture_data[path] = data
45    return data
46
47
48class TestParameters(unittest.TestCase):
49    def test_module_parameters(self):
50        args = dict(
51            allow=['all'],
52            banner='enabled',
53            banner_text='asdf',
54            inactivity_timeout='100',
55            log_level='debug',
56            login='enabled',
57            port=1010,
58        )
59        p = ModuleParameters(params=args)
60        assert p.allow == ['all']
61        assert p.banner == 'enabled'
62        assert p.banner_text == 'asdf'
63        assert p.inactivity_timeout == 100
64        assert p.log_level == 'debug'
65        assert p.login == 'enabled'
66        assert p.port == 1010
67
68
69class TestManager(unittest.TestCase):
70
71    def setUp(self):
72        self.spec = ArgumentSpec()
73        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_sshd.tmos_version')
74        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_sshd.send_teem')
75        self.m2 = self.p2.start()
76        self.m2.return_value = '14.1.0'
77        self.m3 = self.p3.start()
78        self.m3.return_value = True
79
80    def tearDown(self):
81        self.p2.stop()
82        self.p3.stop()
83
84    def test_update_settings(self, *args):
85        set_module_args(dict(
86            allow=['all'],
87            banner='enabled',
88            banner_text='asdf',
89            inactivity_timeout='100',
90            log_level='debug',
91            login='enabled',
92            port=1010,
93            provider=dict(
94                server='localhost',
95                password='password',
96                user='admin'
97            )
98        ))
99
100        # Configure the parameters that would be returned by querying the
101        # remote device
102        current = ApiParameters(
103            params=dict(
104                allow=['172.27.1.1']
105            )
106        )
107
108        module = AnsibleModule(
109            argument_spec=self.spec.argument_spec,
110            supports_check_mode=self.spec.supports_check_mode
111        )
112        mm = ModuleManager(module=module)
113
114        # Override methods to force specific logic in the module to happen
115        mm.update_on_device = Mock(return_value=True)
116        mm.read_current_from_device = Mock(return_value=current)
117
118        results = mm.exec_module()
119
120        assert results['changed'] is True
121        assert results['allow'] == ['all']
122