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_wait import (
20    Parameters, ModuleManager, V2Manager, 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            type='standard',
53            delay=3,
54            timeout=500,
55            sleep=10,
56            msg='We timed out during waiting for BIG-IP :-('
57        )
58
59        p = Parameters(params=args)
60        assert p.delay == 3
61        assert p.timeout == 500
62        assert p.sleep == 10
63        assert p.msg == 'We timed out during waiting for BIG-IP :-('
64
65    def test_module_string_parameters(self):
66        args = dict(
67            type='standard',
68            delay='3',
69            timeout='500',
70            sleep='10',
71            msg='We timed out during waiting for BIG-IP :-('
72        )
73
74        p = Parameters(params=args)
75        assert p.delay == 3
76        assert p.timeout == 500
77        assert p.sleep == 10
78        assert p.msg == 'We timed out during waiting for BIG-IP :-('
79
80
81class TestManager(unittest.TestCase):
82    def setUp(self):
83        self.spec = ArgumentSpec()
84        self.patcher1 = patch('time.sleep')
85        self.patcher1.start()
86        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_wait.tmos_version')
87        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_wait.send_teem')
88        self.m2 = self.p2.start()
89        self.m2.return_value = '14.1.0'
90        self.m3 = self.p3.start()
91        self.m3.return_value = True
92
93    def tearDown(self):
94        self.p2.stop()
95        self.p3.stop()
96        self.patcher1.stop()
97
98    def test_wait_already_available(self, *args):
99        set_module_args(dict(
100            provider=dict(
101                server='localhost',
102                password='password',
103                user='admin',
104            )
105        ))
106
107        module = AnsibleModule(
108            argument_spec=self.spec.argument_spec,
109            supports_check_mode=self.spec.supports_check_mode
110        )
111
112        # Override methods to force specific logic in the module to happen
113        m1 = V2Manager(module=module)
114        mm = ModuleManager(module=module)
115        mm.get_manager = Mock(return_value=m1)
116
117        m1._connect_to_device = Mock(return_value=True)
118        m1._device_is_rebooting = Mock(return_value=False)
119        m1._is_mprov_running_on_device = Mock(return_value=False)
120        m1._get_client_connection = Mock(return_value=True)
121        m1._rest_endpoints_ready = Mock(side_effect=[False, False, True])
122
123        results = mm.exec_module()
124
125        assert results['changed'] is False
126        assert results['elapsed'] == 0
127