From d72e7ce30ebc098a6832b32d1a1155b1a7efd51c Mon Sep 17 00:00:00 2001 From: RHE Date: Tue, 28 Nov 2017 16:05:46 +0100 Subject: moon_utilities python unit test Change-Id: Ia3e52d778ce2aba7f6ae1df727606dd40d3be480 Signed-off-by: RHE --- .gitignore | 3 + moonv4/bin/README.md | 5 + moonv4/bin/moon_utilities_update.sh | 35 ++++ moonv4/moon_utilities/README.md | 12 ++ moonv4/moon_utilities/README.rst | 9 - moonv4/moon_utilities/build.sh | 38 ---- moonv4/moon_utilities/setup.py | 2 +- moonv4/moon_utilities/tests/README.md | 12 ++ moonv4/moon_utilities/tests/unit_python/Dockerfile | 7 + .../moon_utilities/tests/unit_python/conftest.py | 202 +++++++++++++++++++++ .../tests/unit_python/requirements.txt | 10 + .../moon_utilities/tests/unit_python/run_tests.sh | 8 + .../moon_utilities/tests/unit_python/test_cache.py | 6 + 13 files changed, 301 insertions(+), 48 deletions(-) create mode 100644 moonv4/bin/README.md create mode 100644 moonv4/bin/moon_utilities_update.sh create mode 100644 moonv4/moon_utilities/README.md delete mode 100644 moonv4/moon_utilities/README.rst delete mode 100644 moonv4/moon_utilities/build.sh create mode 100644 moonv4/moon_utilities/tests/README.md create mode 100644 moonv4/moon_utilities/tests/unit_python/Dockerfile create mode 100644 moonv4/moon_utilities/tests/unit_python/conftest.py create mode 100644 moonv4/moon_utilities/tests/unit_python/requirements.txt create mode 100644 moonv4/moon_utilities/tests/unit_python/run_tests.sh create mode 100644 moonv4/moon_utilities/tests/unit_python/test_cache.py diff --git a/.gitignore b/.gitignore index a21f3f22..3a90e4b5 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ moon-abe/pbc-0.5.14/.libs/ moonclient/dist moonclient/build moonclient/python_moonclient.egg-info + +# cache +.cache \ No newline at end of file diff --git a/moonv4/bin/README.md b/moonv4/bin/README.md new file mode 100644 index 00000000..3125c468 --- /dev/null +++ b/moonv4/bin/README.md @@ -0,0 +1,5 @@ +# Automated Tools/Scripts + +## moon_utilities_update +- update moon_utilities to PIP: `./moon_utilities_update.sh upload` +- locally update moon_utilities for each moon Python package: `./moon_utilities_update.sh copy` \ No newline at end of file diff --git a/moonv4/bin/moon_utilities_update.sh b/moonv4/bin/moon_utilities_update.sh new file mode 100644 index 00000000..9dc42179 --- /dev/null +++ b/moonv4/bin/moon_utilities_update.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +VERSION=moon_utilities-$(grep __version__ moon_utilities/__init__.py | cut -d "\"" -f 2) + +python3 setup.py sdist bdist_wheel + +if [ "$1" = "upload" ]; then + # Instead of "A0A96E75", use your own GPG ID + rm dist/*.asc 2>/dev/null + gpg --detach-sign -u "A0A96E75" -a dist/${VERSION}-py3-none-any.whl + gpg --detach-sign -u "A0A96E75" -a dist/${VERSION}.tar.gz + twine upload dist/${VERSION}-py3-none-any.whl dist/${VERSION}-py3-none-any.whl.asc + twine upload dist/${VERSION}.tar.gz dist/${VERSION}.tar.gz.asc +fi + +rm -f ../moon_manager/dist/moon_utilities* +rm -f ../moon_orchestrator/dist/moon_utilities* +rm -f ../moon_wrapper/dist/moon_utilities* +rm -f ../moon_interface/dist/moon_utilities* +rm -f ../moon_authz/dist/moon_utilities* + + +if [ "$1" = "copy" ]; then + mkdir -p ../moon_manager/dist/ 2>/dev/null + cp -v dist/${VERSION}-py3-none-any.whl ../moon_manager/dist/ + mkdir -p ../moon_orchestrator/dist/ 2>/dev/null + cp -v dist/${VERSION}-py3-none-any.whl ../moon_orchestrator/dist/ + mkdir -p ../moon_wrapper/dist/ 2>/dev/null + cp -v dist/${VERSION}-py3-none-any.whl ../moon_wrapper/dist/ + mkdir -p ../moon_interface/dist/ 2>/dev/null + cp -v dist/${VERSION}-py3-none-any.whl ../moon_interface/dist/ + mkdir -p ../moon_authz/dist/ 2>/dev/null + cp -v dist/${VERSION}-py3-none-any.whl ../moon_authz/dist/ +fi + diff --git a/moonv4/moon_utilities/README.md b/moonv4/moon_utilities/README.md new file mode 100644 index 00000000..b658a864 --- /dev/null +++ b/moonv4/moon_utilities/README.md @@ -0,0 +1,12 @@ +# Moon Python Utilities Package +This package contains the core module for the Moon project +It is designed to provide authorization features to all OpenStack components. + +For any other information, refer to the parent project: + + https://git.opnfv.org/moon + +moon_utilities is a common Python lib for other Moon Python packages + + + diff --git a/moonv4/moon_utilities/README.rst b/moonv4/moon_utilities/README.rst deleted file mode 100644 index ded4e99a..00000000 --- a/moonv4/moon_utilities/README.rst +++ /dev/null @@ -1,9 +0,0 @@ -Core module for the Moon project -================================ - -This package contains the core module for the Moon project -It is designed to provide authorization features to all OpenStack components. - -For any other information, refer to the parent project: - - https://git.opnfv.org/moon diff --git a/moonv4/moon_utilities/build.sh b/moonv4/moon_utilities/build.sh deleted file mode 100644 index 4a86e5af..00000000 --- a/moonv4/moon_utilities/build.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -VERSION=moon_utilities-$(grep __version__ moon_utilities/__init__.py | cut -d "\"" -f 2) - -python3 setup.py sdist bdist_wheel - -rm dist/*.asc 2>/dev/null - -gpg --detach-sign -u "A0A96E75" -a dist/${VERSION}-py3-none-any.whl -gpg --detach-sign -u "A0A96E75" -a dist/${VERSION}.tar.gz - -if [ "$1" = "upload" ]; then - twine upload dist/${VERSION}-py3-none-any.whl dist/${VERSION}-py3-none-any.whl.asc - twine upload dist/${VERSION}.tar.gz dist/${VERSION}.tar.gz.asc - rm -f ../moon_orchestrator/dist/moon_utilities* - rm -f ../moon_interface/dist/moon_utilities* - rm -f ../moon_manager/dist/moon_utilities* - rm -f ../moon_authz/dist/moon_utilities* - rm -f ../moon_wrapper/dist/moon_utilities* -fi - -if [ "$1" = "copy" ]; then - mkdir -p ../moon_orchestrator/dist/ 2>/dev/null - rm -f ../moon_orchestrator/dist/moon_utilities* - cp -v dist/${VERSION}-py3-none-any.whl ../moon_orchestrator/dist/ - mkdir -p ../moon_interface/dist/ 2>/dev/null - rm -f ../moon_interface/dist/moon_utilities* - cp -v dist/${VERSION}-py3-none-any.whl ../moon_interface/dist/ - mkdir -p ../moon_manager/dist/ 2>/dev/null - rm -f ../moon_manager/dist/moon_utilities* - cp -v dist/${VERSION}-py3-none-any.whl ../moon_manager/dist/ - mkdir -p ../moon_authz/dist/ 2>/dev/null - rm -f ../moon_authz/dist/moon_utilities* - cp -v dist/${VERSION}-py3-none-any.whl ../moon_authz/dist/ - mkdir -p ../moon_wrapper/dist/ 2>/dev/null - rm -f ../moon_wrapper/dist/moon_utilities* - cp -v dist/${VERSION}-py3-none-any.whl ../moon_wrapper/dist/ -fi diff --git a/moonv4/moon_utilities/setup.py b/moonv4/moon_utilities/setup.py index 6c9ffd3d..cd7a2577 100644 --- a/moonv4/moon_utilities/setup.py +++ b/moonv4/moon_utilities/setup.py @@ -23,7 +23,7 @@ setup( description="Some utilities for all the Moon components", - long_description=open('README.rst').read(), + long_description=open('README.md').read(), install_requires=required, diff --git a/moonv4/moon_utilities/tests/README.md b/moonv4/moon_utilities/tests/README.md new file mode 100644 index 00000000..ddb4f55d --- /dev/null +++ b/moonv4/moon_utilities/tests/README.md @@ -0,0 +1,12 @@ +# Tests + +## Python Unit Test for moon_utilities +- build the moon_utilities package +- launch Docker for Python unit tests + - `cd moon_utilities` + - `docker run --volume $(pwd):/data moon_utilities_test` +- upload moon_utilities to PIP + + + + diff --git a/moonv4/moon_utilities/tests/unit_python/Dockerfile b/moonv4/moon_utilities/tests/unit_python/Dockerfile new file mode 100644 index 00000000..681edcd7 --- /dev/null +++ b/moonv4/moon_utilities/tests/unit_python/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 + +RUN pip install pytest requests_mock requests --upgrade +ADD requirements.txt /root +RUN pip install -r /root/requirements.txt --upgrade + +CMD ["sh", "/data/tests/unit_python/run_tests.sh"] \ No newline at end of file diff --git a/moonv4/moon_utilities/tests/unit_python/conftest.py b/moonv4/moon_utilities/tests/unit_python/conftest.py new file mode 100644 index 00000000..177a406e --- /dev/null +++ b/moonv4/moon_utilities/tests/unit_python/conftest.py @@ -0,0 +1,202 @@ +import base64 +import json +import logging +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", +) + + +def get_b64_conf(component=None): + if component in CONF: + return base64.b64encode( + json.dumps( + CONF[component]).encode('utf-8')+b"\n").decode('utf-8') + elif component == "components": + return base64.b64encode( + json.dumps(CONF["components"]).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(autouse=True) +def no_requests(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': "components", 'Value': get_b64_conf("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" + }]} + ) + print("End registering URI") + # from moon_db.db_manager import init_engine, run + # engine = init_engine() + # print("engine={}".format(engine)) + # run("upgrade", logging.getLogger("db_manager"), engine) + # print("End populating the DB.") + yield m + + +# @pytest.fixture(autouse=True, scope="session") +# def manage_database(): +# from moon_db.db_manager import init_engine, run +# engine = init_engine() +# run("upgrade", logging.getLogger("db_manager"), engine) +# yield +# print("Will close the DB") + + diff --git a/moonv4/moon_utilities/tests/unit_python/requirements.txt b/moonv4/moon_utilities/tests/unit_python/requirements.txt new file mode 100644 index 00000000..b611b008 --- /dev/null +++ b/moonv4/moon_utilities/tests/unit_python/requirements.txt @@ -0,0 +1,10 @@ +kombu !=4.0.1,!=4.0.0 +oslo.messaging +oslo.config +oslo.log +vine +werkzeug +flask +requests +pytest +requests_mock \ No newline at end of file diff --git a/moonv4/moon_utilities/tests/unit_python/run_tests.sh b/moonv4/moon_utilities/tests/unit_python/run_tests.sh new file mode 100644 index 00000000..527e3dc4 --- /dev/null +++ b/moonv4/moon_utilities/tests/unit_python/run_tests.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +cd /data +pip3 install -r tests/unit_python/requirements.txt --upgrade +pip3 install . + +cd /data/tests/unit_python +pytest . diff --git a/moonv4/moon_utilities/tests/unit_python/test_cache.py b/moonv4/moon_utilities/tests/unit_python/test_cache.py new file mode 100644 index 00000000..5ecacfcf --- /dev/null +++ b/moonv4/moon_utilities/tests/unit_python/test_cache.py @@ -0,0 +1,6 @@ + +def test_cache(): + from moon_utilities import cache + c = cache.Cache() + r = c.authz_requests() + assert isinstance(r, dict) -- cgit 1.2.3-korg