test_win_license.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.win_license as win_license
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class LicenseTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {win_license: {}}
  13. def test_activate(self):
  14. """
  15. Test activating the given product key
  16. """
  17. expected = {
  18. "changes": {},
  19. "comment": "Windows is now activated.",
  20. "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
  21. "result": True,
  22. }
  23. info = {
  24. "description": "Prof",
  25. "licensed": False,
  26. "name": "Win7",
  27. "partial_key": "XXXXX",
  28. }
  29. info_mock = MagicMock(return_value=info)
  30. install_mock = MagicMock(return_value="Installed successfully")
  31. activate_mock = MagicMock(return_value="Activated successfully")
  32. with patch.dict(
  33. win_license.__salt__,
  34. {
  35. "license.info": info_mock,
  36. "license.install": install_mock,
  37. "license.activate": activate_mock,
  38. },
  39. ):
  40. out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  41. info_mock.assert_called_once_with()
  42. install_mock.assert_called_once_with("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  43. activate_mock.assert_called_once_with()
  44. self.assertEqual(out, expected)
  45. def test_installed_not_activated(self):
  46. """
  47. Test activating the given product key when the key is installed but not activated
  48. """
  49. expected = {
  50. "changes": {},
  51. "comment": "Windows is now activated.",
  52. "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
  53. "result": True,
  54. }
  55. info = {
  56. "description": "Prof",
  57. "licensed": False,
  58. "name": "Win7",
  59. "partial_key": "ABCDE",
  60. }
  61. info_mock = MagicMock(return_value=info)
  62. install_mock = MagicMock(return_value="Installed successfully")
  63. activate_mock = MagicMock(return_value="Activated successfully")
  64. with patch.dict(
  65. win_license.__salt__,
  66. {
  67. "license.info": info_mock,
  68. "license.install": install_mock,
  69. "license.activate": activate_mock,
  70. },
  71. ):
  72. out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  73. info_mock.assert_called_once_with()
  74. assert not install_mock.called
  75. activate_mock.assert_called_once_with()
  76. self.assertEqual(out, expected)
  77. def test_installed_activated(self):
  78. """
  79. Test activating the given product key when its already activated
  80. """
  81. expected = {
  82. "changes": {},
  83. "comment": "Windows is already activated.",
  84. "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
  85. "result": True,
  86. }
  87. info = {
  88. "description": "Prof",
  89. "licensed": True,
  90. "name": "Win7",
  91. "partial_key": "ABCDE",
  92. }
  93. info_mock = MagicMock(return_value=info)
  94. install_mock = MagicMock(return_value="Installed successfully")
  95. activate_mock = MagicMock(return_value="Activated successfully")
  96. with patch.dict(
  97. win_license.__salt__,
  98. {
  99. "license.info": info_mock,
  100. "license.install": install_mock,
  101. "license.activate": activate_mock,
  102. },
  103. ):
  104. out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  105. info_mock.assert_called_once_with()
  106. assert not install_mock.called
  107. assert not activate_mock.called
  108. self.assertEqual(out, expected)
  109. def test_installed_install_fail(self):
  110. """
  111. Test activating the given product key when the install fails
  112. """
  113. expected = {
  114. "changes": {},
  115. "comment": "Unable to install the given product key is it valid?",
  116. "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
  117. "result": False,
  118. }
  119. info = {
  120. "description": "Prof",
  121. "licensed": False,
  122. "name": "Win7",
  123. "partial_key": "12345",
  124. }
  125. info_mock = MagicMock(return_value=info)
  126. install_mock = MagicMock(return_value="Failed")
  127. activate_mock = MagicMock()
  128. with patch.dict(
  129. win_license.__salt__,
  130. {
  131. "license.info": info_mock,
  132. "license.install": install_mock,
  133. "license.activate": activate_mock,
  134. },
  135. ):
  136. out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  137. info_mock.assert_called_once_with()
  138. install_mock.assert_called_once_with("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  139. assert not activate_mock.called
  140. self.assertEqual(out, expected)
  141. def test_installed_activate_fail(self):
  142. """
  143. Test activating the given product key when the install fails
  144. """
  145. expected = {
  146. "changes": {},
  147. "comment": "Unable to activate the given product key.",
  148. "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
  149. "result": False,
  150. }
  151. info = {
  152. "description": "Prof",
  153. "licensed": False,
  154. "name": "Win7",
  155. "partial_key": "ABCDE",
  156. }
  157. info_mock = MagicMock(return_value=info)
  158. install_mock = MagicMock()
  159. activate_mock = MagicMock(return_value="Failed to activate")
  160. with patch.dict(
  161. win_license.__salt__,
  162. {
  163. "license.info": info_mock,
  164. "license.install": install_mock,
  165. "license.activate": activate_mock,
  166. },
  167. ):
  168. out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
  169. info_mock.assert_called_once_with()
  170. assert not install_mock.called
  171. activate_mock.assert_called_once_with()
  172. self.assertEqual(out, expected)