Add unit tests for moon_db 59/48159/1
authorThomas Duval <thomas.duval@orange.com>
Fri, 1 Dec 2017 10:18:17 +0000 (11:18 +0100)
committerThomas Duval <thomas.duval@orange.com>
Fri, 1 Dec 2017 10:18:17 +0000 (11:18 +0100)
Change-Id: I98c18e696bd9dffe8c0ccc62df6c5210d78b4ab4

moonv4/moon_db/tests/README.md [new file with mode: 0644]
moonv4/moon_db/tests/unit_python/configure_db.sh [deleted file]
moonv4/moon_db/tests/unit_python/conftest.py [new file with mode: 0644]
moonv4/moon_db/tests/unit_python/requirements.txt [new file with mode: 0644]
moonv4/moon_db/tests/unit_python/test_intraextension.py [deleted file]
moonv4/moon_db/tests/unit_python/test_policies.py [new file with mode: 0644]
moonv4/moon_db/tests/unit_python/test_tenant.py [deleted file]

diff --git a/moonv4/moon_db/tests/README.md b/moonv4/moon_db/tests/README.md
new file mode 100644 (file)
index 0000000..73a9fcd
--- /dev/null
@@ -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 (file)
index bdc259f..0000000
+++ /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 (file)
index 0000000..6cce944
--- /dev/null
@@ -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 (file)
index 0000000..d6110d1
--- /dev/null
@@ -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 (file)
index a226721..0000000
+++ /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 (file)
index 0000000..3bd1360
--- /dev/null
@@ -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 (file)
index 7e6cfa8..0000000
+++ /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"]
-