1# -*- coding: utf-8 -*-
2#
3# Copyright: (c) 2018, 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_gtm_topology_record 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            source=dict(
53                subnet='192.168.1.0/24',
54                negate=True
55            ),
56            destination=dict(
57                region='Foobar',
58            ),
59            weight=10
60        )
61
62        p = ModuleParameters(params=args)
63        assert p.name == 'ldns: not subnet 192.168.1.0/24 server: region /Common/Foobar'
64        assert p.weight == 10
65
66    def test_api_parameters(self):
67        args = dict(
68            source=dict(
69                subnet='192.168.1.0/24',
70                negate=True
71            ),
72            destination=dict(
73                region='Foobar',
74            ),
75            score=10
76        )
77
78        p = ApiParameters(params=args)
79        assert p.weight == 10
80
81
82class TestManager(unittest.TestCase):
83
84    def setUp(self):
85        self.spec = ArgumentSpec()
86        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_topology_record.tmos_version')
87        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_topology_record.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_topology_record(self, *args):
98        set_module_args(dict(
99            source=dict(
100                subnet='192.168.1.0/24',
101                negate=True
102            ),
103            destination=dict(
104                region='Foobar',
105            ),
106            weight=10,
107            provider=dict(
108                server='localhost',
109                password='password',
110                user='admin'
111            )
112        ))
113
114        module = AnsibleModule(
115            argument_spec=self.spec.argument_spec,
116            supports_check_mode=self.spec.supports_check_mode
117        )
118
119        # Override methods in the specific type of manager
120        mm = ModuleManager(module=module)
121        mm.exists = Mock(side_effect=[False, True])
122        mm.create_on_device = Mock(return_value=True)
123
124        results = mm.exec_module()
125
126        assert results['changed'] is True
127