test_powerpath.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  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.powerpath as powerpath
  16. class PowerpathTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.powerpath
  19. '''
  20. def setup_loader_modules(self):
  21. return {powerpath: {}}
  22. # 'license_present' function tests: 1
  23. def test_license_present(self):
  24. '''
  25. Test to ensures that the specified PowerPath license key is present
  26. on the host.
  27. '''
  28. name = 'mylic'
  29. ret = {'name': name,
  30. 'changes': {},
  31. 'result': True,
  32. 'comment': ''}
  33. mock_t = MagicMock(side_effect=[{'result': True, 'output': name},
  34. {'result': False, 'output': name}])
  35. mock = MagicMock(side_effect=[False, True, True, True, True])
  36. mock_l = MagicMock(return_value=[{'key': name}])
  37. with patch.dict(powerpath.__salt__,
  38. {'powerpath.has_powerpath': mock,
  39. 'powerpath.list_licenses': mock_l,
  40. 'powerpath.add_license': mock_t}):
  41. comt = ('PowerPath is not installed.')
  42. ret.update({'comment': comt, 'result': False})
  43. self.assertDictEqual(powerpath.license_present(name), ret)
  44. comt = ('License key {0} already present'.format(name))
  45. ret.update({'comment': comt, 'result': True})
  46. self.assertDictEqual(powerpath.license_present(name), ret)
  47. with patch.dict(powerpath.__opts__, {'test': True}):
  48. comt = ('License key Mylic is set to be added')
  49. ret.update({'comment': comt, 'result': None, 'name': 'Mylic'})
  50. self.assertDictEqual(powerpath.license_present('Mylic'), ret)
  51. with patch.dict(powerpath.__opts__, {'test': False}):
  52. ret.update({'comment': name, 'result': True,
  53. 'changes': {'Mylic': 'added'}})
  54. self.assertDictEqual(powerpath.license_present('Mylic'), ret)
  55. ret.update({'result': False, 'changes': {}})
  56. self.assertDictEqual(powerpath.license_present('Mylic'), ret)
  57. # 'license_absent' function tests: 1
  58. def test_license_absent(self):
  59. '''
  60. Test to ensures that the specified PowerPath license key is absent
  61. on the host.
  62. '''
  63. name = 'mylic'
  64. ret = {'name': name,
  65. 'changes': {},
  66. 'result': True,
  67. 'comment': ''}
  68. mock_t = MagicMock(side_effect=[{'result': True, 'output': name},
  69. {'result': False, 'output': name}])
  70. mock = MagicMock(side_effect=[False, True, True, True, True])
  71. mock_l = MagicMock(return_value=[{'key': 'salt'}])
  72. with patch.dict(powerpath.__salt__,
  73. {'powerpath.has_powerpath': mock,
  74. 'powerpath.list_licenses': mock_l,
  75. 'powerpath.remove_license': mock_t}):
  76. comt = ('PowerPath is not installed.')
  77. ret.update({'comment': comt, 'result': False})
  78. self.assertDictEqual(powerpath.license_absent(name), ret)
  79. comt = ('License key {0} not present'.format(name))
  80. ret.update({'comment': comt, 'result': True})
  81. self.assertDictEqual(powerpath.license_absent(name), ret)
  82. with patch.dict(powerpath.__opts__, {'test': True}):
  83. comt = ('License key salt is set to be removed')
  84. ret.update({'comment': comt, 'result': None, 'name': 'salt'})
  85. self.assertDictEqual(powerpath.license_absent('salt'), ret)
  86. with patch.dict(powerpath.__opts__, {'test': False}):
  87. ret.update({'comment': name, 'result': True,
  88. 'changes': {'salt': 'removed'}})
  89. self.assertDictEqual(powerpath.license_absent('salt'), ret)
  90. ret.update({'result': False, 'changes': {}})
  91. self.assertDictEqual(powerpath.license_absent('salt'), ret)