Abstract instruction tests - parse(), parse_condition()

This commit is contained in:
Emmanuel BENOîT 2022-10-01 14:38:43 +02:00
parent 44253014de
commit 3431f2b3f2
No known key found for this signature in database
GPG key ID: 2356DC6956CF54EF

View file

@ -1,6 +1,7 @@
"""Tests for the instruction base class.""" """Tests for the instruction base class."""
import pytest import pytest
from unittest import mock from unittest import mock
from ansible.errors import AnsibleParserError
from . import reconstructed from . import reconstructed
@ -166,3 +167,117 @@ class TestDump:
assert rv[-1] == _INSTR_REPR assert rv[-1] == _INSTR_REPR
assert "{var a=1}" in rv assert "{var a=1}" in rv
assert "{var b=2}" in rv assert "{var b=2}" in rv
# ------------------------------------------------------------------------------
class TestParse:
"""Tests for the main ``parse()`` method."""
@pytest.fixture
def instr(self):
"""Create a mock instruction suitable for testing the ``parse()`` method."""
instr = _Instruction(
mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), "stop"
)
instr.parse_condition = mock.MagicMock()
instr.parse_loop = mock.MagicMock()
instr.parse_vars = mock.MagicMock()
instr.parse_vars.return_value.keys.return_value = [
instr.parse_vars.return_value.keys.return_value
]
instr.parse_run_once = mock.MagicMock()
instr.parse_action = mock.MagicMock()
return instr
def test_record_without_action(self, instr):
"""An assertion rejects calls in which the record does not contain an \
action."""
with pytest.raises(AssertionError):
instr.parse({})
def test_record_with_mismatch_action(self, instr):
"""An assertion rejects calls in which the record contains an action \
that is different from what the class can handle."""
with pytest.raises(AssertionError):
instr.parse({"action": instr._action + "nope"})
def test_record_with_unknown_fields(self, instr):
"""Unknown fields in the input record cause an Ansible parser error."""
field = "not a valid field name anyway"
with pytest.raises(AnsibleParserError):
instr.parse({"action": instr._action, field: 1})
def test_action_only(self, instr):
"""Records with only the ``action`` field get parsed."""
record = {"action": instr._action}
#
instr.parse(record)
#
instr.parse_condition.assert_called_once_with(record)
instr.parse_loop.assert_called_once_with(record)
instr.parse_vars.assert_called_once_with(record)
assert instr._vars == instr.parse_vars.return_value
instr.parse_run_once.assert_called_once_with(record)
assert instr._save == tuple(instr._vars.keys.return_value)
instr.parse_action.assert_called_once_with(record)
@pytest.mark.parametrize("action", list(reconstructed.INSTR_OWN_FIELDS.keys()))
def test_known_fields(self, instr, action):
"""Records with only known fields get parsed."""
instr._action = action
record = {"action": action}
for field in reconstructed.INSTR_COMMON_FIELDS:
if field != "action":
record[field] = True
for field in reconstructed.INSTR_OWN_FIELDS[action]:
record[field] = True
#
instr.parse(record)
#
instr.parse_condition.assert_called_once_with(record)
instr.parse_loop.assert_called_once_with(record)
instr.parse_vars.assert_called_once_with(record)
assert instr._vars == instr.parse_vars.return_value
instr.parse_run_once.assert_called_once_with(record)
assert instr._save == tuple(instr._vars.keys.return_value)
instr.parse_action.assert_called_once_with(record)
def test_save_loop_var(self, instr):
"""If a loop variable is defined, it must be saved."""
record = {"action": instr._action}
instr._loop = []
instr._loop_var = "test"
#
instr.parse(record)
#
instr.parse_condition.assert_called_once_with(record)
instr.parse_loop.assert_called_once_with(record)
instr.parse_vars.assert_called_once_with(record)
assert instr._vars == instr.parse_vars.return_value
instr.parse_run_once.assert_called_once_with(record)
assert instr._save == tuple(instr._vars.keys.return_value) + (instr._loop_var,)
instr.parse_action.assert_called_once_with(record)
class TestParseCondition:
"""Tests for the ``parse_condition()`` method."""
def test_no_condition(self, instr):
"""Records that do not contain a ``when`` field do not set the condition."""
instr.parse_condition({})
assert instr._condition is None
def test_invalid_condition(self, instr):
"""Records that contain a ``when`` field that isn't a string cause a \
parse error and do not set the condition."""
with pytest.raises(AnsibleParserError):
instr.parse_condition({"when": ()})
assert instr._condition is None
def test_condition(self, instr):
"""Records that contain a ``when`` field that is a string set the condition."""
cond = "test"
instr.parse_condition({"when": cond})
assert instr._condition == cond