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 def _test_no_env_var(self, var):
37 with self.assertRaises(Exception):
38 k8stest.K8sTesting().check_envs()
40 def test_no_deploy_scenario(self):
41 self._test_no_env_var("DEPLOY_SCENARIO")
43 def test_no_kube_master_ip(self):
44 self._test_no_env_var("KUBE_MASTER_IP")
46 def test_no_kube_master_url(self):
47 self._test_no_env_var("KUBE_MASTER_URL")
49 def test_no_kubernetes_provider(self):
50 self._test_no_env_var("KUBERNETES_PROVIDER")
52 @mock.patch('functest_kubernetes.k8stest.os.path.isfile',
54 def test_run_missing_config_file(self, mock_func):
55 self.k8stesting.config = 'not_file'
56 with mock.patch.object(self.k8stesting,
57 '_K8sTesting__logger') as mock_logger:
58 self.assertEquals(self.k8stesting.run(),
59 testcase.TestCase.EX_RUN_ERROR)
60 mock_logger.error.assert_called_with(
61 "Cannot run k8s testcases. Config file not found")
62 mock_func.assert_called_with('not_file')
64 def test_run_kubetest_cmd_none(self):
65 self.k8stesting.cmd = None
66 with self.assertRaises(TypeError):
67 self.k8stesting.run_kubetest()
69 @mock.patch('functest_kubernetes.k8stest.os.path.isfile')
70 def test_error_logging(self, mock_isfile):
71 # pylint: disable=unused-argument
72 with mock.patch('functest_kubernetes.k8stest.'
73 'subprocess.Popen') as mock_popen, \
74 mock.patch.object(self.k8stesting,
75 '_K8sTesting__logger') as mock_logger:
76 mock_stdout = mock.Mock()
77 attrs = {'stdout.read.return_value': 'Error loading client'}
78 mock_stdout.configure_mock(**attrs)
79 mock_popen.return_value = mock_stdout
80 self.k8stesting.run_kubetest()
81 mock_logger.error.assert_called_with("Error loading client")
83 @mock.patch('functest_kubernetes.k8stest.os.path.isfile')
84 @mock.patch('functest_kubernetes.k8stest.subprocess.Popen')
85 def test_run(self, mock_open, mock_isfile):
86 self.assertEquals(self.k8stesting.run(),
87 testcase.TestCase.EX_OK)
88 mock_isfile.assert_called()
89 mock_open.assert_called()
92 if __name__ == "__main__":
93 logging.disable(logging.CRITICAL)
94 unittest.main(verbosity=2)