salt/tests/unit/states/test_win_update.py

134 lines
4.3 KiB
Python
Raw Normal View History

2015-05-06 16:15:41 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Testing Libs
2017-04-10 13:00:57 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
2015-05-06 16:15:41 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.states.win_update as win_update
2015-05-06 16:15:41 +00:00
class MockPyWinUpdater(object):
'''
Mock PyWinUpdater class
'''
def __init__(self):
pass
@staticmethod
def SetCategories(arg):
'''
Mock SetCategories
'''
return arg
@staticmethod
def SetIncludes(arg):
'''
Mock SetIncludes
'''
return arg
@staticmethod
def GetInstallationResults():
'''
Mock GetInstallationResults
'''
return True
@staticmethod
def GetDownloadResults():
'''
Mock GetDownloadResults
'''
return True
2016-02-10 21:36:21 +00:00
@staticmethod
def SetSkips(arg):
return True
2015-05-06 16:15:41 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-04-10 13:00:57 +00:00
class WinUpdateTestCase(TestCase, LoaderModuleMockMixin):
2015-05-06 16:15:41 +00:00
'''
Validate the win_update state
'''
2017-04-10 13:00:57 +00:00
def setup_loader_modules(self):
return {win_update: {'PyWinUpdater': MockPyWinUpdater}}
2015-05-06 16:15:41 +00:00
def test_installed(self):
'''
Test to install specified windows updates
'''
ret = {'name': 'salt',
'changes': {},
'result': False,
'comment': '',
'warnings': ["The 'win_update' module is deprecated, and will "
"be removed in Salt Fluorine. Please use the "
"'win_wua' module instead."]}
2015-05-06 16:15:41 +00:00
mock = MagicMock(side_effect=[['Saltstack', False, 5],
['Saltstack', True, 5],
['Saltstack', True, 5],
['Saltstack', True, 5]])
with patch.object(win_update, '_search', mock):
ret.update({'comment': 'Saltstack'})
self.assertDictEqual(win_update.installed('salt'), ret)
mock = MagicMock(side_effect=[['dude', False, 5],
['dude', True, 5],
['dude', True, 5]])
with patch.object(win_update, '_download', mock):
ret.update({'comment': 'Saltstackdude'})
self.assertDictEqual(win_update.installed('salt'), ret)
mock = MagicMock(side_effect=[['@Me', False, 5],
['@Me', True, 5]])
with patch.object(win_update, '_install', mock):
ret.update({'comment': 'Saltstackdude@Me'})
self.assertDictEqual(win_update.installed('salt'), ret)
ret.update({'changes': True, 'result': True})
self.assertDictEqual(win_update.installed('salt'), ret)
def test_downloaded(self):
'''
Test to cache updates for later install.
'''
ret = {'name': 'salt',
'changes': {},
'result': False,
'comment': '',
'warnings': ["The 'win_update' module is deprecated, and will "
"be removed in Salt Fluorine. Please use the "
"'win_wua' module instead."]}
2015-05-06 16:15:41 +00:00
mock = MagicMock(side_effect=[['Saltstack', False, 5],
['Saltstack', True, 5],
['Saltstack', True, 5]])
with patch.object(win_update, '_search', mock):
ret.update({'comment': 'Saltstack'})
self.assertDictEqual(win_update.downloaded('salt'), ret)
mock = MagicMock(side_effect=[['dude', False, 5],
['dude', True, 5]])
with patch.object(win_update, '_download', mock):
ret.update({'comment': 'Saltstackdude'})
self.assertDictEqual(win_update.downloaded('salt'), ret)
ret.update({'changes': True, 'result': True})
self.assertDictEqual(win_update.downloaded('salt'), ret)