Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madflojo committed May 27, 2016
1 parent 2b6633f commit 49df924
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests.py
@@ -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)
77 changes: 77 additions & 0 deletions tests/unit/test_runbooks_cache_runbooks.py
@@ -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))
76 changes: 76 additions & 0 deletions tests/unit/test_runbooks_rediscover.py
@@ -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)

0 comments on commit 49df924

Please sign in to comment.