-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from TomasTomecek/fix-101
workaround ansible bug with selinux on and...
- Loading branch information
Showing
2 changed files
with
93 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
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,65 @@ | ||
import importlib | ||
from functools import partial | ||
from pathlib import Path | ||
|
||
import pytest | ||
from flexmock import flexmock | ||
|
||
from ansible_bender.core import PbVarsParser | ||
|
||
|
||
def mock_read_text(return_val=None, raise_exc=False): | ||
if raise_exc: | ||
def _f(): | ||
raise FileNotFoundError() | ||
flexmock(Path, read_text=_f) | ||
else: | ||
flexmock(Path, read_text=lambda: return_val) | ||
|
||
|
||
def mock_import_module(raise_exc=False): | ||
if raise_exc: | ||
def _f(name, package=None): | ||
raise ModuleNotFoundError() | ||
flexmock(importlib, import_module=_f) | ||
else: | ||
flexmock(importlib, import_module=lambda name: None) | ||
|
||
|
||
@pytest.mark.parametrize("mock_r_t,mock_i_m,should_raise", ( | ||
( | ||
partial(mock_read_text, "1"), | ||
partial(mock_import_module, False), | ||
False | ||
), | ||
( | ||
partial(mock_read_text, "1"), | ||
partial(mock_import_module, True), | ||
True | ||
), | ||
( | ||
partial(mock_read_text, "0"), | ||
partial(mock_import_module, False), | ||
False | ||
), | ||
( | ||
partial(mock_read_text, "0"), | ||
partial(mock_import_module, True), | ||
True | ||
), | ||
( | ||
partial(mock_read_text, None, True), | ||
partial(mock_import_module, False), | ||
False | ||
), | ||
)) | ||
def test_ansible_selinux_workaround(mock_r_t, mock_i_m, should_raise): | ||
mock_r_t() | ||
mock_i_m() | ||
p = PbVarsParser("") | ||
if should_raise: | ||
with pytest.raises(RuntimeError) as ex: | ||
p._check_selinux_iz_gud() | ||
assert "libselinux" in str(ex.value) | ||
else: | ||
p._check_selinux_iz_gud() |