Add Exceptions and Constants in opnfv module
[releng.git] / modules / tests / unit / utils / test_OPNFVExceptions.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0import requests
9 import unittest
10 import requests
11
12 from opnfv.utils import OPNFVExceptions
13
14
15 def base_function():
16     raise OPNFVExceptions.TestDbNotReachable('Test database is not reachable')
17
18
19 def base_function_wrong():
20     raise OPNFVExceptions.NotSelfDefinedException
21
22
23 def db_connectivity():
24     url = 'http://testresults.opnfv2.org/test/api/v1/projects/functest/cases'
25     r = requests.get(url)
26     if r.status_code is not 200:
27         raise OPNFVExceptions.TestDbNotReachable('Database not found')
28
29
30 def project_unknown():
31     url = 'http://testresults.opnfv.org/test/api/v1/projects/functest2/cases'
32     r = requests.get(url)
33     if len(r.json()['testcases']) is 0:
34         raise OPNFVExceptions.UnknownProject
35
36
37 class TestBasicRaise(unittest.TestCase):
38     def test(self):
39         with self.assertRaises(Exception) as context:
40             base_function()
41         self.assertTrue('Test database is not reachable' in context.exception)
42
43
44 class TestWrongRaise(unittest.TestCase):
45     def test(self):
46         try:
47             base_function_wrong()
48         except OPNFVExceptions.OPNFVException:
49             assert(False)
50         except AttributeError:
51             assert(True)
52
53
54 class TestCaseDBNotReachable(unittest.TestCase):
55     def test(self):
56         with self.assertRaises(Exception) as context:
57             db_connectivity()
58         self.assertTrue('Database not found' in context.exception)
59
60
61 class TestUnkownProject(unittest.TestCase):
62     def test(self):
63         try:
64             project_unknown()
65         except OPNFVExceptions.TestDashboardError:
66             # should not be there
67             assert(False)
68         except OPNFVExceptions.UnknownProject:
69             assert(True)
70         except Exception:
71             assert(False)
72
73 if __name__ == '__main__':
74     unittest.main()