Add API unit tests

Add unit tests for Promenade API endpoints. Also adds validatedesign
API endpoint.

Change-Id: I4c6a5da1f521f913c94ae8a07c8bc43cf4114f75
This commit is contained in:
Samantha Blanco 2017-11-30 12:17:33 -05:00
parent 5b4eee16b8
commit fd31aae8e8
9 changed files with 117 additions and 5 deletions

View File

@ -19,6 +19,7 @@ from promenade.control.health_api import HealthResource
from promenade.control.join_scripts import JoinScriptsResource from promenade.control.join_scripts import JoinScriptsResource
from promenade.control.middleware import (AuthMiddleware, ContextMiddleware, from promenade.control.middleware import (AuthMiddleware, ContextMiddleware,
LoggingMiddleware) LoggingMiddleware)
from promenade.control.validatedesign import ValidateDesignResource
from promenade import exceptions as exc from promenade import exceptions as exc
from promenade import logging from promenade import logging
@ -39,6 +40,7 @@ def start_api():
# API for managing region data # API for managing region data
('/health', HealthResource()), ('/health', HealthResource()),
('/join-scripts', JoinScriptsResource()), ('/join-scripts', JoinScriptsResource()),
('/validatedesign', ValidateDesignResource()),
] ]
# Set up the 1.0 routes # Set up the 1.0 routes

View File

@ -0,0 +1,59 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import falcon
from promenade.config import Configuration
from promenade.control import base
from promenade import exceptions
from promenade import validation
LOG = logging.getLogger(__name__)
class ValidateDesignResource(base.BaseResource):
def _return_msg(self, resp, status_code, status="Valid", message=""):
if status_code is falcon.HTTP_200:
count = 0
msg_list = []
else:
count = 1
msg_list = [message]
resp.body = json.dumps({
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": status,
"message": message,
"reason": "Validation",
"details": {
"errorCount": count,
"messageList": msg_list,
},
"code": status_code,
})
def on_post(self, req, resp):
href = req.get_param('href', required=True)
try:
config = Configuration.from_design_ref(href)
validation.check_design(config)
msg = "Promenade validations succeeded"
return self._return_msg(resp, falcon.HTTP_200, message=msg)
except exceptions.ValidationException as e:
msg = "Promenade validations failed: %s" % str(e)
return self._return_msg(
resp, falcon.HTTP_400, status="Invalid", message=msg)

View File

@ -24,6 +24,22 @@ __all__ = ['check_schema', 'check_schemas']
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def check_design(config):
kinds = ['Docker', 'HostSystem', 'Kubelet', 'KubernetesNetwork']
for kind in kinds:
count = 0
for doc in config.documents:
schema = doc.get('schema', None)
if not schema:
raise exceptions.ValidationException(
'"schema" is a required document key.')
name = schema.split('/')[1]
if name == kind:
count += 1
if count != 1:
raise exceptions.ValidationException()
def check_schemas(documents): def check_schemas(documents):
for document in documents: for document in documents:
check_schema(document) check_schema(document)

View File

@ -1,2 +0,0 @@
def test_placeholder():
pass

0
tests/unit/__init__.py Normal file
View File

View File

View File

@ -0,0 +1,15 @@
import falcon
from falcon import testing
import pytest
from promenade.control import health_api
from promenade.promenade import promenade
@pytest.fixture()
def client():
return testing.TestClient(promenade)
def test_get_health(client):
response = client.simulate_get('/api/v1.0/health')
assert response.status == falcon.HTTP_204

View File

@ -0,0 +1,18 @@
import falcon
from falcon import testing
import pytest
from promenade.control.api import VersionsResource
from promenade.promenade import promenade
@pytest.fixture()
def client():
return testing.TestClient(promenade)
def test_get_versions(client):
response = client.simulate_get('/versions')
assert response.status == falcon.HTTP_200
body = response.content.decode('utf-8')
assert '"path": "/api/v1.0"' in body
assert '"status": "stable"' in body

10
tox.ini
View File

@ -1,14 +1,18 @@
[tox] [tox]
envlist = bandit,lint,docs envlist = py35,unit,bandit,lint,docs
[testenv] [testenv]
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
setenv= setenv=
PYTHONWARNING=all PYTHONWARNING=all
commands= commands=
pytest \ pytest \
{posargs} {posargs}
deps = -r{toxinidir}/requirements-frozen.txt
-r{toxinidir}/test-requirements.txt
[testenv:unit]
commands =
pytest tests/unit
[testenv:bandit] [testenv:bandit]
deps = bandit==1.4.0 deps = bandit==1.4.0