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_firewall_schedule 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 description',
54            daily_hour_end='21:00',
55            daily_hour_start='11:00',
56            date_valid_end='2019-03-11:15:30:00',
57            date_valid_start='2019-03-01:15:30:00',
58            days_of_week='all',
59        )
60        p = ModuleParameters(params=args)
61
62        assert p.name == 'foo'
63        assert p.description == 'my description'
64        assert p.daily_hour_end == '21:00'
65        assert p.daily_hour_start == '11:00'
66        assert p.date_valid_end == '2019-03-11T15:30:00Z'
67        assert p.date_valid_start == '2019-03-01T15:30:00Z'
68        assert 'monday' in p.days_of_week
69
70    def test_api_parameters(self):
71        args = load_fixture('load_afm_schedule.json')
72
73        p = ApiParameters(params=args)
74        assert p.name == 'foobar'
75        assert p.description == 'some description'
76        assert p.daily_hour_end == '12:00'
77        assert p.daily_hour_start == '6:00'
78        assert p.date_valid_end == '2019-06-13T16:00:00Z'
79        assert p.date_valid_start == '2019-05-31T07:00:00Z'
80        assert 'sunday' in p.days_of_week
81
82
83class TestManager(unittest.TestCase):
84
85    def setUp(self):
86        self.spec = ArgumentSpec()
87        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_firewall_schedule.tmos_version')
88        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_firewall_schedule.send_teem')
89        self.m2 = self.p2.start()
90        self.m2.return_value = '14.1.0'
91        self.m3 = self.p3.start()
92        self.m3.return_value = True
93
94    def tearDown(self):
95        self.p2.stop()
96        self.p3.stop()
97
98    def test_create(self, *args):
99        set_module_args(dict(
100            name='foo',
101            description='this is a description',
102            provider=dict(
103                server='localhost',
104                password='password',
105                user='admin'
106            )
107        ))
108
109        module = AnsibleModule(
110            argument_spec=self.spec.argument_spec,
111            supports_check_mode=self.spec.supports_check_mode
112        )
113        mm = ModuleManager(module=module)
114
115        # Override methods to force specific logic in the module to happen
116        mm.exists = Mock(side_effect=[False, True])
117        mm.create_on_device = Mock(return_value=True)
118
119        results = mm.exec_module()
120
121        assert results['changed'] is True
122        assert results['description'] == 'this is a description'
123