1
0

test_status.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.status as status
  16. class StatusTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.status
  19. '''
  20. def setup_loader_modules(self):
  21. return {status: {}}
  22. # 'loadavg' function tests: 1
  23. def test_loadavg(self):
  24. '''
  25. Test to return the current load average for the specified minion.
  26. '''
  27. name = 'mymonitor'
  28. ret = {'name': name,
  29. 'changes': {},
  30. 'result': True,
  31. 'data': {},
  32. 'comment': ''}
  33. mock = MagicMock(return_value=[])
  34. with patch.dict(status.__salt__, {'status.loadavg': mock}):
  35. comt = ('Requested load average mymonitor not available ')
  36. ret.update({'comment': comt, 'result': False})
  37. self.assertDictEqual(status.loadavg(name), ret)
  38. mock = MagicMock(return_value={name: 3})
  39. with patch.dict(status.__salt__, {'status.loadavg': mock}):
  40. comt = ('Min must be less than max')
  41. ret.update({'comment': comt, 'result': False})
  42. self.assertDictEqual(status.loadavg(name, 1, 5), ret)
  43. comt = ('Load avg is below minimum of 4 at 3.0')
  44. ret.update({'comment': comt, 'data': 3})
  45. self.assertDictEqual(status.loadavg(name, 5, 4), ret)
  46. comt = ('Load avg above maximum of 2 at 3.0')
  47. ret.update({'comment': comt, 'data': 3})
  48. self.assertDictEqual(status.loadavg(name, 2, 1), ret)
  49. comt = ('Load avg in acceptable range')
  50. ret.update({'comment': comt, 'result': True})
  51. self.assertDictEqual(status.loadavg(name, 3, 1), ret)
  52. # 'process' function tests: 1
  53. def test_process(self):
  54. '''
  55. Test to return whether the specified signature
  56. is found in the process tree.
  57. '''
  58. name = 'mymonitor'
  59. ret = {'name': name,
  60. 'changes': {},
  61. 'result': True,
  62. 'data': {},
  63. 'comment': ''}
  64. mock = MagicMock(side_effect=[{}, {name: 1}])
  65. with patch.dict(status.__salt__, {'status.pid': mock}):
  66. comt = ('Process signature "mymonitor" not found ')
  67. ret.update({'comment': comt, 'result': False})
  68. self.assertDictEqual(status.process(name), ret)
  69. comt = ('Process signature "mymonitor" was found ')
  70. ret.update({'comment': comt, 'result': True,
  71. 'data': {name: 1}})
  72. self.assertDictEqual(status.process(name), ret)