test_batch.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. from salt.cli.batch import Batch
  9. from tests.support.mock import MagicMock, patch
  10. # Import Salt Testing Libs
  11. from tests.support.unit import TestCase
  12. class BatchTestCase(TestCase):
  13. """
  14. Unit Tests for the salt.cli.batch module
  15. """
  16. def setUp(self):
  17. opts = {
  18. "batch": "",
  19. "conf_file": {},
  20. "tgt": "",
  21. "transport": "",
  22. "timeout": 5,
  23. "gather_job_timeout": 5,
  24. }
  25. mock_client = MagicMock()
  26. with patch("salt.client.get_local_client", MagicMock(return_value=mock_client)):
  27. with patch("salt.client.LocalClient.cmd_iter", MagicMock(return_value=[])):
  28. self.batch = Batch(opts, quiet="quiet")
  29. # get_bnum tests
  30. def test_get_bnum_str(self):
  31. """
  32. Tests passing batch value as a number(str)
  33. """
  34. self.batch.opts = {"batch": "2", "timeout": 5}
  35. self.batch.minions = ["foo", "bar"]
  36. self.assertEqual(Batch.get_bnum(self.batch), 2)
  37. def test_get_bnum_int(self):
  38. """
  39. Tests passing batch value as a number(int)
  40. """
  41. self.batch.opts = {"batch": 2, "timeout": 5}
  42. self.batch.minions = ["foo", "bar"]
  43. self.assertEqual(Batch.get_bnum(self.batch), 2)
  44. def test_get_bnum_percentage(self):
  45. """
  46. Tests passing batch value as percentage
  47. """
  48. self.batch.opts = {"batch": "50%", "timeout": 5}
  49. self.batch.minions = ["foo"]
  50. self.assertEqual(Batch.get_bnum(self.batch), 1)
  51. def test_get_bnum_high_percentage(self):
  52. """
  53. Tests passing batch value as percentage over 100%
  54. """
  55. self.batch.opts = {"batch": "160%", "timeout": 5}
  56. self.batch.minions = ["foo", "bar", "baz"]
  57. self.assertEqual(Batch.get_bnum(self.batch), 4)
  58. def test_get_bnum_invalid_batch_data(self):
  59. """
  60. Tests when an invalid batch value is passed
  61. """
  62. ret = Batch.get_bnum(self.batch)
  63. self.assertEqual(ret, None)
  64. def test_return_value_in_run_for_ret(self):
  65. """
  66. cmd_iter_no_block should have been called with a return no matter if
  67. the return value was in ret or return.
  68. """
  69. self.batch.opts = {
  70. "batch": "100%",
  71. "timeout": 5,
  72. "fun": "test",
  73. "arg": "foo",
  74. "gather_job_timeout": 5,
  75. "ret": "my_return",
  76. }
  77. self.batch.minions = ["foo", "bar", "baz"]
  78. self.batch.local.cmd_iter_no_block = MagicMock(return_value=iter([]))
  79. ret = Batch.run(self.batch)
  80. # We need to fetch at least one object to trigger the relevant code path.
  81. x = next(ret)
  82. self.batch.local.cmd_iter_no_block.assert_called_with(
  83. ["baz", "bar", "foo"],
  84. "test",
  85. "foo",
  86. 5,
  87. "list",
  88. raw=False,
  89. ret="my_return",
  90. show_jid=False,
  91. verbose=False,
  92. gather_job_timeout=5,
  93. )
  94. def test_return_value_in_run_for_return(self):
  95. """
  96. cmd_iter_no_block should have been called with a return no matter if
  97. the return value was in ret or return.
  98. """
  99. self.batch.opts = {
  100. "batch": "100%",
  101. "timeout": 5,
  102. "fun": "test",
  103. "arg": "foo",
  104. "gather_job_timeout": 5,
  105. "return": "my_return",
  106. }
  107. self.batch.minions = ["foo", "bar", "baz"]
  108. self.batch.local.cmd_iter_no_block = MagicMock(return_value=iter([]))
  109. ret = Batch.run(self.batch)
  110. # We need to fetch at least one object to trigger the relevant code path.
  111. x = next(ret)
  112. self.batch.local.cmd_iter_no_block.assert_called_with(
  113. ["baz", "bar", "foo"],
  114. "test",
  115. "foo",
  116. 5,
  117. "list",
  118. raw=False,
  119. ret="my_return",
  120. show_jid=False,
  121. verbose=False,
  122. gather_job_timeout=5,
  123. )