test_publish.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.modules.publish as publish
  9. from salt.exceptions import SaltReqTimeoutError
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class SAuth(object):
  15. """
  16. Mock SAuth class
  17. """
  18. def __init__(self, __opts__):
  19. self.tok = None
  20. def gen_token(self, tok):
  21. """
  22. Mock gen_token method
  23. """
  24. self.tok = tok
  25. return "salt_tok"
  26. class Channel(object):
  27. """
  28. Mock Channel class
  29. """
  30. flag = None
  31. def __init__(self):
  32. self.tok = None
  33. self.load = None
  34. def factory(self, tok):
  35. """
  36. Mock factory method
  37. """
  38. self.tok = tok
  39. return Channel()
  40. def send(self, load):
  41. """
  42. Mock send method
  43. """
  44. self.load = load
  45. if self.flag == 1:
  46. raise SaltReqTimeoutError
  47. return True
  48. def close(self):
  49. pass
  50. def __enter__(self):
  51. return self
  52. def __exit__(self, *args):
  53. pass
  54. class PublishTestCase(TestCase, LoaderModuleMockMixin):
  55. """
  56. Test cases for salt.modules.publish
  57. """
  58. def setup_loader_modules(self):
  59. return {publish: {}}
  60. @classmethod
  61. def setUpClass(cls):
  62. cls.channel_patcher = patch("salt.transport.client.ReqChannel", Channel())
  63. cls.channel_patcher.start()
  64. @classmethod
  65. def tearDownClass(cls):
  66. cls.channel_patcher.stop()
  67. del cls.channel_patcher
  68. def setUp(self):
  69. patcher = patch("salt.crypt.SAuth", return_value=SAuth(publish.__opts__))
  70. patcher.start()
  71. self.addCleanup(patcher.stop)
  72. # 'publish' function tests: 1
  73. def test_publish(self):
  74. """
  75. Test if it publish a command from the minion out to other minions.
  76. """
  77. self.assertDictEqual(publish.publish("os:Fedora", "publish.salt"), {})
  78. # 'full_data' function tests: 1
  79. def test_full_data(self):
  80. """
  81. Test if it return the full data about the publication
  82. """
  83. self.assertDictEqual(publish.publish("*", "publish.salt"), {})
  84. # 'runner' function tests: 1
  85. def test_runner(self):
  86. """
  87. Test if it execute a runner on the master and return the data
  88. from the runner function
  89. """
  90. ret = "No access to master. If using salt-call with --local," " please remove."
  91. self.assertEqual(publish.runner("manage.down"), ret)
  92. mock = MagicMock(return_value=True)
  93. mock_id = MagicMock(return_value="salt_id")
  94. with patch.dict(publish.__opts__, {"master_uri": mock, "id": mock_id}):
  95. Channel.flag = 0
  96. self.assertTrue(publish.runner("manage.down"))
  97. Channel.flag = 1
  98. self.assertEqual(
  99. publish.runner("manage.down"), "'manage.down' runner publish timed out"
  100. )