2016-03-02 15:28:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import patch, MagicMock, NO_MOCK, NO_MOCK_REASON
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
# Import Salt libs
|
2017-02-27 15:59:04 +00:00
|
|
|
import tests.integration as integration
|
2016-12-01 22:33:36 +00:00
|
|
|
import multiprocessing
|
2016-03-02 15:28:18 +00:00
|
|
|
from salt.cli import daemons
|
|
|
|
|
|
|
|
|
|
|
|
class LoggerMock(object):
|
|
|
|
'''
|
|
|
|
Logger data collector
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
'''
|
|
|
|
init
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
'''
|
|
|
|
Reset values
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
2016-03-02 18:57:35 +00:00
|
|
|
self.messages = list()
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
def info(self, data):
|
|
|
|
'''
|
|
|
|
Collects the data from the logger of info type.
|
|
|
|
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
2016-03-02 18:57:35 +00:00
|
|
|
self.messages.append({'message': data, 'type': 'info'})
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
def warning(self, data):
|
|
|
|
'''
|
|
|
|
Collects the data from the logger of warning type.
|
|
|
|
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
2016-03-02 18:57:35 +00:00
|
|
|
self.messages.append({'message': data, 'type': 'warning'})
|
|
|
|
|
|
|
|
def has_message(self, msg, log_type=None):
|
|
|
|
'''
|
|
|
|
Check if log has message.
|
|
|
|
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for data in self.messages:
|
|
|
|
if (data['type'] == log_type or not log_type) and data['message'].find(msg) > -1:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class DaemonsStarterTestCase(TestCase, integration.SaltClientTestCaseMixIn):
|
|
|
|
'''
|
|
|
|
Unit test for the daemons starter classes.
|
|
|
|
'''
|
|
|
|
|
2016-12-01 22:33:36 +00:00
|
|
|
def _multiproc_exec_test(self, exec_test):
|
|
|
|
m_parent, m_child = multiprocessing.Pipe()
|
|
|
|
p_ = multiprocessing.Process(target=exec_test, args=(m_child,))
|
|
|
|
p_.start()
|
|
|
|
self.assertTrue(m_parent.recv())
|
|
|
|
p_.join()
|
|
|
|
|
2016-03-02 15:28:18 +00:00
|
|
|
def test_master_daemon_hash_type_verified(self):
|
|
|
|
'''
|
|
|
|
Verify if Master is verifying hash_type config option.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
2016-12-01 22:33:36 +00:00
|
|
|
def exec_test(child_pipe):
|
|
|
|
def _create_master():
|
|
|
|
'''
|
|
|
|
Create master instance
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
obj = daemons.Master()
|
|
|
|
obj.config = {'user': 'dummy', 'hash_type': alg}
|
|
|
|
for attr in ['start_log_info', 'prepare', 'shutdown', 'master']:
|
|
|
|
setattr(obj, attr, MagicMock())
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
_logger = LoggerMock()
|
|
|
|
ret = True
|
|
|
|
with patch('salt.cli.daemons.check_user', MagicMock(return_value=True)):
|
|
|
|
with patch('salt.cli.daemons.log', _logger):
|
|
|
|
for alg in ['md5', 'sha1']:
|
|
|
|
_create_master().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and _logger.has_message('Do not use {alg}'.format(alg=alg),
|
|
|
|
log_type='warning')
|
2016-12-01 22:33:36 +00:00
|
|
|
|
|
|
|
_logger.reset()
|
|
|
|
|
|
|
|
for alg in ['sha224', 'sha256', 'sha384', 'sha512']:
|
|
|
|
_create_master().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and not _logger.has_message('Do not use ')
|
2016-12-01 22:33:36 +00:00
|
|
|
child_pipe.send(ret)
|
|
|
|
child_pipe.close()
|
|
|
|
self._multiproc_exec_test(exec_test)
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
def test_minion_daemon_hash_type_verified(self):
|
|
|
|
'''
|
|
|
|
Verify if Minion is verifying hash_type config option.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
2016-12-01 22:33:36 +00:00
|
|
|
def exec_test(child_pipe):
|
|
|
|
def _create_minion():
|
|
|
|
'''
|
|
|
|
Create minion instance
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
obj = daemons.Minion()
|
|
|
|
obj.config = {'user': 'dummy', 'hash_type': alg}
|
|
|
|
for attr in ['start_log_info', 'prepare', 'shutdown']:
|
|
|
|
setattr(obj, attr, MagicMock())
|
|
|
|
setattr(obj, 'minion', MagicMock(restart=False))
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
ret = True
|
|
|
|
_logger = LoggerMock()
|
|
|
|
with patch('salt.cli.daemons.check_user', MagicMock(return_value=True)):
|
|
|
|
with patch('salt.cli.daemons.log', _logger):
|
|
|
|
for alg in ['md5', 'sha1']:
|
|
|
|
_create_minion().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and _logger.has_message('Do not use {alg}'.format(alg=alg),
|
|
|
|
log_type='warning')
|
2016-12-01 22:33:36 +00:00
|
|
|
_logger.reset()
|
|
|
|
|
|
|
|
for alg in ['sha224', 'sha256', 'sha384', 'sha512']:
|
|
|
|
_create_minion().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and not _logger.has_message('Do not use ')
|
2016-12-01 22:33:36 +00:00
|
|
|
|
|
|
|
child_pipe.send(ret)
|
|
|
|
child_pipe.close()
|
|
|
|
|
|
|
|
self._multiproc_exec_test(exec_test)
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
def test_proxy_minion_daemon_hash_type_verified(self):
|
|
|
|
'''
|
|
|
|
Verify if ProxyMinion is verifying hash_type config option.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
2016-12-01 22:33:36 +00:00
|
|
|
def exec_test(child_pipe):
|
|
|
|
def _create_proxy_minion():
|
|
|
|
'''
|
|
|
|
Create proxy minion instance
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
obj = daemons.ProxyMinion()
|
|
|
|
obj.config = {'user': 'dummy', 'hash_type': alg}
|
2016-12-02 19:18:18 +00:00
|
|
|
for attr in ['minion', 'start_log_info', 'prepare', 'shutdown', 'tune_in']:
|
2016-12-01 22:33:36 +00:00
|
|
|
setattr(obj, attr, MagicMock())
|
|
|
|
|
2016-12-02 19:18:18 +00:00
|
|
|
obj.minion.restart = False
|
2016-12-01 22:33:36 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
ret = True
|
|
|
|
_logger = LoggerMock()
|
|
|
|
with patch('salt.cli.daemons.check_user', MagicMock(return_value=True)):
|
|
|
|
with patch('salt.cli.daemons.log', _logger):
|
|
|
|
for alg in ['md5', 'sha1']:
|
|
|
|
_create_proxy_minion().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and _logger.has_message('Do not use {alg}'.format(alg=alg),
|
|
|
|
log_type='warning')
|
2016-12-01 22:33:36 +00:00
|
|
|
|
|
|
|
_logger.reset()
|
|
|
|
|
|
|
|
for alg in ['sha224', 'sha256', 'sha384', 'sha512']:
|
|
|
|
_create_proxy_minion().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and not _logger.has_message('Do not use ')
|
2016-12-01 22:33:36 +00:00
|
|
|
child_pipe.send(ret)
|
|
|
|
child_pipe.close()
|
|
|
|
|
|
|
|
self._multiproc_exec_test(exec_test)
|
2016-03-02 15:28:18 +00:00
|
|
|
|
|
|
|
def test_syndic_daemon_hash_type_verified(self):
|
|
|
|
'''
|
|
|
|
Verify if Syndic is verifying hash_type config option.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
2016-12-01 22:33:36 +00:00
|
|
|
def exec_test(child_pipe):
|
|
|
|
def _create_syndic():
|
|
|
|
'''
|
|
|
|
Create syndic instance
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
obj = daemons.Syndic()
|
|
|
|
obj.config = {'user': 'dummy', 'hash_type': alg}
|
|
|
|
for attr in ['syndic', 'start_log_info', 'prepare', 'shutdown']:
|
|
|
|
setattr(obj, attr, MagicMock())
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
ret = True
|
|
|
|
_logger = LoggerMock()
|
|
|
|
with patch('salt.cli.daemons.check_user', MagicMock(return_value=True)):
|
|
|
|
with patch('salt.cli.daemons.log', _logger):
|
|
|
|
for alg in ['md5', 'sha1']:
|
|
|
|
_create_syndic().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and _logger.has_message('Do not use {alg}'.format(alg=alg),
|
|
|
|
log_type='warning')
|
2016-12-01 22:33:36 +00:00
|
|
|
|
|
|
|
_logger.reset()
|
|
|
|
|
|
|
|
for alg in ['sha224', 'sha256', 'sha384', 'sha512']:
|
|
|
|
_create_syndic().start()
|
2016-12-02 19:18:18 +00:00
|
|
|
ret = ret and _logger.messages \
|
|
|
|
and not _logger.has_message('Do not use ')
|
2016-12-01 22:33:36 +00:00
|
|
|
|
|
|
|
child_pipe.send(ret)
|
|
|
|
child_pipe.close()
|
2016-02-26 18:20:46 +00:00
|
|
|
|
2016-12-02 19:18:18 +00:00
|
|
|
self._multiproc_exec_test(exec_test)
|