test_chef.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch)
  13. # Import Salt Libs
  14. import salt.states.chef as chef
  15. class ChefTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.chef
  18. '''
  19. def setup_loader_modules(self):
  20. return {chef: {}}
  21. # 'client' function tests: 1
  22. def test_client(self):
  23. '''
  24. Test to run chef-client
  25. '''
  26. name = 'my-chef-run'
  27. ret = {'name': name,
  28. 'result': False,
  29. 'changes': {},
  30. 'comment': ''}
  31. mock = MagicMock(return_value={'retcode': 1, 'stdout': '',
  32. 'stderr': 'error'})
  33. with patch.dict(chef.__salt__, {'chef.client': mock}):
  34. with patch.dict(chef.__opts__, {'test': True}):
  35. comt = ('\nerror')
  36. ret.update({'comment': comt})
  37. self.assertDictEqual(chef.client(name), ret)
  38. # 'solo' function tests: 1
  39. def test_solo(self):
  40. '''
  41. Test to run chef-solo
  42. '''
  43. name = 'my-chef-run'
  44. ret = {'name': name,
  45. 'result': False,
  46. 'changes': {},
  47. 'comment': ''}
  48. mock = MagicMock(return_value={'retcode': 1, 'stdout': '',
  49. 'stderr': 'error'})
  50. with patch.dict(chef.__salt__, {'chef.solo': mock}):
  51. with patch.dict(chef.__opts__, {'test': True}):
  52. comt = ('\nerror')
  53. ret.update({'comment': comt})
  54. self.assertDictEqual(chef.solo(name), ret)