From 7d4b8e08d45d4083f7136397f093a020e31e7767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Fri, 3 Aug 2018 15:20:12 +0200 Subject: [PATCH] core.parameters: Consider same-path origin as the same origin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently we compare the full TreeNodeEnvironment object to make sure they come from the same origin, which only works for connected tree. But Avocado supports (and uses in json-load) passing leaves of unconnected trees, but then the TreeNodeEnvironments are not matching. Let's rely only on "TreeNodeEnvironment.path" and declare these matching environments, no matter what values are stored (see selftest for details) Signed-off-by: Lukáš Doktor --- avocado/core/parameters.py | 3 ++- selftests/unit/test_parameters.py | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/avocado/core/parameters.py b/avocado/core/parameters.py index c11d407e..a45b8867 100644 --- a/avocado/core/parameters.py +++ b/avocado/core/parameters.py @@ -250,7 +250,8 @@ class AvocadoParam(object): if not ret: raise NoMatchError("No matches to %s => %s in %s" % (path.pattern, key, self.str_leaves_variant)) - if len(set([_[1] for _ in ret])) == 1: # single source of results + # make sure all params come from the same origin + if len(set([_[1].path for _ in ret])) == 1: return ret[0][0] else: raise ValueError("Multiple %s leaves contain the key '%s'; %s" diff --git a/selftests/unit/test_parameters.py b/selftests/unit/test_parameters.py index 11910200..3f65febb 100644 --- a/selftests/unit/test_parameters.py +++ b/selftests/unit/test_parameters.py @@ -21,3 +21,35 @@ class Parameters(unittest.TestCase): 'foo/') self.assertEqual(params._greedy_path_to_re('/foo/*').pattern, '/foo/') + + def test_same_origin_of_different_nodes(self): + # ideally we have one tree, therefor shared key + # have identical origin (id of the origin env) + foo = tree.TreeNode().get_node("/foo", True) + root = foo.parent + root.value = {'timeout': 1} + bar = root.get_node("/bar", True) + params1 = parameters.AvocadoParams([foo, bar], '/') + self.assertEqual(params1.get('timeout'), 1) + self.assertEqual(params1.get('timeout', '/foo/'), 1) + self.assertEqual(params1.get('timeout', '/bar/'), 1) + # Sometimes we get multiple trees, but if they claim the origin + # is of the same path, let's trust it (even when the values + # differ) + # note: This is an artificial example which should not happen + # in production. Anyway in json-variants-loader we do create + # only leave-nodes without connecting the parents, which result + # in same paths with different node objects. Let's make sure + # they behave correctly. + baz = tree.TreeNode().get_node("/baz", True) + baz.parent.value = {'timeout': 2} + params2 = parameters.AvocadoParams([foo, baz], '/') + self.assertEqual(params2.get('timeout'), 1) + self.assertEqual(params2.get('timeout', '/foo/'), 1) + self.assertEqual(params2.get('timeout', '/baz/'), 2) + # Note: Different origin of the same value, which should produce + # a crash, are tested in yaml2mux selftest + + +if __name__ == '__main__': + unittest.main() -- GitLab