Skip to content

Commit

Permalink
feat: added index_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed Sep 27, 2023
1 parent a4e1de7 commit 98bd886
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/nethsec/mwan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,27 @@ def delete_policy(e_uci: EUci, name: str) -> list[str]:
e_uci.delete('mwan3', name)
e_uci.save('mwan3')
return [f'mwan3.{name}']


def index_rules(e_uci: EUci) -> list[dict]:
data = []
rules = utils.get_all_by_type(e_uci, 'mwan3', 'rule')
for rule_key in rules.keys():
rule_data = {}
rule_value = rules[rule_key]
rule_data['name'] = rule_key
rule_data['policy'] = {}
rule_data['policy']['name'] = rule_value['use_policy']
if rule_value['use_policy'] in utils.get_all_by_type(e_uci, 'mwan3', 'policy').keys():
rule_data['policy']['label'] = utils.get_all_by_type(e_uci, 'mwan3', 'policy')[rule_value['use_policy']]['label']
if 'label' in rule_value:
rule_data['label'] = rule_value['label']
if 'proto' in rule_value:
rule_data['protocol'] = rule_value['proto']
if 'src_ip' in rule_value:
rule_data['source_addresses'] = rule_value['src_ip']
if 'dest_ip' in rule_value:
rule_data['destination_addresses'] = rule_value['dest_ip']

data.append(rule_data)
return data
53 changes: 44 additions & 9 deletions tests/test_mwan.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def test_create_interface(e_uci):
def test_fail_create_invalid_interface(e_uci):
with pytest.raises(ValueError) as err:
mwan.__store_interface(e_uci, 'RED_4')
assert err.value.args[0] == ('RED_4', 'invalid')
assert err.value.args[0] == 'name'
assert err.value.args[1] == 'invalid'
assert err.value.args[2] == 'RED_4'


def test_interface_avoid_edit_of_metric(e_uci):
Expand Down Expand Up @@ -286,16 +288,16 @@ def test_unique_rule(e_uci):
with pytest.raises(ValueError) as e:
mwan.store_rule(e_uci, 'additional rule', 'ns_default')
mwan.store_rule(e_uci, 'additional rule', 'ns_default')
assert e.value.args[0] == 'name'
assert e.value.args[1] == 'invalid'
assert e.value.args[0] == 'name'
assert e.value.args[1] == 'unique'


def test_delete_non_existent_policy(e_uci):
with pytest.raises(ValidationError) as e:
mwan.delete_policy(e_uci, 'ns_default')
assert e.value.args[0] == 'name'
assert e.value.args[1] == 'invalid'
assert e.value.args[2] == 'ns_default'
assert e.value.args[0] == 'name'
assert e.value.args[1] == 'invalid'
assert e.value.args[2] == 'ns_default'


def test_delete_policy(e_uci):
Expand Down Expand Up @@ -343,6 +345,39 @@ def test_edit_policy(e_uci):
def test_missing_policy(e_uci):
with pytest.raises(ValidationError) as e:
mwan.edit_policy(e_uci, 'dummy', '', [])
assert e.value[0] == 'name'
assert e.value[1] == 'invalid'
assert e.value[2] == 'dummy'
assert e.value.args[0] == 'name'
assert e.value.args[1] == 'invalid'
assert e.value.args[2] == 'dummy'


def test_index_rules(e_uci):
mwan.store_policy(e_uci, 'default', [
{
'name': 'RED_1',
'metric': '10',
'weight': '100',
},
{
'name': 'RED_2',
'metric': '10',
'weight': '100',
}
])
mwan.store_rule(e_uci, 'additional rule', 'ns_default')
index = mwan.index_rules(e_uci)
assert index[0] == {
'name': 'ns_default_rule',
'label': 'Default Rule',
'policy': {
'name': 'ns_default',
'label': 'default',
}
}
assert index[1] == {
'name': 'ns_additional_r',
'label': 'additional rule',
'policy': {
'name': 'ns_default',
'label': 'default',
}
}

0 comments on commit 98bd886

Please sign in to comment.