From 75f7c79569597a1ca452701acb757fd26ec79114 Mon Sep 17 00:00:00 2001 From: asteroide Date: Sat, 10 Jun 2017 10:58:38 +0200 Subject: Update models, policies and metarules if they already exist, create them otherwise Change-Id: I8504dbf5b0080d12368ace7889f64a64f3ac0e48 --- .../tests/apitests/populate_default_values.py | 38 ++++++++++++++++++---- .../moon_interface/tests/apitests/utils/models.py | 36 +++++++++++--------- .../tests/apitests/utils/policies.py | 1 + 3 files changed, 52 insertions(+), 23 deletions(-) (limited to 'moonv4') diff --git a/moonv4/moon_interface/tests/apitests/populate_default_values.py b/moonv4/moon_interface/tests/apitests/populate_default_values.py index 0e3438db..a0f872a1 100644 --- a/moonv4/moon_interface/tests/apitests/populate_default_values.py +++ b/moonv4/moon_interface/tests/apitests/populate_default_values.py @@ -25,10 +25,11 @@ m = SourceFileLoader("scenario", args.filename[0]) scenario = m.load_module() -def create_model(): +def create_model(model_id=None): if args.verbose: logger.warning("Creating model {}".format(scenario.model_name)) - _model_id = add_model(name=scenario.model_name) + if not model_id: + model_id = add_model(name=scenario.model_name) for cat in scenario.subject_categories: scenario.subject_categories[cat] = add_subject_category(name=cat) for cat in scenario.object_categories: @@ -47,20 +48,34 @@ def create_model(): ob_cat.append(scenario.object_categories[item]) elif item in scenario.action_categories: act_cat.append(scenario.action_categories[item]) - meta_rule_id = add_meta_rule(item_name, sub_cat, ob_cat, act_cat) + meta_rules = check_meta_rule(meta_rule_id=None) + for _meta_rule_id, _meta_rule_value in meta_rules['meta_rules'].items(): + if _meta_rule_value['name'] == item_name: + meta_rule_id = _meta_rule_id + break + else: + meta_rule_id = add_meta_rule(item_name, sub_cat, ob_cat, act_cat) item_value["id"] = meta_rule_id - meta_rule_list.append(meta_rule_id) - return _model_id, meta_rule_list + if meta_rule_id not in meta_rule_list: + meta_rule_list.append(meta_rule_id) + return model_id, meta_rule_list def create_policy(model_id, meta_rule_list): if args.verbose: logger.warning("Creating policy {}".format(scenario.policy_name)) - policy_id = add_policy(name=scenario.policy_name, genre=scenario.policy_genre) + _policies = check_policy() + for _policy_id, _policy_value in _policies["policies"].items(): + if _policy_value['name'] == scenario.policy_name: + policy_id = _policy_id + break + else: + policy_id = add_policy(name=scenario.policy_name, genre=scenario.policy_genre) update_policy(policy_id, model_id) for meta_rule_id in meta_rule_list: + print("add_meta_rule_to_model {} {}".format(model_id, meta_rule_id)) add_meta_rule_to_model(model_id, meta_rule_id) for subject_cat_name in scenario.subject_data: @@ -168,12 +183,21 @@ def create_pdp(policy_id=None): for pdp_id, pdp_value in pdps.items(): if scenario.pdp_name == pdp_value["name"]: update_pdp(pdp_id, policy_id=policy_id) + logger.info("Found existing PDP named {} (will add policy {})".format(scenario.pdp_name, policy_id)) return pdp_id _pdp_id = add_pdp(name=scenario.pdp_name, policy_id=policy_id) map_to_keystone(pdp_id=_pdp_id, keystone_project_id=admin_project_id) return _pdp_id if __name__ == "__main__": - model_id, meta_rule_list = create_model() + _models = check_model() + for _model_id, _model_value in _models['models'].items(): + if _model_value['name'] == scenario.model_name: + model_id = _model_id + meta_rule_list = _model_value['meta_rules'] + create_model(model_id) + break + else: + model_id, meta_rule_list = create_model() policy_id = create_policy(model_id, meta_rule_list) pdp_id = create_pdp(policy_id) diff --git a/moonv4/moon_interface/tests/apitests/utils/models.py b/moonv4/moon_interface/tests/apitests/utils/models.py index 1ba6e440..8343f77e 100644 --- a/moonv4/moon_interface/tests/apitests/utils/models.py +++ b/moonv4/moon_interface/tests/apitests/utils/models.py @@ -23,7 +23,7 @@ meta_rule_template = { } -def check_model(model_id=None): +def check_model(model_id=None, check_model_name=True): req = requests.get(URL.format("/models")) assert req.status_code == 200 result = req.json() @@ -33,8 +33,9 @@ def check_model(model_id=None): assert result["models"] assert model_id in result['models'] assert "name" in result['models'][model_id] - assert model_template["name"] == result['models'][model_id]["name"] - return result['models'] + if check_model_name: + assert model_template["name"] == result['models'][model_id]["name"] + return result def add_model(name=None): @@ -224,6 +225,8 @@ def check_meta_rule(meta_rule_id, scat_id=None, ocat_id=None, acat_id=None): assert "meta_rules" in result if "result" in result: assert result["result"] + if not meta_rule_id: + return result assert meta_rule_id in result['meta_rules'] assert "name" in result['meta_rules'][meta_rule_id] if scat_id: @@ -244,17 +247,18 @@ def delete_meta_rule(meta_rule_id): def add_meta_rule_to_model(model_id, meta_rule_id): - model = check_model(model_id) + model = check_model(model_id, check_model_name=False)['models'] meta_rule_list = model[model_id]["meta_rules"] - meta_rule_list.append(meta_rule_id) - req = requests.patch(URL.format("/models/{}".format(model_id)), - json={"meta_rules": meta_rule_list}, - headers=HEADERS) - assert req.status_code == 200 - result = req.json() - assert type(result) is dict - model_id = list(result['models'].keys())[0] - if "result" in result: - assert result["result"] - assert "meta_rules" in result['models'][model_id] - assert meta_rule_list == result['models'][model_id]["meta_rules"] + if meta_rule_id not in meta_rule_list: + meta_rule_list.append(meta_rule_id) + req = requests.patch(URL.format("/models/{}".format(model_id)), + json={"meta_rules": meta_rule_list}, + headers=HEADERS) + assert req.status_code == 200 + result = req.json() + assert type(result) is dict + model_id = list(result['models'].keys())[0] + if "result" in result: + assert result["result"] + assert "meta_rules" in result['models'][model_id] + assert meta_rule_list == result['models'][model_id]["meta_rules"] diff --git a/moonv4/moon_interface/tests/apitests/utils/policies.py b/moonv4/moon_interface/tests/apitests/utils/policies.py index 7b345fad..e6df7f71 100644 --- a/moonv4/moon_interface/tests/apitests/utils/policies.py +++ b/moonv4/moon_interface/tests/apitests/utils/policies.py @@ -61,6 +61,7 @@ def check_policy(policy_id=None): assert policy_id in result['policies'] assert "name" in result['policies'][policy_id] assert policy_template["name"] == result['policies'][policy_id]["name"] + return result def add_policy(name="test_policy", genre="authz"): -- cgit 1.2.3-korg