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
75
76
77
78
79
80
81
82
83
84
|
#!/usr/bin/env python
# Copyright (c) 2017 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
# pylint: disable=missing-docstring
import logging
import unittest
import mock
from functest.core import feature
from functest.core import testcase
logging.disable(logging.CRITICAL)
class FeatureTestingBase(unittest.TestCase):
_case_name = "foo"
_project_name = "bar"
_repo = "dir_repo_copper"
_cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -"
_output_file = '/home/opnfv/functest/results/bar.log'
@mock.patch('time.time', side_effect=[1, 2])
def _test_run(self, status, mock_method=None):
self.assertEqual(self.feature.run(), status)
if status == testcase.TestCase.EX_OK:
self.assertEqual(self.feature.criteria, 'PASS')
else:
self.assertEqual(self.feature.criteria, 'FAIL')
mock_method.assert_has_calls([mock.call(), mock.call()])
self.assertEqual(self.feature.start_time, 1)
self.assertEqual(self.feature.stop_time, 2)
class FeatureTesting(FeatureTestingBase):
def setUp(self):
self.feature = feature.Feature(
project_name=self._project_name, case_name=self._case_name,
cmd=self._cmd)
def test_run(self):
self._test_run(testcase.TestCase.EX_RUN_ERROR)
class BashFeatureTesting(FeatureTestingBase):
def setUp(self):
self.feature = feature.BashFeature(
project_name=self._project_name, case_name=self._case_name,
cmd=self._cmd)
@mock.patch("functest.utils.functest_utils.execute_command",
return_value=1)
def test_run_ko(self, mock_method=None):
self._test_run(testcase.TestCase.EX_RUN_ERROR)
mock_method.assert_called_once_with(
self._cmd, output_file=self._output_file)
@mock.patch("functest.utils.functest_utils.execute_command",
side_effect=Exception)
def test_run_exc(self, mock_method=None):
self._test_run(testcase.TestCase.EX_RUN_ERROR)
mock_method.assert_called_once_with(
self._cmd, output_file=self._output_file)
@mock.patch("functest.utils.functest_utils.execute_command",
return_value=0)
def test_run(self, mock_method):
self._test_run(testcase.TestCase.EX_OK)
mock_method.assert_called_once_with(
self._cmd, output_file=self._output_file)
if __name__ == "__main__":
unittest.main(verbosity=2)
|