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_log_publisher 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='my desc',
54            destinations=[
55                'dest1',
56                'dest2'
57            ],
58            provider=dict(
59                server='localhost',
60                password='password',
61                user='admin'
62            )
63        )
64        p = ModuleParameters(params=args)
65        assert p.name == 'foo'
66        assert p.description == 'my desc'
67        assert p.destinations == ['/Common/dest1', '/Common/dest2']
68
69    def test_api_parameters(self):
70        args = load_fixture('load_sys_log_config_publisher_1.json')
71        p = ApiParameters(params=args)
72        assert p.name == 'foo'
73        assert p.description == 'my description'
74        assert p.destinations == [
75            '/Common/SECURITYLOGSERVERS-LOGGING',
76            '/Common/local-db',
77            '/Common/local-syslog',
78        ]
79
80
81class TestManager(unittest.TestCase):
82    def setUp(self):
83        self.spec = ArgumentSpec()
84        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_log_publisher.tmos_version')
85        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_log_publisher.send_teem')
86        self.m2 = self.p2.start()
87        self.m2.return_value = '14.1.0'
88        self.m3 = self.p3.start()
89        self.m3.return_value = True
90
91    def tearDown(self):
92        self.p2.stop()
93        self.p3.stop()
94
95    def test_create_policy(self, *args):
96        set_module_args(dict(
97            name="foo",
98            description='foo description',
99            destinations=[
100                'dest1',
101                'dest2'
102            ],
103            state='present',
104            provider=dict(
105                server='localhost',
106                password='password',
107                user='admin'
108            )
109        ))
110
111        module = AnsibleModule(
112            argument_spec=self.spec.argument_spec,
113            supports_check_mode=self.spec.supports_check_mode
114        )
115
116        # Override methods to force specific logic in the module to happen
117        mm = ModuleManager(module=module)
118        mm.exists = Mock(return_value=False)
119        mm.create_on_device = Mock(return_value=True)
120
121        results = mm.exec_module()
122
123        assert results['changed'] is True
124        assert results['description'] == 'foo description'
125        assert results['destinations'] == ['/Common/dest1', '/Common/dest2']
126