aboutsummaryrefslogtreecommitdiffstats
path: root/python_moonutilities/tests/unit_python/mock_repo
diff options
context:
space:
mode:
authorRuan HE <ruan.he@orange.com>2017-12-27 15:30:35 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-12-27 15:30:35 +0000
commitaa5ddd83324338e98a06a98dd7f526a1b66edac0 (patch)
tree892b99ac7100577516e724cb2bbce0e2fc3ff8c3 /python_moonutilities/tests/unit_python/mock_repo
parentde710abf2bcff9fc14862eef1ad4c00c207d9e14 (diff)
parent12df3719531f1aac21e35efab3688927a42a4b70 (diff)
Merge "adding unit tests for cache module"
Diffstat (limited to 'python_moonutilities/tests/unit_python/mock_repo')
-rw-r--r--python_moonutilities/tests/unit_python/mock_repo/__init__.py38
-rw-r--r--python_moonutilities/tests/unit_python/mock_repo/components_utilities.py136
-rw-r--r--python_moonutilities/tests/unit_python/mock_repo/data.py215
-rw-r--r--python_moonutilities/tests/unit_python/mock_repo/urls.py147
4 files changed, 536 insertions, 0 deletions
diff --git a/python_moonutilities/tests/unit_python/mock_repo/__init__.py b/python_moonutilities/tests/unit_python/mock_repo/__init__.py
new file mode 100644
index 00000000..60dfbc3b
--- /dev/null
+++ b/python_moonutilities/tests/unit_python/mock_repo/__init__.py
@@ -0,0 +1,38 @@
+import mock_repo.urls as register_urls
+import mock_repo.data as data_mock
+
+
+def register_cache(m):
+ """ Modify the response from Requests module
+ """
+ register_urls.register_components(m)
+ register_urls.register_keystone(m)
+
+ register_urls.register_pdp(m)
+ register_urls.register_meta_rules(m)
+ register_urls.register_policies(m)
+ register_urls.register_models(m)
+
+ register_urls.register_policy_subject(m, data_mock.shared_ids["policy"]["policy_id_1"])
+ register_urls.register_policy_subject_invalid_response(m, data_mock.shared_ids["policy"]["policy_id_invalid_response"])
+ register_urls.register_policy_object(m, data_mock.shared_ids["policy"]["policy_id_1"])
+ register_urls.register_policy_action(m, data_mock.shared_ids["policy"]["policy_id_1"])
+
+ register_urls.register_policy_subject_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_1"])
+
+ register_urls.register_policy_subject_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"])
+
+ register_urls.register_policy_object_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_2"])
+
+ register_urls.register_policy_object_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"])
+
+ register_urls.register_policy_action_assignment(m, data_mock.shared_ids["policy"]["policy_id_1"], data_mock.shared_ids["perimeter"]["perimeter_id_3"])
+
+ register_urls.register_policy_action_assignment_list(m, data_mock.shared_ids["policy"]["policy_id_2"])
+ # register_urls.register_pods(m)
+
+ # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2")
+ # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2")
+ # register_urls.register_policy_action_assignment(m, "policy_id_2", "perimeter_id_2")
+
+ register_urls.register_rules(m, "policy_id1")
diff --git a/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py b/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py
new file mode 100644
index 00000000..72956f3a
--- /dev/null
+++ b/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py
@@ -0,0 +1,136 @@
+import base64
+import json
+
+
+CONF = {
+ "openstack": {
+ "keystone": {
+ "url": "http://keystone:5000/v3",
+ "user": "admin",
+ "check_token": False,
+ "password": "p4ssw0rd",
+ "domain": "default",
+ "certificate": False,
+ "project": "admin"
+ }
+ },
+ "components": {
+ "wrapper": {
+ "bind": "0.0.0.0",
+ "port": 8080,
+ "container": "wukongsun/moon_wrapper:v4.3",
+ "timeout": 5,
+ "hostname": "wrapper"
+ },
+ "manager": {
+ "bind": "0.0.0.0",
+ "port": 8082,
+ "container": "wukongsun/moon_manager:v4.3",
+ "hostname": "manager"
+ },
+ "port_start": 31001,
+ "orchestrator": {
+ "bind": "0.0.0.0",
+ "port": 8083,
+ "container": "wukongsun/moon_orchestrator:v4.3",
+ "hostname": "interface"
+ },
+ "interface": {
+ "bind": "0.0.0.0",
+ "port": 8080,
+ "container": "wukongsun/moon_interface:v4.3",
+ "hostname": "interface"
+ }
+ },
+ "plugins": {
+ "session": {
+ "port": 8082,
+ "container": "asteroide/session:latest"
+ },
+ "authz": {
+ "port": 8081,
+ "container": "wukongsun/moon_authz:v4.3"
+ }
+ },
+ "logging": {
+ "handlers": {
+ "file": {
+ "filename": "C:/moon.log",
+ "class": "logging.handlers.RotatingFileHandler",
+ "level": "DEBUG",
+ "formatter": "custom",
+ "backupCount": 3,
+ "maxBytes": 1048576
+ },
+ "console": {
+ "class": "logging.StreamHandler",
+ "formatter": "brief",
+ "level": "INFO",
+ "stream": "ext://sys.stdout"
+ }
+ },
+ "formatters": {
+ "brief": {
+ "format": "%(levelname)s %(name)s %(message)-30s"
+ },
+ "custom": {
+ "format": "%(asctime)-15s %(levelname)s %(name)s %(message)s"
+ }
+ },
+ "root": {
+ "handlers": [
+ "console"
+ ],
+ "level": "ERROR"
+ },
+ "version": 1,
+ "loggers": {
+ "moon": {
+ "handlers": [
+ "console",
+ "file"
+ ],
+ "propagate": False,
+ "level": "DEBUG"
+ }
+ }
+ },
+ "slave": {
+ "name": None,
+ "master": {
+ "url": None,
+ "login": None,
+ "password": None
+ }
+ },
+ "docker": {
+ "url": "tcp://172.88.88.1:2376",
+ "network": "moon"
+ },
+ "database": {
+ "url": "sqlite:///database.db",
+ # "url": "mysql+pymysql://moon:p4sswOrd1@db/moon",
+ "driver": "sql"
+ },
+ "messenger": {
+ "url": "rabbit://moon:p4sswOrd1@messenger:5672/moon"
+ }
+}
+
+
+def get_b64_conf(component=None):
+ if component == "components":
+ return base64.b64encode(
+ json.dumps(CONF["components"]).encode('utf-8')+b"\n").decode('utf-8')
+ elif component in CONF:
+ return base64.b64encode(
+ json.dumps(
+ CONF[component]).encode('utf-8')+b"\n").decode('utf-8')
+ elif not component:
+ return base64.b64encode(
+ json.dumps(CONF).encode('utf-8')+b"\n").decode('utf-8')
+ elif "/" in component:
+ key1, _, key2 = component.partition("/")
+ return base64.b64encode(
+ json.dumps(
+ CONF[key1][key2]).encode('utf-8')+b"\n").decode('utf-8')
diff --git a/python_moonutilities/tests/unit_python/mock_repo/data.py b/python_moonutilities/tests/unit_python/mock_repo/data.py
new file mode 100644
index 00000000..736d4704
--- /dev/null
+++ b/python_moonutilities/tests/unit_python/mock_repo/data.py
@@ -0,0 +1,215 @@
+components = (
+ "logging",
+ "openstack/keystone",
+ "database",
+ "slave",
+ "components/manager",
+ "components/orchestrator",
+ "components/interface",
+ "components/port_start"
+)
+
+shared_ids = {
+ "policy": {
+ "policy_id_1": "policy_id_1",
+ "policy_id_2": "policy_id_2",
+ "policy_id_3": "policy_id_3",
+ "policy_id_invalid_response": "policy_id_invalid_response"
+ },
+ "category": {
+ "category_id_1": "category_id_1",
+ "invalid_category_id_1": " invalid_category_id_1"
+ },
+ "perimeter": {
+ "perimeter_id_1": "subject_id_1",
+ "perimeter_id_2": "object_id_1",
+ "perimeter_id_3": "action_id_1"
+ },
+ "meta_rule": {
+ "meta_rule_id_1": "meta_rule_id_1",
+ "meta_rule_id_2": "meta_rule_id_2"
+ },
+ "rule": {
+ "rule_id_1": "rule_id_2",
+ "rule_id_2": "rule_id_2"
+ },
+ "model": {
+ "model_id_1": "model_id_1"
+ }
+}
+
+pdp_mock = {
+ "pdp_id1": {
+ "name": "...",
+ "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "keystone_project_id": "keystone_project_id1",
+ "description": "...",
+ }
+}
+
+meta_rules_mock = {
+ shared_ids["meta_rule"]["meta_rule_id_1"]: {
+ "name": "meta_rule1",
+ "algorithm": "name of the meta rule algorithm",
+ "subject_categories": ["subject_category_id1",
+ "subject_category_id2"],
+ "object_categories": ["object_category_id1"],
+ "action_categories": ["action_category_id1"]
+ },
+ shared_ids["meta_rule"]["meta_rule_id_2"]: {
+ "name": "name of the meta rules2",
+ "algorithm": "name of the meta rule algorithm",
+ "subject_categories": ["subject_category_id1",
+ "subject_category_id2"],
+ "object_categories": ["object_category_id1"],
+ "action_categories": ["action_category_id1"]
+ }
+}
+
+policies_mock = {
+ shared_ids["policy"]["policy_id_1"]: {
+ "name": "test_policy1",
+ "model_id": shared_ids["model"]["model_id_1"],
+ "genre": "authz",
+ "description": "test",
+ }
+}
+
+subject_mock = {
+ shared_ids["policy"]["policy_id_1"]: {
+ "subject_id": {
+ "name": "subject_name",
+ "keystone_id": "keystone_project_id1",
+ "description": "a description"
+ }
+ },
+ shared_ids["policy"]["policy_id_invalid_response"]: {
+ "subject_id": {
+ "name": "subject_name",
+ "keystone_id": "keystone_project_id1",
+ "description": "a description"
+ }
+ }
+
+}
+
+subject_assignment_mock = {
+ "subject_id_1": {
+ "policy_id": shared_ids["policy"]["policy_id_1"],
+ "subject_id": "subject_id_1",
+ "category_id": shared_ids["category"]["category_id_1"],
+ "assignments": ["data_id_1, data_id_2"],
+ }
+}
+
+object_mock = {
+ shared_ids["policy"]["policy_id_1"]: {
+ "object_id": {
+ "name": "object_name",
+ "description": "a description"
+ }
+ }
+}
+
+object_assignment_mock = {
+ "object_id_1": {
+ "policy_id": shared_ids["policy"]["policy_id_1"],
+ "object_id": "object_id_1",
+ "category_id": shared_ids["category"]["category_id_1"],
+ "assignments": ["data_id_1, data_id_2"],
+ }
+}
+
+action_mock = {
+ shared_ids["policy"]["policy_id_1"]: {
+ "action_id": {
+ "name": "action_name",
+ "description": "a description"
+ }
+ }
+}
+
+action_assignment_mock = {
+ "action_id_1": {
+ "policy_id": shared_ids["policy"]["policy_id_1"],
+ "action_id": "action_id_1",
+ "category_id": shared_ids["category"]["category_id_1"],
+ "assignments": ["data_id_1, data_id_2"],
+ }
+}
+
+models_mock = {
+ shared_ids["model"]["model_id_1"]: {
+ "name": "test_model",
+ "description": "test",
+ "meta_rules": [shared_ids["meta_rule"]["meta_rule_id_1"]]
+ }
+}
+
+rules_mock = {
+ "rules": {
+ "meta_rule_id": shared_ids["meta_rule"]["meta_rule_id_1"],
+ shared_ids["rule"]["rule_id_1"]: {
+ "rule": ["subject_data_id1",
+ "object_data_id1",
+ "action_data_id1"],
+ "instructions": (
+ {"decision": "grant"},
+ # "grant" to immediately exit,
+ # "continue" to wait for the result of next policy
+ # "deny" to deny the request
+ )
+ },
+ shared_ids["rule"]["rule_id_2"]: {
+ "rule": ["subject_data_id2",
+ "object_data_id2",
+ "action_data_id2"],
+ "instructions": (
+ {
+ "update": {
+ "operation": "add",
+ # operations may be "add" or "delete"
+ "target": "rbac:role:admin"
+ # add the role admin to the current user
+ }
+ },
+ {"chain": {"name": "rbac"}}
+ # chain with the policy named rbac
+ )
+ }
+ }
+}
+
+# pods_mock = {
+# # "name": "pod_id1",
+# # "hostname": "pod_host",
+# # "port": {
+# # "PrivatePort": "8998",
+# # "Type": "tcp",
+# # "IP": "0.0.0.0",
+# # "PublicPort": "8080"
+# # },
+# # "keystone_project_id": "keystone_project_id1",
+# # "pdp_id": "",
+# # "meta_rule_id": "meta_rule_id1",
+# # "container_name": "container_name1",
+# # "plugin_name": "plugin_name1",
+# # "container_id": "container_id"
+# "pod_id1": {
+# "name": "pod_id1",
+# "hostname": "pod_host",
+# "port": {
+# "PrivatePort": "8998",
+# "Type": "tcp",
+# "IP": "0.0.0.0",
+# "PublicPort": "8080"
+# },
+# "keystone_project_id": [1],
+# "pdp_id": "",
+# "meta_rule_id": "meta_rule_id1",
+# "container_name": "container_name1",
+# "plugin_name": "plugin_name1",
+# "container_id": "container_id"
+# },
+#
+# }
diff --git a/python_moonutilities/tests/unit_python/mock_repo/urls.py b/python_moonutilities/tests/unit_python/mock_repo/urls.py
new file mode 100644
index 00000000..a5b1e63b
--- /dev/null
+++ b/python_moonutilities/tests/unit_python/mock_repo/urls.py
@@ -0,0 +1,147 @@
+import mock_repo.components_utilities as comp_util
+import mock_repo.data as data_mock
+
+
+def register_components(m):
+ for component in data_mock.components:
+ m.register_uri(
+ 'GET', 'http://consul:8500/v1/kv/{}'.format(component),
+ json=[{'Key': component, 'Value': comp_util.get_b64_conf(component)}]
+ )
+ m.register_uri(
+ 'GET', 'http://consul:8500/v1/kv/components_port_start',
+ json=[{'Key': 'components_port_start', 'Value': comp_util.get_b64_conf("components/port_start")}]
+ )
+ m.register_uri(
+ 'PUT', 'http://consul:8500/v1/kv/components_port_start',
+ json=[]
+ )
+
+ m.register_uri(
+ 'GET', 'http://consul:8500/v1/kv/components?recurse=true',
+ json=[
+ {"Key": key, "Value": comp_util.get_b64_conf(key)} for key in data_mock.components
+ ],
+ # json={'Key': "components", 'Value': get_b64_comp_util.CONF("components")}
+ )
+
+
+def register_keystone(m):
+ m.register_uri(
+ 'POST', 'http://keystone:5000/v3/auth/tokens',
+ headers={'X-Subject-Token': "111111111"}
+ )
+ m.register_uri(
+ 'DELETE', 'http://keystone:5000/v3/auth/tokens',
+ headers={'X-Subject-Token': "111111111"}
+ )
+ m.register_uri(
+ 'POST', 'http://keystone:5000/v3/users?name=testuser&domain_id=default',
+ json={"users": {}}
+ )
+ m.register_uri(
+ 'GET', 'http://keystone:5000/v3/users?name=testuser&domain_id=default',
+ json={"users": {}}
+ )
+ m.register_uri(
+ 'POST', 'http://keystone:5000/v3/users/',
+ json={"users": [{
+ "id": "1111111111111"
+ }]}
+ )
+
+def register_model_any(m, module_name, mocked_data, key=None):
+ if key is None:
+ key = module_name
+ m.register_uri(
+ 'GET', 'http://{}:{}/{}'.format(comp_util.CONF['components']['manager']['hostname'],
+ comp_util.CONF['components']['manager']['port'], module_name),
+
+ json={key: mocked_data}
+ )
+
+def register_policy_any(m, policy_id, module_name, mocked_data, key=None):
+ if key is None:
+ key = module_name
+ m.register_uri(
+ 'GET', 'http://{}:{}/{}/{}/{}'.format(comp_util.CONF['components']['manager']['hostname'],
+ comp_util.CONF['components']['manager']['port'], 'policies',
+ policy_id, module_name),
+ json={key: mocked_data}
+ )
+
+def register_pdp(m):
+ register_model_any(m, 'pdp', data_mock.pdp_mock,'pdps')
+
+def register_meta_rules(m):
+ register_model_any(m, 'meta_rules',data_mock.meta_rules_mock)
+
+def register_policies(m):
+ register_model_any(m, 'policies', data_mock.policies_mock)
+
+
+def register_models(m):
+ register_model_any(m, 'models', data_mock.models_mock)
+
+def register_policy_subject(m, policy_id):
+ register_policy_any(m, policy_id, 'subjects', data_mock.subject_mock[policy_id])
+
+
+def register_policy_subject_invalid_response(m, policy_id):
+ register_policy_any(m, policy_id, 'subjects', data_mock.subject_mock[policy_id],'subjects_invalid_key')
+
+def register_policy_object(m, policy_id):
+ register_policy_any(m, policy_id, 'objects', data_mock.object_mock[policy_id])
+
+def register_policy_action(m, policy_id):
+ register_policy_any(m, policy_id, 'actions', data_mock.action_mock[policy_id])
+
+def register_policy_subject_assignment_list(m, policy_id):
+ register_policy_any(m, policy_id, 'subject_assignments', data_mock.subject_assignment_mock)
+
+def register_policy_object_assignment_list(m, policy_id):
+ register_policy_any(m, policy_id, 'object_assignments', data_mock.object_assignment_mock)
+
+
+def register_policy_action_assignment_list(m, policy_id):
+ register_policy_any(m, policy_id, 'action_assignments', data_mock.action_assignment_mock)
+
+def register_policy_subject_assignment(m, policy_id, perimeter_id):
+ m.register_uri(
+ 'GET', 'http://{}:{}/{}/{}/subject_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'],
+ comp_util.CONF['components']['manager']['port'],
+ 'policies',
+ policy_id,
+ perimeter_id),
+ json={'subject_assignments': data_mock.subject_assignment_mock}
+ )
+
+def register_policy_object_assignment(m, policy_id, perimeter_id):
+ m.register_uri(
+ 'GET', 'http://{}:{}/{}/{}/object_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'],
+ comp_util.CONF['components']['manager']['port'],
+ 'policies',
+ policy_id,
+ perimeter_id),
+ json={'object_assignments': data_mock.object_assignment_mock}
+ )
+
+def register_policy_action_assignment(m, policy_id, perimeter_id):
+ m.register_uri(
+ 'GET', 'http://{}:{}/{}/{}/action_assignments/{}'.format(comp_util.CONF['components']['manager']['hostname'],
+ comp_util.CONF['components']['manager']['port'],
+ 'policies',
+ policy_id,
+ perimeter_id),
+ json={'action_assignments': data_mock.action_assignment_mock}
+ )
+
+def register_rules(m, policy_id):
+ register_policy_any(m, policy_id, 'rules', data_mock.rules_mock)
+
+# def register_pods(m):
+# m.register_uri(
+# 'GET', 'http://{}:{}/pods'.format(comp_util.CONF['components']['orchestrator']['hostname'],
+# comp_util.CONF['components']['orchestrator']['port']),
+# json={'pods': data_mock.pods_mock}
+# )