Add unit test for config file validation
[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 K8sTests(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.K8sTesting()
34
35     def _test_no_env_var(self, var):
36         del os.environ[var]
37         with self.assertRaises(Exception):
38             k8stest.K8sTesting().check_envs()
39
40     def test_no_deploy_scenario(self):
41         self._test_no_env_var("DEPLOY_SCENARIO")
42
43     def test_no_kube_master_ip(self):
44         self._test_no_env_var("KUBE_MASTER_IP")
45
46     def test_no_kube_master_url(self):
47         self._test_no_env_var("KUBE_MASTER_URL")
48
49     def test_no_kubernetes_provider(self):
50         self._test_no_env_var("KUBERNETES_PROVIDER")
51
52     @mock.patch('functest_kubernetes.k8stest.os.path.isfile',
53                 return_value=False)
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')
63
64
65 if __name__ == "__main__":
66     logging.disable(logging.CRITICAL)
67     unittest.main(verbosity=2)