From 3c6d68fa97e0adcace6d1ec9b1a71cc618d5446b Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Fri, 18 Nov 2022 18:51:09 +0200 Subject: [PATCH] mqtt: fix unexpected behaviour Currently mqtt_response with the unexpected key set to true fails if no message is received. This is strange, was we don't expect a message, and should only fail if a message is received. Formatted stage: mqtt_publish: payload: 10.10.10.10 topic: inet6/add mqtt_response: payload: !anything '' timeout: 5 topic: vallumd/will unexpected: true Errors: E tavern.util.exceptions.TestFailError: Test 'add IPv4 IP to IPv6 ipset' failed: - Expected '' on topic 'vallumd/will' but no such message received Fix this by adding an extra check for the expected key when we received no message. Signed-off-by: Stijn Tintel --- tavern/_plugins/mqtt/response.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tavern/_plugins/mqtt/response.py b/tavern/_plugins/mqtt/response.py index 71c29702c..02bf55d12 100644 --- a/tavern/_plugins/mqtt/response.py +++ b/tavern/_plugins/mqtt/response.py @@ -181,11 +181,12 @@ def addwarning(w, *args, **kwargs): self._maybe_run_validate_functions(msg) else: - self._adderr( - "Expected '%s' on topic '%s' but no such message received", - expected_payload, - topic, - ) + if not self.expected.get("unexpected"): + self._adderr( + "Expected '%s' on topic '%s' but no such message received", + expected_payload, + topic, + ) if self.errors: if warnings: @@ -198,6 +199,9 @@ def addwarning(w, *args, **kwargs): saved = {} + if not msg: + return saved + saved.update(self.maybe_get_save_values_from_save_block("json", msg.payload)) saved.update(self.maybe_get_save_values_from_ext(msg, self.expected))