test_stalekey.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. unit tests for the stalekey engine
  3. """
  4. import logging
  5. import salt.engines.stalekey as stalekey
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock, mock_open, patch
  8. from tests.support.unit import TestCase
  9. log = logging.getLogger(__name__)
  10. class MockWheel:
  11. def __init__(self, *args, **kwargs):
  12. pass
  13. def cmd(self, *args, **kwargs):
  14. return True
  15. class EngineStalekeyTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.engine.stalekeys
  18. """
  19. def setup_loader_modules(self):
  20. return {stalekey: {}}
  21. def test__delete_keys(self):
  22. """
  23. Test to ensure the _delete_keys function deletes multiple keys
  24. """
  25. with patch("salt.wheel.WheelClient", MockWheel):
  26. stale_keys = ["minion1", "minion2"]
  27. minions = {
  28. "minion1": 1601430462.5281658,
  29. "minion2": 1601430462.5281658,
  30. }
  31. ret = stalekey._delete_keys(stale_keys, minions)
  32. self.assertEqual(ret, {})
  33. stale_keys = ["minion1"]
  34. minions = {
  35. "minion1": 1601430462.5281658,
  36. "minion2": 1601430462.5281658,
  37. }
  38. ret = stalekey._delete_keys(stale_keys, minions)
  39. self.assertEqual(ret, {"minion2": 1601430462.5281658})
  40. def test__read_presence(self):
  41. """
  42. Test for _read_presence returning False for no error and minions presence data
  43. """
  44. presence_data = {b"minion": 1601477127.532849}
  45. expected = (False, {"minion": 1601477127.532849})
  46. with patch("os.path.exists", return_value=True):
  47. with patch("salt.utils.files.fopen", mock_open()):
  48. with patch(
  49. "salt.utils.msgpack.load", MagicMock(return_value=presence_data)
  50. ):
  51. ret = stalekey._read_presence("presence_file")
  52. self.assertEqual(ret, expected)
  53. def test__write_presence(self):
  54. """
  55. Test for _write_presence returning False, meaning no error has occured
  56. """
  57. expected = False
  58. minions = {
  59. "minion1": 1601430462.5281658,
  60. "minion2": 1601430462.5281658,
  61. }
  62. with patch("salt.utils.files.fopen", mock_open()):
  63. ret = stalekey._write_presence("presence_file", minions)
  64. self.assertEqual(ret, expected)