コード例 #1
0
def test_match_array_attribute(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.get_value(r_i.lhs_operand)
    # assert type(val) is str  # TODO Check again
    assert type(val) is type(None)
    # assert val == 'eastus'
    assert val is None
    children = [
        "{1}['name' = 'mno-nonprod-shared-cet-eastus2-tab-as04'].location",
        "=", "'eastus'"
    ]
    r_i = RuleInterpreter(children, **otherdata)
    val = r_i.get_value(r_i.lhs_operand)
    assert type(val) is type(None)
    # assert val == 'eastus'
    children = [
        "exist", "(",
        "{1}['name' = 'mno-nonprod-shared-cet-eastus2-tab-as04'].location", ")"
    ]
    r_i = RuleInterpreter(children, **otherdata)
    val = r_i.get_value(r_i.lhs_operand)
    assert type(val) is bool
    assert val == False
コード例 #2
0
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'
コード例 #3
0
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'