3 # Copyright (c) 2017 Orange and others.
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
10 """Define the class required to fully cover decorators."""
12 from datetime import datetime
21 from functest.core import testcase
22 from functest.utils import decorators
24 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
27 FILE = '{}/null'.format(DIR)
28 URL = 'file://{}'.format(FILE)
31 class DecoratorsTesting(unittest.TestCase):
32 # pylint: disable=missing-docstring
35 _project_name = 'functest'
42 _deploy_scenario = 'foo'
43 _installer_type = 'debian'
46 os.environ['INSTALLER_TYPE'] = self._installer_type
47 os.environ['DEPLOY_SCENARIO'] = self._deploy_scenario
48 os.environ['NODE_NAME'] = self._node_name
49 os.environ['BUILD_TAG'] = self._build_tag
52 self.assertEqual(testcase.TestCase.push_to_db.__name__,
56 stop_time = datetime.fromtimestamp(self._stop_time).strftime(
58 start_time = datetime.fromtimestamp(self._start_time).strftime(
60 data = {'project_name': self._project_name,
61 'stop_date': stop_time, 'start_date': start_time,
62 'case_name': self._case_name, 'build_tag': self._build_tag,
63 'pod_name': self._node_name, 'installer': self._installer_type,
64 'scenario': self._deploy_scenario, 'version': self._version,
65 'details': {}, 'criteria': self._result}
66 return json.dumps(data, sort_keys=True)
68 def _get_testcase(self):
69 test = testcase.TestCase(
70 project_name=self._project_name, case_name=self._case_name)
71 test.start_time = self._start_time
72 test.stop_time = self._stop_time
77 @mock.patch('requests.post')
78 def test_http_shema(self, *args):
79 os.environ['TEST_DB_URL'] = 'http://127.0.0.1'
80 test = self._get_testcase()
81 self.assertEqual(test.push_to_db(), testcase.TestCase.EX_OK)
82 args[0].assert_called_once_with(
83 'http://127.0.0.1', data=self._get_json(),
84 headers={'Content-Type': 'application/json'})
86 def test_wrong_shema(self):
87 os.environ['TEST_DB_URL'] = '/dev/null'
88 test = self._get_testcase()
90 test.push_to_db(), testcase.TestCase.EX_PUSH_TO_DB_ERROR)
93 os.environ['TEST_DB_URL'] = URL
94 with mock.patch.object(decorators, 'open', mock.mock_open(),
95 create=True) as mock_open:
96 test = self._get_testcase()
97 self.assertEqual(test.push_to_db(), testcase.TestCase.EX_OK)
98 mock_open.assert_called_once_with(FILE, 'a')
100 call_args, _ = handle.write.call_args
101 self.assertIn('POST', call_args[0])
102 self.assertIn(self._get_json(), call_args[0])
104 @mock.patch('os.makedirs')
105 def test_default_dump(self, mock_method=None):
107 mock_method.assert_called_once_with(DIR)
109 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
110 def test_makedirs_dir_exists(self, mock_method=None):
112 mock_method.assert_called_once_with(DIR)
114 @mock.patch('os.makedirs', side_effect=OSError)
115 def test_makedirs_exc(self, *args):
116 os.environ['TEST_DB_URL'] = URL
117 test = self._get_testcase()
119 test.push_to_db(), testcase.TestCase.EX_PUSH_TO_DB_ERROR)
120 args[0].assert_called_once_with(DIR)
123 if __name__ == "__main__":
124 logging.disable(logging.CRITICAL)
125 unittest.main(verbosity=2)