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_policy import (
20    Parameters, ModuleManager, SimpleManager, 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_none_strategy(self):
51        args = dict(
52            name='foo',
53            description='asdf asdf asdf',
54        )
55        p = Parameters(params=args)
56        assert p.name == 'foo'
57        assert p.description == 'asdf asdf asdf'
58        assert p.strategy is None
59
60    def test_module_parameters_with_strategy_no_partition(self):
61        args = dict(
62            name='foo',
63            description='asdf asdf asdf',
64            strategy='foo',
65            partition='Common'
66        )
67        p = Parameters(params=args)
68        assert p.name == 'foo'
69        assert p.description == 'asdf asdf asdf'
70        assert p.strategy == '/Common/foo'
71
72    def test_module_parameters_with_strategy_partition(self):
73        args = dict(
74            name='foo',
75            description='asdf asdf asdf',
76            strategy='/Common/foo',
77            partition='Common'
78        )
79        p = Parameters(params=args)
80        assert p.name == 'foo'
81        assert p.description == 'asdf asdf asdf'
82        assert p.strategy == '/Common/foo'
83
84    def test_module_parameters_with_strategy_different_partition(self):
85        args = dict(
86            name='foo',
87            description='asdf asdf asdf',
88            strategy='/Foo/bar',
89            partition='Common'
90        )
91        p = Parameters(params=args)
92        assert p.name == 'foo'
93        assert p.description == 'asdf asdf asdf'
94        assert p.strategy == '/Foo/bar'
95
96    def test_api_parameters(self):
97        args = dict(
98            name='foo',
99            description='asdf asdf asdf',
100            strategy='/Common/asdf'
101        )
102        p = Parameters(params=args)
103        assert p.name == 'foo'
104        assert p.description == 'asdf asdf asdf'
105        assert p.strategy == '/Common/asdf'
106
107
108class TestSimpleTrafficPolicyManager(unittest.TestCase):
109    def setUp(self):
110        self.spec = ArgumentSpec()
111        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_policy.tmos_version')
112        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_policy.send_teem')
113        self.m2 = self.p2.start()
114        self.m2.return_value = '14.1.0'
115        self.m3 = self.p3.start()
116        self.m3.return_value = True
117
118    def tearDown(self):
119        self.p2.stop()
120        self.p3.stop()
121
122    def test_create_policy(self, *args):
123        set_module_args(dict(
124            name="Policy-Foo",
125            state='present',
126            strategy='best',
127            provider=dict(
128                server='localhost',
129                password='password',
130                user='admin'
131            )
132        ))
133
134        module = AnsibleModule(
135            argument_spec=self.spec.argument_spec,
136            supports_check_mode=self.spec.supports_check_mode
137        )
138
139        # Override methods in the specific type of manager
140        tm = SimpleManager(module=module, params=module.params)
141        tm.exists = Mock(return_value=False)
142        tm.create_on_device = Mock(return_value=True)
143
144        # Override methods to force specific logic in the module to happen
145        mm = ModuleManager(module=module)
146        mm.version_is_less_than_12 = Mock(return_value=True)
147        mm.get_manager = Mock(return_value=tm)
148
149        results = mm.exec_module()
150
151        assert results['changed'] is True
152