def test_load_file_type_check(loader): with patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') as mock_get_contents: mock_get_contents.return_value = '---\ntest: string' assert not loader._cache # type check passes res = loader.load_file('/tmp/test', dict) assert res is not None mock_get_contents.reset_mock() mock_get_contents.return_value = 'test string' loader._cache = {} # type check fails with raises(ConfigurationError): res = loader.load_file('/tmp/test', dict) assert res is not None
def test_load_file_text(loader): with patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') as mock_get_contents: mock_get_contents.return_value = 'test string' assert not loader._cache # cache miss res = loader.load_file('/tmp/test') assert mock_get_contents.called assert mock_get_contents.called_with_args('/tmp/test') assert res == 'test string' assert '/tmp/test' in loader._cache mock_get_contents.reset_mock() # cache hit res = loader.load_file('/tmp/test') assert not mock_get_contents.called assert res == 'test string' assert '/tmp/test' in loader._cache
def test_load_file_type_check(loader, mocker, tmp_path): mock_get_contents = mocker.patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') mock_get_contents.return_value = '---\ntest: string' assert not loader._cache testfile = tmp_path.joinpath('test').as_posix() # type check passes res = loader.load_file(testfile, dict) assert res is not None mock_get_contents.reset_mock() mock_get_contents.return_value = 'test string' loader._cache = {} # type check fails with raises(ConfigurationError): res = loader.load_file(testfile, dict) assert res is not None
def test_load_file_json(loader): with patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') as mock_get_contents: mock_get_contents.return_value = '---\ntest: string' assert not loader._cache res = loader.load_file('/tmp/test') assert mock_get_contents.called assert mock_get_contents.called_with_args('/tmp/test') assert '/tmp/test' in loader._cache assert res['test'] == 'string'
def test_load_file_json(loader, mocker, tmp_path): mock_get_contents = mocker.patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') mock_get_contents.return_value = '---\ntest: string' assert not loader._cache testfile = tmp_path.joinpath('test').as_posix() res = loader.load_file(testfile) assert mock_get_contents.called assert mock_get_contents.called_with_args(testfile) assert testfile in loader._cache assert res['test'] == 'string'
def test_load_file_text_cache_hit(loader, mocker, tmp_path): mock_get_contents = mocker.patch.object(ansible_runner.loader.ArtifactLoader, 'get_contents') mock_get_contents.return_value = 'test\nstring' assert not loader._cache testfile = tmp_path.joinpath('test').as_posix() # cache miss res = loader.load_file(testfile, string_types) assert mock_get_contents.called assert mock_get_contents.called_with_args(testfile) assert res == b'test\nstring' assert testfile in loader._cache mock_get_contents.reset_mock() # cache hit res = loader.load_file(testfile, string_types) assert not mock_get_contents.called assert res == b'test\nstring' assert testfile in loader._cache