Skip to content

Commit

Permalink
Add get_state_names() to ActorState enum
Browse files Browse the repository at this point in the history
This should fix an issue in which Python 3.10
does not provide a name for states with more
than one flag set.
  • Loading branch information
albireox committed Jan 23, 2024
1 parent f6a96cb commit 1c1e8e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/lvmopstools/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class ActorState(enum.Flag):
NOT_READY = TROUBLESHOOTING | TROUBLESHOOT_FAILED | RESTARTING
SKIP_CHECK = CHECKING | TROUBLESHOOTING | RESTARTING

def get_state_names(self):
"""Returns the state names that are set."""

return [state.name for state in self.__class__ if self & state]


@dataclass
class ErrorData:
Expand Down Expand Up @@ -140,7 +145,7 @@ async def actor_state(command: Command[LVMActor], *args, **kwargs):

state = command.actor.state
code = state.value
flags = state.name.split("|")
flags = state.get_state_names()

if (model := command.actor.model) is not None:
state_kw = model["state"]
Expand Down Expand Up @@ -280,7 +285,7 @@ def update_state(
"d",
state={
"code": self.state.value,
"flags": self.state.name.split("|"),
"flags": self.state.get_state_names(),
"error": error_data,
},
internal=internal,
Expand Down
8 changes: 7 additions & 1 deletion tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def test_get_error_codes_not_valid():

@pytest.mark.parametrize(
"side_effect",
[ValueError("Test error"), CheckError("Test error", ErrorCodes.UNKNOWN)],
[
ValueError("Test error"),
CheckError("Test error", ErrorCodes.UNKNOWN),
CheckError("Test error", 9999),
],
)
async def test_actor_check_fails(lvm_actor: LVMActor, mocker, side_effect: Exception):
lvm_actor._check_internal = mocker.AsyncMock(side_effect=side_effect)
Expand All @@ -101,6 +105,8 @@ async def test_actor_check_fails(lvm_actor: LVMActor, mocker, side_effect: Excep
assert replies[2]["state"]["code"] & ActorState.TROUBLESHOOTING.value
assert replies[3]["state"]["code"] & ActorState.READY.value

assert replies[1]["state"]["flags"] == ["RUNNING", "READY"]


async def test_actor_restart(lvm_actor: LVMActor, mocker: MockerFixture):
lvm_actor.restart_after = 2
Expand Down

0 comments on commit 1c1e8e3

Please sign in to comment.