salt/tests/unit/modules/test_aliases.py

170 lines
6.0 KiB
Python
Raw Normal View History

2014-05-15 22:51:44 +00:00
# -*- coding: utf-8 -*-
'''
2014-07-07 22:12:11 +00:00
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
2014-05-15 22:51:44 +00:00
'''
# Import python libs
from __future__ import absolute_import
2014-05-15 22:51:44 +00:00
# Import Salt Libs
2017-02-19 15:35:30 +00:00
import salt.modules.aliases as aliases
2014-05-15 22:51:44 +00:00
from salt.exceptions import SaltInvocationError
# Import Salt Testing Libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
2014-05-15 22:51:44 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class AliasesTestCase(TestCase, LoaderModuleMockMixin):
2014-05-15 22:51:44 +00:00
'''
TestCase for salt.modules.aliases module
'''
mock_alias = [('foo', 'bar@example.com', '')]
mock_alias_mult = [('foo', 'bar@example.com', ''),
('hello', 'world@earth.com, earth@world.com', '')]
def setup_loader_modules(self):
return {aliases: {}}
2014-05-15 22:51:44 +00:00
def test_list_aliases(self):
'''
Tests the return of a file containing one alias
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias)):
ret = {'foo': 'bar@example.com'}
self.assertEqual(aliases.list_aliases(), ret)
2014-05-15 22:51:44 +00:00
def test_list_aliases_mult(self):
'''
Tests the return of a file containing multiple aliases
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = {'foo': 'bar@example.com',
'hello': 'world@earth.com, earth@world.com'}
self.assertEqual(aliases.list_aliases(), ret)
2014-05-15 22:51:44 +00:00
def test_get_target(self):
'''
Tests the target returned by an alias with one target
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias)):
ret = 'bar@example.com'
self.assertEqual(aliases.get_target('foo'), ret)
2014-05-15 22:51:44 +00:00
def test_get_target_mult(self):
'''
Tests the target returned by an alias with multiple targets
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = 'world@earth.com, earth@world.com'
self.assertEqual(aliases.get_target('hello'), ret)
2014-05-15 22:51:44 +00:00
def test_get_target_no_alias(self):
'''
Tests return of an alias doesn't exist
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias)):
self.assertEqual(aliases.get_target('pizza'), '')
2014-05-15 22:51:44 +00:00
def test_has_target(self):
'''
Tests simple return known alias and target
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias)):
ret = aliases.has_target('foo', 'bar@example.com')
self.assertTrue(ret)
2014-05-15 22:51:44 +00:00
def test_has_target_no_alias(self):
'''
Tests return of empty alias and known target
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias)):
ret = aliases.has_target('', 'bar@example.com')
self.assertFalse(ret)
2014-05-15 22:51:44 +00:00
def test_has_target_no_target(self):
'''
Tests return of known alias and empty target
'''
self.assertRaises(SaltInvocationError, aliases.has_target, 'foo', '')
def test_has_target_mult(self):
'''
Tests return of multiple targets to one alias
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = aliases.has_target('hello',
'world@earth.com, earth@world.com')
self.assertTrue(ret)
2014-05-15 22:51:44 +00:00
def test_has_target_mult_differs(self):
'''
Tests return of multiple targets to one alias in opposite order
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = aliases.has_target('hello',
'earth@world.com, world@earth.com')
self.assertFalse(ret)
2014-05-15 22:51:44 +00:00
def test_has_target_list_mult(self):
'''
Tests return of target as same list to know alias
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = aliases.has_target('hello', ['world@earth.com',
'earth@world.com'])
self.assertTrue(ret)
2014-05-15 22:51:44 +00:00
def test_has_target_list_mult_differs(self):
'''
Tests return of target as differing list to known alias
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.__parse_aliases',
MagicMock(return_value=self.mock_alias_mult)):
ret = aliases.has_target('hello', ['world@earth.com',
'mars@space.com'])
self.assertFalse(ret)
2014-05-15 22:51:44 +00:00
def test_set_target_equal(self):
'''
Tests return when target is already present
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.get_target', MagicMock(return_value='bar@example.com')):
alias = 'foo'
target = 'bar@example.com'
ret = aliases.set_target(alias, target)
self.assertTrue(ret)
2014-05-15 22:51:44 +00:00
def test_set_target_empty_alias(self):
'''
Tests return of empty alias
'''
self.assertRaises(SaltInvocationError, aliases.set_target, '', 'foo')
def test_set_target_empty_target(self):
'''
Tests return of known alias and empty target
'''
self.assertRaises(SaltInvocationError, aliases.set_target, 'foo', '')
def test_rm_alias_absent(self):
'''
Tests return when alias is not present
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.aliases.get_target', MagicMock(return_value='')):
ret = aliases.rm_alias('foo')
self.assertTrue(ret)