salt/tests/unit/modules/test_win_path.py

110 lines
3.0 KiB
Python
Raw Normal View History

2015-04-07 08:46:45 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Testing Libs
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
2015-04-07 08:46:45 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.win_path as win_path
2015-04-07 08:46:45 +00:00
# Globals
win_path.__salt__ = {}
class MockWin32Gui(object):
'''
Mock class for win32gui
'''
def __init__(self):
pass
@staticmethod
def SendMessageTimeout(*args):
'''
Mock method for SendMessageTimeOut
'''
return [args[0]]
class MockWin32Con(object):
'''
Mock class for win32con
'''
HWND_BROADCAST = 1
WM_SETTINGCHANGE = 1
def __init__(self):
pass
win_path.win32gui = MockWin32Gui
win_path.win32con = MockWin32Con
@skipIf(NO_MOCK, NO_MOCK_REASON)
class WinPathTestCase(TestCase):
'''
Test cases for salt.modules.win_path
'''
def test_rehash(self):
'''
Test to rehash the Environment variables
'''
self.assertTrue(win_path.rehash())
def test_get_path(self):
'''
Test to Returns the system path
'''
2015-09-08 22:14:34 +00:00
mock = MagicMock(return_value={'vdata': 'c:\\salt'})
2015-09-15 22:27:47 +00:00
with patch.dict(win_path.__salt__, {'reg.read_value': mock}):
2015-04-07 08:46:45 +00:00
self.assertListEqual(win_path.get_path(), ['c:\\salt'])
def test_exists(self):
'''
Test to check if the directory is configured
'''
mock = MagicMock(return_value='c:\\salt')
with patch.object(win_path, 'get_path', mock):
self.assertTrue(win_path.exists("c:\\salt"))
def test_add(self):
'''
Test to add the directory to the SYSTEM path
'''
2015-09-08 22:14:34 +00:00
mock_get = MagicMock(return_value=['c:\\salt'])
with patch.object(win_path, 'get_path', mock_get):
mock_set = MagicMock(return_value=True)
with patch.dict(win_path.__salt__, {'reg.set_value': mock_set}):
mock_rehash = MagicMock(side_effect=[True, False])
with patch.object(win_path, 'rehash', mock_rehash):
2015-04-07 08:46:45 +00:00
self.assertTrue(win_path.add("c:\\salt", 1))
self.assertFalse(win_path.add("c:\\salt", 1))
def test_remove(self):
'''
Test to remove the directory from the SYSTEM path
'''
2015-09-08 22:14:34 +00:00
mock_get = MagicMock(side_effect=[[1], ['c:\\salt'], ['c:\\salt']])
with patch.object(win_path, 'get_path', mock_get):
2015-04-07 08:46:45 +00:00
self.assertTrue(win_path.remove("c:\\salt"))
2015-09-08 22:14:34 +00:00
mock_set = MagicMock(side_effect=[True, False])
with patch.dict(win_path.__salt__, {'reg.set_value': mock_set}):
mock_rehash = MagicMock(return_value="Salt")
with patch.object(win_path, 'rehash', mock_rehash):
2015-04-07 08:46:45 +00:00
self.assertEqual(win_path.remove("c:\\salt"), "Salt")
self.assertFalse(win_path.remove("c:\\salt"))