1# -*- coding: utf-8 -*-
2#
3# Copyright: (c) 2019, 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_sip 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            security=False,
55            description='my description',
56            community='foo_community',
57            dialog_aware=True,
58            enable_sip_firewall=False,
59            insert_via_header=True,
60            user_via_header='SIP/2.0/UDP 10.10.10.10:5060',
61            secure_via_header=True,
62            terminate_on_bye=False,
63            max_size=2048,
64            log_profile='alg_profile',
65            log_publisher='baz_publish',
66            insert_record_route_header=True,
67        )
68
69        p = ModuleParameters(params=args)
70        assert p.name == 'foo'
71        assert p.parent == '/Common/bar'
72        assert p.security == 'disabled'
73        assert p.description == 'my description'
74        assert p.community == 'foo_community'
75        assert p.dialog_aware == 'enabled'
76        assert p.enable_sip_firewall == 'no'
77        assert p.insert_via_header == 'enabled'
78        assert p.user_via_header == 'SIP/2.0/UDP 10.10.10.10:5060'
79        assert p.terminate_on_bye == 'disabled'
80        assert p.max_size == 2048
81        assert p.log_profile == '/Common/alg_profile'
82        assert p.log_publisher == '/Common/baz_publish'
83        assert p.insert_record_route_header == 'enabled'
84
85    def test_api_parameters(self):
86        args = load_fixture('load_profile_sip.json')
87        p = ApiParameters(params=args)
88        assert p.name == 'foobar'
89        assert p.max_size == 65535
90        assert p.parent == '/Common/sip'
91        assert p.dialog_aware == 'enabled'
92
93
94class TestManager(unittest.TestCase):
95    def setUp(self):
96        self.spec = ArgumentSpec()
97        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_sip.tmos_version')
98        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_sip.send_teem')
99        self.m2 = self.p2.start()
100        self.m2.return_value = '14.1.0'
101        self.m3 = self.p3.start()
102        self.m3.return_value = True
103
104    def tearDown(self):
105        self.p2.stop()
106        self.p3.stop()
107
108    def test_create_sip_profile(self, *args):
109        # Configure the arguments that would be sent to the Ansible module
110        set_module_args(dict(
111            name='foo',
112            parent='bar',
113            max_size=8192,
114            log_profile='alg_profile',
115            log_publisher='baz_publish',
116            dialog_aware='yes',
117            enable_sip_firewall='no',
118            provider=dict(
119                server='localhost',
120                password='password',
121                user='admin'
122            )
123        ))
124
125        module = AnsibleModule(
126            argument_spec=self.spec.argument_spec,
127            supports_check_mode=self.spec.supports_check_mode
128        )
129        mm = ModuleManager(module=module)
130
131        # Override methods to force specific logic in the module to happen
132        mm.exists = Mock(return_value=False)
133        mm.create_on_device = Mock(return_value=True)
134
135        results = mm.exec_module()
136
137        assert results['changed'] is True
138        assert results['parent'] == '/Common/bar'
139        assert results['max_size'] == 8192
140        assert results['log_profile'] == '/Common/alg_profile'
141        assert results['log_publisher'] == '/Common/baz_publish'
142        assert results['dialog_aware'] == 'yes'
143        assert results['enable_sip_firewall'] == 'no'
144
145    def test_update_sip_profile(self, *args):
146        set_module_args(dict(
147            name='foobar',
148            description='my description',
149            insert_via_header=True,
150            user_via_header='SIP/2.0/UDP 10.10.10.10:5060',
151            secure_via_header=True,
152            terminate_on_bye=False,
153            max_size=2048,
154            log_profile='alg_profile',
155            log_publisher='baz_publish',
156            provider=dict(
157                server='localhost',
158                password='password',
159                user='admin'
160            )
161        ))
162
163        current = ApiParameters(params=load_fixture('load_profile_sip.json'))
164
165        module = AnsibleModule(
166            argument_spec=self.spec.argument_spec,
167            supports_check_mode=self.spec.supports_check_mode,
168        )
169
170        # Override methods in the specific type of manager
171        mm = ModuleManager(module=module)
172        mm.exists = Mock(return_value=True)
173        mm.update_on_device = Mock(return_value=True)
174        mm.read_current_from_device = Mock(return_value=current)
175
176        results = mm.exec_module()
177        assert results['changed'] is True
178        assert results['description'] == 'my description'
179        assert results['insert_via_header'] == 'yes'
180        assert results['user_via_header'] == 'SIP/2.0/UDP 10.10.10.10:5060'
181        assert results['secure_via_header'] == 'yes'
182        assert results['terminate_on_bye'] == 'no'
183        assert results['max_size'] == 2048
184        assert results['log_profile'] == '/Common/alg_profile'
185        assert results['log_publisher'] == '/Common/baz_publish'
186