From: Hong Hui Xiao Date: Mon, 28 Aug 2017 08:54:57 +0000 (+0800) Subject: Add code coverage for multicloud framework X-Git-Tag: v1.0.0~24^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=multicloud%2Fframework.git;a=commitdiff_plain;h=7bd792729198f1a04eac6d2042c7a03a632814a0 Add code coverage for multicloud framework Since multicloud projects are Django projects essentially, use the mechanism from Django to check code coverage. Change-Id: I850e64e601f9cf4d26222a2a2295ec005dfea474 Issue-Id: MULTICLOUD-71 Signed-off-by: Hong Hui Xiao --- diff --git a/.gitignore b/.gitignore index 56ab27e..495cfc8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,9 @@ target/ logs/*.log *.pyc + +# Test related files +multivimbroker/.coverage +multivimbroker/.tox/ +multivimbroker/logs/*.log +multivimbroker/test-reports/ diff --git a/multivimbroker/multivimbroker/settings-cover.py b/multivimbroker/multivimbroker/settings-cover.py new file mode 100644 index 0000000..51b73e8 --- /dev/null +++ b/multivimbroker/multivimbroker/settings-cover.py @@ -0,0 +1,20 @@ +# 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. + +from multivimbroker.settings import * # noqa +from multivimbroker.settings import INSTALLED_APPS + +INSTALLED_APPS.append('django_nose') + +TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' + +NOSE_ARGS = [ + '--with-coverage', + '--cover-package=multivimbroker', +] diff --git a/multivimbroker/multivimbroker/tests/__init__.py b/multivimbroker/multivimbroker/tests/__init__.py new file mode 100644 index 0000000..802f3fb --- /dev/null +++ b/multivimbroker/multivimbroker/tests/__init__.py @@ -0,0 +1,10 @@ +# Copyright (c) 2017 Wind River Systems, Inc. +# +# 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. diff --git a/multivimbroker/multivimbroker/tests/test_urls.py b/multivimbroker/multivimbroker/tests/test_urls.py new file mode 100644 index 0000000..71241f8 --- /dev/null +++ b/multivimbroker/multivimbroker/tests/test_urls.py @@ -0,0 +1,26 @@ +# 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. + +import json +import mock +import unittest + +from multivimbroker.pub.config import config +from multivimbroker import urls + + +class TestUrls(unittest.TestCase): + + def test_request_msb(self): + with mock.patch("multivimbroker.pub.utils.restcall." + "req_by_msb") as req_by_msb: + urls.req_msb(True) + req_by_msb.assert_called_once_with( + config.REG_TO_MSB_REG_URL, "POST", + json.JSONEncoder().encode(config.REG_TO_MSB_REG_PARAM)) diff --git a/multivimbroker/multivimbroker/urls.py b/multivimbroker/multivimbroker/urls.py index c8e0f42..7619e5a 100644 --- a/multivimbroker/multivimbroker/urls.py +++ b/multivimbroker/multivimbroker/urls.py @@ -10,8 +10,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. from django.conf.urls import include, url -from multivimbroker.pub.config.config \ - import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM +import json + +from multivimbroker.pub.config import config + urlpatterns = [ url(r'^', include('multivimbroker.swagger.urls')), @@ -19,9 +21,13 @@ urlpatterns = [ url(r'^', include('multivimbroker.forwarder.urls')), ] -# regist to MSB when startup -if REG_TO_MSB_WHEN_START: - import json - from multivimbroker.pub.utils.restcall import req_by_msb - req_by_msb(REG_TO_MSB_REG_URL, "POST", - json.JSONEncoder().encode(REG_TO_MSB_REG_PARAM)) + +def req_msb(request_when_start): + # regist to MSB when startup + if request_when_start: + from multivimbroker.pub.utils.restcall import req_by_msb + req_by_msb(config.REG_TO_MSB_REG_URL, "POST", + json.JSONEncoder().encode(config.REG_TO_MSB_REG_PARAM)) + + +req_msb(config.REG_TO_MSB_WHEN_START) diff --git a/multivimbroker/requirements.txt b/multivimbroker/requirements.txt index 6d58957..caf8687 100644 --- a/multivimbroker/requirements.txt +++ b/multivimbroker/requirements.txt @@ -18,6 +18,7 @@ python-glanceclient==2.5.0 python-neutronclient==6.0.0 # for unit test +django-nose>=1.4.0 coverage==4.2 mock==2.0.0 unittest_xml_reporting==1.12.0 diff --git a/multivimbroker/tox.ini b/multivimbroker/tox.ini index 88d43d9..4db044a 100644 --- a/multivimbroker/tox.ini +++ b/multivimbroker/tox.ini @@ -7,8 +7,21 @@ downloadcache = ~/cache/pip [testenv] deps = -r{toxinidir}/requirements.txt -commands = coverage run --branch manage.py test multivimbroker +commands = + /usr/bin/find . -type f -name "*.py[c|o]" -delete + python manage.py test multivimbroker [testenv:pep8] deps=flake8 commands=flake8 + +[testenv:py27] +commands = + {[testenv]commands} + +[testenv:cover] +setenv= + DJANGO_SETTINGS_MODULE = multivimbroker.settings-cover +commands = + coverage erase + {[testenv]commands}