123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- # -*- coding: utf-8 -*-
- """
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- """
- # Import Python Libs
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Salt Libs
- import salt.modules.pyenv as pyenv
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase
- class PyenvTestCase(TestCase, LoaderModuleMockMixin):
- """
- Test cases for salt.modules.pyenv
- """
- def setup_loader_modules(self):
- return {pyenv: {}}
- # 'install' function tests: 1
- def test_install(self):
- """
- Test if it install pyenv systemwide
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_ret = MagicMock(return_value=0)
- with patch.dict(
- pyenv.__salt__, {"config.option": mock_opt, "cmd.retcode": mock_ret}
- ):
- self.assertTrue(pyenv.install())
- # 'update' function tests: 1
- def test_update(self):
- """
- Test if it updates the current versions of pyenv and python-Build
- """
- mock_opt = MagicMock(return_value="salt stack")
- with patch.dict(pyenv.__salt__, {"config.option": mock_opt}):
- self.assertFalse(pyenv.update())
- # 'is_installed' function tests: 1
- def test_is_installed(self):
- """
- Test if it check if pyenv is installed.
- """
- mock_cmd = MagicMock(return_value=True)
- mock_opt = MagicMock(return_value="salt stack")
- with patch.dict(
- pyenv.__salt__, {"config.option": mock_opt, "cmd.has_exec": mock_cmd}
- ):
- self.assertTrue(pyenv.is_installed())
- # 'install_python' function tests: 1
- def test_install_python(self):
- """
- Test if it install a python implementation.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(pyenv.__grains__, {"os": "Linux"}):
- mock_all = MagicMock(
- return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "config.option": mock_opt,
- "cmd.has_exec": mock_cmd,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertEqual(pyenv.install_python("2.0.0-p0"), "error")
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "config.option": mock_opt,
- "cmd.has_exec": mock_cmd,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertFalse(pyenv.install_python("2.0.0-p0"))
- # 'uninstall_python' function tests: 1
- def test_uninstall_python(self):
- """
- Test if it uninstall a python implementation.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertTrue(pyenv.uninstall_python("2.0.0-p0"))
- # 'versions' function tests: 1
- def test_versions(self):
- """
- Test if it list the installed versions of python.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertListEqual(pyenv.versions(), [])
- # 'default' function tests: 1
- def test_default(self):
- """
- Test if it returns or sets the currently defined default python.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertEqual(pyenv.default(), "")
- self.assertTrue(pyenv.default("2.0.0-p0"))
- # 'list_' function tests: 1
- def test_list(self):
- """
- Test if it list the installable versions of python.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertListEqual(pyenv.list_(), [])
- # 'rehash' function tests: 1
- def test_rehash(self):
- """
- Test if it run pyenv rehash to update the installed shims.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertTrue(pyenv.rehash())
- # 'do' function tests: 1
- def test_do(self):
- """
- Test if it execute a python command with pyenv's
- shims from the user or the system.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertFalse(pyenv.do("gem list bundler"))
- mock_all = MagicMock(
- return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "config.option": mock_opt,
- "cmd.has_exec": mock_cmd,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertEqual(pyenv.do("gem list bundler"), "salt")
- # 'do_with_python' function tests: 1
- def test_do_with_python(self):
- """
- Test if it execute a python command with pyenv's
- shims using a specific python version.
- """
- mock_opt = MagicMock(return_value="salt stack")
- mock_cmd = MagicMock(return_value=True)
- mock_all = MagicMock(
- return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
- )
- with patch.dict(
- pyenv.__salt__,
- {
- "cmd.has_exec": mock_cmd,
- "config.option": mock_opt,
- "cmd.run_all": mock_all,
- },
- ):
- self.assertFalse(pyenv.do_with_python("2.0.0-p0", "gem list bundler"))
|