Skip to content

Commit

Permalink
mutest: fail tests with any variation of bad json data
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hopps <chopps@labn.net>
  • Loading branch information
choppsv1 committed Mar 21, 2024
1 parent 689dc5d commit 5daeb47
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion munet/mutest/userapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def _command_json(
try:
js = json.loads(out)
except Exception as error:
js = {}
js = None
self.olog.warning(
"JSON load failed. Check command output is in JSON format: %s",
error,
Expand Down Expand Up @@ -482,13 +482,17 @@ def _match_command_json(
exact_match: if True then the json must exactly match.
"""
js = self._command_json(target, cmd)
if js is None:
return expect_fail, {}

try:
expect = json.loads(match)
except Exception as error:
expect = {}
self.olog.warning(
"JSON load failed. Check match value is in JSON format: %s", error
)
return expect_fail, {}

if exact_match:
deep_diff = json_cmp(expect, js)
Expand Down
61 changes: 61 additions & 0 deletions tests/mutest/mutest_matchwait_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from munet.mutest.userapi import match_step_json
from munet.mutest.userapi import section
from munet.mutest.userapi import step_json
from munet.mutest.userapi import test_step
from munet.mutest.userapi import wait_step_json


Expand Down Expand Up @@ -315,3 +316,63 @@
expect_fail=True,
exact_match=False,
)

section("Test json errors")

jsonblank = '{}'
jsongood = '{"foo":"bar"}'
jsonbad = '{"bad":"trailing-comma",}'

_, ret = match_step_json(
"r1",
f"echo '{jsongood}'",
jsongood,
"Output json is good, match json is good, expect pass",
)

_, ret = match_step_json(
"r1",
f"echo '{jsonbad}'",
jsongood,
"Output json is bad, match json is good, fail is pass",
expect_fail=True,
)

_, ret = match_step_json(
"r1",
f"echo '{jsongood}'",
jsonblank,
"Output json is good, match json is blank, expect pass",
)

_, ret = match_step_json(
"r1",
f"echo '{jsonbad}'",
jsonblank,
"Output json is bad, match json is blank, fail is pass",
expect_fail=True,
)

_, ret = match_step_json(
"r1",
f"echo '{jsongood}'",
jsonbad,
"Output json is good, match json is bad, fail is pass",
expect_fail=True,
)

_, ret = match_step_json(
"r1",
f"echo '{jsonblank}'",
jsonbad,
"Output json is blank, match json is bad, fail is pass",
expect_fail=True,
)

_, ret = match_step_json(
"r1",
f"echo '{jsonbad}'",
jsonbad,
"Output json and match json are bad, fail is pass",
expect_fail=True,
)

0 comments on commit 5daeb47

Please sign in to comment.