1
0

test_batch.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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)