Skip to content

Commit

Permalink
Merge commit 'af7593ae53b3f7902cad2e14aee0850dc57c2966' into develop
Browse files Browse the repository at this point in the history
* commit 'af7593ae53b3f7902cad2e14aee0850dc57c2966':
  Introduce a lightweight unit test runner for execution modules (saltstack#32792)
  • Loading branch information
jojohans committed May 2, 2016
2 parents d0fb75e + af7593a commit d4cce99
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
10 changes: 5 additions & 5 deletions salt/modules/servicenow.py
Expand Up @@ -4,8 +4,8 @@
.. versionadded:: Carbon
:depends: servicenow_rest python module
:configuration: Configure this module by specifying the name of a configuration
profile in the minion config, minion pillar, or master config. The module
will use the 'servicenow' key by default, if defined.
Expand All @@ -19,15 +19,15 @@
username: ''
password: ''
'''

# Import python libs
from __future__ import absolute_import
import logging

# Import third party libs
HAS_LIBS = False
try:
import servicenow_rest.api as api_client
from servicenow_rest.api import Client

HAS_LIBS = True
except ImportError:
pass
Expand All @@ -36,7 +36,7 @@

__virtualname__ = 'servicenow'

SERVICE_NAME = "servicenow"
SERVICE_NAME = 'servicenow'


def __virtual__():
Expand All @@ -54,7 +54,7 @@ def _get_client():
instance_name = config['instance_name']
username = config['username']
password = config['password']
return api_client.Client(instance_name, username, password)
return Client(instance_name, username, password)


def set_change_request_state(change_id, state='approved'):
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/__init__.py
@@ -1 +1,78 @@
# -*- coding: utf-8 -*-
'''
A lightweight version of tests.integration for testing of unit tests
This test class will not import the salt minion, runner and config modules.
'''
from salttesting.case import TestCase
from salttesting.parser import SaltTestcaseParser

__all__ = ['run_tests', 'ModuleTestCase']


def run_tests(*test_cases, **kwargs):
'''
Run unit tests for the chosen test cases.
:param test_cases: The list of test cases to execute
:type test_cases: ``list`` of :class:`TestCase`
'''
parser = SaltTestcaseParser()
parser.parse_args()
for case in test_cases:
if parser.run_testcase(case) is False:
parser.finalize(1)
parser.finalize(0)


def hasDependency(module, fake_module=None):
'''
Use this function in your test class setUp to
mock modules into your namespace
:param module: The module name
:type module: ``str``
:param fake_module: The module to inject into sys.modules
if not provided, a mock will be injected
:type fake_module: ``object``
..
hasDependency('super_module')
'''
import mock
import sys
if fake_module is None:
fake_module = mock.MagicMock()
sys.modules[module] = fake_module


class MockLoader(object):
'''
The default replacement for __salt__'s loader
class.
'''
def set_result(self, module, key, func):
if module.__salt__ is None:
module.__salt__ = {}
module.__salt__[key] = func


class ModuleTestCase(TestCase):
'''
A base class for test cases of execution modules
..
class MyModuleTestCase(ModuleTestCase):
def setUp(self):
self.setup_loader()
'''
# Set this class-level attribute to change
# the loader behavior
loaderCls = MockLoader

def setup_loader(self):
'''
Instantiate a loader to your test case
'''
self.loader = self.loaderCls()
57 changes: 57 additions & 0 deletions tests/unit/modules/servicenow_test.py
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
'''

# Import Python Libs
from __future__ import absolute_import

# Import Salt Testing Libs
from salttesting import skipIf
from tests.unit import ModuleTestCase, hasDependency
from salttesting.mock import (
patch,
NO_MOCK,
NO_MOCK_REASON
)
from salttesting.helpers import ensure_in_syspath
from salt.modules import servicenow

ensure_in_syspath('../../')

SERVICE_NAME = 'servicenow'
servicenow.__salt__ = {}


class MockServiceNowClient(object):
def __init__(self, instance_name, username, password):
pass


@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('servicenow_rest.api.Client', MockServiceNowClient)
class ServiceNowModuleTestCase(ModuleTestCase):
def setUp(self):
hasDependency('servicenow_rest')
servicenow.Client = MockServiceNowClient

def get_config(service):
if service == SERVICE_NAME:
return {
'instance_name': 'test',
'username': 'mr_test',
'password': 'test123'
}
else:
raise KeyError("service name invalid")

self.setup_loader()
self.loader.set_result(servicenow, 'config.option', get_config)

def test_module_creation(self):
client = servicenow._get_client()
self.assertFalse(client is None)

if __name__ == '__main__':
from unit import run_tests
run_tests(ServiceNowModuleTestCase)

0 comments on commit d4cce99

Please sign in to comment.