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

Commit

Permalink
Fixing handling of malformed runbooks/init.yml files
Browse files Browse the repository at this point in the history
  • Loading branch information
madflojo committed Jun 13, 2017
1 parent 9e0c0ec commit c165500
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
23 changes: 13 additions & 10 deletions runbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ def cache_runbooks(config, logger):
if runbooks:
for target in runbooks:
all_books[target] = {}
for books in runbooks[target]:
logger.debug("Processing book: {0}".format(books))
book_path = "{0}/{1}".format(config['runbook_path'], books)
if os.path.isdir(book_path):
book_path = book_path + "/init.yml"
if os.path.isfile(book_path) is False:
logger.warn("Runbook File Error: {0} is not a file".format(book_path))
else:
with open(book_path) as bh:
all_books[target][books] = bh.read()
if runbooks[target]:
for books in runbooks[target]:
logger.debug("Processing book: {0}".format(books))
book_path = "{0}/{1}".format(config['runbook_path'], books)
if os.path.isdir(book_path):
book_path = book_path + "/init.yml"
if os.path.isfile(book_path) is False:
logger.warn("Runbook File Error: {0} is not a file".format(book_path))
else:
with open(book_path) as bh:
all_books[target][books] = bh.read()
else:
logger.warn("Error: No runbooks specified under {0}".format(target))
return all_books

def render_runbooks(runbook, facts):
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/test_runbooks_cache_runbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,46 @@ def runTest(self, mock_template, mock_yaml, mock_isfile, mock_open):
self.config = mock.MagicMock(spec_set={'runbook_path' : '/path/'})
mock_isfile.return_value = True
mock_yaml.return_value = None
mock_Template = mock.MagicMock(**{
mock_template = mock.MagicMock(**{
'render.return_value' : ""
})
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 RunwithNoBooks(CacheRunbooksTest):
''' Test with a partially created init.yml file '''
@mock.patch('runbooks.open', create=True)
@mock.patch('runbooks.os.path.isfile')
@mock.patch('runbooks.yaml.load')
@mock.patch('runbooks.Template')
def runTest(self, mock_template, 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_template = mock.MagicMock(**{
'render.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.load')
@mock.patch('runbooks.Template')
def runTest(self, mock_Template, mock_yaml, mock_isdir, mock_isfile, mock_open):
def runTest(self, mock_template, 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_Template = mock.MagicMock(**{
mock_template = mock.MagicMock(**{
'render.return_value' : """
'*':
- book
Expand Down

0 comments on commit c165500

Please sign in to comment.