Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzeG committed Mar 5, 2024
1 parent 76113fe commit e6bf2b0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
45 changes: 45 additions & 0 deletions catalystwan/integration_tests/test_find_template_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import unittest
from typing import Any, List, cast

from catalystwan.session import create_manager_session
from catalystwan.utils.feature_template.find_template_values import find_template_values


class TestFindTemplateValues(unittest.TestCase):
def setUp(self) -> None:
self.session = create_manager_session(
url=cast(str, os.environ.get("TEST_VMANAGE_URL")),
port=cast(int, int(os.environ.get("TEST_VMANAGE_PORT"))), # type: ignore
username=cast(str, os.environ.get("TEST_VMANAGE_USERNAME")),
password=cast(str, os.environ.get("TEST_VMANAGE_PASSWORD")),
)
self.templates = self.session.api.templates._get_feature_templates()

def test_find_template_value(self):
for template in self.templates:
definition = template.template_definiton
with self.subTest(template_name=template.name):
parsed_values = find_template_values(definition)
self.assertFalse(
self.is_key_present(parsed_values, ["vipType", "vipValue", "vipVariableName", "vipObjectType"])
)

def is_key_present(self, d: dict, keys: List[Any]):
"""
Checks if any key from keys is present within the dictionary d
"""
for key, value in d.items():
if key in keys:
return True
if isinstance(value, dict):
if self.is_key_present(value, keys):
return True
if isinstance(value, list):
for v in value:
if self.is_key_present(v, keys):
return True
return False

def tearDown(self) -> None:
self.session.close()
10 changes: 7 additions & 3 deletions catalystwan/utils/dict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2023 Cisco Systems, Inc. and its affiliates

from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

from pydantic import BaseModel

Expand Down Expand Up @@ -34,9 +34,13 @@ def flatten_dict(original_dict: Dict[str, Any]) -> Dict[str, List[FlattenedDictV

def get_flattened_dict(
original_dict: Dict[str, Any],
flattened_dict: Dict[str, List[FlattenedDictValue]] = {},
path: List[str] = [],
flattened_dict: Optional[Dict[str, List[FlattenedDictValue]]] = None,
path: Optional[List[str]] = None,
):
if flattened_dict is None:
flattened_dict = {}
if path is None:
path = []
for key, value in original_dict.items():
if isinstance(value, dict):
get_flattened_dict(value, flattened_dict, path=path + [key])
Expand Down
4 changes: 3 additions & 1 deletion catalystwan/utils/feature_template/find_template_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def find_template_values(
template_definition: dict,
templated_values: dict = {},
templated_values: Optional[dict] = None,
target_key: str = "vipType",
target_key_value_to_ignore: str = "ignore",
target_key_for_template_value: str = "vipValue",
Expand All @@ -29,6 +29,8 @@ def find_template_values(
"""
if path is None:
path = []
if templated_values is None:
templated_values = {}

# if value object is reached, try to extract the value
if target_key in template_definition:
Expand Down

0 comments on commit e6bf2b0

Please sign in to comment.