From 12d66121165397cc995e0f5a5873391c18279c0a Mon Sep 17 00:00:00 2001 From: RHE Date: Mon, 18 Dec 2017 18:37:37 +0100 Subject: fix moon functional tests Change-Id: I9ebc0d8d20cca44a083d85a49587e7249a85b5bd Signed-off-by: RHE --- moonv4/tests/get_keystone_projects.py | 19 ++++ moonv4/tests/populate_default_values.py | 131 +++++++++++++--------------- moonv4/tests/send_authz.py | 148 +++++++++----------------------- moonv4/tests/utils/config.py | 62 ++++++++----- moonv4/tests/utils/models.py | 19 ++-- moonv4/tests/utils/parse.py | 83 ++++++++++++++++++ moonv4/tests/utils/pdp.py | 34 +++++--- moonv4/tests/utils/policies.py | 19 ++-- 8 files changed, 292 insertions(+), 223 deletions(-) create mode 100644 moonv4/tests/get_keystone_projects.py create mode 100644 moonv4/tests/utils/parse.py diff --git a/moonv4/tests/get_keystone_projects.py b/moonv4/tests/get_keystone_projects.py new file mode 100644 index 00000000..7b37b0e7 --- /dev/null +++ b/moonv4/tests/get_keystone_projects.py @@ -0,0 +1,19 @@ +from utils import pdp +from utils import parse +from utils import models +from utils import policies +from utils import pdp + +if __name__ == "__main__": + args = parse.parse() + consul_host = args.consul_host + consul_port = args.consul_port + + models.init(consul_host, consul_port) + policies.init(consul_host, consul_port) + pdp.init(consul_host, consul_port) + + projects = pdp.get_keystone_projects() + + for _project in projects['projects']: + print("{} {}".format(_project['id'], _project['name'])) diff --git a/moonv4/tests/populate_default_values.py b/moonv4/tests/populate_default_values.py index fa099458..28795526 100644 --- a/moonv4/tests/populate_default_values.py +++ b/moonv4/tests/populate_default_values.py @@ -1,45 +1,11 @@ -import argparse import logging from importlib.machinery import SourceFileLoader -from utils.pdp import * -from utils.models import * -from utils.policies import * - -parser = argparse.ArgumentParser() -parser.add_argument('filename', help='scenario filename', nargs=1) -parser.add_argument("--verbose", "-v", action='store_true', - help="verbose mode") -parser.add_argument("--debug", "-d", action='store_true', help="debug mode") -parser.add_argument("--keystone-pid", "-k", dest="keystone_pid", default="", - help="Force a particular Keystone Project ID") -args = parser.parse_args() - -FORMAT = '%(asctime)-15s %(levelname)s %(message)s' -if args.debug: - logging.basicConfig( - format=FORMAT, - level=logging.DEBUG) -elif args.verbose: - logging.basicConfig( - format=FORMAT, - level=logging.INFO) -else: - logging.basicConfig( - format=FORMAT, - level=logging.WARNING) - -requests_log = logging.getLogger("requests.packages.urllib3") -requests_log.setLevel(logging.WARNING) -requests_log.propagate = True - -logger = logging.getLogger("moonforming") - -if args.filename: - print("Loading: {}".format(args.filename[0])) - -m = SourceFileLoader("scenario", args.filename[0]) - -scenario = m.load_module() +from utils import parse +from utils import models +from utils import policies +from utils import pdp + +logger = None def create_model(model_id=None): @@ -47,16 +13,16 @@ def create_model(model_id=None): logger.info("Creating model {}".format(scenario.model_name)) if not model_id: logger.info("Add model") - model_id = add_model(name=scenario.model_name) + model_id = models.add_model(name=scenario.model_name) logger.info("Add subject categories") for cat in scenario.subject_categories: - scenario.subject_categories[cat] = add_subject_category(name=cat) + scenario.subject_categories[cat] = models.add_subject_category(name=cat) logger.info("Add object categories") for cat in scenario.object_categories: - scenario.object_categories[cat] = add_object_category(name=cat) + scenario.object_categories[cat] = models.add_object_category(name=cat) logger.info("Add action categories") for cat in scenario.action_categories: - scenario.action_categories[cat] = add_action_category(name=cat) + scenario.action_categories[cat] = models.add_action_category(name=cat) sub_cat = [] ob_cat = [] act_cat = [] @@ -69,14 +35,14 @@ def create_model(model_id=None): ob_cat.append(scenario.object_categories[item]) elif item in scenario.action_categories: act_cat.append(scenario.action_categories[item]) - meta_rules = check_meta_rule(meta_rule_id=None) + meta_rules = models.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: logger.info("Add meta rule") - meta_rule_id = add_meta_rule(item_name, sub_cat, ob_cat, act_cat) + meta_rule_id = models.add_meta_rule(item_name, sub_cat, ob_cat, act_cat) item_value["id"] = meta_rule_id if meta_rule_id not in meta_rule_list: meta_rule_list.append(meta_rule_id) @@ -86,51 +52,51 @@ def create_model(model_id=None): def create_policy(model_id, meta_rule_list): if args.verbose: logger.info("Creating policy {}".format(scenario.policy_name)) - _policies = check_policy() + _policies = 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) + policy_id = policies.add_policy(name=scenario.policy_name, genre=scenario.policy_genre) - update_policy(policy_id, model_id) + policies.update_policy(policy_id, model_id) for meta_rule_id in meta_rule_list: logger.debug("add_meta_rule_to_model {} {}".format(model_id, meta_rule_id)) - add_meta_rule_to_model(model_id, meta_rule_id) + models.add_meta_rule_to_model(model_id, meta_rule_id) logger.info("Add subject data") for subject_cat_name in scenario.subject_data: for subject_data_name in scenario.subject_data[subject_cat_name]: - data_id = scenario.subject_data[subject_cat_name][subject_data_name] = add_subject_data( + data_id = scenario.subject_data[subject_cat_name][subject_data_name] = policies.add_subject_data( policy_id=policy_id, category_id=scenario.subject_categories[subject_cat_name], name=subject_data_name) scenario.subject_data[subject_cat_name][subject_data_name] = data_id logger.info("Add object data") for object_cat_name in scenario.object_data: for object_data_name in scenario.object_data[object_cat_name]: - data_id = scenario.object_data[object_cat_name][object_data_name] = add_object_data( + data_id = scenario.object_data[object_cat_name][object_data_name] = policies.add_object_data( policy_id=policy_id, category_id=scenario.object_categories[object_cat_name], name=object_data_name) scenario.object_data[object_cat_name][object_data_name] = data_id logger.info("Add action data") for action_cat_name in scenario.action_data: for action_data_name in scenario.action_data[action_cat_name]: - data_id = scenario.action_data[action_cat_name][action_data_name] = add_action_data( + data_id = scenario.action_data[action_cat_name][action_data_name] = policies.add_action_data( policy_id=policy_id, category_id=scenario.action_categories[action_cat_name], name=action_data_name) scenario.action_data[action_cat_name][action_data_name] = data_id logger.info("Add subjects") for name in scenario.subjects: - scenario.subjects[name] = add_subject(policy_id, name=name) + scenario.subjects[name] = policies.add_subject(policy_id, name=name) logger.info("Add objects") for name in scenario.objects: - scenario.objects[name] = add_object(policy_id, name=name) + scenario.objects[name] = policies.add_object(policy_id, name=name) logger.info("Add actions") for name in scenario.actions: - scenario.actions[name] = add_action(policy_id, name=name) + scenario.actions[name] = policies.add_action(policy_id, name=name) logger.info("Add subject assignments") for subject_name in scenario.subject_assignments: @@ -141,14 +107,14 @@ def create_policy(model_id, meta_rule_list): subject_cat_id = scenario.subject_categories[subject_category_name] for data in scenario.subject_assignments[subject_name]: subject_data_id = scenario.subject_data[subject_category_name][data[subject_category_name]] - add_subject_assignments(policy_id, subject_id, subject_cat_id, subject_data_id) + policies.add_subject_assignments(policy_id, subject_id, subject_cat_id, subject_data_id) else: for subject_category_name in scenario.subject_assignments[subject_name]: subject_id = scenario.subjects[subject_name] subject_cat_id = scenario.subject_categories[subject_category_name] subject_data_id = scenario.subject_data[subject_category_name][scenario.subject_assignments[subject_name][subject_category_name]] - add_subject_assignments(policy_id, subject_id, subject_cat_id, subject_data_id) - + policies.add_subject_assignments(policy_id, subject_id, subject_cat_id, subject_data_id) + logger.info("Add object assignments") for object_name in scenario.object_assignments: if type(scenario.object_assignments[object_name]) in (list, tuple): @@ -158,13 +124,13 @@ def create_policy(model_id, meta_rule_list): object_cat_id = scenario.object_categories[object_category_name] for data in scenario.object_assignments[object_name]: object_data_id = scenario.object_data[object_category_name][data[object_category_name]] - add_object_assignments(policy_id, object_id, object_cat_id, object_data_id) + policies.add_object_assignments(policy_id, object_id, object_cat_id, object_data_id) else: for object_category_name in scenario.object_assignments[object_name]: object_id = scenario.objects[object_name] object_cat_id = scenario.object_categories[object_category_name] object_data_id = scenario.object_data[object_category_name][scenario.object_assignments[object_name][object_category_name]] - add_object_assignments(policy_id, object_id, object_cat_id, object_data_id) + policies.add_object_assignments(policy_id, object_id, object_cat_id, object_data_id) logger.info("Add action assignments") for action_name in scenario.action_assignments: @@ -175,13 +141,13 @@ def create_policy(model_id, meta_rule_list): action_cat_id = scenario.action_categories[action_category_name] for data in scenario.action_assignments[action_name]: action_data_id = scenario.action_data[action_category_name][data[action_category_name]] - add_action_assignments(policy_id, action_id, action_cat_id, action_data_id) + policies.add_action_assignments(policy_id, action_id, action_cat_id, action_data_id) else: for action_category_name in scenario.action_assignments[action_name]: action_id = scenario.actions[action_name] action_cat_id = scenario.action_categories[action_category_name] action_data_id = scenario.action_data[action_category_name][scenario.action_assignments[action_name][action_category_name]] - add_action_assignments(policy_id, action_id, action_cat_id, action_data_id) + policies.add_action_assignments(policy_id, action_id, action_cat_id, action_data_id) logger.info("Add rules") for meta_rule_name in scenario.rules: @@ -198,31 +164,50 @@ def create_policy(model_id, meta_rule_list): elif category_name in scenario.action_categories: data_list.append(scenario.action_data[category_name][data_name]) instructions = rule["instructions"] - add_rule(policy_id, meta_rule_value["id"], data_list, instructions) + policies.add_rule(policy_id, meta_rule_value["id"], data_list, instructions) return policy_id -def create_pdp(policy_id=None): +def create_pdp(policy_id=None, project_id=None): logger.info("Creating PDP {}".format(scenario.pdp_name)) - projects = get_keystone_projects() - project_id = args.keystone_pid + projects = pdp.get_keystone_projects() if not project_id: for _project in projects['projects']: if _project['name'] == "admin": project_id = _project['id'] assert project_id - pdps = check_pdp()["pdps"] + pdps = pdp.check_pdp()["pdps"] for pdp_id, pdp_value in pdps.items(): if scenario.pdp_name == pdp_value["name"]: - update_pdp(pdp_id, policy_id=policy_id) + pdp.update_pdp(pdp_id, policy_id=policy_id) logger.debug("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=project_id) + _pdp_id = pdp.add_pdp(name=scenario.pdp_name, policy_id=policy_id) + pdp.map_to_keystone(pdp_id=_pdp_id, keystone_project_id=project_id) return _pdp_id + if __name__ == "__main__": - _models = check_model() + logger = logging.getLogger("moonforming") + requests_log = logging.getLogger("requests.packages.urllib3") + requests_log.setLevel(logging.WARNING) + requests_log.propagate = True + + args = parse.parse() + consul_host = args.consul_host + consul_port = args.consul_port + project_id = args.keystone_pid + + models.init(consul_host, consul_port) + policies.init(consul_host, consul_port) + pdp.init(consul_host, consul_port) + + if args.filename: + print("Loading: {}".format(args.filename[0])) + m = SourceFileLoader("scenario", args.filename[0]) + scenario = m.load_module() + + _models = models.check_model() for _model_id, _model_value in _models['models'].items(): if _model_value['name'] == scenario.model_name: model_id = _model_id @@ -232,4 +217,4 @@ if __name__ == "__main__": else: model_id, meta_rule_list = create_model() policy_id = create_policy(model_id, meta_rule_list) - pdp_id = create_pdp(policy_id) + pdp_id = create_pdp(policy_id=policy_id, project_id=project_id) diff --git a/moonv4/tests/send_authz.py b/moonv4/tests/send_authz.py index a307a5c3..5766a0ec 100644 --- a/moonv4/tests/send_authz.py +++ b/moonv4/tests/send_authz.py @@ -1,7 +1,6 @@ import sys -import argparse -import logging import copy +import logging import threading from importlib.machinery import SourceFileLoader import requests @@ -10,107 +9,20 @@ import json import random from uuid import uuid4 from utils.pdp import check_pdp +from utils.parse import parse +import utils.config logger = None -HOST = None -PORT = None +HOST_MANAGER = None +PORT_MANAGER = None HOST_AUTHZ = None PORT_AUTHZ = None HOST_KEYSTONE = None PORT_KEYSTONE = None lock = threading.Lock() - - -def init(): - global logger, HOST, PORT, HOST_AUTHZ, PORT_AUTHZ, HOST_KEYSTONE, PORT_KEYSTONE - parser = argparse.ArgumentParser() - parser.add_argument('filename', help='scenario filename', nargs=1) - parser.add_argument("--verbose", "-v", action='store_true', - help="verbose mode") - parser.add_argument("--debug", "-d", action='store_true', - help="debug mode") - parser.add_argument("--dry-run", "-n", action='store_true', - help="Dry run", dest="dry_run") - parser.add_argument("--destination", - help="Set the type of output needed " - "(default: wrapper, other possible type: " - "interface).", - default="wrapper") - parser.add_argument("--host", - help="Set the name of the host to test " - "(default: 172.18.0.11).", - default="127.0.0.1") - parser.add_argument("--port", "-p", - help="Set the port of the host to test " - "(default: 8082).", - default="8082") - parser.add_argument("--host-authz", - help="Set the name of the host to test authorization " - "(default: 172.18.0.11).", - default="127.0.0.1", - dest="host_authz") - parser.add_argument("--port-authz", - help="Set the port of the host to test authorization " - "(default: 8081).", - default="8081", - dest="port_authz") - parser.add_argument("--host-keystone", - help="Set the name of the Keystone host " - "(default: 172.18.0.11).", - default="127.0.0.1") - parser.add_argument("--port-keystone", - help="Set the port of the Keystone host " - "(default: 5000).", - default="5000") - parser.add_argument("--stress-test", "-s", action='store_true', - dest='stress_test', - help="Execute stressing tests (warning delta measures " - "will be false, implies -t)") - parser.add_argument("--write", "-w", help="Write test data to a JSON file", - default="/tmp/data.json") - parser.add_argument("--pdp", help="Test on pdp PDP") - parser.add_argument("--request-per-second", - help="Number of requests per seconds", - type=int, dest="request_second", default=-1) - parser.add_argument("--limit", help="Limit request to LIMIT", type=int, - default=500) - args = parser.parse_args() - - FORMAT = '%(asctime)-15s %(levelname)s %(message)s' - if args.debug: - logging.basicConfig( - format=FORMAT, - level=logging.DEBUG) - elif args.verbose: - logging.basicConfig( - format=FORMAT, - level=logging.INFO) - else: - logging.basicConfig( - format=FORMAT, - level=logging.WARNING) - - logger = logging.getLogger(__name__) - - requests_log = logging.getLogger("requests.packages.urllib3") - requests_log.setLevel(logging.WARNING) - requests_log.propagate = True - - if args.stress_test: - args.testonly = True - - if args.filename: - logger.info("Loading: {}".format(args.filename[0])) - - HOST = args.host - PORT = args.port - HOST_AUTHZ = args.host_authz - PORT_AUTHZ = args.port_authz - HOST_KEYSTONE = args.host_keystone - PORT_KEYSTONE = args.port_keystone - return args +logger = logging.getLogger(__name__) def get_scenario(args): @@ -119,8 +31,9 @@ def get_scenario(args): def get_keystone_id(pdp_name): + global HOST_MANAGER, PORT_MANAGER keystone_project_id = None - for pdp_key, pdp_value in check_pdp(moon_url="http://{}:{}".format(HOST, PORT))["pdps"].items(): + for pdp_key, pdp_value in check_pdp(moon_url="http://{}:{}".format(HOST_MANAGER, PORT_MANAGER))["pdps"].items(): if pdp_name: if pdp_name != pdp_value["name"]: continue @@ -181,17 +94,23 @@ def _send(url, data=None, stress_test=False): logger.warning("Unable to connect to server") return {} if not stress_test: - if res.status_code == 200: - logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) - elif res.status_code == 401: - logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) - else: - logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) try: j = res.json() + if res.status_code == 200: + logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) + elif res.status_code == 401: + logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) + else: + logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) except Exception as e: - logger.exception(e) - logger.error(res.text) + if res.text == "True": + logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) + elif res.text == "False": + logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) + else: + logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) + logger.exception(e) + logger.error(res.text) else: if j.get("result"): # logger.warning("{} \033[32m{}\033[m".format(url, j.get("result"))) @@ -224,6 +143,7 @@ class AsyncGet(threading.Thread): def send_requests(scenario, keystone_project_id, request_second=1, limit=500, dry_run=None, stress_test=False, destination="wrapper"): + global HOST_AUTHZ, PORT_AUTHZ backgrounds = [] time_data = list() start_timing = time.time() @@ -234,11 +154,14 @@ def send_requests(scenario, keystone_project_id, request_second=1, limit=500, while request_cpt < limit: rule = (random.choice(SUBJECTS), random.choice(OBJECTS), random.choice(ACTIONS)) if destination.lower() == "wrapper": - url = "http://{}:{}/authz".format(HOST_AUTHZ, PORT_AUTHZ, keystone_project_id, "/".join(rule)) + url = "http://{}:{}/authz".format(HOST_AUTHZ, PORT_AUTHZ) data = { 'target': { "user_id": random.choice(SUBJECTS), - "target": {"name": random.choice(OBJECTS)} + "target": { + "name": random.choice(OBJECTS) + }, + "project_id": keystone_project_id }, 'credentials': None, 'rule': random.choice(ACTIONS) @@ -287,7 +210,20 @@ def get_delta(time_data): def main(): - args = init() + global HOST_MANAGER, PORT_MANAGER, HOST_AUTHZ, PORT_AUTHZ + + args = parse() + consul_host = args.consul_host + consul_port = args.consul_port + conf_data = utils.config.get_config_data(consul_host, consul_port) + + HOST_MANAGER = conf_data['manager_host'] + PORT_MANAGER = conf_data['manager_port'] + HOST_AUTHZ = args.authz_host + PORT_AUTHZ = args.authz_port + # HOST_KEYSTONE = conf_data['keystone_host'] + # PORT_KEYSTONE = conf_data['manager_host'] + scenario = get_scenario(args) keystone_project_id = get_keystone_id(args.pdp) time_data = send_requests( diff --git a/moonv4/tests/utils/config.py b/moonv4/tests/utils/config.py index 30c8ea4f..d6317820 100644 --- a/moonv4/tests/utils/config.py +++ b/moonv4/tests/utils/config.py @@ -1,22 +1,44 @@ -import yaml +import base64 +import json +import requests -def get_config_data(filename="moon.conf"): - data_config = None - for _file in ( - filename, - "conf/moon.conf", - "../moon.conf", - "../conf/moon.conf", - "/etc/moon/moon.conf", - ): - try: - data_config = yaml.safe_load(open(_file)) - except FileNotFoundError: - data_config = None - continue - else: - break - if not data_config: - raise Exception("Configuration file not found...") - return data_config +def get_configuration(consul_host, consul_port, key): + url = "http://{}:{}/v1/kv/{}".format(consul_host, consul_port, key) + req = requests.get(url) + if req.status_code != 200: + raise Exception("xxx") + data = req.json() + if len(data) == 1: + data = data[0] + return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} + else: + return [ + {item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8"))} + for item in data + ] + + +def get_config_data(consul_host, consul_port): + conf_data = dict() + conf_data['manager_host'] = get_configuration(consul_host, consul_port, + 'components/manager')['components/manager']['external']['hostname'] + conf_data['manager_port'] = get_configuration(consul_host, consul_port, + 'components/manager')['components/manager']['external']['port'] + # conf_data['authz_host'] = get_configuration(consul_host, consul_port, + # 'components/interface')['components/interface']['external']['hostname'] + # conf_data['authz_port'] = get_configuration(consul_host, consul_port, + # 'components/interface')['components/interface']['external']['port'] + conf_data['keystone_host'] = get_configuration(consul_host, consul_port, + 'openstack/keystone')['openstack/keystone']['external']['url'] + # conf_data['keystone_port'] = '5000' + conf_data['keystone_user'] = get_configuration(consul_host, consul_port, + 'openstack/keystone')['openstack/keystone']['user'] + conf_data['keystone_password'] = get_configuration(consul_host, consul_port, + 'openstack/keystone')['openstack/keystone']['password'] + conf_data['keystone_project'] = get_configuration(consul_host, consul_port, + 'openstack/keystone')['openstack/keystone']['project'] + return conf_data + +# get_conf_data('88.88.88.2', '30005') +# get_conf_data('127.0.0.1', 8082) diff --git a/moonv4/tests/utils/models.py b/moonv4/tests/utils/models.py index 3cf31354..61fa6179 100644 --- a/moonv4/tests/utils/models.py +++ b/moonv4/tests/utils/models.py @@ -2,13 +2,8 @@ import requests import copy import utils.config -config = utils.config.get_config_data() - -URL = "http://{}:{}".format( - config['components']['manager']['hostname'], - config['components']['manager']['port']) -URL = URL + "{}" -HEADERS = {"content-type": "application/json"} +URL = None +HEADERS = None model_template = { "name": "test_model", @@ -29,6 +24,16 @@ meta_rule_template = { } +def init(consul_host, consul_port): + conf_data = utils.config.get_config_data(consul_host, consul_port) + global URL, HEADERS + URL = "http://{}:{}".format( + conf_data['manager_host'], + conf_data['manager_port']) + URL = URL + "{}" + HEADERS = {"content-type": "application/json"} + + def check_model(model_id=None, check_model_name=True): req = requests.get(URL.format("/models")) assert req.status_code == 200 diff --git a/moonv4/tests/utils/parse.py b/moonv4/tests/utils/parse.py new file mode 100644 index 00000000..34a4a996 --- /dev/null +++ b/moonv4/tests/utils/parse.py @@ -0,0 +1,83 @@ +import logging +import argparse + + +logger = None + + +def parse(): + global logger + logger = logging.getLogger(__name__) + requests_log = logging.getLogger("requests.packages.urllib3") + requests_log.setLevel(logging.WARNING) + requests_log.propagate = True + + parser = argparse.ArgumentParser() + parser.add_argument('filename', help='scenario filename', nargs=1) + parser.add_argument("--verbose", "-v", action='store_true', + help="verbose mode") + parser.add_argument("--debug", "-d", action='store_true', + help="debug mode") + parser.add_argument("--dry-run", "-n", action='store_true', + help="Dry run", dest="dry_run") + parser.add_argument("--destination", + help="Set the type of output needed " + "(default: wrapper, other possible type: " + "interface).", + default="wrapper") + parser.add_argument("--consul-host", + help="Set the name of the consul server" + "(default: 127.0.0.1).", + default="127.0.0.1") + parser.add_argument("--consul-port", + help="Set the port of the consult server" + "(default: 8082).", + default="8082") + parser.add_argument("--authz-host", + help="Set the name of the authz server to test" + "(default: 127.0.0.1).", + default="127.0.0.1") + parser.add_argument("--authz-port", + help="Set the port of the authz server to test" + "(default: 31002).", + default="31002") + parser.add_argument("--keystone-pid", "--keystone-project-id", + help="Set the Keystone project ID" + "(default: None).", + default=None) + parser.add_argument("--stress-test", "-s", action='store_true', + dest='stress_test', + help="Execute stressing tests (warning delta measures " + "will be false, implies -t)") + parser.add_argument("--write", "-w", help="Write test data to a JSON file", + default="/tmp/data.json") + parser.add_argument("--pdp", help="Test on pdp PDP") + parser.add_argument("--request-per-second", + help="Number of requests per seconds", + type=int, dest="request_second", default=-1) + parser.add_argument("--limit", help="Limit request to LIMIT", type=int, + default=500) + + args = parser.parse_args() + + FORMAT = '%(asctime)-15s %(levelname)s %(message)s' + if args.debug: + logging.basicConfig( + format=FORMAT, + level=logging.DEBUG) + elif args.verbose: + logging.basicConfig( + format=FORMAT, + level=logging.INFO) + else: + logging.basicConfig( + format=FORMAT, + level=logging.WARNING) + + if args.stress_test: + args.testonly = True + + if args.filename: + logger.info("Loading: {}".format(args.filename[0])) + + return args diff --git a/moonv4/tests/utils/pdp.py b/moonv4/tests/utils/pdp.py index f3c6df37..50998507 100644 --- a/moonv4/tests/utils/pdp.py +++ b/moonv4/tests/utils/pdp.py @@ -2,17 +2,16 @@ import logging import requests import utils.config -config = utils.config.get_config_data() logger = logging.getLogger("moonforming.utils.policies") +URL = None +HEADER = None +KEYSTONE_USER = None +KEYSTONE_PASSWORD = None +KEYSTONE_PROJECT = None +KEYSTONE_SERVER = None + +# config = utils.config.get_config_data() -URL = "http://{}:{}".format( - config['components']['manager']['hostname'], - config['components']['manager']['port']) -HEADERS = {"content-type": "application/json"} -KEYSTONE_USER = config['openstack']['keystone']['user'] -KEYSTONE_PASSWORD = config['openstack']['keystone']['password'] -KEYSTONE_PROJECT = config['openstack']['keystone']['project'] -KEYSTONE_SERVER = config['openstack']['keystone']['url'] pdp_template = { "name": "test_pdp", @@ -22,8 +21,22 @@ pdp_template = { } -def get_keystone_projects(): +def init(consul_host, consul_port): + conf_data = utils.config.get_config_data(consul_host, consul_port) + global URL, HEADER, KEYSTONE_USER, KEYSTONE_PASSWORD, KEYSTONE_PROJECT, KEYSTONE_SERVER + URL = "http://{}:{}".format( + conf_data['manager_host'], + conf_data['manager_port']) + # URL = URL + "{}" + HEADER = {"content-type": "application/json"} + KEYSTONE_USER = conf_data['keystone_user'] + KEYSTONE_PASSWORD = conf_data['keystone_password'] + KEYSTONE_PROJECT = conf_data['keystone_project'] + KEYSTONE_SERVER = conf_data['keystone_host'] + +def get_keystone_projects(): + global HEADERS HEADERS = { "Content-Type": "application/json" } @@ -160,4 +173,3 @@ def delete_pdp(pdp_id): assert type(result) is dict assert "result" in result assert result["result"] - diff --git a/moonv4/tests/utils/policies.py b/moonv4/tests/utils/policies.py index bd08291a..fd4d238f 100644 --- a/moonv4/tests/utils/policies.py +++ b/moonv4/tests/utils/policies.py @@ -2,13 +2,10 @@ import logging import requests import utils.config -config = utils.config.get_config_data() -logger = logging.getLogger("moonforming.utils.policies") - -URL = "http://{}:{}".format(config['components']['manager']['hostname'], config['components']['manager']['port']) -URL = URL + "{}" -HEADERS = {"content-type": "application/json"} +URL = None +HEADERS = None FILE = open("/tmp/test.log", "w") +logger = logging.getLogger("utils.policies") policy_template = { "name": "test_policy", @@ -56,6 +53,16 @@ subject_assignment_template = { } +def init(consul_host, consul_port): + conf_data = utils.config.get_config_data(consul_host, consul_port) + global URL, HEADERS + URL = "http://{}:{}".format( + conf_data['manager_host'], + conf_data['manager_port']) + URL = URL + "{}" + HEADERS = {"content-type": "application/json"} + + def check_policy(policy_id=None): req = requests.get(URL.format("/policies")) assert req.status_code == 200 -- cgit 1.2.3-korg