ansible.utils/tests/unit/module_utils/test_to_paths.py

68 lines
2.3 KiB
Python
Raw Normal View History

2020-10-09 18:43:22 +00:00
# -*- coding: utf-8 -*-
# Copyright 2020 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
2020-10-09 19:32:48 +00:00
import heapq
2020-10-09 18:43:22 +00:00
import os
import unittest
from ansible_collections.ansible.utils.plugins.module_utils.common.get_path import (
2020-10-09 18:43:22 +00:00
get_path,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.to_paths import (
2020-10-09 18:43:22 +00:00
to_paths,
)
2020-10-09 18:43:22 +00:00
from ansible.template import Templar
class TestToPaths(unittest.TestCase):
2020-10-09 18:43:22 +00:00
def setUp(self):
self._environment = Templar(loader=None).environment
def test_to_paths(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
expected = {"a.b.c.d[0]": 0, "a.b.c.d[1]": 1}
result = to_paths(var, prepend=None, wantlist=None)
2020-10-09 18:43:22 +00:00
self.assertEqual(result, expected)
def test_to_paths_wantlist(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
expected = [{"a.b.c.d[0]": 0, "a.b.c.d[1]": 1}]
result = to_paths(var, prepend=None, wantlist=True)
2020-10-09 18:43:22 +00:00
self.assertEqual(result, expected)
def test_to_paths_special_char(self):
var = {"a": {"b": {"c": {"Eth1/1": True}}}}
expected = [{"a.b.c['Eth1/1']": True}]
result = to_paths(var, prepend=None, wantlist=True)
2020-10-09 18:43:22 +00:00
self.assertEqual(result, expected)
def test_to_paths_prepend(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
expected = [{"var.a.b.c.d[0]": 0, "var.a.b.c.d[1]": 1}]
result = to_paths(var, wantlist=True, prepend="var")
self.assertEqual(result, expected)
def test_roundtrip_large(self):
2020-10-12 19:18:10 +00:00
"""Test the 1000 longest keys, otherwise this takes a _really_ long time"""
2020-10-09 18:43:22 +00:00
big_json_path = os.path.join(
os.path.dirname(__file__), "fixtures", "large.json"
)
with open(big_json_path) as fhand:
big_json = fhand.read()
var = json.loads(big_json)
paths = to_paths(var, prepend=None, wantlist=None)
2020-10-09 19:32:48 +00:00
to_tests = heapq.nlargest(1000, list(paths.keys()), key=len)
for to_test in to_tests:
gotten = get_path(
var, to_test, environment=self._environment, wantlist=False
)
2020-10-09 19:32:48 +00:00
self.assertEqual(gotten, paths[to_test])