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_dns 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            cache='disable',
53            ip_version=4,
54            name_servers=['10.10.10.10', '11.11.11.11'],
55            search=['14.14.14.14', '15.15.15.15'],
56        )
57        p = Parameters(params=args)
58        assert p.cache == 'disable'
59        assert p.name_servers == ['10.10.10.10', '11.11.11.11']
60        assert p.search == ['14.14.14.14', '15.15.15.15']
61
62        # BIG-IP considers "ipv4" to be an empty value
63        assert p.ip_version == 4
64
65    def test_ipv6_parameter(self):
66        args = dict(
67            ip_version=6
68        )
69        p = Parameters(params=args)
70        assert p.ip_version == 6
71
72
73class TestManager(unittest.TestCase):
74
75    def setUp(self):
76        self.spec = ArgumentSpec()
77        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_dns.tmos_version')
78        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_dns.send_teem')
79        self.m2 = self.p2.start()
80        self.m2.return_value = '14.1.0'
81        self.m3 = self.p3.start()
82        self.m3.return_value = True
83
84    def tearDown(self):
85        self.p2.stop()
86        self.p3.stop()
87
88    def test_update_settings(self, *args):
89        set_module_args(dict(
90            cache='disable',
91            ip_version=4,
92            name_servers=['10.10.10.10', '11.11.11.11'],
93            search=['14.14.14.14', '15.15.15.15'],
94            provider=dict(
95                server='localhost',
96                password='password',
97                user='admin'
98            )
99        ))
100
101        # Configure the parameters that would be returned by querying the
102        # remote device
103        current = Parameters(
104            dict(
105                cache='enable'
106            )
107        )
108
109        module = AnsibleModule(
110            argument_spec=self.spec.argument_spec,
111            supports_check_mode=self.spec.supports_check_mode,
112            required_one_of=self.spec.required_one_of
113        )
114        mm = ModuleManager(module=module)
115
116        # Override methods to force specific logic in the module to happen
117        mm.update_on_device = Mock(return_value=True)
118        mm.read_current_from_device = Mock(return_value=current)
119
120        results = mm.exec_module()
121
122        assert results['changed'] is True
123