def test_is_method(): from processor.comparison.interpreter import RuleInterpreter children = [ "count", "(", "{1}.firewall.rules[]", ")", "=", "count", "(", "{2}.firewall.rules[]", ")" ] otherdata = {} r_i = RuleInterpreter(children, **otherdata) assert True == r_i.is_method(''.join(r_i.lhs_operand)) assert True == r_i.is_method(''.join(r_i.rhs_operand))
def test_eval_expression(monkeypatch): monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.get_documents', mock_get_documents) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_file', mock_exists_file) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_dir', mock_exists_dir) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.json_from_file', mock_json_from_file) from processor.comparison.interpreter import RuleInterpreter otherdata = { 'dbname': 'validator', 'snapshots': {}, 'container': 'mycontainer1' } children = ["exist", "(", "{1}.location", ")"] r_i = RuleInterpreter(children, **otherdata) val = r_i.eval_expression(''.join(r_i.lhs_operand)) assert type(val) is bool assert val == True children = ["exist", "(", "{1}.location1", ")", "=", "False"] r_i = RuleInterpreter(children, **otherdata) val = r_i.eval_expression(''.join(r_i.lhs_operand)) assert type(val) is bool assert val == False children = ["count", "(", "{1}.location", ")", "=", "7"] r_i = RuleInterpreter(children, **otherdata) val = r_i.eval_expression(''.join(r_i.lhs_operand)) assert type(val) is int assert val == 7
def test_match_method(): from processor.comparison.interpreter import RuleInterpreter children = [ "count", "(", "{1}.firewall.rules[]", ")", "=", "count", "(", "{2}.firewall.rules[]", ")" ] otherdata = {'container': 'mycontainer1'} r_i = RuleInterpreter(children, **otherdata) method, method_args = r_i.match_method(''.join(r_i.lhs_operand)) assert method == 'count' assert method_args == "{1}.firewall.rules[]"
def test_get_field_value(): from processor.comparison.interpreter import RuleInterpreter otherdata = {'dbname': 'validator', 'snapshots': {}} children = ["exist", "(", "{1}.location", ")"] r_i = RuleInterpreter(children, **otherdata) data = {'a': 1, 'b': [{'c': 'd'}, {'c': 'f'}]} val = RuleInterpreter.get_field_value(data, 'b[].c') assert val == 'd' val = RuleInterpreter.get_field_value(data, 'b[2].c') assert val is None # val = RuleInterpreter.get_field_value(data, 'b[2d].c') # assert val is None # TODO Check again data = {'a': 1, 'b': [{'c': 'd'}, {'c': 'f'}], 'c': {'d': 'e'}} val = RuleInterpreter.get_field_value(data, 'c[0].d') assert val is None val = RuleInterpreter.get_field_value(data, 'c.e') assert val is None
def test_compare(monkeypatch): monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.get_documents', mock_get_multiple_documents) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_file', mock_exists_file) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_dir', mock_exists_dir) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.json_from_file', mock_multiple_json_from_file) from processor.comparison.interpreter import RuleInterpreter otherdata = { 'dbname': 'validator', 'snapshots': {}, 'container': 'mycontainer1' } children = ["{1}[1].location", "=", "'eastus'"] r_i = RuleInterpreter(children, **otherdata) val = r_i.compare() assert type(val) is bool assert val == False # TODO Check again children = [ "{1}['name' = 'mno-nonprod-shared-cet-eastus2-tab-as04'].location", "=", "False" ] r_i = RuleInterpreter(children, **otherdata) val = r_i.compare() assert type(val) is bool assert val == False
def test_get_value(monkeypatch): monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.get_documents', mock_get_documents) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_file', mock_exists_file) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_dir', mock_exists_dir) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.json_from_file', mock_json_from_file) from processor.comparison.interpreter import RuleInterpreter otherdata = { 'dbname': 'validator', 'snapshots': {}, 'container': 'mycontainer1' } children = ["{1}.location", "+", "{1}.location", "=", "'eastus'"] r_i = RuleInterpreter(children, **otherdata) val = r_i.get_value(r_i.lhs_operand) assert type(val) is str assert val == 'eastus2eastus2'
def test_match_number(monkeypatch): monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.get_documents', mock_get_documents) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_file', mock_exists_file) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.exists_dir', mock_exists_dir) monkeypatch.setattr( 'processor.comparison.comparisonantlr.rule_interpreter.json_from_file', mock_json_from_file) from processor.comparison.interpreter import RuleInterpreter children = ["count", "(", "{1}.firewall.rules[]", ")", "=", "22"] otherdata = { 'dbname': 'validator', 'snapshots': {}, 'container': 'mycontainer1' } r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'(\d+)(\.\d+)?', inval, re.I) val = r_i.match_number(inval, m) assert type(val) is int children = ["count", "(", "{1}.firewall.rules[]", ")", "=", "22.45"] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'(\d+)(\.\d+)?', inval, re.I) val = r_i.match_number(inval, m) assert type(val) is float children = ["count", "(", "{1}.firewall.rules[]", ")", "=", "False"] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'true|false', inval, re.I) val = r_i.match_boolean(inval, m) assert type(val) is bool children = ["count", "(", "{1}.firewall.rules[]", ")", "=", "'eastus2'"] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'\'.*\'', inval, re.I) val = r_i.match_string(inval, m) assert type(val) is str children = [ "count", "(", "{1}.firewall.rules[]", ")", "=", "['eastus2', 'abc']" ] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'\[.*\]', inval, re.I) val = r_i.match_array_string(inval, m) assert type(val) is list children = [ "count", "(", "{1}.firewall.rules[]", ")", "=", "{'eastus2': 'abc'}" ] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.rhs_operand) m = re.match(r'\{.*\}', inval, re.I) val = r_i.match_dictionary_string(inval, m) assert type(val) is dict # children = ["exist", "(", "{1}.location", ")", "=", "'eastus2'"] children = ["{1}.location", "=", "'eastus2'"] r_i = RuleInterpreter(children, **otherdata) inval = ''.join(r_i.lhs_operand) val = r_i.get_value(r_i.lhs_operand) assert type(val) is str assert val == 'eastus2'
def test_comparatorParser(): from antlr4 import InputStream from antlr4 import CommonTokenStream from processor.comparison.comparisonantlr.comparatorLexer import comparatorLexer from processor.comparison.comparisonantlr.comparatorParser import comparatorParser from processor.comparison.interpreter import RuleInterpreter vals = [ 'count({1}.firewall.rules[] + {2}.firewall.rules[]) = 13', 'count({1}.firewall.rules[]) + count({2}.firewall.rules[]) = 13', 'count({1}.firewall.rules[] + {2}.firewall.rules[]) > 13', 'count({1}.firewall.rules[] + {2}.firewall.rules[]) < 13', 'count({1}.firewall.rules[] + {2}.firewall.rules[]) >= 13', 'count({1}.firewall.rules[] + {2}.firewall.rules[]) <= 13', 'count({1}.firewall.rules[] + {2}.firewall.rules[]) != 13', 'count({1}.firewall.rules[]) = count({2}.firewall.rules[])', "{2}.properties.cost=2.34", "{2}.properties.addressSpace={'addressPrefixes': ['172.18.116.0/23']}", "{1}.[0].name=abcd", "{1}.['name' = 'abcd'].location = 'eastus2'", '{1}.dns.ip = 1.2.4.5', '{1}.dns.ip = 1.2.4.5/32', '{1}.location = [1,2,4]', "{2}.properties.dhcpOptions.dnsServers[]+{3}.properties.dhcpOptions.dnsServers[]=['172.18.96.214', '172.18.96.216', '172.18.96.214', '172.18.96.216']", 'count(count(count({1}.location.name[0]))+count(count({2}.location.name[0])))= 13', "{1}.firewall.rules['name' = 'rule1'].port = {2}.firewall.rules['name' = 'rule1'].port", 'count({1}.firewall.rules[]) = count({2}.firewall.rules[])', 'count(count({1}.firewall.rules[]) + count({1}.firewall.rules[])) = 13', 'exist({1}.location)', 'exist({1}.location) = TRUE', 'exist({1}.location) = true', 'exist({1}.location) = FALSE', 'exist({1}.location) = false', 'exist({1}[0].location)', 'exist({1}.firewall.location)', 'exist({1}.firewall.rules[])', 'count({1}.firewall.rules[]) != 13', 'count({1}.firewall.rules[]) = 13', '{1}.firewall.port = 443', "{1}.location = 'eastus2'", 'exist({1}.location) = FAlSE', '{1}.firewall.port = 443', "{1}.firewall.rules['name' = 'rule1'].port = 443", "{1}.firewall.port = {2}.firewall.port", '{1}.firewall.rules[0].port = {2}.firewall.port', 'exist({1}[0].location)', "exist({1}['name' = 'abc'])", "{1}.firewall.rules['name' = 'abc'].port = {2}.firewall.port", "{1}.firewall.rules['name' = 'abc'].ports[2].port = {2}.firewall.port", "{1}.firewall.cost = 443.25", "{1}[0].location = 'eastus2'", ] for line in vals: code = line.rstrip() # print('#' * 75) # print('Actual Rule: ', code) inputStream = InputStream(code) lexer = comparatorLexer(inputStream) stream = CommonTokenStream(lexer) parser = comparatorParser(stream) tree = parser.expression() # print(tree.toStringTree(recog=parser)) children = [] for child in tree.getChildren(): children.append((child.getText())) assert len(children) > 0 # print('*' * 50) # print("All the parsed tokens: ", children) r_i = RuleInterpreter(children) assert r_i is not None