test_aggregation.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import salt libs
  5. from salt.utils.aggregation import Map, Scalar, aggregate
  6. # Import Salt Testing libs
  7. from tests.support.unit import TestCase
  8. class TestAggregation(TestCase):
  9. def test_merging(self):
  10. a = {"foo": 42, "bar": "first"}
  11. b = {"bar": "second"}
  12. c, d = "first", "second"
  13. # introspection
  14. for level in (None, False, 0, []):
  15. assert aggregate(a, b, level=level) == {"bar": "second"}
  16. assert aggregate(c, d, level=level) == "second"
  17. # first level aggregation
  18. for level in (1, [1, 0], [True], "10000"):
  19. assert aggregate(a, b, level=level) == {"foo": 42, "bar": "second"}
  20. assert aggregate(c, d, level=level) == ["first", "second"]
  21. # 1-2nd level aggregation
  22. for level in (2, [1, 1], [True, True], "11"):
  23. assert aggregate(a, b, level=level) == {
  24. "foo": 42,
  25. "bar": ["first", "second"],
  26. }, aggregate(a, b, level=2)
  27. assert aggregate(c, d, level=level) == ["first", "second"]
  28. # full aggregation
  29. for level in (True,):
  30. assert aggregate(a, b, level=level) == {
  31. "foo": 42,
  32. "bar": ["first", "second"],
  33. }
  34. assert aggregate(c, d, level=level) == ["first", "second"]
  35. def test_nested(self):
  36. a = {"foo": {"bar": "first"}}
  37. b = {"foo": {"bar": "second"}}
  38. assert aggregate(a, b) == {"foo": {"bar": "second"}}, aggregate(a, b)
  39. a = {"foo": {"bar": Scalar("first")}}
  40. b = {"foo": {"bar": Scalar("second")}}
  41. assert aggregate(a, b) == {"foo": {"bar": ["first", "second"]}}, aggregate(a, b)
  42. def test_introspection(self):
  43. a = {"foo": {"lvl1": {"lvl2-a": "first", "lvl2-b": "first"}}}
  44. b = {"foo": {"lvl1": {"lvl2-a": "second"}}}
  45. assert aggregate(a, b) == {"foo": {"lvl1": {"lvl2-a": "second"}}}, aggregate(
  46. a, b
  47. )
  48. def test_instruction(self):
  49. a = {"foo": Map({"bar": Scalar("first")})}
  50. b = {"foo": Map({"bar": Scalar("second")})}
  51. c = {"foo": Map({"another": "value"})}
  52. result = aggregate(c, aggregate(a, b), level=2)
  53. assert result == {
  54. "foo": {"bar": ["first", "second"], "another": "value"}
  55. }, result