salt/tests/pytests/unit/utils/test_data.py
Megan Wilhite fc7d0a9296
Merge freeze into master (#62438)
* fixes saltstack/salt#62372 unable to use random shuffle and sample functions as Jinja filters

* move random_shuffle and random_sample logic to utils

* static seed in tests seems to have shifted

* static seed in tests require hash module

* Change Tiamat to onedir in release notes

* Reinstate known issues

* Update release notes with onedir package support policy

* need to check the version of Netmiko python library and then import the exceptions from different locations depending on the result.

* Adding changelog.

* swap out if...else for double try...except.

* Remove extra fix we don't need anymore

* [Docs] include onedir system python note

* Update all platforms to use pycparser 2.21 or greater for Py 3.9 or higher, fixes fips fault with openssl v3.x

* Remove the PyObjC dependency

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>

* Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime

* Document usage of bundled Python runtime for Client API

* Use explicit locals for custom script execution, handle exception in similar fashion as Python

* Remove old __file__ replacement

* Apply suggestions from code review

Co-authored-by: Pedro Algarvio <pedro@algarvio.me>

Co-authored-by: nicholasmhughes <nicholasmhughes@gmail.com>
Co-authored-by: Alyssa Rock <alyssa.rock@gmail.com>
Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com>
Co-authored-by: Twangboy <leesh@vmware.com>
Co-authored-by: David Murphy < dmurphy@saltstack.com>
Co-authored-by: Pedro Algarvio <palgarvio@vmware.com>
Co-authored-by: Lukas Raska <lukas@raska.me>
Co-authored-by: Pedro Algarvio <pedro@algarvio.me>
2022-08-08 11:27:10 -06:00

103 lines
2.9 KiB
Python

import sys
import pytest
import salt.utils.data
def test_get_value_simple_path():
data = {"a": {"b": {"c": "foo"}}}
assert [{"value": "foo"}] == salt.utils.data.get_value(data, "a:b:c")
@pytest.mark.skipif(
sys.version_info < (3, 6),
reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
)
def test_get_value_placeholder_dict():
data = {"a": {"b": {"name": "foo"}, "c": {"name": "bar"}}}
assert [
{"value": "foo", "id": "b"},
{"value": "bar", "id": "c"},
] == salt.utils.data.get_value(data, "a:{id}:name")
@pytest.mark.skipif(
sys.version_info < (3, 6),
reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
)
def test_get_value_placeholder_list():
data = {"a": [{"name": "foo"}, {"name": "bar"}]}
assert [
{"value": "foo", "id": 0},
{"value": "bar", "id": 1},
] == salt.utils.data.get_value(data, "a:{id}:name")
@pytest.mark.skipif(
sys.version_info < (3, 6),
reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
)
def test_get_value_nested_placeholder():
data = {
"a": {
"b": {"b1": {"name": "foo1"}, "b2": {"name": "foo2"}},
"c": {"c1": {"name": "bar"}},
}
}
assert [
{"value": "foo1", "id": "b", "sub": "b1"},
{"value": "foo2", "id": "b", "sub": "b2"},
{"value": "bar", "id": "c", "sub": "c1"},
] == salt.utils.data.get_value(data, "a:{id}:{sub}:name")
def test_get_value_nested_notfound():
data = {"a": {"b": {"c": "foo"}}}
assert [{"value": []}] == salt.utils.data.get_value(data, "a:b:d", [])
def test_get_value_not_found():
assert [{"value": []}] == salt.utils.data.get_value({}, "a", [])
def test_get_value_none():
assert [{"value": None}] == salt.utils.data.get_value({"a": None}, "a")
def test_get_value_simple_type_path():
assert [{"value": []}] == salt.utils.data.get_value({"a": 1024}, "a:b", [])
def test_get_value_None_path():
assert [{"value": None}] == salt.utils.data.get_value({"a": None}, "a:b", [])
def test_flatten_recursion_error():
"""
Test the flatten function for reference cycle detection
"""
data = [1, 2, 3, [4]]
data.append(data)
with pytest.raises(RecursionError) as err:
salt.utils.data.flatten(data)
assert str(err.value) == "Reference cycle detected. Check input list."
def test_sample():
lst = ["one", "two", "three", "four"]
assert len(salt.utils.data.sample(lst, 0)) == 0
assert len(salt.utils.data.sample(lst, 2)) == 2
pytest.raises(ValueError, salt.utils.data.sample, lst, 5)
assert salt.utils.data.sample(lst, 2, seed="static") == ["four", "two"]
def test_shuffle():
lst = ["one", "two", "three", "four"]
assert len(salt.utils.data.shuffle(lst)) == 4
assert salt.utils.data.shuffle(lst, seed="static") == [
"four",
"two",
"three",
"one",
]