Add units

pull/1/head
cidrblock 2020-10-09 11:43:22 -07:00
parent 9d3a6cad0a
commit 187b776c3f
3 changed files with 55409 additions and 1 deletions

View File

@ -48,7 +48,7 @@ def get_path(var, path, environment, wantlist=False):
def to_paths(var, prepend=False, wantlist=False):
if prepend:
if not isinstance(prepend, str):
raise AnsibleError("The value of 'prepend' must be a sting.")
raise AnsibleError("The value of 'prepend' must be a string.")
var = {prepend: var}
out = {}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,90 @@
# -*- 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
import os
import unittest
from ansible_collections.ansible.utils.plugins.module_utils.path_utils import (
get_path,
to_paths,
)
from ansible.template import Templar
class TestPathUtils(unittest.TestCase):
def setUp(self):
self._environment = Templar(loader=None).environment
def test_get_path_pass(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
path = "a.b.c.d[0]"
result = get_path(var, path, environment=self._environment)
expected = "0"
self.assertEqual(result, expected)
def test_get_path_pass_wantlist(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
path = "a.b.c.d[0]"
result = get_path(
var, path, environment=self._environment, wantlist=True
)
expected = ["0"]
self.assertEqual(result, expected)
def test_get_path_fail(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
path = "a.b.e"
expected = "dict object' has no attribute 'e'"
with self.assertRaises(Exception) as exc:
get_path(var, path, environment=self._environment)
self.assertIn(expected, str(exc.exception))
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)
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, wantlist=True)
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, wantlist=True)
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_to_paths_prepend(self):
var = {"a": {"b": {"c": {"d": [0, 1]}}}}
expected = "must be a string"
with self.assertRaises(Exception) as exc:
to_paths(var, wantlist=True, prepend=5)
self.assertIn(expected, str(exc.exception))
def test_roundtrip_large(self):
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)
for key, value in paths.items():
gotten = get_path(var, key, environment=self._environment)
self.assertEqual(gotten, value)