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_profile_dns 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            enable_dns_express=True,
55            enable_zone_transfer=True,
56            enable_dnssec=True,
57            enable_gtm=True,
58            process_recursion_desired=True,
59            use_local_bind=True,
60            enable_dns_firewall=True,
61        )
62
63        p = ModuleParameters(params=args)
64        assert p.name == 'foo'
65        assert p.parent == '/Common/bar'
66        assert p.enable_dns_express is True
67        assert p.enable_zone_transfer is True
68        assert p.enable_dnssec is True
69        assert p.enable_gtm is True
70        assert p.process_recursion_desired is True
71        assert p.use_local_bind is True
72        assert p.enable_dns_firewall is True
73
74    def test_api_parameters(self):
75        args = load_fixture('load_ltm_profile_dns_1.json')
76        p = ApiParameters(params=args)
77        assert p.name == 'foo'
78        assert p.parent == '/Common/dns'
79        assert p.enable_dns_express is False
80        assert p.enable_zone_transfer is True
81        assert p.enable_dnssec is False
82        assert p.enable_gtm is False
83        assert p.process_recursion_desired is True
84        assert p.use_local_bind is False
85        assert p.enable_dns_firewall is True
86
87
88class TestManager(unittest.TestCase):
89    def setUp(self):
90        self.spec = ArgumentSpec()
91        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_dns.tmos_version')
92        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_dns.send_teem')
93        self.m2 = self.p2.start()
94        self.m2.return_value = '14.1.0'
95        self.m3 = self.p3.start()
96        self.m3.return_value = True
97
98    def tearDown(self):
99        self.p2.stop()
100        self.p3.stop()
101
102    def test_create(self, *args):
103        # Configure the arguments that would be sent to the Ansible module
104        set_module_args(dict(
105            name='foo',
106            parent='bar',
107            enable_dns_express=True,
108            enable_zone_transfer=True,
109            enable_dnssec=True,
110            enable_gtm=True,
111            process_recursion_desired=True,
112            use_local_bind=True,
113            enable_dns_firewall=True,
114            provider=dict(
115                server='localhost',
116                password='password',
117                user='admin'
118            )
119        ))
120
121        module = AnsibleModule(
122            argument_spec=self.spec.argument_spec,
123            supports_check_mode=self.spec.supports_check_mode
124        )
125        mm = ModuleManager(module=module)
126
127        # Override methods to force specific logic in the module to happen
128        mm.exists = Mock(return_value=False)
129        mm.create_on_device = Mock(return_value=True)
130
131        results = mm.exec_module()
132
133        assert results['changed'] is True
134        assert results['enable_dns_express'] == 'yes'
135        assert results['enable_zone_transfer'] == 'yes'
136        assert results['enable_dnssec'] == 'yes'
137        assert results['enable_gtm'] == 'yes'
138        assert results['process_recursion_desired'] == 'yes'
139        assert results['use_local_bind'] == 'yes'
140        assert results['enable_dns_firewall'] == 'yes'
141