def test(self): self.instance = LogicalOperator("AND", [EqualOperator("host.id", "1"), NotEqualOperator("host.path", "2")]) self.instance.eval(None) assert self.instance.expr() == "((host.id eq '1') AND (host.path not_eq '2'))" os = ObjectSet() os.add_object("host", {"id": "1", "path": "2"}) assert self.instance.render(os) == "(('1' eq '1') AND ('2' not_eq '2'))"
def test_make_expression(): d = {"op": "eq", "field": "host.id", "value": "a1"} expr = make_expression(d) assert expr.expr() == "(host.id eq 'a1')" d1 = ObjectSet() d1.add_object("host", {"id": "a1", "name": "b1"}) assert expr.eval(d1)
def test(self): self.instance = BinaryOperator("eq", "host.id", "1") assert self.instance.expr() == "(host.id eq '1')" os = ObjectSet() os.add_object( "host", { "id": "1", }, ) assert self.instance.render(os) == "('1' eq '1')" self.instance.calculate(1, 1)
def test_not_equal_operator(): not_eq = NotEqualOperator("host.id", "localhost") assert not_eq.op == OP.NOT_EQ assert not_eq.expr() == "(host.id not_eq 'localhost')" d1 = ObjectSet() d1.add_object("host", {"id": "localhost"}) assert not not_eq.eval(d1) d2 = ObjectSet() d2.add_object("host", {"id": "remote"}) assert not_eq.eval(d2)
def test_text_operator(): d1 = ObjectSet() d1.add_object( "person", { "name": "hello", }, ) d2 = ObjectSet() d2.add_object( "person", { "name": "world", }, ) # STARTS_WITH sw = StartsWithOperator("person.name", "hel") assert sw.op == OP.STARTS_WITH assert sw.expr() == "(person.name starts_with 'hel')" assert sw.eval(d1) assert not sw.eval(d2) # NOT_STARTS_WITH nsw = NotStartsWithOperator("person.name", "hel") assert nsw.op == OP.NOT_STARTS_WITH assert nsw.expr() == "(person.name not_starts_with 'hel')" assert not nsw.eval(d1) assert nsw.eval(d2) # ENDS_WITH ew = EndsWithOperator("person.name", "llo") assert ew.op == OP.ENDS_WITH assert ew.expr() == "(person.name ends_with 'llo')" assert ew.eval(d1) assert not ew.eval(d2) # NOT_ENDS_WITH new = NotEndsWithOperator("person.name", "llo") assert new.op == OP.NOT_ENDS_WITH assert new.expr() == "(person.name not_ends_with 'llo')" assert not new.eval(d1) assert new.eval(d2)
def test_make_expression_or(): d = { "op": "OR", "content": [ { "op": "eq", "field": "host.id", "value": "a1" }, { "op": "eq", "field": "host.name", "value": "b1" }, ], } expr = make_expression(d) assert expr.expr() == "((host.id eq 'a1') OR (host.name eq 'b1'))" d1 = ObjectSet() d1.add_object("host", {"id": "a1", "name": "b1"}) assert expr.eval(d1)
def test_logical_operator(): d1 = ObjectSet() d1.add_object("host", {"id": "a1", "name": "b1"}) d2 = ObjectSet() d2.add_object("host", {"id": "a1", "name": "c1"}) d3 = ObjectSet() d3.add_object("host", {"id": "a2", "name": "c1"}) eq1 = EqualOperator("host.id", "a1") eq2 = EqualOperator("host.name", "b1") # AND andop = AndOperator([eq1, eq2]) assert andop.op == OP.AND assert andop.expr() == "((host.id eq 'a1') AND (host.name eq 'b1'))" assert andop.eval(d1) assert not andop.eval(d2) # OR orop = OrOperator([eq1, eq2]) assert orop.op == OP.OR assert orop.expr() == "((host.id eq 'a1') OR (host.name eq 'b1'))" assert orop.eval(d1) assert orop.eval(d2) assert not orop.eval(d3)
def test_binary_operator_eval_negative(): d1 = ObjectSet() d1.add_object( "host", { "id": 1, }, ) d2 = ObjectSet() d2.add_object( "host", { "id": [1, 2], }, ) neq1 = NotEqualOperator("host.id", 2) assert neq1.eval(d1) neq2 = NotEqualOperator("host.id", [2]) assert neq2.eval(d1) neq3 = NotEqualOperator("host.id", [3, 4]) assert neq3.eval(d2) neq4 = NotEqualOperator("host.id", 3) assert neq4.eval(d2) neq5 = NotEqualOperator("host.id", [2, 3]) assert not neq5.eval(d2) # NOT_IN d3 = ObjectSet() d3.add_object( "host", { "id": [1, 2], }, ) # all of [1,2] not in [3,4] eq6 = NotInOperator("host.id", [3, 4]) assert eq6.eval(d3) # NOT_CONTAINS d4 = ObjectSet() d4.add_object( "host", { "id": [1, 2], }, ) # [1,2] not contains all of [3,4] eq6 = NotContainsOperator("host.id", [3, 4]) assert eq6.eval(d4)
def test_binary_operator_eval_positive(): """ op = eq => one of attr equals one of value attr = 1; value = 1; True attr = 1; value = [1, 2]; True attr = [1, 2]; value = 2; True attr = [1, 2]; value = [5, 1]; True attr = [1, 2]; value = [3, 4]; False """ d1 = ObjectSet() d1.add_object( "host", { "id": 1, }, ) d2 = ObjectSet() d2.add_object( "host", { "id": [1, 2], }, ) eq1 = EqualOperator("host.id", 1) assert eq1.eval(d1) eq2 = EqualOperator("host.id", [1, 2]) assert eq2.eval(d1) eq3 = EqualOperator("host.id", 2) assert eq3.eval(d2) eq4 = EqualOperator("host.id", [5, 1]) assert eq4.eval(d2) eq5 = EqualOperator("host.id", [3, 4]) assert not eq5.eval(d2) # IN d3 = ObjectSet() d3.add_object( "host", { "id": [1, 2], }, ) # one of [1,2] in [2, 4] eq6 = InOperator("host.id", [2, 4]) assert eq6.eval(d3) # CONTAINS d4 = ObjectSet() d4.add_object( "host", { "id": [1, 2], }, ) # [1, 2] contains 1 of [2,4] eq6 = ContainsOperator("host.id", [2, 4]) assert eq6.eval(d4)
def test_any_operator(): a = AnyOperator("host.id", "localhost") assert a.op == OP.ANY assert a.expr() == "(host.id any 'localhost')" d1 = ObjectSet() d1.add_object("host", {"id": "localhost"}) assert a.eval(d1) d2 = ObjectSet() d2.add_object("host", {"id": "remote"}) assert a.eval(d2) d3 = ObjectSet() d3.add_object("host", {"id": 1}) assert a.eval(d3)
def test_math_operator(): d1 = ObjectSet() d1.add_object( "person", { "age": 19, }, ) d2 = ObjectSet() d2.add_object( "person", { "age": 20, }, ) d3 = ObjectSet() d3.add_object( "person", { "age": 21, }, ) # LT lt = LTOperator("person.age", 20) assert lt.op == OP.LT assert lt.expr() == "(person.age lt 20)" assert lt.eval(d1) assert not lt.eval(d2) assert not lt.eval(d3) # LTE lte = LTEOperator("person.age", 20) assert lte.op == OP.LTE assert lte.expr() == "(person.age lte 20)" assert lte.eval(d1) assert lte.eval(d2) assert not lte.eval(d3) # GT gt = GTOperator("person.age", 20) assert gt.op == OP.GT assert gt.expr() == "(person.age gt 20)" assert not gt.eval(d1) assert not gt.eval(d2) assert gt.eval(d3) # GTE gte = GTEOperator("person.age", 20) assert gte.op == OP.GTE assert gte.expr() == "(person.age gte 20)" assert not gte.eval(d1) assert gte.eval(d2) assert gte.eval(d3)
def test_contains_operator(): d1 = ObjectSet() d1.add_object( "host", { "owner": ["a1", "a3"], }, ) d2 = ObjectSet() d2.add_object( "host", { "owner": ["a2"], }, ) # CONTAINS c = ContainsOperator("host.owner", "a1") assert c.op == OP.CONTAINS assert c.expr() == "(host.owner contains 'a1')" assert c.eval(d1) assert not c.eval(d2) # NOT_CONTAINS nc = NotContainsOperator("host.owner", "a1") assert nc.op == OP.NOT_CONTAINS assert nc.expr() == "(host.owner not_contains 'a1')" assert not nc.eval(d1) assert nc.eval(d2) # value is a list c1 = ContainsOperator("host.owner", ["a1", "a2"]) nc1 = NotContainsOperator("host.owner", ["a1", "a2"]) d3 = ObjectSet() d3.add_object("host", {"owner": ["a3", "a2"]}) assert c1.eval(d3) assert not nc1.eval(d3) d4 = ObjectSet() d4.add_object("host", {"owner": ["b1", "b2"]}) assert not c1.eval(d4) assert nc1.eval(d4)
def test_in_operator(): d1 = ObjectSet() d1.add_object( "host", { "id": "a1", }, ) d2 = ObjectSet() d2.add_object( "host", { "id": "a2", }, ) # IN inop = InOperator("host.id", ["a1", "a3"]) assert inop.op == OP.IN assert inop.expr() == "(host.id in ['a1', 'a3'])" assert inop.eval(d1) assert not inop.eval(d2) # NOT_IN notinop = NotInOperator("host.id", ["a1", "a3"]) assert notinop.op == OP.NOT_IN assert notinop.expr() == "(host.id not_in ['a1', 'a3'])" assert not notinop.eval(d1) assert notinop.eval(d2) # attr is a list # common: a3 d3 = ObjectSet() d3.add_object( "host", { "id": ["a4", "a3"], }, ) assert inop.eval(d3) assert not notinop.eval(d3) # no common d4 = ObjectSet() d4.add_object( "host", { "id": ["b1", "b2"], }, ) assert not inop.eval(d4) assert notinop.eval(d4)
def test_equal_operator(): eq = EqualOperator("host.id", "localhost") # the __repr__ assert str(eq) == "operator:eq" assert eq.op == OP.EQ assert eq.expr() == "(host.id eq 'localhost')" d1 = ObjectSet() d1.add_object("host", {"id": "localhost"}) assert eq.eval(d1) d2 = ObjectSet() d2.add_object("host", {"id": "remote"}) assert not eq.eval(d2) # TODO: add some more case? d3 = ObjectSet() d3.add_object("host", {"id": 1}) assert not eq.eval(d3)
def eval_exmaple(): # create a resource, type is host h = {"id": "hello"} host = DictObject(h) print("host.id =", host.id) print("host.notexists =", host.notexists) # create a resource, type is module class Module(object): pass m = Module() m.id = "world" module = DictObject(m) print("module.id =", module.id) print("module.notexists =", module.notexists) print_spearator() # make a object set contains two resource type s = ObjectSet() s.add_object("host", host) s.add_object("module", module) print("object_set host.id", s.get("host.id")) print("object_set host.id", s.get("host.notexists")) print("object_set module.id", s.get("module.id")) print("object_set module.id", s.get("module.notexists")) print("object_set cluster.id", s.get("cluster.id")) print_spearator() # define a policy data = { "op": "OR", "content": [ { "op": "eq", "field": "host.id", "value": "hello" }, { "op": "not_eq", "field": "module.id", "value": "world" }, ], } # make a policy expression expr = make_expression(data) print("the expression:", expr.expr()) print("the expression render:", expr.render(s)) print("the eval result:", expr.eval(s)) print_spearator() data1 = { "op": "eq", "field": "host.id", "value": "hello", } expr1 = make_expression(data1) print("the expression:", expr1.expr()) print("the expression render:", expr1.render(s)) print("the eval result:", expr1.eval(s)) print_spearator() data1 = { "op": "not_eq", "field": "host.id", "value": "hello", } expr1 = make_expression(data1) print("the expression:", expr1.expr()) print("the expression render:", expr1.render(s)) print("the eval result:", expr1.eval(s))
def test_object_set(): host = DictObject(obj={"id": "192.168.1.1"}) module = DictObject(obj={"id": "iam"}) os = ObjectSet() os.add_object("host", host) os.add_object("module", module) assert os.has_object("host") assert os.has_object("module") assert not os.has_object("cluster") assert os.get("host.id") == "192.168.1.1" assert os.get("module.id") == "iam" assert os.get("invalidkey") is None assert os.get("cluster.id") is None assert os.get("host.path") is None # get/del obj = os.get_object("host") assert obj assert obj.id == "192.168.1.1" os.del_object("host") assert not os.has_object("host")