This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
''' | ||
Execute Unit, Integration and Functional tests | ||
''' | ||
|
||
import unittest | ||
import sys | ||
|
||
def run_unittests(): | ||
''' Execute Unit Tests ''' | ||
tests = unittest.TestLoader().discover('tests/unit') | ||
result = unittest.TextTestRunner(verbosity=2).run(tests) | ||
return result.wasSuccessful() | ||
|
||
def run_integration_tests(): | ||
''' Execute Integration Tests ''' | ||
tests = unittest.TestLoader().discover('tests/integration') | ||
result = unittest.TextTestRunner(verbosity=2).run(tests) | ||
return result.wasSuccessful() | ||
|
||
def run_functional_tests(): | ||
''' Execute Functional Tests ''' | ||
tests = unittest.TestLoader().discover('tests/functional') | ||
result = unittest.TextTestRunner(verbosity=2).run(tests) | ||
return result.wasSuccessful() | ||
|
||
if __name__ == '__main__': | ||
unit_results = run_unittests() | ||
integration_results = run_integration_tests() | ||
functional_results = run_functional_tests() | ||
if unit_results and integration_results and functional_results: | ||
sys.exit(0) | ||
else: | ||
sys.exit(1) |
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,77 @@ | ||
''' | ||
Test Runbooks.py cache_runbooks() | ||
''' | ||
|
||
import mock | ||
import unittest | ||
|
||
from runbooks import cache_runbooks | ||
|
||
|
||
class CacheRunbooksTest(unittest.TestCase): | ||
''' Run unit tests against the cache_runbooks method ''' | ||
|
||
def setUp(self): | ||
''' Setup mocked data ''' | ||
self.config = mock.Mock() | ||
self.logger = mock.Mock(**{ | ||
'info.return_value' : True, | ||
'debug.return_value' : True, | ||
'critical.return_value' : True, | ||
'warn.return_value' : True, | ||
'error.return_value' : True | ||
}) | ||
|
||
def tearDown(self): | ||
''' Destroy mocked data ''' | ||
self.config = None | ||
self.logger = None | ||
|
||
class RunwithNoFile(CacheRunbooksTest): | ||
''' Test when no init.yml file is found ''' | ||
@mock.patch('runbooks.open', create=True) | ||
@mock.patch('runbooks.os.path.isfile') | ||
@mock.patch('runbooks.yaml.safe_load') | ||
def runTest(self, mock_yaml, mock_isfile, mock_open): | ||
''' Execute test ''' | ||
self.config = mock.MagicMock(spec_set={'runbook_path' : '/path/'}) | ||
mock_isfile.return_value = False | ||
mock_yaml.return_value = None | ||
self.assertEqual(cache_runbooks(self.config, self.logger), {}) | ||
self.assertFalse(mock_open.called) | ||
|
||
class RunwithEmptyFile(CacheRunbooksTest): | ||
''' Test with empty init.yml file ''' | ||
@mock.patch('runbooks.open', create=True) | ||
@mock.patch('runbooks.os.path.isfile') | ||
@mock.patch('runbooks.yaml.safe_load') | ||
def runTest(self, mock_yaml, mock_isfile, mock_open): | ||
''' Execute test ''' | ||
self.config = mock.MagicMock(spec_set={'runbook_path' : '/path/'}) | ||
mock_isfile.return_value = True | ||
mock_yaml.return_value = None | ||
mock_open.return_value = mock.MagicMock(spec=file) | ||
self.assertEqual(cache_runbooks(self.config, self.logger), {}) | ||
self.assertTrue(mock_open.called) | ||
self.assertTrue(mock_yaml.called) | ||
|
||
class RunwithYMLFile(CacheRunbooksTest): | ||
''' Test with a valid YML file ''' | ||
@mock.patch('runbooks.open', create=True) | ||
@mock.patch('runbooks.os.path.isfile') | ||
@mock.patch('runbooks.os.path.isdir') | ||
@mock.patch('runbooks.yaml.safe_load') | ||
def runTest(self, mock_yaml, mock_isdir, mock_isfile, mock_open): | ||
''' Execute test ''' | ||
self.config = mock.MagicMock(spec_set={'runbook_path' : '/path/'}) | ||
mock_isfile.return_value = True | ||
mock_isdir.return_value = True | ||
mock_yaml.return_value = {'*':['book'], 'target':['book1', 'book2']} | ||
mock_open.return_value = mock.MagicMock(spec=file) | ||
result = cache_runbooks(self.config, self.logger) | ||
self.assertEqual(result.keys(), ['*', 'target'], "Expected dictionary keys not found") | ||
self.assertTrue(mock_open.called, "open not called") | ||
self.assertTrue(mock_yaml.called, "yaml.safe_load not called") | ||
self.assertTrue(mock_isdir.called, "os.path.isdir not called") | ||
self.assertEqual(mock_isfile.call_count, 4, | ||
"mock_open.call_count {0} is not 4".format(mock_open.call_count)) |
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,76 @@ | ||
''' | ||
Test Runbooks.py rediscover() | ||
''' | ||
|
||
import mock | ||
import unittest | ||
|
||
from runbooks import rediscover | ||
|
||
|
||
class RediscoverTest(unittest.TestCase): | ||
''' Run unit tests against the Rediscover method ''' | ||
|
||
def setUp(self): | ||
''' Setup mocked data ''' | ||
self.config = mock.Mock() | ||
self.dbc = mock.Mock() | ||
self.logger = mock.Mock(**{ | ||
'info.return_value' : True, | ||
'debug.return_value' : True, | ||
'critical.return_value' : True, | ||
'warn.return_value' : True, | ||
'error.return_value' : True | ||
}) | ||
|
||
def tearDown(self): | ||
''' Destroy mocked data ''' | ||
self.config = None | ||
self.dbc = None | ||
self.logger = None | ||
|
||
class RunwithNoTargets(RediscoverTest): | ||
''' Test when no targets are found in Redis ''' | ||
def runTest(self): | ||
''' Execute test ''' | ||
self.config = mock.Mock() | ||
attr = { | ||
'pop_target.return_value' :{}, | ||
'new_discovery.return_value' : True | ||
} | ||
self.dbc.configure_mock(**attr) | ||
self.assertEqual(rediscover(self.config, self.dbc, self.logger), 0) | ||
|
||
|
||
class RunwithBasicTargets(RediscoverTest): | ||
''' Test with a basic Target dictionary ''' | ||
def runTest(self): | ||
''' Execute test ''' | ||
self.config = mock.Mock() | ||
attr = { | ||
'pop_target.return_value' :{ | ||
'target1' : { 'ip' : "127.0.0.1" }, | ||
'target2' : { 'ip' : "127.0.0.1" }, | ||
}, | ||
'new_discovery.return_value' : True | ||
} | ||
self.dbc.configure_mock(**attr) | ||
self.assertEqual(rediscover(self.config, self.dbc, self.logger), 2) | ||
self.assertTrue(self.dbc.new_discovery.called, "Function new_discovery was not called") | ||
|
||
|
||
|
||
class RunwithFailedNewDiscoveryCall(RediscoverTest): | ||
''' Test with a Failed call to New Discovery ''' | ||
def runTest(self): | ||
''' Execute test ''' | ||
self.config = mock.Mock() | ||
attr = { | ||
'pop_target.return_value' :{ | ||
'target1' : { 'ip' : "127.0.0.1" }, | ||
'target2' : { 'ip' : "127.0.0.1" }, | ||
}, | ||
'new_discovery.side_effect' : Exception | ||
} | ||
self.dbc.configure_mock(**attr) | ||
self.assertEqual(rediscover(self.config, self.dbc, self.logger), 0) |