-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Surbhi Kanthed
committed
Jun 18, 2024
1 parent
86ab4eb
commit 3e63aa6
Showing
5 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import re | ||
import operator | ||
import logging | ||
|
||
# Configure the logger | ||
LOG = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
# Define constants for operator pattern and filter pattern | ||
OPS = { | ||
'>=': operator.ge, | ||
'<=': operator.le, | ||
'>': operator.gt, | ||
'<': operator.lt, | ||
'=': operator.eq, | ||
} | ||
|
||
OPERATOR_PATTERN = '|'.join(re.escape(op) for op in OPS.keys()) | ||
FILTER_PATTERN = re.compile(rf'([^><=]+)({OPERATOR_PATTERN})(.+)') | ||
|
||
|
||
def convert_value(value_str): | ||
"""Convert a value string to an appropriate type for comparison.""" | ||
try: | ||
return int(value_str) | ||
except ValueError: | ||
try: | ||
return float(value_str) | ||
except ValueError: | ||
return value_str | ||
|
||
|
||
def parse_property_filter(filter_str): | ||
"""Parse a property filter string into a key, operator, and value.""" | ||
match = FILTER_PATTERN.match(filter_str) | ||
if not match: | ||
raise ValueError(f"Invalid property filter format: {filter_str}") | ||
key, op_str, value_str = match.groups() | ||
if op_str not in OPS: | ||
raise ValueError(f"Invalid operator in property filter: {op_str}") | ||
value = convert_value(value_str) | ||
return key.strip(), OPS[op_str], value | ||
|
||
|
||
def node_matches_property_filters(node, property_filters): | ||
"""Check if a node matches all property filters.""" | ||
for key, op, value in property_filters: | ||
if key not in node.properties: | ||
return False | ||
node_value = convert_value(node.properties.get(key, '')) | ||
if not op(node_value, value): | ||
return False | ||
return True | ||
|
||
|
||
def filter_nodes_by_properties(nodes, properties): | ||
"""Filter a list of nodes based on property filters.""" | ||
if not properties: | ||
return nodes | ||
property_filters = [] | ||
for prop in properties: | ||
try: | ||
property_filters.append(parse_property_filter(prop)) | ||
except ValueError as e: | ||
LOG.error(f"Error parsing property filter '{prop}': {e}") | ||
raise | ||
|
||
filtered_nodes = [ | ||
node for node in nodes | ||
if node_matches_property_filters(node, property_filters) | ||
] | ||
|
||
return filtered_nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import unittest | ||
from esileapclient.common import utils | ||
|
||
|
||
class TestUtils(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.nodes = [ | ||
{'properties': {'cpus': '40', 'memory_mb': '131072'}}, | ||
{'properties': {'cpus': '80', 'memory_mb': '262144'}}, | ||
{'properties': {'cpus': '20', 'memory_mb': '65536'}}, | ||
] | ||
|
||
def test_convert_value(self): | ||
self.assertEqual(utils.convert_value('10'), 10) | ||
self.assertEqual(utils.convert_value('10.5'), 10.5) | ||
self.assertEqual(utils.convert_value('text'), 'text') | ||
|
||
def test_parse_property_filter(self): | ||
key, op, value = utils.parse_property_filter('cpus>=40') | ||
self.assertEqual(key, 'cpus') | ||
self.assertEqual(op, utils.OPS['>=']) | ||
self.assertEqual(value, 40) | ||
|
||
key, op, value = utils.parse_property_filter('memory_mb<=131072') | ||
self.assertEqual(key, 'memory_mb') | ||
self.assertEqual(op, utils.OPS['<=']) | ||
self.assertEqual(value, 131072) | ||
|
||
# Test for invalid filter format | ||
self.assertRaisesRegex( | ||
ValueError, | ||
'Invalid property filter format: invalid_filter', | ||
utils.parse_property_filter, 'invalid_filter' | ||
) | ||
|
||
# Test for invalid operator | ||
self.assertRaisesRegex( | ||
ValueError, | ||
'Invalid operator in property filter: !', | ||
utils.parse_property_filter, 'cpus!40' | ||
) | ||
|
||
def test_node_matches_property_filters(self): | ||
filters = [ | ||
utils.parse_property_filter('cpus>=40'), | ||
utils.parse_property_filter('memory_mb>=131072') | ||
] | ||
self.assertTrue(utils.node_matches_property_filters( | ||
self.nodes[1], filters)) | ||
self.assertFalse(utils.node_matches_property_filters( | ||
self.nodes[2], filters)) | ||
|
||
# Test for non-existent property | ||
filters = [utils.parse_property_filter('non_existent_property>=100')] | ||
self.assertFalse(utils.node_matches_property_filters( | ||
self.nodes[0], filters)) | ||
|
||
def test_filter_nodes_by_properties(self): | ||
properties = ['cpus>=40'] | ||
filtered_nodes = utils.filter_nodes_by_properties( | ||
self.nodes, properties) | ||
self.assertEqual(len(filtered_nodes), 2) | ||
|
||
properties = ['memory_mb<=131072'] | ||
filtered_nodes = utils.filter_nodes_by_properties( | ||
self.nodes, properties) | ||
self.assertEqual(len(filtered_nodes), 2) | ||
|
||
properties = ['cpus>100'] | ||
filtered_nodes = utils.filter_nodes_by_properties( | ||
self.nodes, properties) | ||
self.assertEqual(len(filtered_nodes), 0) | ||
|
||
properties = ['cpus<40'] | ||
filtered_nodes = utils.filter_nodes_by_properties( | ||
self.nodes, properties) | ||
self.assertEqual(len(filtered_nodes), 1) | ||
self.assertEqual(filtered_nodes[0]['properties']['cpus'], '20') | ||
|
||
# Test for error parsing property filter | ||
properties = ['invalid_filter'] | ||
with self.assertLogs('esileapclient.common.utils', level='ERROR') as c: | ||
self.assertRaisesRegex( | ||
ValueError, | ||
"Error parsing property filter 'invalid_filter'", | ||
utils.filter_nodes_by_properties, self.nodes, properties | ||
) | ||
self.assertTrue(any( | ||
"Error parsing property filter 'invalid_filter'" in message | ||
for message in c.output)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters