Add Exceptions and Constants in opnfv module 17/24517/3
authorMorgan Richomme <morgan.richomme@orange.com>
Thu, 17 Nov 2016 14:43:03 +0000 (15:43 +0100)
committerMorgan Richomme <morgan.richomme@orange.com>
Mon, 21 Nov 2016 16:31:34 +0000 (17:31 +0100)
JIRA: FUNCTEST-497

Change-Id: I4bc0d545058c4778b632911bb317789110de29d1
Signed-off-by: Morgan Richomme <morgan.richomme@orange.com>
modules/README.rst
modules/opnfv/utils/OPNFVExceptions.py [new file with mode: 0644]
modules/opnfv/utils/constants.py [new file with mode: 0644]
modules/run_unit_tests.sh [new file with mode: 0755]
modules/setup.py
modules/tests/__init__.py [new file with mode: 0644]
modules/tests/unit/__init__.py [new file with mode: 0644]
modules/tests/unit/utils/__init__.py [new file with mode: 0644]
modules/tests/unit/utils/test_OPNFVExceptions.py [new file with mode: 0644]

index de9ff55..caec46b 100644 (file)
@@ -5,6 +5,8 @@ as follows:
 
   from opnfv.utils import SSHUtils
   from opnfv.utils import OPNFVLogger
+  from opnfv.utils import OPNFVException
+  from opnfv.utils import constants
 
 For further information about how to use this modules directory, contact:
   fatih.degirmenci@ericsson.com
diff --git a/modules/opnfv/utils/OPNFVExceptions.py b/modules/opnfv/utils/OPNFVExceptions.py
new file mode 100644 (file)
index 0000000..03b3ea9
--- /dev/null
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2016 Orange and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# This class defines Python OPNFV exceptions
+#
+
+
+class OPNFVException(Exception):
+    def __call__(self, *args):
+        return self.__class__(*(self.args + args))
+
+
+# ************************************
+# Generic
+# ************************************
+class OPNFVSUTNotReachable(OPNFVException):
+    """Target System Under Test is not reachable"""
+    pass
+
+
+class OPNFVCiExecutionError(OPNFVException):
+    """Error occurs during CI exection"""
+    pass
+
+
+class TestDashboardError(OPNFVException):
+    """Impossible to report results to dashboard"""
+    pass
+
+
+class TestReportingError(OPNFVException):
+    """Impossible to report results to reporting"""
+    pass
+
+
+# ************************************
+# Exceptions related to test DB
+# ************************************
+class TestDbNotReachable(OPNFVException):
+    """Test database is not reachable"""
+    pass
+
+
+class UnknownScenario(OPNFVException):
+    """Test scenario is unknown"""
+    pass
+
+
+class UnknownPod(OPNFVException):
+    """Test POD is unknown"""
+    pass
+
+
+class UnknownProject(OPNFVException):
+    """Project is unknown"""
+    pass
+
+
+class UnknownTestCase(OPNFVException):
+    """Test case is unknown"""
+    pass
+
+
+class UnknownVersion(OPNFVException):
+    """Version is unknown"""
+    pass
+
+
+class UnknownInstaller(OPNFVException):
+    """Installer is not supported"""
+    pass
+
+
+# *******************
+# Test project errors
+# *******************
+class FunctestExecutionError(OPNFVException):
+    """Internal Functest error"""
+    pass
+
+
+class YardstickExecutionError(OPNFVException):
+    """Internal Yardstick error"""
+    pass
+
+
+# **********************************
+# Errors related to Feature projects
+# **********************************
+class TestCaseNotRunnable(OPNFVException):
+    """test case incompatible with SUT, scenario, installer"""
+    pass
+
+
+class FeatureTestIntegrationError(OPNFVException):
+    """Impossible to integrate Feature test"""
+    pass
+
+
+class FeatureTestExecutionError(OPNFVException):
+    """Error during Feature test execution"""
+    pass
+
+
+# *********************************
+# Errors related to VNF on boarding
+# *********************************
+class VNFTestNotRunnable(OPNFVException):
+    """VNF test is not compatible with SUT, scenario, installer"""
+    pass
+
+
+class VNFIntegrationError(OPNFVException):
+    """Impossible to integrate the VNF test"""
+    pass
+
+
+class VNFExecutionError(OPNFVException):
+    """Error during VNF test execution"""
+    pass
diff --git a/modules/opnfv/utils/constants.py b/modules/opnfv/utils/constants.py
new file mode 100644 (file)
index 0000000..29f0d02
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2016 Orange and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
+INSTALLERS = ['apex', 'fuel', 'compass', 'joid']
+VERSIONS = ['arno', 'brahmaputra', 'colorado', 'danube']
+
+EXIT_OK = 0
+EXIT_RUN_ERROR = -1
+EXIT_PUSH_TO_TEST_DB_ERROR = -2
diff --git a/modules/run_unit_tests.sh b/modules/run_unit_tests.sh
new file mode 100755 (executable)
index 0000000..df511ce
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/bash
+set -o errexit
+set -o pipefail
+
+# Either Workspace is set (CI)
+if [ -z $WORKSPACE ]
+then
+    WORKSPACE="."
+fi
+
+
+# ***************
+# Run unit tests
+# ***************
+echo "Running unit tests..."
+
+# start vitual env
+virtualenv $WORKSPACE/modules_venv
+source $WORKSPACE/modules_venv/bin/activate
+
+# install python packages
+easy_install -U setuptools
+easy_install -U pip
+pip install $WORKSPACE
+
+
+# unit tests
+nosetests --with-xunit \
+         --cover-package=opnfv \
+         --with-coverage \
+         --cover-xml \
+         --cover-html \
+         tests/unit
+rc=$?
+
+deactivate
index 26f8a6e..8ac5cea 100644 (file)
@@ -17,5 +17,9 @@ setup(
     package_data={
     },
     url="https://www.opnfv.org",
-    install_requires=["paramiko>=2.0.1"]
+    install_requires=["paramiko>=2.0.1",
+                      "mock==1.3.0",
+                      "nose==1.3.7",
+                      "coverage==4.1",
+                      "requests==2.9.1"]
 )
diff --git a/modules/tests/__init__.py b/modules/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/modules/tests/unit/__init__.py b/modules/tests/unit/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/modules/tests/unit/utils/__init__.py b/modules/tests/unit/utils/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/modules/tests/unit/utils/test_OPNFVExceptions.py b/modules/tests/unit/utils/test_OPNFVExceptions.py
new file mode 100644 (file)
index 0000000..fca927b
--- /dev/null
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2016 Orange and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0import requests
+import unittest
+import requests
+
+from opnfv.utils import OPNFVExceptions
+
+
+def base_function():
+    raise OPNFVExceptions.TestDbNotReachable('Test database is not reachable')
+
+
+def base_function_wrong():
+    raise OPNFVExceptions.NotSelfDefinedException
+
+
+def db_connectivity():
+    url = 'http://testresults.opnfv2.org/test/api/v1/projects/functest/cases'
+    r = requests.get(url)
+    if r.status_code is not 200:
+        raise OPNFVExceptions.TestDbNotReachable('Database not found')
+
+
+def project_unknown():
+    url = 'http://testresults.opnfv.org/test/api/v1/projects/functest2/cases'
+    r = requests.get(url)
+    if len(r.json()['testcases']) is 0:
+        raise OPNFVExceptions.UnknownProject
+
+
+class TestBasicRaise(unittest.TestCase):
+    def test(self):
+        with self.assertRaises(Exception) as context:
+            base_function()
+        self.assertTrue('Test database is not reachable' in context.exception)
+
+
+class TestWrongRaise(unittest.TestCase):
+    def test(self):
+        try:
+            base_function_wrong()
+        except OPNFVExceptions.OPNFVException:
+            assert(False)
+        except AttributeError:
+            assert(True)
+
+
+class TestCaseDBNotReachable(unittest.TestCase):
+    def test(self):
+        with self.assertRaises(Exception) as context:
+            db_connectivity()
+        self.assertTrue('Database not found' in context.exception)
+
+
+class TestUnkownProject(unittest.TestCase):
+    def test(self):
+        try:
+            project_unknown()
+        except OPNFVExceptions.TestDashboardError:
+            # should not be there
+            assert(False)
+        except OPNFVExceptions.UnknownProject:
+            assert(True)
+        except Exception:
+            assert(False)
+
+if __name__ == '__main__':
+    unittest.main()