2020-10-28 20:39:20 +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
|
|
|
|
|
2022-05-26 17:18:57 +00:00
|
|
|
|
2020-10-28 20:39:20 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2024-03-28 06:00:53 +00:00
|
|
|
from unittest import TestCase
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
from ansible.errors import AnsibleLookupError
|
2022-05-26 17:18:57 +00:00
|
|
|
|
|
|
|
from ansible_collections.ansible.utils.plugins.lookup.validate import LookupModule
|
|
|
|
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
DATA = {
|
2020-10-30 21:36:30 +00:00
|
|
|
"GigabitEthernet0/0/0/0": {
|
|
|
|
"auto_negotiate": False,
|
|
|
|
"counters": {
|
|
|
|
"in_crc_errors": 0,
|
|
|
|
"in_errors": 0,
|
|
|
|
"rate": {"in_rate": 0, "out_rate": 0},
|
|
|
|
},
|
|
|
|
"description": "configured using Ansible",
|
|
|
|
"duplex_mode": "full",
|
|
|
|
"enabled": True,
|
|
|
|
"line_protocol": "up",
|
|
|
|
"mtu": 1514,
|
|
|
|
"oper_status": "down",
|
|
|
|
"type": "GigabitEthernet",
|
2020-10-28 20:39:20 +00:00
|
|
|
},
|
2020-10-30 21:36:30 +00:00
|
|
|
"GigabitEthernet0/0/0/1": {
|
|
|
|
"auto_negotiate": False,
|
|
|
|
"counters": {
|
|
|
|
"in_crc_errors": 10,
|
|
|
|
"in_errors": 0,
|
|
|
|
"rate": {"in_rate": 0, "out_rate": 0},
|
|
|
|
},
|
|
|
|
"description": "# interface is configures with Ansible",
|
|
|
|
"duplex_mode": "full",
|
|
|
|
"enabled": False,
|
|
|
|
"line_protocol": "up",
|
|
|
|
"mtu": 1514,
|
|
|
|
"oper_status": "up",
|
|
|
|
"type": "GigabitEthernet",
|
2020-10-28 20:39:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
CRITERIA_CRC_ERROR_CHECK = {
|
2020-10-30 21:36:30 +00:00
|
|
|
"type": "object",
|
2020-10-28 20:39:20 +00:00
|
|
|
"patternProperties": {
|
|
|
|
"^.*": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2022-06-01 12:35:10 +00:00
|
|
|
"counters": {"properties": {"in_crc_errors": {"type": "number", "maximum": 0}}},
|
2020-10-30 21:36:30 +00:00
|
|
|
},
|
2022-06-01 12:35:10 +00:00
|
|
|
},
|
2020-10-30 21:36:30 +00:00
|
|
|
},
|
2020-10-28 20:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CRITERIA_ENABLED_CHECK = {
|
2020-10-30 21:36:30 +00:00
|
|
|
"type": "object",
|
2022-05-26 17:18:57 +00:00
|
|
|
"patternProperties": {"^.*": {"type": "object", "properties": {"enabled": {"enum": [True]}}}},
|
2020-10-28 20:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CRITERIA_OPER_STATUS_UP_CHECK = {
|
2020-10-30 21:36:30 +00:00
|
|
|
"type": "object",
|
2020-10-28 20:39:20 +00:00
|
|
|
"patternProperties": {
|
|
|
|
"^.*": {
|
|
|
|
"type": "object",
|
2020-10-30 21:36:30 +00:00
|
|
|
"properties": {"oper_status": {"type": "string", "pattern": "up"}},
|
2022-06-01 12:35:10 +00:00
|
|
|
},
|
2020-10-30 21:36:30 +00:00
|
|
|
},
|
2020-10-28 20:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CRITERIA_IN_RATE_CHECK = {
|
|
|
|
"type": "object",
|
|
|
|
"patternProperties": {
|
|
|
|
"^.*": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"counters": {
|
|
|
|
"properties": {
|
2022-06-01 12:35:10 +00:00
|
|
|
"rate": {"properties": {"in_rate": {"type": "number", "maximum": 0}}},
|
|
|
|
},
|
|
|
|
},
|
2020-10-30 21:36:30 +00:00
|
|
|
},
|
2022-06-01 12:35:10 +00:00
|
|
|
},
|
2020-10-30 21:36:30 +00:00
|
|
|
},
|
2020-10-28 20:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-28 06:00:53 +00:00
|
|
|
class TestValidate(TestCase):
|
2020-10-28 20:39:20 +00:00
|
|
|
def setUp(self):
|
|
|
|
self._lp = LookupModule()
|
|
|
|
|
|
|
|
def test_invalid_argspec(self):
|
|
|
|
"""Check passing invalid argspec"""
|
|
|
|
|
|
|
|
# missing required arguments
|
|
|
|
with self.assertRaises(AnsibleLookupError) as error:
|
|
|
|
self._lp.run([DATA], {})
|
2022-05-26 17:18:57 +00:00
|
|
|
self.assertIn("missing either 'data' or 'criteria' value", str(error.exception))
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
terms = [DATA, [CRITERIA_IN_RATE_CHECK]]
|
2020-10-30 21:36:30 +00:00
|
|
|
kwargs = {"engine": "ansible.utils.sample"}
|
2020-10-28 20:39:20 +00:00
|
|
|
variables = {}
|
|
|
|
with self.assertRaises(AnsibleLookupError) as error:
|
|
|
|
self._lp.run(terms, variables, **kwargs)
|
|
|
|
self.assertIn(
|
2020-10-30 21:36:30 +00:00
|
|
|
"For engine 'ansible.utils.sample' error loading",
|
|
|
|
str(error.exception),
|
2020-10-28 20:39:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
terms = ["invalid data", [CRITERIA_IN_RATE_CHECK]]
|
2020-10-30 21:36:30 +00:00
|
|
|
kwargs = {"engine": "ansible.utils.jsonschema"}
|
2020-10-28 20:39:20 +00:00
|
|
|
variables = {}
|
|
|
|
with self.assertRaises(AnsibleLookupError) as error:
|
|
|
|
self._lp.run(terms, variables, **kwargs)
|
2020-10-30 21:36:30 +00:00
|
|
|
self.assertIn("'data' option value is invalid", str(error.exception))
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
terms = [DATA, "invalid criteria"]
|
2020-10-30 21:36:30 +00:00
|
|
|
kwargs = {"engine": "ansible.utils.jsonschema"}
|
2020-10-28 20:39:20 +00:00
|
|
|
variables = {}
|
|
|
|
with self.assertRaises(AnsibleLookupError) as error:
|
|
|
|
self._lp.run(terms, variables, **kwargs)
|
2022-05-26 17:18:57 +00:00
|
|
|
self.assertIn("'criteria' option value is invalid", str(error.exception))
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
def test_invalid_validate_plugin_config_options(self):
|
|
|
|
"""Check passing invalid validate plugin options"""
|
|
|
|
|
2020-10-30 21:36:30 +00:00
|
|
|
terms = [
|
|
|
|
DATA,
|
|
|
|
[
|
|
|
|
CRITERIA_CRC_ERROR_CHECK,
|
|
|
|
CRITERIA_ENABLED_CHECK,
|
|
|
|
CRITERIA_OPER_STATUS_UP_CHECK,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
kwargs = {"engine": "ansible.utils.jsonschema"}
|
2020-10-28 20:39:20 +00:00
|
|
|
variables = {}
|
|
|
|
result = self._lp.run(terms, variables, **kwargs)
|
2020-10-30 21:36:30 +00:00
|
|
|
self.assertIn(
|
|
|
|
"GigabitEthernet0/0/0/1.counters.in_crc_errors",
|
|
|
|
result[0]["data_path"],
|
|
|
|
)
|
|
|
|
self.assertIn("GigabitEthernet0/0/0/1.enabled", result[1]["data_path"])
|
2022-05-26 17:18:57 +00:00
|
|
|
self.assertIn("GigabitEthernet0/0/0/0.oper_status", result[2]["data_path"])
|
2020-10-28 20:39:20 +00:00
|
|
|
|
|
|
|
def test_valid_data(self):
|
|
|
|
"""Check passing valid data as per criteria"""
|
|
|
|
|
|
|
|
terms = [DATA, CRITERIA_IN_RATE_CHECK]
|
2020-10-30 21:36:30 +00:00
|
|
|
kwargs = {"engine": "ansible.utils.jsonschema"}
|
2020-10-28 20:39:20 +00:00
|
|
|
variables = {}
|
|
|
|
result = self._lp.run(terms, variables, **kwargs)
|
|
|
|
self.assertEqual(result, [])
|