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_software_image 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            filename='/path/to/BIGIP-13.0.0.0.0.1645.iso',
53            image='/path/to/BIGIP-13.0.0.0.0.1645.iso',
54        )
55
56        p = ModuleParameters(params=args)
57        assert p.filename == 'BIGIP-13.0.0.0.0.1645.iso'
58        assert p.image == '/path/to/BIGIP-13.0.0.0.0.1645.iso'
59
60    def test_api_parameters(self):
61        args = dict(
62            file_size='1000 MB',
63            build='0.0.3',
64            checksum='8cdbd094195fab4b2b47ff4285577b70',
65            image_type='release',
66            version='13.1.0.8'
67        )
68
69        p = ApiParameters(params=args)
70        assert p.file_size == 1000
71        assert p.build == '0.0.3'
72        assert p.checksum == '8cdbd094195fab4b2b47ff4285577b70'
73        assert p.image_type == 'release'
74        assert p.version == '13.1.0.8'
75
76
77class TestManager(unittest.TestCase):
78    def setUp(self):
79        self.spec = ArgumentSpec()
80        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_software_image.tmos_version')
81        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_software_image.send_teem')
82        self.m2 = self.p2.start()
83        self.m2.return_value = '14.1.0'
84        self.m3 = self.p3.start()
85        self.m3.return_value = True
86
87    def tearDown(self):
88        self.p2.stop()
89        self.p3.stop()
90
91    def test_create(self, *args):
92        set_module_args(dict(
93            image='/path/to/BIGIP-13.0.0.0.0.1645.iso',
94            provider=dict(
95                server='localhost',
96                password='password',
97                user='admin'
98            )
99        ))
100
101        current = ApiParameters(params=load_fixture('load_sys_software_image_1.json'))
102        module = AnsibleModule(
103            argument_spec=self.spec.argument_spec,
104            supports_check_mode=self.spec.supports_check_mode
105        )
106
107        # Override methods in the specific type of manager
108        mm = ModuleManager(module=module)
109        mm.exists = Mock(side_effect=[False, True])
110        mm.read_current_from_device = Mock(return_value=current)
111        mm.create_on_device = Mock(return_value=True)
112
113        results = mm.exec_module()
114
115        assert results['changed'] is True
116        assert results['file_size'] == 1948
117