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
8 # http://www.apache.org/licenses/LICENSE-2.0
11 """Define the classes required to fully cover k8s."""
18 from xtesting.core import testcase
20 from functest_kubernetes import k8stest
23 class K8sTests(unittest.TestCase):
25 # pylint: disable=missing-docstring
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"
33 self.k8stesting = k8stest.K8sTesting()
35 @mock.patch('functest_kubernetes.k8stest.os.path.isfile',
37 def test_run_missing_config_file(self, mock_func):
38 self.k8stesting.config = 'not_file'
39 with mock.patch.object(self.k8stesting,
40 '_K8sTesting__logger') as mock_logger:
41 self.assertEquals(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')
47 def test_run_kubetest_cmd_none(self):
48 self.k8stesting.cmd = None
49 with self.assertRaises(TypeError):
50 self.k8stesting.run_kubetest()
52 @mock.patch('functest_kubernetes.k8stest.os.path.isfile')
53 def test_error_logging(self, mock_isfile):
54 # pylint: disable=unused-argument
55 with mock.patch('functest_kubernetes.k8stest.'
56 'subprocess.Popen') as mock_popen, \
57 mock.patch.object(self.k8stesting,
58 '_K8sTesting__logger') as mock_logger:
59 mock_stdout = mock.Mock()
60 attrs = {'stdout.read.return_value': 'Error loading client'}
61 mock_stdout.configure_mock(**attrs)
62 mock_popen.return_value = mock_stdout
64 mock_logger.exception.assert_called_with(
65 "Error with running kubetest:")
67 @mock.patch('functest_kubernetes.k8stest.os.path.isfile')
68 @mock.patch('functest_kubernetes.k8stest.subprocess.Popen')
69 def test_run(self, mock_open, mock_isfile):
70 self.assertEquals(self.k8stesting.run(),
71 testcase.TestCase.EX_OK)
72 mock_isfile.assert_called()
73 mock_open.assert_called()
76 if __name__ == "__main__":
77 logging.disable(logging.CRITICAL)
78 unittest.main(verbosity=2)