From a3603a5187839e25e9d8a51de70fea0b68439839 Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Sat, 18 Feb 2017 20:01:53 -0500 Subject: lib_openshift oc file lookup improvements - Move binary file search to separate method called from constructor - Use six.PY3 instaed of sys.version_info - Combine additional paths with the paths from the environment - For py3 pass the combined paths to shutil.which - For py2 explictly search for existance of file from combined paths instead of using distutils.spawn.find_executable and falling back - Use 'oc adm' instead of 'oadm' - Fix generate_validation test issue - fix tests for oc binary location - add tests for file lookup --- roles/lib_openshift/src/test/unit/test_oc_env.py | 132 ++++++++++++++++++++++- 1 file changed, 128 insertions(+), 4 deletions(-) (limited to 'roles/lib_openshift/src/test/unit/test_oc_env.py') diff --git a/roles/lib_openshift/src/test/unit/test_oc_env.py b/roles/lib_openshift/src/test/unit/test_oc_env.py index 15bd7e464..dab5099c2 100755 --- a/roles/lib_openshift/src/test/unit/test_oc_env.py +++ b/roles/lib_openshift/src/test/unit/test_oc_env.py @@ -11,6 +11,7 @@ # OK import os +import six import sys import unittest import mock @@ -23,7 +24,7 @@ import mock # place class in our python path module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 sys.path.insert(0, module_path) -from oc_env import OCEnv # noqa: E402 +from oc_env import OCEnv, locate_oc_binary # noqa: E402 class OCEnvTest(unittest.TestCase): @@ -35,9 +36,10 @@ class OCEnvTest(unittest.TestCase): ''' setup method will create a file and set to known configuration ''' pass + @mock.patch('oc_env.locate_oc_binary') @mock.patch('oc_env.Utils.create_tmpfile_copy') @mock.patch('oc_env.OCEnv._run') - def test_listing_all_env_vars(self, mock_cmd, mock_tmpfile_copy): + def test_listing_all_env_vars(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary): ''' Testing listing all environment variables from a dc''' # Arrange @@ -123,6 +125,10 @@ class OCEnvTest(unittest.TestCase): (0, dc_results, ''), # First call to the mock ] + mock_oc_binary.side_effect = [ + 'oc' + ] + mock_tmpfile_copy.side_effect = [ '/tmp/mock_adminkubeconfig', ] @@ -144,9 +150,10 @@ class OCEnvTest(unittest.TestCase): mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), ]) + @mock.patch('oc_env.locate_oc_binary') @mock.patch('oc_env.Utils.create_tmpfile_copy') @mock.patch('oc_env.OCEnv._run') - def test_adding_env_vars(self, mock_cmd, mock_tmpfile_copy): + def test_adding_env_vars(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary): ''' Test add environment variables to a dc''' # Arrange @@ -304,6 +311,10 @@ class OCEnvTest(unittest.TestCase): (0, dc_results_after, ''), ] + mock_oc_binary.side_effect = [ + 'oc' + ] + mock_tmpfile_copy.side_effect = [ '/tmp/mock_adminkubeconfig', ] @@ -325,9 +336,10 @@ class OCEnvTest(unittest.TestCase): mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), ]) + @mock.patch('oc_env.locate_oc_binary') @mock.patch('oc_env.Utils.create_tmpfile_copy') @mock.patch('oc_env.OCEnv._run') - def test_removing_env_vars(self, mock_cmd, mock_tmpfile_copy): + def test_removing_env_vars(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary): ''' Test add environment variables to a dc''' # Arrange @@ -419,6 +431,10 @@ class OCEnvTest(unittest.TestCase): (0, '', ''), ] + mock_oc_binary.side_effect = [ + 'oc' + ] + mock_tmpfile_copy.side_effect = [ '/tmp/mock_adminkubeconfig', ] @@ -435,6 +451,114 @@ class OCEnvTest(unittest.TestCase): mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), ]) + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_path_exists.side_effect = lambda _: False + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_shutil_which.side_effect = lambda _f, path=None: None + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + def tearDown(self): '''TearDown method''' pass -- cgit v1.2.3