123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- # -*- coding: utf-8 -*-
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Salt Libs
- import salt.states.win_license as win_license
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase
- class LicenseTestCase(TestCase, LoaderModuleMockMixin):
- def setup_loader_modules(self):
- return {win_license: {}}
- def test_activate(self):
- """
- Test activating the given product key
- """
- expected = {
- "changes": {},
- "comment": "Windows is now activated.",
- "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
- "result": True,
- }
- info = {
- "description": "Prof",
- "licensed": False,
- "name": "Win7",
- "partial_key": "XXXXX",
- }
- info_mock = MagicMock(return_value=info)
- install_mock = MagicMock(return_value="Installed successfully")
- activate_mock = MagicMock(return_value="Activated successfully")
- with patch.dict(
- win_license.__salt__,
- {
- "license.info": info_mock,
- "license.install": install_mock,
- "license.activate": activate_mock,
- },
- ):
- out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- info_mock.assert_called_once_with()
- install_mock.assert_called_once_with("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- activate_mock.assert_called_once_with()
- self.assertEqual(out, expected)
- def test_installed_not_activated(self):
- """
- Test activating the given product key when the key is installed but not activated
- """
- expected = {
- "changes": {},
- "comment": "Windows is now activated.",
- "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
- "result": True,
- }
- info = {
- "description": "Prof",
- "licensed": False,
- "name": "Win7",
- "partial_key": "ABCDE",
- }
- info_mock = MagicMock(return_value=info)
- install_mock = MagicMock(return_value="Installed successfully")
- activate_mock = MagicMock(return_value="Activated successfully")
- with patch.dict(
- win_license.__salt__,
- {
- "license.info": info_mock,
- "license.install": install_mock,
- "license.activate": activate_mock,
- },
- ):
- out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- info_mock.assert_called_once_with()
- assert not install_mock.called
- activate_mock.assert_called_once_with()
- self.assertEqual(out, expected)
- def test_installed_activated(self):
- """
- Test activating the given product key when its already activated
- """
- expected = {
- "changes": {},
- "comment": "Windows is already activated.",
- "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
- "result": True,
- }
- info = {
- "description": "Prof",
- "licensed": True,
- "name": "Win7",
- "partial_key": "ABCDE",
- }
- info_mock = MagicMock(return_value=info)
- install_mock = MagicMock(return_value="Installed successfully")
- activate_mock = MagicMock(return_value="Activated successfully")
- with patch.dict(
- win_license.__salt__,
- {
- "license.info": info_mock,
- "license.install": install_mock,
- "license.activate": activate_mock,
- },
- ):
- out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- info_mock.assert_called_once_with()
- assert not install_mock.called
- assert not activate_mock.called
- self.assertEqual(out, expected)
- def test_installed_install_fail(self):
- """
- Test activating the given product key when the install fails
- """
- expected = {
- "changes": {},
- "comment": "Unable to install the given product key is it valid?",
- "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
- "result": False,
- }
- info = {
- "description": "Prof",
- "licensed": False,
- "name": "Win7",
- "partial_key": "12345",
- }
- info_mock = MagicMock(return_value=info)
- install_mock = MagicMock(return_value="Failed")
- activate_mock = MagicMock()
- with patch.dict(
- win_license.__salt__,
- {
- "license.info": info_mock,
- "license.install": install_mock,
- "license.activate": activate_mock,
- },
- ):
- out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- info_mock.assert_called_once_with()
- install_mock.assert_called_once_with("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- assert not activate_mock.called
- self.assertEqual(out, expected)
- def test_installed_activate_fail(self):
- """
- Test activating the given product key when the install fails
- """
- expected = {
- "changes": {},
- "comment": "Unable to activate the given product key.",
- "name": "AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE",
- "result": False,
- }
- info = {
- "description": "Prof",
- "licensed": False,
- "name": "Win7",
- "partial_key": "ABCDE",
- }
- info_mock = MagicMock(return_value=info)
- install_mock = MagicMock()
- activate_mock = MagicMock(return_value="Failed to activate")
- with patch.dict(
- win_license.__salt__,
- {
- "license.info": info_mock,
- "license.install": install_mock,
- "license.activate": activate_mock,
- },
- ):
- out = win_license.activate("AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE")
- info_mock.assert_called_once_with()
- assert not install_mock.called
- activate_mock.assert_called_once_with()
- self.assertEqual(out, expected)
|