2015-02-03 08:48:42 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-02-03 18:28:03 +00:00
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import time
|
|
|
|
|
2015-02-03 08:48:42 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-02-03 08:48:42 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.ldapmod as ldapmod
|
2015-02-03 08:48:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class LdapmodTestCase(TestCase, LoaderModuleMockMixin):
|
2015-02-03 08:48:42 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.ldapmod
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {ldapmod: {}}
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2015-02-03 08:48:42 +00:00
|
|
|
# 'search' function tests: 1
|
|
|
|
|
|
|
|
def test_search(self):
|
|
|
|
'''
|
|
|
|
Test if it run an arbitrary LDAP query and return the results.
|
|
|
|
'''
|
|
|
|
class MockConnect(object):
|
|
|
|
'''
|
|
|
|
Mocking _connect method
|
|
|
|
'''
|
|
|
|
def __init__(self):
|
|
|
|
self.bdn = None
|
|
|
|
self.scope = None
|
|
|
|
self._filter = None
|
|
|
|
self.attrs = None
|
|
|
|
|
|
|
|
def search_s(self, bdn, scope, _filter, attrs):
|
|
|
|
'''
|
|
|
|
Mock function for search_s
|
|
|
|
'''
|
|
|
|
self.bdn = bdn
|
|
|
|
self.scope = scope
|
|
|
|
self._filter = _filter
|
|
|
|
self.attrs = attrs
|
|
|
|
return 'SALT'
|
|
|
|
|
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.dict(ldapmod.__salt__, {'config.option': mock}):
|
|
|
|
with patch.object(ldapmod, '_connect',
|
|
|
|
MagicMock(return_value=MockConnect())):
|
|
|
|
with patch.object(time, 'time', MagicMock(return_value=8e-04)):
|
|
|
|
self.assertDictEqual(ldapmod.search(filter='myhost'),
|
|
|
|
{'count': 4, 'results': 'SALT',
|
|
|
|
'time': {'raw': '0.0',
|
|
|
|
'human': '0.0ms'}})
|