def test_exists(self): ''' Test to add the directory to the system PATH at index location ''' ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=['Salt', 'Saltdude']) with patch.dict(win_path.__salt__, {"win_path.get_path": mock}): mock = MagicMock(side_effect=['Saltdude', 'Saltdude', '/Saltdude', 'Saltdude']) with patch.object(win_path, '_normalize_dir', mock): ret.update({'comment': 'salt is already present in the' ' PATH at the right location'}) self.assertDictEqual(win_path.exists('salt', 1), ret) self.assertDictEqual(win_path.exists('salt'), ret) with patch.dict(win_path.__opts__, {"test": True}): ret.update({'comment': '', 'result': None, 'changes': {'added': 'salt will be' ' added at index 2'}}) self.assertDictEqual(win_path.exists('salt'), ret) with patch.dict(win_path.__opts__, {"test": False}): mock = MagicMock(return_value=False) with patch.dict(win_path.__salt__, {"win_path.add": mock}): ret.update({'comment': 'salt is already present in the' ' PATH at the right location', 'result': True, 'changes': {}}) self.assertDictEqual(win_path.exists('salt'), ret)
def test_exists(self): ''' Test to add the directory to the system PATH at index location ''' ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=['Salt', 'Saltdude']) with patch.dict(win_path.__salt__, {"win_path.get_path": mock}): mock = MagicMock(side_effect=['Saltdude', 'Saltdude', '/Saltdude', 'Saltdude']) with patch.object(win_path, '_normalize_dir', mock): ret.update({'comment': 'salt is already present in the' ' PATH at the right location'}) self.assertDictEqual(win_path.exists('salt', 1), ret) self.assertDictEqual(win_path.exists('salt'), ret) with patch.dict(win_path.__opts__, {"test": True}): ret.update({'comment': '', 'result': None, 'changes': {'added': 'salt will be' ' added at index 2'}}) self.assertDictEqual(win_path.exists('salt'), ret) with patch.dict(win_path.__opts__, {"test": False}): mock = MagicMock(return_value=False) with patch.dict(win_path.__salt__, {"win_path.add": mock}): ret.update({'comment': 'salt is already present in the' ' PATH at the right location', 'result': True, 'changes': {}}) self.assertDictEqual(win_path.exists('salt'), ret)
def test_exists_change_negative_index_test_mode(self): ''' Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (test mode enabled). ''' add_mock = Mock() rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', NAME, 'baz'], ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': True} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=-1) add_mock.assert_not_called() rehash_mock.assert_not_called() self.assertDictEqual( ret, { 'name': NAME, 'changes': {'index': {'old': -2, 'new': -1}}, 'result': None, 'comment': '{0} would be moved from index -2 to -1.'.format(NAME) } )
def test_exists_change_negative_index_failure(self): ''' Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (failed run). This tests a negative index. ''' add_mock = MagicMock(return_value=False) rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', NAME, 'baz'], ['foo', 'bar', NAME, 'baz'] ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': False} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=-1) add_mock.assert_called_once_with(NAME, index=-1, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { 'name': NAME, 'changes': {}, 'result': False, 'comment': 'Failed to move {0} from index -2 to -1.'.format(NAME) } )
def test_exists_change_negative_index_add_exception(self): ''' Tests win_path.exists when the directory is already in the PATH but an exception is raised when we attempt to add the key to its new location. This tests a negative index. ''' add_mock = MagicMock(side_effect=Exception('Global Thermonuclear War')) rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', NAME, 'baz'], ['foo', 'bar', NAME, 'baz'], ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': False} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=-1) add_mock.assert_called_once_with(NAME, index=-1, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { 'name': NAME, 'changes': {}, 'result': False, 'comment': 'Encountered error: Global Thermonuclear War. ' 'Failed to move {0} from index -2 to -1.'.format(NAME) } )
def test_exists_change_index_success(self): ''' Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (successful run). ''' add_mock = MagicMock(return_value=True) rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', 'baz', NAME], [NAME, 'foo', 'bar', 'baz'] ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': False} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=0) add_mock.assert_called_once_with(NAME, index=0, rehash=False) self.assert_called_once(rehash_mock) self.assertDictEqual( ret, { 'name': NAME, 'changes': {'index': {'old': 3, 'new': 0}}, 'result': True, 'comment': 'Moved {0} from index 3 to 0.'.format(NAME) } )
def test_exists_add_no_index_success(self): ''' Tests win_path.exists when the directory isn't already in the PATH and no index is specified (successful run). ''' add_mock = MagicMock(return_value=True) rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', 'baz'], ['foo', 'bar', 'baz', NAME] ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': False} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME) add_mock.assert_called_once_with(NAME, index=None, rehash=False) self.assert_called_once(rehash_mock) self.assertDictEqual( ret, { 'name': NAME, 'changes': {'index': {'old': None, 'new': 3}}, 'result': True, 'comment': 'Added {0} to the PATH.'.format(NAME) } )
def test_exists_change_index_success(name): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (successful run). """ add_mock = MagicMock(return_value=True) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", "baz", name], [name, "foo", "bar", "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name, index=0) add_mock.assert_called_once_with(name, index=0, rehash=False) rehash_mock.assert_called_once() assert ret == { "name": name, "changes": { "index": { "old": 3, "new": 0 } }, "result": True, "comment": "Moved {} from index 3 to 0.".format(name), }
def test_exists_change_index_add_exception(name): """ Tests win_path.exists when the directory is already in the PATH but an exception is raised when we attempt to add the key to its new location. """ add_mock = MagicMock(side_effect=Exception("Global Thermonuclear War")) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", "baz", name], ["foo", "bar", "baz", name]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name, index=0) add_mock.assert_called_once_with(name, index=0, rehash=False) rehash_mock.assert_not_called() assert ret == { "name": name, "changes": {}, "result": False, "comment": "Encountered error: Global Thermonuclear War. " "Failed to move {} from index 3 to 0.".format(name), }
def test_exists_add_no_index_failure_exception(self): ''' Tests win_path.exists when the directory isn't already in the PATH and no index is specified (failed run due to exception). ''' add_mock = MagicMock(side_effect=Exception('Global Thermonuclear War')) rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[ ['foo', 'bar', 'baz'], ['foo', 'bar', 'baz'] ]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': False} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME) add_mock.assert_called_once_with(NAME, index=None, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { 'name': NAME, 'changes': {}, 'result': False, 'comment': 'Encountered error: Global Thermonuclear War. ' 'Failed to add {0} to the PATH.'.format(NAME) } )
def test_exists_add_no_index_failure(self): """ Tests win_path.exists when the directory isn't already in the PATH and no index is specified (failed run). """ add_mock = MagicMock(return_value=False) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock( side_effect=[["foo", "bar", "baz"], ["foo", "bar", "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME) add_mock.assert_called_once_with(NAME, index=None, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { "name": NAME, "changes": {}, "result": False, "comment": "Failed to add {0} to the PATH.".format(NAME), }, )
def test_exists_change_negative_index_test_mode(name): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (test mode enabled). """ add_mock = Mock() rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", name, "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": True} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name, index=-1) add_mock.assert_not_called() rehash_mock.assert_not_called() assert ret == { "name": name, "changes": { "index": { "old": -2, "new": -1 } }, "result": None, "comment": "{} would be moved from index -2 to -1.".format(name), }
def test_exists_change_index_failure(self): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (failed run). """ add_mock = MagicMock(return_value=False) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", "baz", NAME], ["foo", "bar", "baz", NAME]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=0) add_mock.assert_called_once_with(NAME, index=0, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { "name": NAME, "changes": {}, "result": False, "comment": "Failed to move {0} from index 3 to 0.".format(NAME), }, )
def test_exists_add_no_index_success(name): """ Tests win_path.exists when the directory isn't already in the PATH and no index is specified (successful run). """ add_mock = MagicMock(return_value=True) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock( side_effect=[["foo", "bar", "baz"], ["foo", "bar", "baz", name]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name) add_mock.assert_called_once_with(name, index=None, rehash=False) rehash_mock.assert_called_once_with() assert ret == { "name": name, "changes": { "index": { "old": None, "new": 3 } }, "result": True, "comment": "Added {} to the PATH.".format(name), }
def test_exists_change_negative_index_failure(name): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (failed run). This tests a negative index. """ add_mock = MagicMock(return_value=False) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", name, "baz"], ["foo", "bar", name, "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name, index=-1) add_mock.assert_called_once_with(name, index=-1, rehash=False) rehash_mock.assert_not_called() assert ret == { "name": name, "changes": {}, "result": False, "comment": "Failed to move {} from index -2 to -1.".format(name), }
def test_exists_add_no_index_failure_exception(name): """ Tests win_path.exists when the directory isn't already in the PATH and no index is specified (failed run due to exception). """ add_mock = MagicMock(side_effect=Exception("Global Thermonuclear War")) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", "baz"], ["foo", "bar", "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(name) add_mock.assert_called_once_with(name, index=None, rehash=False) rehash_mock.assert_not_called() assert ret == { "name": name, "changes": {}, "result": False, "comment": "Encountered error: Global Thermonuclear War. " "Failed to add {} to the PATH.".format(name), }
def test_exists_invalid_index(name): """ Tests win_path.exists when a non-integer index is specified. """ ret = win_path.exists(name, index="foo") assert ret == { "name": name, "changes": {}, "result": False, "comment": "Index must be an integer", }
def test_exists_invalid_index(self): """ Tests win_path.exists when a non-integer index is specified. """ ret = win_path.exists(NAME, index="foo") self.assertDictEqual( ret, { "name": NAME, "changes": {}, "result": False, "comment": "Index must be an integer", }, )
def test_exists_invalid_index(self): ''' Tests win_path.exists when a non-integer index is specified. ''' ret = win_path.exists(NAME, index='foo') self.assertDictEqual( ret, { 'name': NAME, 'changes': {}, 'result': False, 'comment': 'Index must be an integer' } )
def _test_exists_add_already_present(self, index, test_mode): """ Tests win_path.exists when the directory already exists in the PATH. Helper function to test both with and without and index, and with test mode both disabled and enabled. """ current_path = ["foo", "bar", "baz"] if index is None: current_path.append(NAME) else: pos = index if index >= 0 else len(current_path) + index + 1 current_path.insert(pos, NAME) add_mock = Mock() rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[current_path]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": test_mode} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=index) add_mock.assert_not_called() rehash_mock.assert_not_called() self.assertDictEqual( ret, { "name": NAME, "changes": {}, "result": True, "comment": "{0} already exists in the PATH{1}.".format( NAME, " at index {0}".format(index) if index is not None else ""), }, )
def test_exists_change_negative_index_success(self): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (successful run). This tests a negative index. """ add_mock = MagicMock(return_value=True) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", NAME, "baz"], ["foo", "bar", "baz", NAME]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=-1) add_mock.assert_called_once_with(NAME, index=-1, rehash=False) self.assert_called_once(rehash_mock) self.assertDictEqual( ret, { "name": NAME, "changes": { "index": { "old": -2, "new": -1 } }, "result": True, "comment": "Moved {0} from index -2 to -1.".format(NAME), }, )
def _test_exists_add_already_present(self, index, test_mode): ''' Tests win_path.exists when the directory already exists in the PATH. Helper function to test both with and without and index, and with test mode both disabled and enabled. ''' current_path = ['foo', 'bar', 'baz'] if index is None: current_path.append(NAME) else: pos = index if index >= 0 else len(current_path) + index + 1 current_path.insert(pos, NAME) add_mock = Mock() rehash_mock = MagicMock(return_value=True) dunder_salt = { 'win_path.get_path': MagicMock(side_effect=[current_path]), 'win_path.add': add_mock, 'win_path.rehash': rehash_mock, } dunder_opts = {'test': test_mode} with patch.dict(win_path.__salt__, dunder_salt), \ patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=index) add_mock.assert_not_called() rehash_mock.assert_not_called() self.assertDictEqual( ret, { 'name': NAME, 'changes': {}, 'result': True, 'comment': '{0} already exists in the PATH{1}.'.format( NAME, ' at index {0}'.format(index) if index is not None else '' ) } )
def test_exists_change_negative_index_add_exception(self): """ Tests win_path.exists when the directory is already in the PATH but an exception is raised when we attempt to add the key to its new location. This tests a negative index. """ add_mock = MagicMock(side_effect=Exception("Global Thermonuclear War")) rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", NAME, "baz"], ["foo", "bar", NAME, "baz"]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": False} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=-1) add_mock.assert_called_once_with(NAME, index=-1, rehash=False) rehash_mock.assert_not_called() self.assertDictEqual( ret, { "name": NAME, "changes": {}, "result": False, "comment": "Encountered error: Global Thermonuclear War. " "Failed to move {0} from index -2 to -1.".format(NAME), }, )
def test_exists_change_index_test_mode(self): """ Tests win_path.exists when the directory is already in the PATH and needs to be moved to a different position (test mode enabled). """ add_mock = Mock() rehash_mock = MagicMock(return_value=True) dunder_salt = { "win_path.get_path": MagicMock(side_effect=[["foo", "bar", "baz", NAME]]), "win_path.add": add_mock, "win_path.rehash": rehash_mock, } dunder_opts = {"test": True} with patch.dict(win_path.__salt__, dunder_salt), patch.dict(win_path.__opts__, dunder_opts): ret = win_path.exists(NAME, index=0) add_mock.assert_not_called() rehash_mock.assert_not_called() self.assertDictEqual( ret, { "name": NAME, "changes": { "index": { "old": 3, "new": 0 } }, "result": None, "comment": "{0} would be moved from index 3 to 0.".format(NAME), }, )