Stop getting git data from functest dir
[functest.git] / functest / tests / unit / utils / test_decorators.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 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.0
9
10 """Define the class required to fully cover decorators."""
11
12 from datetime import datetime
13 import errno
14 import json
15 import logging
16 import os
17 import unittest
18
19 import mock
20
21 from functest.utils import decorators
22 from functest.utils import functest_utils
23 from functest.utils.constants import CONST
24
25 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
26
27 VERSION = 'master'
28 DIR = '/dev'
29 FILE = '{}/null'.format(DIR)
30 URL = 'file://{}'.format(FILE)
31
32
33 class DecoratorsTesting(unittest.TestCase):
34     # pylint: disable=missing-docstring
35
36     _case_name = 'base'
37     _project_name = 'functest'
38     _start_time = 1.0
39     _stop_time = 2.0
40     _result = 'PASS'
41     _build_tag = VERSION
42     _node_name = 'bar'
43     _deploy_scenario = 'foo'
44     _installer_type = 'debian'
45
46     def setUp(self):
47         os.environ['INSTALLER_TYPE'] = self._installer_type
48         os.environ['DEPLOY_SCENARIO'] = self._deploy_scenario
49         os.environ['NODE_NAME'] = self._node_name
50         os.environ['BUILD_TAG'] = self._build_tag
51
52     def test_wraps(self):
53         self.assertEqual(functest_utils.push_results_to_db.__name__,
54                          "push_results_to_db")
55
56     def _get_json(self):
57         stop_time = datetime.fromtimestamp(self._stop_time).strftime(
58             '%Y-%m-%d %H:%M:%S')
59         start_time = datetime.fromtimestamp(self._start_time).strftime(
60             '%Y-%m-%d %H:%M:%S')
61         data = {'project_name': self._project_name,
62                 'stop_date': stop_time, 'start_date': start_time,
63                 'case_name': self._case_name, 'build_tag': self._build_tag,
64                 'pod_name': self._node_name, 'installer': self._installer_type,
65                 'scenario': self._deploy_scenario, 'version': VERSION,
66                 'details': {}, 'criteria': self._result}
67         return json.dumps(data, sort_keys=True)
68
69     @mock.patch('{}.get_version'.format(functest_utils.__name__),
70                 return_value=VERSION)
71     @mock.patch('requests.post')
72     def test_http_shema(self, *args):
73         CONST.__setattr__('results_test_db_url', 'http://127.0.0.1')
74         self.assertTrue(functest_utils.push_results_to_db(
75             self._project_name, self._case_name, self._start_time,
76             self._stop_time, self._result, {}))
77         args[1].assert_called_once_with()
78         args[0].assert_called_once_with(
79             'http://127.0.0.1', data=self._get_json(),
80             headers={'Content-Type': 'application/json'})
81
82     def test_wrong_shema(self):
83         CONST.__setattr__('results_test_db_url', '/dev/null')
84         self.assertFalse(functest_utils.push_results_to_db(
85             self._project_name, self._case_name, self._start_time,
86             self._stop_time, self._result, {}))
87
88     @mock.patch('{}.get_version'.format(functest_utils.__name__),
89                 return_value=VERSION)
90     def _test_dump(self, *args):
91         CONST.__setattr__('results_test_db_url', URL)
92         with mock.patch.object(decorators, 'open', mock.mock_open(),
93                                create=True) as mock_open:
94             self.assertTrue(functest_utils.push_results_to_db(
95                 self._project_name, self._case_name, self._start_time,
96                 self._stop_time, self._result, {}))
97         mock_open.assert_called_once_with(FILE, 'a')
98         handle = mock_open()
99         call_args, _ = handle.write.call_args
100         self.assertIn('POST', call_args[0])
101         self.assertIn(self._get_json(), call_args[0])
102         args[0].assert_called_once_with()
103
104     @mock.patch('os.makedirs')
105     def test_default_dump(self, mock_method=None):
106         self._test_dump()
107         mock_method.assert_called_once_with(DIR)
108
109     @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
110     def test_makedirs_dir_exists(self, mock_method=None):
111         self._test_dump()
112         mock_method.assert_called_once_with(DIR)
113
114     @mock.patch('os.makedirs', side_effect=OSError)
115     def test_makedirs_exc(self, *args):
116         CONST.__setattr__('results_test_db_url', URL)
117         self.assertFalse(
118             functest_utils.push_results_to_db(
119                 self._project_name, self._case_name, self._start_time,
120                 self._stop_time, self._result, {}))
121         args[0].assert_called_once_with(DIR)
122
123
124 if __name__ == "__main__":
125     logging.disable(logging.CRITICAL)
126     unittest.main(verbosity=2)