forked from saltstack/salt
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'af7593ae53b3f7902cad2e14aee0850dc57c2966' into develop
* commit 'af7593ae53b3f7902cad2e14aee0850dc57c2966': Introduce a lightweight unit test runner for execution modules (saltstack#32792)
- Loading branch information
Showing
3 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |