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_firewall_port_list 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            description='this is a description',
54            ports=[1, 2, 3, 4],
55            port_ranges=['10-20', '30-40', '50-60'],
56            port_lists=['/Common/foo', 'foo']
57        )
58
59        p = ModuleParameters(params=args)
60        assert p.name == 'foo'
61        assert p.description == 'this is a description'
62        assert len(p.ports) == 4
63        assert len(p.port_ranges) == 3
64        assert len(p.port_lists) == 2
65
66    def test_api_parameters(self):
67        args = load_fixture('load_security_port_list_1.json')
68
69        p = ApiParameters(params=args)
70        assert len(p.ports) == 4
71        assert len(p.port_ranges) == 3
72        assert len(p.port_lists) == 1
73        assert sorted(p.ports) == [1, 2, 3, 4]
74        assert sorted(p.port_ranges) == ['10-20', '30-40', '50-60']
75        assert p.port_lists[0] == '/Common/_sys_self_allow_tcp_defaults'
76
77
78class TestManager(unittest.TestCase):
79
80    def setUp(self):
81        self.spec = ArgumentSpec()
82        self.p1 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_firewall_port_list.module_provisioned')
83        self.m1 = self.p1.start()
84        self.m1.return_value = True
85        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_firewall_port_list.tmos_version')
86        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_firewall_port_list.send_teem')
87        self.m2 = self.p2.start()
88        self.m2.return_value = '14.1.0'
89        self.m3 = self.p3.start()
90        self.m3.return_value = True
91
92    def tearDown(self):
93        self.p1.stop()
94        self.p2.stop()
95        self.p3.stop()
96
97    def tearDown(self):
98        self.p1.stop()
99
100    def test_create(self, *args):
101        set_module_args(dict(
102            name='foo',
103            description='this is a description',
104            ports=[1, 2, 3, 4],
105            port_ranges=['10-20', '30-40', '50-60'],
106            port_lists=['/Common/foo', 'foo'],
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        mm = ModuleManager(module=module)
119
120        # Override methods to force specific logic in the module to happen
121        mm.exists = Mock(side_effect=[False, True])
122        mm.create_on_device = Mock(return_value=True)
123        mm.module_provisioned = Mock(return_value=True)
124
125        results = mm.exec_module()
126
127        assert results['changed'] is True
128        assert 'ports' in results
129        assert 'port_lists' in results
130        assert 'port_ranges' in results
131        assert len(results['ports']) == 4
132        assert len(results['port_ranges']) == 3
133        assert len(results['port_lists']) == 2
134        assert results['description'] == 'this is a description'
135