aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/utils/test_OPNFVExceptions.py
blob: fca927b58f9692a538f57c31d229032829decb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()