Ease modifying the test list in E2E testing
[functest-kubernetes.git] / functest_kubernetes / test_k8stest.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2018 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 """Define the classes required to fully cover k8s."""
12
13 import logging
14 import os
15 import unittest
16
17 import mock
18 from xtesting.core import testcase
19
20 from functest_kubernetes import k8stest
21
22
23 class E2EUnitTesting(unittest.TestCase):
24
25     # pylint: disable=missing-docstring
26
27     def setUp(self):
28         os.environ["DEPLOY_SCENARIO"] = "k8-test"
29         os.environ["KUBE_MASTER_IP"] = "127.0.0.1"
30         os.environ["KUBE_MASTER_URL"] = "https://127.0.0.1:6443"
31         os.environ["KUBERNETES_PROVIDER"] = "local"
32
33         self.k8stesting = k8stest.E2ETesting()
34
35     @mock.patch('functest_kubernetes.k8stest.os.path.isfile',
36                 return_value=False)
37     def test_run_missing_config_file(self, mock_func):
38         self.k8stesting.config = 'not_file'
39         with mock.patch.object(self.k8stesting,
40                                '_E2ETesting__logger') as mock_logger:
41             self.assertEqual(self.k8stesting.run(),
42                              testcase.TestCase.EX_RUN_ERROR)
43             mock_logger.error.assert_called_with(
44                 "Cannot run k8s testcases. Config file not found")
45         mock_func.assert_called_with('not_file')
46
47     @mock.patch('re.search')
48     @mock.patch('six.moves.builtins.open', mock.mock_open())
49     @mock.patch('functest_kubernetes.k8stest.os.path.isfile')
50     @mock.patch('functest_kubernetes.k8stest.subprocess.Popen')
51     def test_run(self, *args):
52         self.assertEqual(self.k8stesting.run(),
53                          testcase.TestCase.EX_OK)
54         for loop in range(3):
55             args[loop].assert_called()
56
57
58 if __name__ == "__main__":
59     logging.disable(logging.CRITICAL)
60     unittest.main(verbosity=2)