aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/tests/utils
diff options
context:
space:
mode:
Diffstat (limited to 'moonv4/tests/utils')
-rw-r--r--moonv4/tests/utils/config.py62
-rw-r--r--moonv4/tests/utils/models.py19
-rw-r--r--moonv4/tests/utils/parse.py83
-rw-r--r--moonv4/tests/utils/pdp.py34
-rw-r--r--moonv4/tests/utils/policies.py19
5 files changed, 173 insertions, 44 deletions
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