Add code coverage for multicloud framework 09/8909/3
authorHong Hui Xiao <honghui_xiao@yeah.net>
Mon, 28 Aug 2017 08:54:57 +0000 (16:54 +0800)
committerHong Hui Xiao <honghui_xiao@yeah.net>
Fri, 1 Sep 2017 12:30:57 +0000 (20:30 +0800)
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 <honghui_xiao@yeah.net>
.gitignore
multivimbroker/multivimbroker/settings-cover.py [new file with mode: 0644]
multivimbroker/multivimbroker/tests/__init__.py [new file with mode: 0644]
multivimbroker/multivimbroker/tests/test_urls.py [new file with mode: 0644]
multivimbroker/multivimbroker/urls.py
multivimbroker/requirements.txt
multivimbroker/tox.ini

index 56ab27e..495cfc8 100644 (file)
@@ -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 (file)
index 0000000..51b73e8
--- /dev/null
@@ -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 (file)
index 0000000..802f3fb
--- /dev/null
@@ -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 (file)
index 0000000..71241f8
--- /dev/null
@@ -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))
index c8e0f42..7619e5a 100644 (file)
 # 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)
index 6d58957..caf8687 100644 (file)
@@ -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
index 88d43d9..4db044a 100644 (file)
@@ -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}

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use