123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- # -*- coding: utf-8 -*-
- '''
- :codeauthor: Rahul Handay <rahulha@saltstack.com>
- '''
- # Import Python Libs
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import TestCase
- from tests.support.mock import (
- patch,
- MagicMock,
- )
- # Import Salt Libs
- from salt.exceptions import SaltInvocationError
- import salt.states.test as test
- from salt.utils.odict import OrderedDict
- from salt.ext import six
- class TestTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Validate the test state
- '''
- def setup_loader_modules(self):
- return {test: {'__low__': {'__reqs__': {'watch': ''}}}}
- def test_succeed_without_changes(self):
- '''
- Test to returns successful.
- '''
- ret = {'name': 'salt',
- 'changes': {},
- 'result': True,
- 'comment': ''}
- with patch.dict(test.__opts__, {"test": False}):
- ret.update({'comment': 'Success!'})
- self.assertDictEqual(test.succeed_without_changes('salt'), ret)
- def test_fail_without_changes(self):
- '''
- Test to returns failure.
- '''
- ret = {'name': 'salt',
- 'changes': {},
- 'result': False,
- 'comment': ''}
- with patch.dict(test.__opts__, {"test": False}):
- ret.update({'comment': 'Failure!'})
- self.assertDictEqual(test.fail_without_changes('salt'), ret)
- def test_succeed_with_changes(self):
- '''
- Test to returns successful and changes is not empty
- '''
- ret = {'name': 'salt',
- 'changes': {},
- 'result': False,
- 'comment': ''}
- with patch.dict(test.__opts__, {"test": False}):
- ret.update({'changes': {'testing': {'new': 'Something pretended'
- ' to change',
- 'old': 'Unchanged'}},
- 'comment': 'Success!', 'result': True})
- self.assertDictEqual(test.succeed_with_changes('salt'), ret)
- def test_fail_with_changes(self):
- '''
- Test to returns failure and changes is not empty.
- '''
- ret = {'name': 'salt',
- 'changes': {},
- 'result': False,
- 'comment': ''}
- with patch.dict(test.__opts__, {"test": False}):
- ret.update({'changes': {'testing': {'new': 'Something pretended'
- ' to change',
- 'old': 'Unchanged'}},
- 'comment': 'Success!',
- 'result': True})
- self.assertDictEqual(test.succeed_with_changes('salt'), ret)
- def test_configurable_test_state(self):
- '''
- Test test.configurable_test_state with and without comment
- '''
- # Configure mock parameters
- mock_name = 'cheese_shop'
- mock_comment = "I'm afraid we're fresh out of Red Leicester sir."
- mock_changes = {
- 'testing': {
- 'old': 'Unchanged',
- 'new': 'Something pretended to change'
- }
- }
- # Test default state with comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': ''}
- ret = test.configurable_test_state(mock_name)
- self.assertDictEqual(ret, mock_ret)
- # Test default state without comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- def test_configurable_test_state_changes(self):
- '''
- Test test.configurable_test_state with permutations of changes and with
- comment
- '''
- # Configure mock parameters
- mock_name = 'cheese_shop'
- mock_comment = "I'm afraid we're fresh out of Red Leicester sir."
- mock_changes = {
- 'testing': {
- 'old': 'Unchanged',
- 'new': 'Something pretended to change'
- }
- }
- # Test changes=Random and comment
- with patch.dict(test.__opts__, {'test': False}):
- ret = test.configurable_test_state(mock_name,
- changes='Random',
- comment=mock_comment)
- self.assertEqual(ret['name'], mock_name)
- self.assertIn(ret['changes'], [mock_changes, {}])
- self.assertEqual(ret['result'], True)
- self.assertEqual(ret['comment'], mock_comment)
- # Test changes=True and comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- changes=True,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test changes=False and comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': {},
- 'result': True,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- changes=False,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test changes=Cheese
- with patch.dict(test.__opts__, {'test': False}):
- self.assertRaises(SaltInvocationError,
- test.configurable_test_state,
- mock_name,
- changes='Cheese')
- def test_configurable_test_state_result(self):
- '''
- Test test.configurable_test_state with permutations of result and with
- comment
- '''
- # Configure mock parameters
- mock_name = 'cheese_shop'
- mock_comment = "I'm afraid we're fresh out of Red Leicester sir."
- mock_changes = {
- 'testing': {
- 'old': 'Unchanged',
- 'new': 'Something pretended to change'
- }
- }
- # Test result=Random and comment
- with patch.dict(test.__opts__, {'test': False}):
- ret = test.configurable_test_state(mock_name,
- result='Random',
- comment=mock_comment)
- self.assertEqual(ret['name'], mock_name)
- self.assertEqual(ret['changes'], mock_changes)
- self.assertIn(ret['result'], [True, False])
- self.assertEqual(ret['comment'], mock_comment)
- # Test result=True and comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- result=True,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test result=False and comment
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': False,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- result=False,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test result=Cheese
- with patch.dict(test.__opts__, {'test': False}):
- self.assertRaises(SaltInvocationError,
- test.configurable_test_state,
- mock_name,
- result='Cheese')
- def test_configurable_test_state_warnings(self):
- '''
- Test test.configurable_test_state with and without warnings
- '''
- # Configure mock parameters
- mock_name = 'cheese_shop'
- mock_comment = "I'm afraid we're fresh out of Red Leicester sir."
- mock_warning = 'Today the van broke down.'
- mock_warning_list = [
- mock_warning,
- "Oooooooooohhh........!"
- ]
- mock_changes = {
- 'testing': {
- 'old': 'Unchanged',
- 'new': 'Something pretended to change'
- }
- }
- # Test default state without warnings
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': ''}
- ret = test.configurable_test_state(mock_name)
- self.assertDictEqual(ret, mock_ret)
- # Test default state with warnings (string)
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': '',
- 'warnings': mock_warning_list}
- ret = test.configurable_test_state(mock_name,
- warnings=mock_warning_list)
- self.assertDictEqual(ret, mock_ret)
- # Test default state with warnings (list)
- with patch.dict(test.__opts__, {'test': False}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': True,
- 'comment': '',
- 'warnings': ['Today the van broke down.']}
- ret = test.configurable_test_state(mock_name,
- warnings=mock_warning)
- self.assertDictEqual(ret, mock_ret)
- def test_configurable_test_state_test(self):
- '''
- Test test.configurable_test_state with test=True with and without
- comment
- '''
- # Configure mock parameters
- mock_name = 'cheese_shop'
- mock_comment = "I'm afraid we're fresh out of Red Leicester sir."
- mock_changes = {
- 'testing': {
- 'old': 'Unchanged',
- 'new': 'Something pretended to change'
- }
- }
- # Test test=True without comment
- with patch.dict(test.__opts__, {'test': True}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': None,
- 'comment': 'This is a test'}
- ret = test.configurable_test_state(mock_name)
- self.assertDictEqual(ret, mock_ret)
- # Test test=True with comment
- with patch.dict(test.__opts__, {'test': True}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': None,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test test=True and changes=True with comment
- with patch.dict(test.__opts__, {'test': True}):
- mock_ret = {'name': mock_name,
- 'changes': mock_changes,
- 'result': None,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- changes=True,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- # Test test=True and changes=False with comment
- with patch.dict(test.__opts__, {'test': True}):
- mock_ret = {'name': mock_name,
- 'changes': {},
- 'result': True,
- 'comment': mock_comment}
- ret = test.configurable_test_state(mock_name,
- changes=False,
- comment=mock_comment)
- self.assertDictEqual(ret, mock_ret)
- def test_mod_watch(self):
- '''
- Test to call this function via a watch statement
- '''
- ret = {'name': 'salt',
- 'changes': {},
- 'result': True,
- 'comment': ''}
- ret.update({'changes': {'Requisites with changes': []},
- 'comment': 'Watch statement fired.'})
- self.assertDictEqual(test.mod_watch('salt'), ret)
- def test_check_pillar_present(self):
- '''
- Test to ensure the check_pillar function
- works properly with the 'present' keyword in
- the absence of a 'type' keyword.
- '''
- ret = {
- 'name': 'salt',
- 'changes': {},
- 'result': True,
- 'comment': ''
- }
- pillar_return = 'I am a pillar.'
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertEqual(test.check_pillar('salt', present='my_pillar'), ret)
- def test_check_pillar_string(self):
- '''
- Test to ensure the check_pillar function
- works properly with the 'key_type' checks,
- using the string key_type.
- '''
- ret = {
- 'name': 'salt',
- 'changes': {},
- 'result': True,
- 'comment': ''
- }
- pillar_return = 'I am a pillar.'
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertEqual(test.check_pillar('salt', string='my_pillar'), ret)
- # With unicode (py2) or str (py3) strings
- pillar_return = six.text_type('I am a pillar.')
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertEqual(test.check_pillar('salt', string='my_pillar'), ret)
- # With a dict
- pillar_return = {'this': 'dictionary'}
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
- # With a list
- pillar_return = ['I am a pillar.']
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
- # With a boolean
- pillar_return = True
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
- # With an int
- pillar_return = 1
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
- def test_check_pillar_dictionary(self):
- '''
- Test to ensure the check_pillar function
- works properly with the 'key_type' checks,
- using the dictionary key_type.
- '''
- ret = {
- 'name': 'salt',
- 'changes': {},
- 'result': True,
- 'comment': ''
- }
- pillar_return = {'this': 'dictionary'}
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertEqual(test.check_pillar('salt', dictionary='my_pillar'), ret)
- # With an ordered dict
- pillar_return = OrderedDict({'this': 'dictionary'})
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertEqual(test.check_pillar('salt', dictionary='my_pillar'), ret)
- # With a string
- pillar_return = 'I am a pillar.'
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
- # With a list
- pillar_return = ['I am a pillar.']
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
- # With a boolean
- pillar_return = True
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
- # With an int
- pillar_return = 1
- pillar_mock = MagicMock(return_value=pillar_return)
- with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
- self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
|