Skip to content

Commit e77c38f

Browse files
committed
Fix: PACK redis is now also Valkey
Enh: EVALUATOR now manage and / or in unlimited way
1 parent 84e4c5e commit e77c38f

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
detector:
22
add_groups:
33
- redis
4-
apply_if: "file_exists('/etc/redis/redis.conf') or file_exists('/etc/redis.conf')"
4+
# redis == valkey
5+
apply_if: "file_exists('/etc/redis/redis.conf') or file_exists('/etc/redis.conf') or file_exists('/etc/valkey/valkey.conf')"
56
interval: "10s"

opsbro/evaluater.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,16 @@ def eval_(self, node):
201201
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
202202
return operators[type(node.op)](self.eval_(node.left), self.eval_(node.right))
203203
elif isinstance(node, _ast.BoolOp): # <elt1> OP <elt2> TOD: manage more than 2 params
204-
if len(node.values) != 2:
205-
raise Exception('Cannot manage and/or operators woth more than 2 parts currently.')
206-
# Special case: _ast.And if the first element is False, then we should NOT eval the right part
207-
# and directly returns False
208-
left_part_eval = self.eval_(node.values[0])
209-
if not left_part_eval and isinstance(node.op, _ast.And):
210-
return False
211-
# Special case: _ast.Or if the first element is True, then we should NOT eval the right part
212-
# and directly returns True
213-
left_part_eval = self.eval_(node.values[0])
214-
if left_part_eval and isinstance(node.op, _ast.Or):
204+
if isinstance(node.op, _ast.And):
205+
for value in node.values:
206+
if not self.eval_(value):
207+
return False
215208
return True
216-
# else, give the whole result
217-
return operators[type(node.op)](left_part_eval, self.eval_(node.values[1]))
209+
elif isinstance(node.op, _ast.Or):
210+
for value in node.values:
211+
if self.eval_(value):
212+
return True
213+
return False
218214
elif isinstance(node, ast.Compare): # <left> <operator> <right>
219215
left = self.eval_(node.left)
220216
right = self.eval_(node.comparators[0])

test/opsbro_test.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44
import time
5-
import imp
5+
import importlib.util
66
import datetime
77
import os
88
import string
@@ -63,10 +63,17 @@ def setUp(self):
6363
core_functions_dir = os.path.join(my_dir, '..', 'data', 'core-configuration', 'packs', 'core-functions', 'module')
6464
print("** From directory", core_functions_dir)
6565
sys.path.insert(0, core_functions_dir)
66-
m = imp.load_source('module___titi___toto___tata', os.path.join(core_functions_dir, 'module.py'))
66+
67+
spec = importlib.util.spec_from_file_location('module___titi___toto___tata', os.path.join(core_functions_dir, 'module.py'))
68+
m = importlib.util.module_from_spec(spec)
69+
spec.loader.exec_module(m)
70+
6771
print("** Core functionns module is loaded:", m)
6872
self.assert_(m.CoreFunctionsModule is not None)
6973

74+
# for backport
75+
def assert_(self, expr):
76+
self.assertTrue(expr)
7077

7178
if __name__ == '__main__':
7279
unittest.main()

test/test_evaluator.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def test_evaluator(self):
6969
{'rule': 'False and missing_function()', 'expected': False},
7070
# Do not execute functions at right in Or if the first part is True
7171
{'rule': 'True or missing_function()', 'expected': True},
72+
73+
# Multi operators
74+
{'rule': 'True and False and True', 'expected': False},
75+
{'rule': 'True or False or True', 'expected': True},
7276

7377
]
7478
for r in rules:
@@ -83,7 +87,7 @@ def test_evaluator(self):
8387
print("Expected: %s" % str(expected))
8488
print("Result: %s" % str(r))
8589
print("Is The same?: %s" % (r == expected))
86-
self.assert_(r == expected)
90+
self.assertEqual(expected, r)
8791

8892

8993
if __name__ == '__main__':

0 commit comments

Comments
 (0)