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_region 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='foobar',
53            region_members=[
54                dict(
55                    country='Poland',
56                    negate=True
57                ),
58                dict(
59                    datacenter='bazcenter'
60                )
61            ],
62            partition='Common'
63        )
64
65        p = ModuleParameters(params=args)
66        assert p.name == 'foobar'
67        assert p.partition == 'Common'
68        assert p.region_members == ['not country PL', 'datacenter /Common/bazcenter']
69
70    def test_api_parameters(self):
71        args = dict(
72            name='foobar',
73            region_members=[
74                dict(
75                    name='not country PL'
76                ),
77                dict(
78                    name='datacenter /Common/bazcenter'
79                )
80            ],
81            partition='Common'
82        )
83
84        p = ApiParameters(params=args)
85        assert p.name == 'foobar'
86        assert p.partition == 'Common'
87        assert p.region_members == ['not country PL', 'datacenter /Common/bazcenter']
88
89
90class TestManager(unittest.TestCase):
91
92    def setUp(self):
93        self.spec = ArgumentSpec()
94        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_topology_region.tmos_version')
95        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_gtm_topology_region.send_teem')
96        self.m2 = self.p2.start()
97        self.m2.return_value = '14.1.0'
98        self.m3 = self.p3.start()
99        self.m3.return_value = True
100
101    def tearDown(self):
102        self.p2.stop()
103        self.p3.stop()
104
105    def test_create_topology_region(self, *args):
106        set_module_args(dict(
107            name='foobar',
108            region_members=[
109                dict(
110                    country='Poland',
111                    negate=True
112                ),
113                dict(
114                    datacenter='bazcenter'
115                )
116            ],
117            partition='Common',
118            provider=dict(
119                server='localhost',
120                password='password',
121                user='admin'
122            )
123        )
124        )
125
126        module = AnsibleModule(
127            argument_spec=self.spec.argument_spec,
128            supports_check_mode=self.spec.supports_check_mode
129        )
130
131        # Override methods in the specific type of manager
132        mm = ModuleManager(module=module)
133        mm.exists = Mock(return_value=False)
134        mm.create_on_device = Mock(return_value=True)
135
136        results = mm.exec_module()
137
138        assert results['changed'] is True
139