From 483c91ca22c47e4a907029e8dd2f8d1450a3048d Mon Sep 17 00:00:00 2001 From: abikouo Date: Thu, 20 May 2021 12:23:07 +0200 Subject: [PATCH] attribute should match for all --- plugins/module_utils/jsonpath.py | 2 +- tests/unit/module_utils/test_jsonpath.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/jsonpath.py b/plugins/module_utils/jsonpath.py index a4310683..e4bd7f13 100644 --- a/plugins/module_utils/jsonpath.py +++ b/plugins/module_utils/jsonpath.py @@ -44,7 +44,7 @@ def match_json_property(module, data, expr, value=None): def _match_value(buf, v): if isinstance(buf, list): # convert all values from bool to str and lowercase them - return v.lower() in [str(i).lower() for i in buf] + return all([str(i).lower() == v.lower() for i in buf]) elif isinstance(buf, str): return v.lower() == content.lower() elif isinstance(buf, bool): diff --git a/tests/unit/module_utils/test_jsonpath.py b/tests/unit/module_utils/test_jsonpath.py index aa7fb3f1..7aee8168 100644 --- a/tests/unit/module_utils/test_jsonpath.py +++ b/tests/unit/module_utils/test_jsonpath.py @@ -60,6 +60,24 @@ def test_boolean_value(): assert match_json_property(None, data, "containers[*].connected", "TRUE") assert match_json_property(None, data, "containers[0].poweron", "false") + data = { + "containers": [ + {"image": "nginx", "ready": False}, + {"image": "python", "ready": False}, + {"image": "mongo", "ready": True} + ] + } + assert not match_json_property(None, data, "containers[*].ready", "true") + + data = { + "containers": [ + {"image": "nginx", "ready": True}, + {"image": "python", "ready": True}, + {"image": "mongo", "ready": True} + ] + } + assert match_json_property(None, data, "containers[*].ready", "true") + def test_valid_expression(): data = dict(key="ansible", value="unit-test")