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_smtp 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 smtp_server='1.1.1.1', 54 smtp_server_port='25', 55 smtp_server_username='admin', 56 smtp_server_password='password', 57 local_host_name='smtp.mydomain.com', 58 encryption='tls', 59 update_password='always', 60 from_address='no-reply@mydomain.com', 61 authentication=True, 62 ) 63 64 p = ModuleParameters(params=args) 65 assert p.name == 'foo' 66 assert p.smtp_server == '1.1.1.1' 67 assert p.smtp_server_port == 25 68 assert p.smtp_server_username == 'admin' 69 assert p.smtp_server_password == 'password' 70 assert p.local_host_name == 'smtp.mydomain.com' 71 assert p.encryption == 'tls' 72 assert p.update_password == 'always' 73 assert p.from_address == 'no-reply@mydomain.com' 74 assert p.authentication_disabled is None 75 assert p.authentication_enabled is True 76 77 def test_api_parameters(self): 78 p = ApiParameters(params=load_fixture('load_sys_smtp_server.json')) 79 assert p.name == 'foo' 80 assert p.smtp_server == 'mail.foo.bar' 81 assert p.smtp_server_port == 465 82 assert p.smtp_server_username == 'admin' 83 assert p.smtp_server_password == '$M$Ch$this-is-encrypted==' 84 assert p.local_host_name == 'mail-host.foo.bar' 85 assert p.encryption == 'ssl' 86 assert p.from_address == 'no-reply@foo.bar' 87 assert p.authentication_disabled is None 88 assert p.authentication_enabled is True 89 90 91class TestManager(unittest.TestCase): 92 def setUp(self): 93 self.spec = ArgumentSpec() 94 self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_smtp.tmos_version') 95 self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_smtp.send_teem') 96 self.m2 = self.p2.start() 97 self.m2.return_value = '14.1.0' 98 self.m3 = self.p3.start() 99 self.m3.return_value = True 100 101 def tearDown(self): 102 self.p2.stop() 103 self.p3.stop() 104 105 def test_create_monitor(self, *args): 106 set_module_args(dict( 107 name='foo', 108 smtp_server='1.1.1.1', 109 smtp_server_port='25', 110 smtp_server_username='admin', 111 smtp_server_password='password', 112 local_host_name='smtp.mydomain.com', 113 encryption='tls', 114 update_password='always', 115 from_address='no-reply@mydomain.com', 116 authentication=True, 117 partition='Common', 118 provider=dict( 119 server='localhost', 120 password='password', 121 user='admin' 122 ) 123 )) 124 125 module = AnsibleModule( 126 argument_spec=self.spec.argument_spec, 127 supports_check_mode=self.spec.supports_check_mode 128 ) 129 130 # Override methods in the specific type of manager 131 mm = ModuleManager(module=module) 132 mm.exists = Mock(side_effect=[False, True]) 133 mm.create_on_device = Mock(return_value=True) 134 135 results = mm.exec_module() 136 137 assert results['changed'] is True 138 assert results['encryption'] == 'tls' 139 assert results['smtp_server'] == '1.1.1.1' 140 assert results['smtp_server_port'] == 25 141 assert results['local_host_name'] == 'smtp.mydomain.com' 142 assert results['authentication'] is True 143 assert results['from_address'] == 'no-reply@mydomain.com' 144 assert 'smtp_server_username' not in results 145 assert 'smtp_server_password' not in results 146