Merge "Set flavor's page size to support dpdk enabled vms"
[functest.git] / functest / tests / unit / cli / commands / test_cli_env.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 import logging
9 import unittest
10
11 from git.exc import NoSuchPathError
12 import mock
13
14 mock.patch('logging.FileHandler').start()  # noqa
15 from functest.cli.commands import cli_env
16 from functest.utils.constants import CONST
17 from functest.tests.unit import test_utils
18
19
20 class CliEnvTesting(unittest.TestCase):
21
22     logging.disable(logging.CRITICAL)
23
24     def setUp(self):
25         self.cli_environ = cli_env.CliEnv()
26
27     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
28                 return_value=False)
29     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
30     def test_prepare_default(self, mock_ft_utils, mock_os):
31         cmd = ("python %s/functest/ci/prepare_env.py start" %
32                CONST.dir_repo_functest)
33         self.cli_environ.prepare()
34         mock_ft_utils.assert_called_with(cmd)
35
36     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
37                 return_value=True)
38     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
39     def test_prepare_missing_status(self, mock_ft_utils, mock_os):
40         with mock.patch('__builtin__.raw_input', return_value="y"), \
41                 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
42                 as mock_os_remove:
43             cmd = ("python %s/functest/ci/prepare_env.py start" %
44                    CONST.dir_repo_functest)
45             self.cli_environ.prepare()
46             mock_os_remove.assert_called_once_with(CONST.env_active)
47             mock_ft_utils.assert_called_with(cmd)
48
49     def _test_show_missing_env_var(self, var, *args):
50         if var == 'INSTALLER_TYPE':
51             CONST.INSTALLER_TYPE = None
52             reg_string = "|  INSTALLER: Unknown, \S+\s*|"
53         elif var == 'INSTALLER_IP':
54             CONST.INSTALLER_IP = None
55             reg_string = "|  INSTALLER: \S+, Unknown\s*|"
56         elif var == 'SCENARIO':
57             CONST.DEPLOY_SCENARIO = None
58             reg_string = "|   SCENARIO: Unknown\s*|"
59         elif var == 'NODE':
60             CONST.NODE_NAME = None
61             reg_string = "|        POD: Unknown\s*|"
62         elif var == 'BUILD_TAG':
63             CONST.BUILD_TAG = None
64             reg_string = "|  BUILD TAG: None|"
65         elif var == 'DEBUG':
66             CONST.CI_DEBUG = None
67             reg_string = "| DEBUG FLAG: false\s*|"
68         elif var == 'STATUS':
69             reg_string = "|     STATUS: not ready\s*|"
70
71         with mock.patch('functest.cli.commands.cli_env.click.echo') \
72                 as mock_click_echo:
73             self.cli_environ.show()
74             mock_click_echo.assert_called_with(test_utils.
75                                                RegexMatch(reg_string))
76
77     @mock.patch('functest.cli.commands.cli_env.git.Repo')
78     def test_show_missing_ci_installer_type(self, *args):
79         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
80
81     @mock.patch('functest.cli.commands.cli_env.git.Repo')
82     def test_show_missing_ci_installer_ip(self, *args):
83         self._test_show_missing_env_var('INSTALLER_IP', *args)
84
85     @mock.patch('functest.cli.commands.cli_env.git.Repo')
86     def test_show_missing_ci_scenario(self, *args):
87         self._test_show_missing_env_var('SCENARIO', *args)
88
89     @mock.patch('functest.cli.commands.cli_env.git.Repo')
90     def test_show_missing_ci_node(self, *args):
91         self._test_show_missing_env_var('NODE', *args)
92
93     @mock.patch('functest.cli.commands.cli_env.git.Repo')
94     def test_show_missing_ci_build_tag(self, *args):
95         self._test_show_missing_env_var('BUILD_TAG', *args)
96
97     @mock.patch('functest.cli.commands.cli_env.git.Repo')
98     def test_show_missing_ci_debug(self, *args):
99         self._test_show_missing_env_var('DEBUG', *args)
100
101     @mock.patch('functest.cli.commands.cli_env.git.Repo')
102     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
103                 return_value=False)
104     def test_show_missing_environment(self, *args):
105         self._test_show_missing_env_var('STATUS', *args)
106
107     @mock.patch('functest.cli.commands.cli_env.os.path.exists',
108                 return_value=False)
109     def test_show_missing_git_repo_dir(self, *args):
110         CONST.dir_repo_functest = None
111         self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show())
112
113     @mock.patch('functest.cli.commands.cli_env.click.echo')
114     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
115                 return_value=True)
116     def test_status_environment_present(self, mock_path, mock_click_echo):
117         self.assertEqual(self.cli_environ.status(), 0)
118         mock_click_echo.assert_called_with("Functest environment"
119                                            " ready to run tests.\n")
120
121     @mock.patch('functest.cli.commands.cli_env.click.echo')
122     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
123                 return_value=False)
124     def test_status_environment_absent(self, mock_path, mock_click_echo):
125         self.assertEqual(self.cli_environ.status(), 1)
126         mock_click_echo.assert_called_with("Functest environment"
127                                            " is not installed.\n")
128
129
130 if __name__ == "__main__":
131     unittest.main(verbosity=2)