tripleo-ci/scripts/emit_releases_file/test_yaml_parsing.py
Sorin Sbarnea 04e2fcf4d8 replace pep8 check with generic linters
Includes removal of file pattern from pep8 job which prevented
it from running on most changes. Linters will use multiple tools
and a pattern would make it fail to spot errors.

This check should run even if no files are changed, so it would
be able to check commit messages.

There are no real load implications because linters check is
just a simple tox execution which has minimal resource requirements.

Includes reactivaction or linting which was not running on
the entire repository. This required few minor fixes.

Partial-Bug: #1786286
Change-Id: I4d4279309af55c2663e98bd0cdec9224f03c1fa0
2018-08-29 19:03:09 +01:00

79 lines
3.0 KiB
Python

from emit_releases_file import load_featureset_file
import mock
import pytest
from six import PY2
import yaml
if PY2:
BUILTINS_OPEN = "__builtin__.open"
else:
BUILTINS_OPEN = "builtins.open"
@mock.patch('yaml.safe_load')
@mock.patch('logging.getLogger')
def test_featureset_file_with_bad_file_path(mock_logging, mock_yaml):
mock_logger = mock.MagicMock()
mock_logging.return_value = mock_logger
mock_log_exception = mock.MagicMock()
mock_log_error = mock.MagicMock()
mock_logger.exception = mock_log_exception
mock_logger.error = mock_log_error
bad_file_exception = IOError("Dude where's my YAML!")
mo = mock.mock_open()
with pytest.raises(IOError):
with mock.patch(BUILTINS_OPEN, mo, create=True) as mock_file:
mock_file.side_effect = bad_file_exception
featureset = load_featureset_file('some_non_existent.yaml')
mock_yaml.assert_not_called()
mock_file.assert_called_with('some_non_existent.yaml', 'r')
mock_logging.assert_called_with('emit-releases')
mock_log_error.assert_called()
mock_log_exception.assert_called_with(bad_file_exception)
assert featureset is None
@mock.patch('yaml.safe_load')
@mock.patch('logging.getLogger')
def test_featureset_file_with_bad_yaml(mock_logging, mock_yaml):
mock_logger = mock.MagicMock()
mock_logging.return_value = mock_logger
mock_log_exception = mock.MagicMock()
mock_log_error = mock.MagicMock()
mock_logger.exception = mock_log_exception
mock_logger.error = mock_log_error
mo = mock.mock_open()
mock_yaml.side_effect = yaml.YAMLError()
with pytest.raises(yaml.YAMLError):
with mock.patch(BUILTINS_OPEN, mo, create=True) as mock_file:
featureset = load_featureset_file('some_badly_formatted.yaml')
mock_yaml.assert_called()
mock_file.assert_called_with('some_badly_formatted.yaml', 'r')
mock_logging.assert_called_with('emit-releases')
mock_log_exception.assert_called()
mock_log_error.assert_called()
assert featureset is None
@mock.patch('yaml.safe_load')
@mock.patch('logging.getLogger')
def test_featureset_file_loaded_ok(mock_logging, mock_yaml):
mock_logger = mock.MagicMock()
mock_logging.return_value = mock_logger
mock_log_exception = mock.MagicMock()
mock_log_error = mock.MagicMock()
mock_logger.exception = mock_log_exception
mock_logger.error = mock_log_error
ok_yaml_dict = {'some_featureset_keys': 'some_featureset_values'}
mock_yaml.return_value = ok_yaml_dict
mo = mock.mock_open()
with mock.patch(BUILTINS_OPEN, mo, create=True) as mock_file:
featureset = load_featureset_file('featureset999.yaml')
mock_file.assert_called_with('featureset999.yaml', 'r')
mock_yaml.assert_called()
mock_log_exception.assert_not_called()
mock_log_error.assert_not_called()
assert featureset == ok_yaml_dict