test_chef.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Libs
  8. import salt.states.chef as chef
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class ChefTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.chef
  16. """
  17. def setup_loader_modules(self):
  18. return {chef: {}}
  19. # 'client' function tests: 1
  20. def test_client(self):
  21. """
  22. Test to run chef-client
  23. """
  24. name = "my-chef-run"
  25. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  26. mock = MagicMock(return_value={"retcode": 1, "stdout": "", "stderr": "error"})
  27. with patch.dict(chef.__salt__, {"chef.client": mock}):
  28. with patch.dict(chef.__opts__, {"test": True}):
  29. comt = "\nerror"
  30. ret.update({"comment": comt})
  31. self.assertDictEqual(chef.client(name), ret)
  32. # 'solo' function tests: 1
  33. def test_solo(self):
  34. """
  35. Test to run chef-solo
  36. """
  37. name = "my-chef-run"
  38. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  39. mock = MagicMock(return_value={"retcode": 1, "stdout": "", "stderr": "error"})
  40. with patch.dict(chef.__salt__, {"chef.solo": mock}):
  41. with patch.dict(chef.__opts__, {"test": True}):
  42. comt = "\nerror"
  43. ret.update({"comment": comt})
  44. self.assertDictEqual(chef.solo(name), ret)