aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRuan HE <ruan.he@orange.com>2017-12-01 10:20:51 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-12-01 10:20:51 +0000
commitfe8cf8dcabfcd223e8661f3af8ac02f4e6b1e708 (patch)
treed22a596e34de9ca9207a7224077dd150a4523fd0
parent9ba538bba8e2e9a3528ad0a055f8bfdc886e4429 (diff)
parent9c14ca82d4d17dfa66cb0b68f1e9c72b62ff1749 (diff)
Merge "Add unit tests for moon_db"
-rw-r--r--moonv4/moon_db/tests/README.md35
-rw-r--r--moonv4/moon_db/tests/unit_python/configure_db.sh9
-rw-r--r--moonv4/moon_db/tests/unit_python/conftest.py201
-rw-r--r--moonv4/moon_db/tests/unit_python/requirements.txt5
-rw-r--r--moonv4/moon_db/tests/unit_python/test_intraextension.py44
-rw-r--r--moonv4/moon_db/tests/unit_python/test_policies.py77
-rw-r--r--moonv4/moon_db/tests/unit_python/test_tenant.py86
7 files changed, 318 insertions, 139 deletions
diff --git a/moonv4/moon_db/tests/README.md b/moonv4/moon_db/tests/README.md
new file mode 100644
index 00000000..73a9fcd2
--- /dev/null
+++ b/moonv4/moon_db/tests/README.md
@@ -0,0 +1,35 @@
+# Tests
+
+## Python Unit Test for moon_db
+
+- launch Docker for Python unit tests
+
+
+ cd ${MOON_HOME}/moonv4/moon_db/
+ docker run -ti --volume ${PWD}:/data asteroide/moon_tests
+
+
+## Build and upload python packages
+
+- build python packages
+
+
+ python setup.py sdist bdist_wheel
+
+
+- upload moon_db to PIP
+
+
+ python setup.py upload
+
+
+or
+
+
+ gpg --detach-sign -u "${GPG_ID}" -a dist/moon_db-X.Y.Z-py3-none-any.whl
+ gpg --detach-sign -u "${GPG_ID}" -a dist/moon_db-X.Y.Z.tar.gz
+ twine upload dist/moon_db-X.Y.Z-py3-none-any.whl dist/moon_db-X.Y.Z-py3-none-any.whl.asc
+ twine upload dist/moon_db-X.Y.Z.tar.gz dist/moon_db-X.Y.Z.tar.gz.asc
+
+
+
diff --git a/moonv4/moon_db/tests/unit_python/configure_db.sh b/moonv4/moon_db/tests/unit_python/configure_db.sh
deleted file mode 100644
index bdc259fe..00000000
--- a/moonv4/moon_db/tests/unit_python/configure_db.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/usr/bin/env bash
-
-apt-get install mysql-server python-mysqldb python-pymysql
-
-mysql -uroot -ppassword <<EOF
-CREATE DATABASE moon DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
-GRANT ALL ON moon.* TO 'moonuser'@'%' IDENTIFIED BY 'password';
-GRANT ALL ON moon.* TO 'moonuser'@'localhost' IDENTIFIED BY 'password';
-EOF
diff --git a/moonv4/moon_db/tests/unit_python/conftest.py b/moonv4/moon_db/tests/unit_python/conftest.py
new file mode 100644
index 00000000..6cce944e
--- /dev/null
+++ b/moonv4/moon_db/tests/unit_python/conftest.py
@@ -0,0 +1,201 @@
+import base64
+import json
+import logging
+import os
+import pytest
+import requests_mock
+
+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": "/tmp/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"
+ }
+}
+
+COMPONENTS = (
+ "logging",
+ "openstack/keystone",
+ "database",
+ "slave",
+ "components/manager",
+ "components/orchestrator",
+ "components/interface",
+)
+
+
+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')
+
+
+@pytest.fixture
+def db():
+ return CONF['database']
+
+
+@pytest.fixture(autouse=True)
+def set_consul_and_db(monkeypatch):
+ """ Modify the response from Requests module
+ """
+ with requests_mock.Mocker(real_http=True) as m:
+ for component in COMPONENTS:
+ m.register_uri(
+ 'GET', 'http://consul:8500/v1/kv/{}'.format(component),
+ json=[{'Key': component, 'Value': get_b64_conf(component)}]
+ )
+ m.register_uri(
+ 'GET', 'http://consul:8500/v1/kv/components?recurse=true',
+ json=[
+ {"Key": key, "Value": get_b64_conf(key)} for key in COMPONENTS
+ ],
+ )
+ 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"
+ }]}
+ )
+ from moon_db.db_manager import init_engine, run
+ engine = init_engine()
+ run("upgrade", logging.getLogger("db_manager"), engine)
+ yield m
+ os.unlink(CONF['database']['url'].replace("sqlite:///", ""))
+
+
diff --git a/moonv4/moon_db/tests/unit_python/requirements.txt b/moonv4/moon_db/tests/unit_python/requirements.txt
new file mode 100644
index 00000000..d6110d12
--- /dev/null
+++ b/moonv4/moon_db/tests/unit_python/requirements.txt
@@ -0,0 +1,5 @@
+sqlalchemy
+pymysql
+pytest
+requests_mock
+moon_utilities \ No newline at end of file
diff --git a/moonv4/moon_db/tests/unit_python/test_intraextension.py b/moonv4/moon_db/tests/unit_python/test_intraextension.py
deleted file mode 100644
index a2267214..00000000
--- a/moonv4/moon_db/tests/unit_python/test_intraextension.py
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
-# This software is distributed under the terms and conditions of the 'Apache-2.0'
-# license which can be found in the file 'LICENSE' in this package distribution
-# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-
-
-import moon_db
-import uuid
-
-Connector = moon_db.Driver("sql", "mysql+pymysql://moonuser:password@localhost/moon")
-Connector.driver.init_db()
-
-
-def create_intra_extension(policy_model="policy_authz"):
- ie = dict()
- ie['id'] = uuid.uuid4().hex
- ie["name"] = "test IE " + uuid.uuid4().hex
- ie["policymodel"] = "policy_authz"
- ie["description"] = "a simple description."
- ie["model"] = policy_model
- genre = "admin"
- if "authz" in policy_model:
- genre = "authz"
- ie["genre"] = genre
- # ref = self.admin_api.load_intra_extension_dict(self.root_api.root_admin_id,
- # intra_extension_dict=ie)
- # self.admin_api.populate_default_data(ref)
- return ie
-
-
-def test_get_intraextension():
- t = Connector.driver.get_intra_extensions_dict()
- assert type(t) == dict
-
-
-def test_set_intra_extension():
- number_of_ie = len(Connector.driver.get_intra_extensions_dict())
- ie = create_intra_extension()
- data = Connector.driver.set_intra_extension_dict(ie['id'], ie)
- assert type(data) == dict
- assert len(Connector.driver.get_intra_extensions_dict()) == number_of_ie+1
-
-
-# TODO (dthom): all tests can be got from keystone-moon
diff --git a/moonv4/moon_db/tests/unit_python/test_policies.py b/moonv4/moon_db/tests/unit_python/test_policies.py
new file mode 100644
index 00000000..3bd1360e
--- /dev/null
+++ b/moonv4/moon_db/tests/unit_python/test_policies.py
@@ -0,0 +1,77 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+
+def get_policies():
+ from moon_db.core import PolicyManager
+ return PolicyManager.get_policies("admin")
+
+
+def add_policies(value=None):
+ from moon_db.core import PolicyManager
+ if not value:
+ value = {
+ "name": "test_policiy",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ return PolicyManager.add_policy("admin", value=value)
+
+
+def delete_policies(uuid=None, name=None):
+ from moon_db.core import PolicyManager
+ if not uuid:
+ for policy_id, policy_value in get_policies():
+ if name == policy_value['name']:
+ uuid = policy_id
+ break
+ PolicyManager.delete_policy("admin", uuid)
+
+
+def test_get_policies(db):
+ policies = get_policies()
+ assert isinstance(policies, dict)
+ assert not policies
+
+
+def test_add_policies(db):
+ value = {
+ "name": "test_policy",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ policies = add_policies(value)
+ assert isinstance(policies, dict)
+ assert policies
+ assert len(policies.keys()) == 1
+ policy_id = list(policies.keys())[0]
+ for key in ("genre", "name", "model_id", "description"):
+ assert key in policies[policy_id]
+ assert policies[policy_id][key] == value[key]
+
+
+def test_delete_policies(db):
+ value = {
+ "name": "test_policy1",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ policies = add_policies(value)
+ policy_id1 = list(policies.keys())[0]
+ value = {
+ "name": "test_policy2",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ policies = add_policies(value)
+ policy_id2 = list(policies.keys())[0]
+ assert policy_id1 != policy_id2
+ delete_policies(policy_id1)
+ policies = get_policies()
+ assert policy_id1 not in policies
diff --git a/moonv4/moon_db/tests/unit_python/test_tenant.py b/moonv4/moon_db/tests/unit_python/test_tenant.py
deleted file mode 100644
index 7e6cfa82..00000000
--- a/moonv4/moon_db/tests/unit_python/test_tenant.py
+++ /dev/null
@@ -1,86 +0,0 @@
-# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
-# This software is distributed under the terms and conditions of the 'Apache-2.0'
-# license which can be found in the file 'LICENSE' in this package distribution
-# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-
-import moon_db
-import uuid
-
-Connector = moon_db.Driver("sql", "mysql+pymysql://moonuser:password@localhost/moon")
-Connector.driver.init_db()
-
-
-def test_get_tenants():
- t = Connector.driver.get_tenants_dict()
- print(t)
- assert type(t) == dict
-
-
-def test_add_tenant():
- new_tenant = {
- "id": uuid.uuid4().hex,
- "name": "demo",
- "description": uuid.uuid4().hex,
- "intra_authz_extension_id": "",
- "intra_admin_extension_id": "",
- }
- data = Connector.driver.add_tenant_dict(tenant_id=new_tenant['id'],
- tenant_dict=new_tenant)
- data_id = list(data.keys())[0]
- assert new_tenant["id"] == data_id
- assert new_tenant["name"] == data[data_id]["name"]
- assert new_tenant["intra_authz_extension_id"] == data[data_id]["intra_authz_extension_id"]
- assert new_tenant["intra_admin_extension_id"] == data[data_id]["intra_admin_extension_id"]
- data = Connector.driver.get_tenants_dict()
- assert data != {}
-
-
-def test_del_tenant():
- new_tenant = {
- "id": uuid.uuid4().hex,
- "name": "demo",
- "description": uuid.uuid4().hex,
- "intra_authz_extension_id": "",
- "intra_admin_extension_id": "",
- }
- data = Connector.driver.get_tenants_dict()
- number_of_tenant = len(data.keys())
- data = Connector.driver.add_tenant_dict(tenant_id=new_tenant['id'],
- tenant_dict=new_tenant)
- data_id = list(data.keys())[0]
- assert new_tenant["name"] == data[data_id]["name"]
- assert new_tenant["intra_authz_extension_id"] == data[data_id]["intra_authz_extension_id"]
- assert new_tenant["intra_admin_extension_id"] == data[data_id]["intra_admin_extension_id"]
- data = Connector.driver.get_tenants_dict()
- assert len(data.keys()) == number_of_tenant+1
- Connector.driver.del_tenant(data_id)
- data = Connector.driver.get_tenants_dict()
- assert len(data.keys()) == number_of_tenant
-
-
-def test_set_tenant():
- new_tenant = {
- "id": uuid.uuid4().hex,
- "name": "demo",
- "description": uuid.uuid4().hex,
- "intra_authz_extension_id": "123456",
- "intra_admin_extension_id": "0987654",
- }
- data = Connector.driver.get_tenants_dict()
- number_of_tenant = len(data.keys())
- data = Connector.driver.add_tenant_dict(tenant_id=new_tenant['id'],
- tenant_dict=new_tenant)
- data_id = list(data.keys())[0]
- assert new_tenant["name"] == data[data_id]["name"]
- assert new_tenant["intra_authz_extension_id"] == data[data_id]["intra_authz_extension_id"]
- assert new_tenant["intra_admin_extension_id"] == data[data_id]["intra_admin_extension_id"]
- data = Connector.driver.get_tenants_dict()
- assert len(data.keys()) == number_of_tenant+1
-
- new_tenant["name"] = "demo2"
- data = Connector.driver.set_tenant_dict(tenant_id=data_id, tenant_dict=new_tenant)
- data_id = list(data.keys())[0]
- assert new_tenant["name"] == data[data_id]["name"]
- assert new_tenant["intra_authz_extension_id"] == data[data_id]["intra_authz_extension_id"]
- assert new_tenant["intra_admin_extension_id"] == data[data_id]["intra_admin_extension_id"]
-