2017-04-30 14:26:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:synopsis: Unit Tests for 'module.aptkernelpkg'
|
|
|
|
:platform: Linux
|
|
|
|
:maturity: develop
|
2018-02-22 03:47:41 +00:00
|
|
|
versionadded:: 2018.3.0
|
2017-04-30 14:26:44 +00:00
|
|
|
'''
|
2017-08-16 15:44:15 +00:00
|
|
|
# pylint: disable=invalid-name,no-member
|
2017-04-30 14:26:44 +00:00
|
|
|
|
|
|
|
# Import Python libs
|
2018-01-23 16:09:56 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
from salt.ext import six
|
2017-04-30 14:26:44 +00:00
|
|
|
|
2017-05-05 12:27:37 +00:00
|
|
|
try:
|
|
|
|
# Import Salt Testing Libs
|
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch)
|
2017-04-30 14:26:44 +00:00
|
|
|
|
2017-05-05 12:27:37 +00:00
|
|
|
# Import Salt Libs
|
|
|
|
import salt.states.kernelpkg as kernelpkg
|
|
|
|
HAS_MODULES = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_MODULES = False
|
2017-04-30 14:26:44 +00:00
|
|
|
|
|
|
|
KERNEL_LIST = ['4.4.0-70-generic', '4.4.0-71-generic', '4.5.1-14-generic']
|
|
|
|
STATE_NAME = 'kernelpkg-test'
|
|
|
|
|
2017-05-05 12:27:37 +00:00
|
|
|
|
2017-04-30 14:26:44 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-05-05 12:27:37 +00:00
|
|
|
@skipIf(not HAS_MODULES, 'Salt modules could not be loaded')
|
2017-04-30 14:26:44 +00:00
|
|
|
class KernelPkgTestCase(TestCase, LoaderModuleMockMixin):
|
|
|
|
'''
|
|
|
|
Test cases for salt.states.aptpkg
|
|
|
|
'''
|
|
|
|
|
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {
|
|
|
|
kernelpkg: {
|
|
|
|
'__salt__': {
|
|
|
|
'system.reboot': MagicMock(return_value=None),
|
|
|
|
'kernelpkg.upgrade': MagicMock(return_value={
|
|
|
|
'upgrades': {
|
|
|
|
'kernel': {
|
|
|
|
'old': '1.0.0',
|
|
|
|
'new': '2.0.0',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2017-05-05 01:15:04 +00:00
|
|
|
'kernelpkg.active': MagicMock(return_value=0),
|
2017-04-30 14:26:44 +00:00
|
|
|
'kernelpkg.latest_installed': MagicMock(return_value=0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_latest_installed_with_changes(self):
|
|
|
|
'''
|
|
|
|
Test - latest_installed when an upgrade is available
|
|
|
|
'''
|
|
|
|
installed = MagicMock(return_value=KERNEL_LIST[:-1])
|
|
|
|
upgrade = MagicMock(return_value=KERNEL_LIST[-1])
|
|
|
|
with patch.dict(kernelpkg.__salt__, {'kernelpkg.list_installed': installed}):
|
|
|
|
with patch.dict(kernelpkg.__salt__, {'kernelpkg.latest_available': upgrade}):
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': False}):
|
|
|
|
kernelpkg.__salt__['kernelpkg.upgrade'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_installed(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertIsInstance(ret['changes'], dict)
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-07-07 16:28:19 +00:00
|
|
|
self.assert_called_once(kernelpkg.__salt__['kernelpkg.upgrade'])
|
2017-04-30 14:26:44 +00:00
|
|
|
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': True}):
|
|
|
|
kernelpkg.__salt__['kernelpkg.upgrade'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_installed(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertIsNone(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['kernelpkg.upgrade'].assert_not_called()
|
|
|
|
|
|
|
|
def test_latest_installed_at_latest(self):
|
|
|
|
'''
|
|
|
|
Test - latest_installed when no upgrade is available
|
|
|
|
'''
|
|
|
|
installed = MagicMock(return_value=KERNEL_LIST)
|
|
|
|
upgrade = MagicMock(return_value=KERNEL_LIST[-1])
|
|
|
|
with patch.dict(kernelpkg.__salt__, {'kernelpkg.list_installed': installed}):
|
|
|
|
with patch.dict(kernelpkg.__salt__, {'kernelpkg.latest_available': upgrade}):
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': False}):
|
|
|
|
ret = kernelpkg.latest_installed(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['kernelpkg.upgrade'].assert_not_called()
|
|
|
|
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': True}):
|
|
|
|
ret = kernelpkg.latest_installed(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['kernelpkg.upgrade'].assert_not_called()
|
|
|
|
|
|
|
|
def test_latest_active_with_changes(self):
|
|
|
|
'''
|
|
|
|
Test - latest_active when a new kernel is available
|
|
|
|
'''
|
|
|
|
reboot = MagicMock(return_value=True)
|
2018-10-01 17:57:59 +00:00
|
|
|
latest = MagicMock(return_value=1)
|
|
|
|
with patch.dict(
|
|
|
|
kernelpkg.__salt__, {'kernelpkg.needs_reboot': reboot,
|
|
|
|
'kernelpkg.latest_installed': latest}), \
|
|
|
|
patch.dict(kernelpkg.__opts__, {'test': False}):
|
|
|
|
kernelpkg.__salt__['system.reboot'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_active(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertIsInstance(ret['changes'], dict)
|
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
|
|
|
self.assert_called_once(kernelpkg.__salt__['system.reboot'])
|
2017-04-30 14:26:44 +00:00
|
|
|
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': True}):
|
|
|
|
kernelpkg.__salt__['system.reboot'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_active(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertIsNone(ret['result'])
|
2018-10-01 17:57:59 +00:00
|
|
|
self.assertDictEqual(
|
|
|
|
ret['changes'],
|
|
|
|
{'kernel': {'new': 1, 'old': 0}}
|
|
|
|
)
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['system.reboot'].assert_not_called()
|
|
|
|
|
|
|
|
def test_latest_active_at_latest(self):
|
|
|
|
'''
|
|
|
|
Test - latest_active when the newest kernel is already active
|
|
|
|
'''
|
|
|
|
reboot = MagicMock(return_value=False)
|
|
|
|
with patch.dict(kernelpkg.__salt__, {'kernelpkg.needs_reboot': reboot}):
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': False}):
|
|
|
|
kernelpkg.__salt__['system.reboot'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_active(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['system.reboot'].assert_not_called()
|
|
|
|
|
|
|
|
with patch.dict(kernelpkg.__opts__, {'test': True}):
|
|
|
|
kernelpkg.__salt__['system.reboot'].reset_mock()
|
|
|
|
ret = kernelpkg.latest_active(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|
2017-04-30 14:26:44 +00:00
|
|
|
kernelpkg.__salt__['system.reboot'].assert_not_called()
|
|
|
|
|
|
|
|
def test_latest_wait(self):
|
|
|
|
'''
|
|
|
|
Test - latest_wait static results
|
|
|
|
'''
|
|
|
|
ret = kernelpkg.latest_wait(name=STATE_NAME)
|
|
|
|
self.assertEqual(ret['name'], STATE_NAME)
|
|
|
|
self.assertTrue(ret['result'])
|
|
|
|
self.assertDictEqual(ret['changes'], {})
|
2018-01-23 16:09:56 +00:00
|
|
|
self.assertIsInstance(ret['comment'], six.text_type)
|