test_publish.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.case import ModuleCase
  6. from tests.support.mixins import SaltReturnAssertsMixin
  7. class PublishModuleTest(ModuleCase, SaltReturnAssertsMixin):
  8. '''
  9. Validate the publish module
  10. '''
  11. def test_publish(self):
  12. '''
  13. publish.publish
  14. '''
  15. ret = self.run_function(
  16. 'publish.publish',
  17. ['minion', 'test.ping'],
  18. f_timeout=50
  19. )
  20. self.assertEqual(ret, {'minion': True})
  21. ret = self.run_function(
  22. 'publish.publish',
  23. ['minion', 'test.kwarg'],
  24. f_arg='cheese=spam',
  25. f_timeout=50
  26. )
  27. ret = ret['minion']
  28. check_true = (
  29. 'cheese',
  30. '__pub_arg',
  31. '__pub_fun',
  32. '__pub_id',
  33. '__pub_jid',
  34. '__pub_ret',
  35. '__pub_tgt',
  36. '__pub_tgt_type',
  37. )
  38. for name in check_true:
  39. if name not in ret:
  40. print(name)
  41. self.assertTrue(name in ret)
  42. self.assertEqual(ret['cheese'], 'spam')
  43. self.assertEqual(ret['__pub_arg'], [{'cheese': 'spam'}])
  44. self.assertEqual(ret['__pub_id'], 'minion')
  45. self.assertEqual(ret['__pub_fun'], 'test.kwarg')
  46. def test_publish_yaml_args(self):
  47. '''
  48. test publish.publish yaml args formatting
  49. '''
  50. ret = self.run_function(
  51. 'publish.publish',
  52. ['minion', 'test.ping'],
  53. f_timeout=50
  54. )
  55. self.assertEqual(ret, {'minion': True})
  56. test_args_list = ['saltines, si', 'crackers, nein', 'cheese, indeed']
  57. test_args = '["{args[0]}", "{args[1]}", "{args[2]}"]'.format(args=test_args_list)
  58. ret = self.run_function(
  59. 'publish.publish',
  60. ['minion', 'test.arg', test_args],
  61. f_timeout=50
  62. )
  63. ret = ret['minion']
  64. check_true = (
  65. '__pub_arg',
  66. '__pub_fun',
  67. '__pub_id',
  68. '__pub_jid',
  69. '__pub_ret',
  70. '__pub_tgt',
  71. '__pub_tgt_type',
  72. )
  73. for name in check_true:
  74. if name not in ret['kwargs']:
  75. print(name)
  76. self.assertTrue(name in ret['kwargs'])
  77. self.assertEqual(ret['args'], test_args_list)
  78. self.assertEqual(ret['kwargs']['__pub_id'], 'minion')
  79. self.assertEqual(ret['kwargs']['__pub_fun'], 'test.arg')
  80. def test_full_data(self):
  81. '''
  82. publish.full_data
  83. '''
  84. ret = self.run_function(
  85. 'publish.full_data',
  86. ['minion', 'test.fib', 20],
  87. f_timeout=50
  88. )
  89. self.assertTrue(ret)
  90. self.assertEqual(ret['minion']['ret'][0], 6765)
  91. def test_kwarg(self):
  92. '''
  93. Verify that the pub data is making it to the minion functions
  94. '''
  95. ret = self.run_function(
  96. 'publish.full_data',
  97. ['minion', 'test.kwarg'],
  98. f_arg='cheese=spam',
  99. f_timeout=50
  100. )
  101. ret = ret['minion']['ret']
  102. check_true = (
  103. 'cheese',
  104. '__pub_arg',
  105. '__pub_fun',
  106. '__pub_id',
  107. '__pub_jid',
  108. '__pub_ret',
  109. '__pub_tgt',
  110. '__pub_tgt_type',
  111. )
  112. for name in check_true:
  113. if name not in ret:
  114. print(name)
  115. self.assertTrue(name in ret)
  116. self.assertEqual(ret['cheese'], 'spam')
  117. self.assertEqual(ret['__pub_arg'], [{'cheese': 'spam'}])
  118. self.assertEqual(ret['__pub_id'], 'minion')
  119. self.assertEqual(ret['__pub_fun'], 'test.kwarg')
  120. ret = self.run_function(
  121. 'publish.full_data',
  122. ['minion', 'test.kwarg'],
  123. cheese='spam',
  124. f_timeout=50
  125. )
  126. self.assertIn(
  127. 'The following keyword arguments are not valid', ret
  128. )
  129. def test_reject_minion(self):
  130. '''
  131. Test bad authentication
  132. '''
  133. ret = self.run_function(
  134. 'publish.publish',
  135. ['minion', 'cmd.run', ['echo foo']],
  136. f_timeout=50
  137. )
  138. self.assertEqual(ret, {})