test_batch.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # Import Salt Testing Libs
  10. from tests.support.unit import skipIf, TestCase
  11. from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
  12. @skipIf(NO_MOCK, NO_MOCK_REASON)
  13. class BatchTestCase(TestCase):
  14. '''
  15. Unit Tests for the salt.cli.batch module
  16. '''
  17. def setUp(self):
  18. opts = {'batch': '',
  19. 'conf_file': {},
  20. 'tgt': '',
  21. 'transport': '',
  22. 'timeout': 5,
  23. 'gather_job_timeout': 5}
  24. mock_client = MagicMock()
  25. with patch('salt.client.get_local_client', MagicMock(return_value=mock_client)):
  26. with patch('salt.client.LocalClient.cmd_iter', MagicMock(return_value=[])):
  27. self.batch = Batch(opts, quiet='quiet')
  28. # get_bnum tests
  29. def test_get_bnum_str(self):
  30. '''
  31. Tests passing batch value as a number(str)
  32. '''
  33. self.batch.opts = {'batch': '2', 'timeout': 5}
  34. self.batch.minions = ['foo', 'bar']
  35. self.assertEqual(Batch.get_bnum(self.batch), 2)
  36. def test_get_bnum_int(self):
  37. '''
  38. Tests passing batch value as a number(int)
  39. '''
  40. self.batch.opts = {'batch': 2, 'timeout': 5}
  41. self.batch.minions = ['foo', 'bar']
  42. self.assertEqual(Batch.get_bnum(self.batch), 2)
  43. def test_get_bnum_percentage(self):
  44. '''
  45. Tests passing batch value as percentage
  46. '''
  47. self.batch.opts = {'batch': '50%', 'timeout': 5}
  48. self.batch.minions = ['foo']
  49. self.assertEqual(Batch.get_bnum(self.batch), 1)
  50. def test_get_bnum_high_percentage(self):
  51. '''
  52. Tests passing batch value as percentage over 100%
  53. '''
  54. self.batch.opts = {'batch': '160%', 'timeout': 5}
  55. self.batch.minions = ['foo', 'bar', 'baz']
  56. self.assertEqual(Batch.get_bnum(self.batch), 4)
  57. def test_get_bnum_invalid_batch_data(self):
  58. '''
  59. Tests when an invalid batch value is passed
  60. '''
  61. ret = Batch.get_bnum(self.batch)
  62. self.assertEqual(ret, None)