test_ipmi.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # Import Salt Libs
  14. import salt.states.ipmi as ipmi
  15. class IpmiTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.ipmi
  18. '''
  19. def setup_loader_modules(self):
  20. return {ipmi: {}}
  21. # 'boot_device' function tests: 1
  22. def test_boot_device(self):
  23. '''
  24. Test to request power state change.
  25. '''
  26. name = 'salt'
  27. ret = {'name': name,
  28. 'result': True,
  29. 'comment': '',
  30. 'changes': {}}
  31. mock = MagicMock(return_value=name)
  32. with patch.dict(ipmi.__salt__, {'ipmi.get_bootdev': mock,
  33. 'ipmi.set_bootdev': mock}):
  34. comt = ('system already in this state')
  35. ret.update({'comment': comt})
  36. self.assertDictEqual(ipmi.boot_device(name), ret)
  37. with patch.dict(ipmi.__opts__, {'test': False}):
  38. comt = ('changed boot device')
  39. ret.update({'name': 'default', 'comment': comt, 'result': True,
  40. 'changes': {'new': 'default', 'old': 'salt'}})
  41. self.assertDictEqual(ipmi.boot_device(), ret)
  42. with patch.dict(ipmi.__opts__, {'test': True}):
  43. comt = ('would change boot device')
  44. ret.update({'comment': comt, 'result': None})
  45. self.assertDictEqual(ipmi.boot_device(), ret)
  46. # 'power' function tests: 1
  47. def test_power(self):
  48. '''
  49. Test to request power state change
  50. '''
  51. ret = {'name': 'power_on',
  52. 'result': True,
  53. 'comment': '',
  54. 'changes': {}}
  55. mock = MagicMock(return_value='on')
  56. with patch.dict(ipmi.__salt__, {'ipmi.get_power': mock,
  57. 'ipmi.set_power': mock}):
  58. comt = ('system already in this state')
  59. ret.update({'comment': comt})
  60. self.assertDictEqual(ipmi.power(), ret)
  61. with patch.dict(ipmi.__opts__, {'test': False}):
  62. comt = ('changed system power')
  63. ret.update({'name': 'off', 'comment': comt, 'result': True,
  64. 'changes': {'new': 'off', 'old': 'on'}})
  65. self.assertDictEqual(ipmi.power('off'), ret)
  66. with patch.dict(ipmi.__opts__, {'test': True}):
  67. comt = ('would power: off system')
  68. ret.update({'comment': comt, 'result': None})
  69. self.assertDictEqual(ipmi.power('off'), ret)
  70. # 'user_present' function tests: 1
  71. def test_user_present(self):
  72. '''
  73. Test to ensure IPMI user and user privileges.
  74. '''
  75. name = 'salt'
  76. ret = {'name': name,
  77. 'result': True,
  78. 'comment': '',
  79. 'changes': {}}
  80. mock_ret = {'access': {'callback': False, 'link_auth': True,
  81. 'ipmi_msg': True,
  82. 'privilege_level': 'administrator'}}
  83. mock = MagicMock(return_value=mock_ret)
  84. mock_bool = MagicMock(side_effect=[True, False, False, False])
  85. with patch.dict(ipmi.__salt__, {'ipmi.get_user': mock,
  86. 'ipmi.set_user_password': mock_bool,
  87. 'ipmi.ensure_user': mock_bool}):
  88. comt = ('user already present')
  89. ret.update({'comment': comt})
  90. self.assertDictEqual(ipmi.user_present(name, 5, 'salt@123'), ret)
  91. with patch.dict(ipmi.__opts__, {'test': True}):
  92. comt = ('would (re)create user')
  93. ret.update({'comment': comt, 'result': None,
  94. 'changes': {'new': 'salt', 'old': mock_ret}})
  95. self.assertDictEqual(ipmi.user_present(name, 5, 'pw@123'), ret)
  96. with patch.dict(ipmi.__opts__, {'test': False}):
  97. comt = ('(re)created user')
  98. ret.update({'comment': comt, 'result': True,
  99. 'changes': {'new': mock_ret, 'old': mock_ret}})
  100. self.assertDictEqual(ipmi.user_present(name, 5, 'pw@123'), ret)
  101. # 'user_absent' function tests: 1
  102. def test_user_absent(self):
  103. '''
  104. Test to delete all user (uid) records having the matching name.
  105. '''
  106. name = 'salt'
  107. ret = {'name': name,
  108. 'result': True,
  109. 'comment': '',
  110. 'changes': {}}
  111. mock = MagicMock(side_effect=[[], [5], [5]])
  112. mock_bool = MagicMock(return_value=True)
  113. with patch.dict(ipmi.__salt__, {'ipmi.get_name_uids': mock,
  114. 'ipmi.delete_user': mock_bool}):
  115. comt = ('user already absent')
  116. ret.update({'comment': comt})
  117. self.assertDictEqual(ipmi.user_absent(name), ret)
  118. with patch.dict(ipmi.__opts__, {'test': True}):
  119. comt = ('would delete user(s)')
  120. ret.update({'comment': comt, 'result': None,
  121. 'changes': {'delete': [5]}})
  122. self.assertDictEqual(ipmi.user_absent(name), ret)
  123. with patch.dict(ipmi.__opts__, {'test': False}):
  124. comt = ('user(s) removed')
  125. ret.update({'comment': comt, 'result': False,
  126. 'changes': {'new': 'None', 'old': [5]}})
  127. self.assertDictEqual(ipmi.user_absent(name), ret)