def test_multi_and(self): self.assertTrue(BoolRule('("s" > "s" or "su" > "st") and (1 < 3 and 2 < 3)').test()) self.assertFalse(BoolRule('(1 < 2 or 2 > 3) and ("s" > "s" or "su" < "st")').test()) self.assertFalse(BoolRule('(1 > 2 or 2 > 3) and ("s" > "s" or "su" > "st")').test()) self.assertFalse(BoolRule('(1 > 2 or 2 > 3) and ("s" > "s" or "su" < "st")').test()) self.assertFalse(BoolRule('(1 < 3 and 2 < 3) and ("s" > "s" or "su" > "st") and (4 > 5 and 5 < 6)').test())
def test_ne(self): self.assertTrue(BoolRule('1 != 2').test()) self.assertTrue(BoolRule('"1" != 2').test()) self.assertTrue(BoolRule('True != "true"').test()) self.assertTrue(BoolRule('"abc" != "cba"').test()) self.assertFalse(BoolRule('1 != 1').test())
def test_lt_or_equal(self): context = {'${v1}': 1, '${v2}': "1"} self.assertTrue(BoolRule('${v1} <= ${v2}').test(context)) self.assertTrue(BoolRule('${v1} <= 1').test(context)) self.assertTrue(BoolRule('${v1} <= "2"').test(context)) self.assertTrue(BoolRule('${v1} <= "123456789111"').test(context)) self.assertTrue(BoolRule('${v1} <= 123456789111').test(context)) self.assertFalse(BoolRule('${v1} <= 0').test(context)) self.assertFalse(BoolRule('${v1} <= "0"').test(context)) self.assertTrue(BoolRule('${v1} <= "a"').test(context)) self.assertTrue(BoolRule('"a" <= "b"').test(context)) self.assertFalse(BoolRule('"a" <= "49"').test(context))
def targets_meet_condition(self, data): targets = [] for condition in self.conditions: deformatted_data = { deformat_constant_key(key): value for key, value in data.items() } try: resolved_evaluate = ConstantTemplate( condition.evaluate).resolve_data(deformatted_data) result = BoolRule(resolved_evaluate).test(data) except Exception as e: raise EvaluationException( 'evaluate[%s] fail with data[%s] message: %s' % (condition.evaluate, json.dumps(deformatted_data), e.message)) if result: targets.append(condition.sequence_flow.target) if not targets: raise ConditionExhaustedException( 'all conditions of branches are False') return targets
def test_lt(self): self.assertTrue(BoolRule('1 < 2').test()) self.assertTrue(BoolRule('"1" < 2').test()) self.assertTrue(BoolRule('2 < "s"').test()) self.assertFalse(BoolRule('2 < 1').test()) self.assertFalse(BoolRule('2 < 2').test()) self.assertFalse(BoolRule('"2" < 1').test()) self.assertFalse(BoolRule('"q" < 1').test())
def test_and(self): self.assertTrue(BoolRule('1 < 2 and 2 < 3').test()) self.assertTrue(BoolRule('"a" < "s" and 2 < 3').test()) self.assertFalse(BoolRule('1 > 2 and 2 > 1').test()) self.assertFalse(BoolRule('2 > 1 and 1 > 2').test()) self.assertFalse(BoolRule('2 > 1 and 1 > 2').test()) self.assertFalse(BoolRule('"s" > "s" and 2 < 3').test()) self.assertFalse(BoolRule('"s" < "s" and 2 < 3').test())
def test_or(self): self.assertTrue(BoolRule('1 < 2 or 2 < 3').test()) self.assertTrue(BoolRule('1 < 2 or 2 < 1').test()) self.assertTrue(BoolRule('1 > 2 or 2 > 1').test()) self.assertTrue(BoolRule('"s" > "s" or "su" > "st"').test()) self.assertFalse(BoolRule('1 > 2 or 2 > 3').test()) self.assertFalse(BoolRule('"a" > "s" or "s" > "st"').test())
def test_in(self): self.assertTrue(BoolRule('1 in (1, 2)').test()) self.assertTrue(BoolRule('1 in ("1", "2")').test()) self.assertTrue(BoolRule('"1" in (1, 2)').test()) self.assertTrue(BoolRule('"1" in ("1", "2")').test()) self.assertFalse(BoolRule('1 in (0, 2)').test()) self.assertFalse(BoolRule('1 in ("11", 2)').test())
def test_context(self): context = {'${v1}': 1, '${v2}': "1"} self.assertTrue(BoolRule('${v1} == ${v2}').test(context)) self.assertTrue(BoolRule('${v1} == 1').test(context)) self.assertTrue(BoolRule('${v1} == "1"').test(context)) self.assertTrue(BoolRule('${v2} == "1"').test(context)) self.assertTrue(BoolRule('${v2} == "1"').test(context)) self.assertTrue(BoolRule('${v1} in ("1")').test(context))
def test_gt_or_equal(self): context = {'${v1}': 1, '${v2}': "1"} self.assertTrue(BoolRule('${v1} >= ${v2}').test(context)) self.assertTrue(BoolRule('${v1} >= 1').test(context)) self.assertTrue(BoolRule('${v1} >= "1"').test(context)) self.assertTrue(BoolRule('${v1} >= 0').test(context)) self.assertTrue(BoolRule('${v1} >= "0"').test(context)) # self.assertTrue(BoolRule('${v1} >= 2').test(context)) self.assertFalse(BoolRule('${v2} >= "2"').test(context))
def _determine_next_flow_with_boolrule(self, data): """ 根据当前传入的数据判断下一个应该流向的 flow ( 不使用 eval 的版本) :param data: :return: """ for condition in self.conditions: try: result = BoolRule(condition.evaluate).test(data) except Exception as e: raise EvaluationException( 'evaluate[%s] fail with data[%s] message: %s' % (condition.evaluate, json.dumps(data), e.message)) if result: return condition.sequence_flow return None
def _determine_next_flow_with_boolrule(self, data): """ 根据当前传入的数据判断下一个应该流向的 flow ( 不使用 eval 的版本) :param data: :return: """ for condition in self.conditions: deformatted_data = {deformat_constant_key(key): value for key, value in data.items()} try: resolved_evaluate = ConstantTemplate(condition.evaluate).resolve_data(deformatted_data) result = BoolRule(resolved_evaluate).test(data) except Exception as e: raise EvaluationException( 'evaluate[%s] fail with data[%s] message: %s' % ( condition.evaluate, json.dumps(deformatted_data), e.message ) ) if result: return condition.sequence_flow return None
def test_false_equal(self): context = {'${v1}': False, '${v2}': "False"} # 下面的表达式测试不符合预期 # self.assertTrue(BoolRule('${v1} == "False"').test(context)) self.assertTrue(BoolRule('${v1} == ${v1}').test(context)) self.assertTrue(BoolRule('${v1} == false').test(context)) self.assertTrue(BoolRule('${v1} == False').test(context)) self.assertTrue(BoolRule('${v1} == "0"').test(context)) self.assertTrue(BoolRule('${v1} == 0').test(context)) self.assertTrue(BoolRule('${v1} == "0"').test(context)) self.assertFalse(BoolRule('${v1} == "1"').test(context)) self.assertFalse(BoolRule('${v1} == true').test(context)) self.assertFalse(BoolRule('${v1} == "true"').test(context)) self.assertFalse(BoolRule('${v1} == True').test(context)) self.assertFalse(BoolRule('${v1} == "True"').test(context)) self.assertFalse(BoolRule('${v1} == "s"').test(context))
def schedule(self, data, parent_data, callback_data=None): if parent_data.get_one_of_inputs("language"): translation.activate(parent_data.get_one_of_inputs("language")) method = data.inputs.bk_http_request_method url = data.inputs.bk_http_request_url.strip() body = data.inputs.bk_http_request_body request_header = data.inputs.bk_http_request_header timeout = min(abs(int(data.inputs.bk_http_timeout)), 60) or 60 success_exp = data.inputs.bk_http_success_exp.strip() other = {"headers": {}, "timeout": timeout} if method.upper() not in ["GET", "HEAD"]: other["data"] = body.encode("utf-8") other["headers"] = {"Content-type": "application/json"} if request_header: headers = {header["name"]: header["value"] for header in request_header} other["headers"].update(headers) self.logger.info("send {} request to {}".format(method, url)) try: response = request(method=method, url=url, verify=False, **other) except Exception as e: err = _("请求异常,详细信息: {}") self.logger.error(err.format(traceback.format_exc())) data.outputs.ex_data = err.format(e) return False data.outputs.status_code = response.status_code try: resp = response.json() except Exception: try: content = response.content.decode(response.encoding) self.logger.error("response data is not a valid JSON: {}".format(content[:500])) except Exception: self.logger.error("response data is not a valid JSON") data.outputs.ex_data = _("请求响应数据格式非 JSON") return False data.outputs.data = resp if not (200 <= response.status_code < 300): err = _("请求失败,状态码: {},响应: {}").format(response.status_code, resp) self.logger.error(err) data.outputs.ex_data = err return False if success_exp: try: rule = BoolRule(success_exp) if not rule.test(context={"resp": resp}): data.outputs.ex_data = _("请求成功判定失败") return False except Exception as e: err = _("请求成功条件判定出错: {}") self.logger.error(err.format(traceback.format_exc())) data.outputs.ex_data = err.format(e) return False self.finish_schedule() return True
def test_notin(self): self.assertTrue(BoolRule('1 notin (0, 2)').test()) self.assertTrue(BoolRule('1 notin ("0", "2")').test()) self.assertTrue(BoolRule('"abc" notin (0, 2)').test())
def test_gt(self): self.assertTrue(BoolRule('2 > 1').test()) self.assertTrue(BoolRule('"2" > 1').test()) self.assertFalse(BoolRule('1 > 2').test()) self.assertFalse(BoolRule('"1" > 2').test())
def test_eq(self): self.assertTrue(BoolRule('1 == 1').test()) self.assertTrue(BoolRule('"1" == 1').test()) self.assertTrue(BoolRule('True == true').test()) self.assertTrue(BoolRule('False == false').test()) self.assertTrue(BoolRule('1 == True').test()) self.assertTrue(BoolRule('0 == False').test()) self.assertTrue(BoolRule('"1" == True').test()) self.assertTrue(BoolRule('"0" == False').test()) self.assertTrue(BoolRule('"3.14" == 3.14').test()) self.assertTrue(BoolRule('"abc" == "abc"').test()) self.assertFalse(BoolRule('1 == 2').test()) self.assertFalse(BoolRule('123 == "123a"').test()) self.assertFalse(BoolRule('1 == "2"').test()) self.assertFalse(BoolRule('True == "true"').test()) self.assertFalse(BoolRule('False == "false"').test())