Example #1
0
 def test_install_download_cache_argument_in_resulting_command(self):
     mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
     with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
         pip.install("pep8", download_cache="/tmp/foo")
         mock.assert_called_once_with(
             "pip install --download-cache=/tmp/foo 'pep8'", saltenv="base", runas=None, cwd=None
         )
Example #2
0
    def test_install_log_argument_in_resulting_command(self, mock_path):
        pkg = 'pep8'
        log_path = '/tmp/pip-install.log'
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(pkg, log=log_path)
            mock.assert_called_once_with(
                ['pip', 'install', '--log', log_path, pkg],
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        # Let's fake a non-writable log file
        mock_path.exists.side_effect = IOError('Fooo!')
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(
                IOError,
                pip.install,
                pkg,
                log=log_path
            )
Example #3
0
    def test_install_multiple_editable(self):
        editables = [
            'git+https://github.com/jek/blinker.git#egg=Blinker',
            'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
        ]

        # Passing editables as a list
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(editable=editables)
            mock.assert_called_once_with(
                'pip install '
                '--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
                '--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
                runas=None,
                cwd=None
            )

        # Passing editables as a comma separated list
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(editable=','.join(editables))
            mock.assert_called_once_with(
                'pip install '
                '--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
                '--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
                runas=None,
                cwd=None
            )
Example #4
0
 def test_install_proxy_argument_in_resulting_command(self):
     mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
     with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
         pip.install("pep8", proxy="salt-user:salt-passwd@salt-proxy:3128")
         mock.assert_called_once_with(
             "pip install " "--proxy='salt-user:salt-passwd@salt-proxy:3128' pep8", runas=None, cwd=None
         )
Example #5
0
 def test_install_cached_requirements_used(self, get_cached_requirements):
     get_cached_requirements.return_value = "my_cached_reqs"
     mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
     with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
         pip.install(requirements="salt://requirements.txt")
         expected_cmd = "pip install --requirement='my_cached_reqs'"
         mock.assert_called_once_with(expected_cmd, runas=None, cwd=None)
Example #6
0
    def test_install_multiple_editable(self):
        editables = [
            "git+https://github.com/jek/blinker.git#egg=Blinker",
            "git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
        ]

        # Passing editables as a list
        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            pip.install(editable=editables)
            mock.assert_called_once_with(
                "pip install "
                "--editable=git+https://github.com/jek/blinker.git#egg=Blinker "
                "--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
                runas=None,
                cwd=None,
            )

        # Passing editables as a comma separated list
        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            pip.install(editable=",".join(editables))
            mock.assert_called_once_with(
                "pip install "
                "--editable=git+https://github.com/jek/blinker.git#egg=Blinker "
                "--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
                runas=None,
                cwd=None,
            )
Example #7
0
    def test_install_timeout_argument_in_resulting_command(self):
        # Passing an int
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', timeout=10)
            mock.assert_called_once_with(
                'pip install --timeout=10 pep8',
                runas=None,
                cwd=None
            )

        # Passing an int as a string
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', timeout='10')
            mock.assert_called_once_with(
                'pip install --timeout=10 pep8',
                runas=None,
                cwd=None
            )

        # Passing a non-int to timeout
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(
                ValueError,
                pip.install,
                'pep8',
                timeout='a'
            )
Example #8
0
    def test_install_multiple_editable(self):
        editables = [
            'git+https://github.com/jek/blinker.git#egg=Blinker',
            'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
        ]

        expected = ['pip', 'install']
        for item in editables:
            expected.extend(['--editable', item])

        # Passing editables as a list
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(editable=editables)
            mock.assert_called_once_with(
                expected,
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        # Passing editables as a comma separated list
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(editable=','.join(editables))
            mock.assert_called_once_with(
                expected,
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )
Example #9
0
 def test_install_extra_index_url_argument_in_resulting_command(self):
     mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
     with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
         pip.install("pep8", extra_index_url="http://foo.tld")
         mock.assert_called_once_with(
             "pip install --extra-index-url='http://foo.tld' 'pep8'", saltenv="base", runas=None, cwd=None
         )
Example #10
0
 def test_install_cached_requirements_used(self, get_cached_requirements):
     get_cached_requirements.return_value = 'my_cached_reqs'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='salt://requirements.txt')
         expected_cmd = 'pip install --requirement=\'my_cached_reqs\''
         mock.assert_called_once_with(expected_cmd, runas=None, cwd=None)
Example #11
0
    def test_install_pre_argument_in_resulting_command(self):
        # Lower than 1.4 versions don't end-up with `--pre` in the resulting
        # output
        mock = MagicMock(side_effect=[
            {'retcode': 0, 'stdout': 'pip 1.2.0 /path/to/site-packages/pip'},
            {'retcode': 0, 'stdout': ''}
        ])
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(
                'pep8', pre_releases=True
            )
            mock.assert_called_with(
                'pip install pep8',
                runas=None,
                cwd=None
            )

        mock = MagicMock(side_effect=[
            {'retcode': 0, 'stdout': 'pip 1.4.0 /path/to/site-pacakges/pip'},
            {'retcode': 0, 'stdout': ''}
        ])
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(
                'pep8', pre_releases=True
            )
            mock.assert_called_with(
                'pip install --pre pep8',
                runas=None,
                cwd=None
            )
Example #12
0
 def test_install_no_install_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', no_install=True)
         mock.assert_called_once_with(
             'pip install --no-install pep8',
             runas=None,
             cwd=None
         )
Example #13
0
 def test_install_download_cache_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', download_cache='/tmp/foo')
         mock.assert_called_once_with(
             'pip install --download-cache=/tmp/foo pep8',
             runas=None,
             cwd=None
         )
Example #14
0
 def test_install_extra_index_url_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', extra_index_url='http://foo.tld')
         mock.assert_called_once_with(
             'pip install --extra-index-url=\'http://foo.tld\' pep8',
             runas=None,
             cwd=None
         )
Example #15
0
 def test_install_proxy_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', proxy='salt-user:salt-passwd@salt-proxy:3128')
         mock.assert_called_once_with(
             'pip install '
             '--proxy=\'salt-user:salt-passwd@salt-proxy:3128\' pep8',
             runas=None,
             cwd=None
         )
Example #16
0
 def test_install_target_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', target='/tmp/foo')
         mock.assert_called_once_with(
             'pip install --target=/tmp/foo \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None
         )
Example #17
0
 def test_fix4361(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='requirements.txt')
         expected_cmd = 'pip install --requirement=\'requirements.txt\''
         mock.assert_called_once_with(
             expected_cmd,
             saltenv='base',
             runas=None,
             cwd=None
         )
Example #18
0
    def test_install_log_argument_in_resulting_command(self, mock_path):
        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            pip.install("pep8", log="/tmp/pip-install.log")
            mock.assert_called_once_with("pip install --log=/tmp/pip-install.log pep8", runas=None, cwd=None)

        # Let's fake a non-writable log file
        mock_path.exists.side_effect = IOError("Fooo!")
        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            self.assertRaises(IOError, pip.install, "pep8", log="/tmp/pip-install.log")
Example #19
0
 def test_install_ignore_installed_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', ignore_installed=True)
         mock.assert_called_once_with(
             'pip install --ignore-installed \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False
         )
Example #20
0
 def test_fix_activate_env(self, mock_path):
     mock_path.is_file.return_value = True
     mock_path.isdir.return_value = True
     def join(*args):
         return '/'.join(args)
     mock_path.join = join
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('mock', bin_env='/test_env', activate=True)
         expected_cmd = '. /test_env/bin/activate && /test_env/bin/pip install mock '
         mock.assert_called_once_with(expected_cmd, runas=None, cwd=None)
 def test_install_no_deps_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', no_deps=True)
         mock.assert_called_once_with(
             'pip install --no-deps \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             python_shell=False,
         )
Example #22
0
 def test_fix4361(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='requirements.txt')
         expected_cmd = 'pip install --requirement=\'requirements.txt\''
         mock.assert_called_once_with(
             expected_cmd,
             saltenv='base',
             runas=None,
             cwd=None,
             python_shell=False,
         )
Example #23
0
 def test_install_no_install_argument_in_resulting_command(self):
     pkg = 'pep8'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, no_install=True)
         mock.assert_called_once_with(
             ['pip', 'install', '--no-install', pkg],
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #24
0
 def test_install_no_deps_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', no_deps=True)
         mock.assert_called_once_with(
             'pip install --no-deps \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #25
0
 def test_install_download_cache_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', download_cache='/tmp/foo')
         mock.assert_called_once_with(
             'pip install --download-cache=/tmp/foo \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #26
0
 def test_install_index_url_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', index_url='http://foo.tld')
         mock.assert_called_once_with(
             'pip install --index-url=\'http://foo.tld\' \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #27
0
 def test_install_download_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', download='/tmp/foo')
         mock.assert_called_once_with(
             'pip install --download=/tmp/foo \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #28
0
 def test_install_extra_index_url_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', extra_index_url='http://foo.tld')
         mock.assert_called_once_with(
             'pip install --extra-index-url=\'http://foo.tld\' \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #29
0
 def test_install_cached_requirements_used(self, get_cached_requirements):
     get_cached_requirements.return_value = 'my_cached_reqs'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='salt://requirements.txt')
         expected = ['pip', 'install', '--requirement', 'my_cached_reqs']
         mock.assert_called_once_with(
             expected,
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #30
0
 def test_install_log_argument_in_resulting_command(self, mock_path):
     pkg = 'pep8'
     log_path = '/tmp/pip-install.log'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, log=log_path)
         mock.assert_called_once_with(
             ['pip', 'install', '--log', log_path, pkg],
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #31
0
 def test_install_proxy_argument_in_resulting_command(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install('pep8', proxy='salt-user:salt-passwd@salt-proxy:3128')
         mock.assert_called_once_with(
             'pip install '
             '--proxy=\'salt-user:salt-passwd@salt-proxy:3128\' \'pep8\'',
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #32
0
    def test_install_deprecated_runas_triggers_warning(self):
        # We *always* want *all* warnings thrown on this module
        warnings.resetwarnings()
        warnings.filterwarnings('always', '', DeprecationWarning, __name__)

        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            with warnings.catch_warnings(record=True) as w:
                pip.install('pep8', runas='me!')
                self.assertEqual(
                    'The \'runas\' argument to pip.install is deprecated, and '
                    'will be removed in Salt Lithium (Unreleased). Please '
                    'use \'user\' instead.', str(w[-1].message))
Example #33
0
 def test_install_no_install_argument_in_resulting_command(self):
     pkg = 'pep8'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, no_install=True)
         mock.assert_called_once_with(
             ['pip', 'install', '--no-install', pkg],
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #34
0
    def test_install_exists_action_argument_in_resulting_command(self):
        for action in ("s", "i", "w", "b"):
            mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
            with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
                pip.install("pep8", exists_action=action)
                mock.assert_called_once_with(
                    "pip install --exists-action={0} pep8".format(action), runas=None, cwd=None
                )

        # Test for invalid action
        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            self.assertRaises(CommandExecutionError, pip.install, "pep8", exists_action="d")
Example #35
0
    def test_install_deprecated_runas_triggers_warning(self):
        # We *always* want *all* warnings thrown on this module
        warnings.resetwarnings()
        warnings.filterwarnings('always', '', DeprecationWarning, __name__)

        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            with warnings.catch_warnings(record=True) as w:
                pip.install('pep8', runas='me!')
                self.assertEqual(
                    'The \'runas\' argument to pip.install is deprecated, and '
                    'will be removed in 0.18.0. Please use \'user\' instead.',
                    str(w[-1].message)
                )
Example #36
0
 def test_fix4361(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='requirements.txt')
         expected_cmd = ['pip', 'install', '--requirement',
                         'requirements.txt']
         mock.assert_called_once_with(
             expected_cmd,
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #37
0
 def test_install_index_url_argument_in_resulting_command(self):
     pkg = 'pep8'
     index_url = 'http://foo.tld'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, index_url=index_url)
         mock.assert_called_once_with(
             ['pip', 'install', '--index-url', index_url, pkg],
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #38
0
    def test_install_deprecated_runas_triggers_warning(self):
        # We *always* want *all* warnings thrown on this module
        warnings.resetwarnings()
        warnings.filterwarnings("always", "", DeprecationWarning, __name__)

        mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
        with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
            with warnings.catch_warnings(record=True) as w:
                pip.install("pep8", runas="me!")
                self.assertEqual(
                    "The 'runas' argument to pip.install is deprecated, and "
                    "will be removed in 0.18.0. Please use 'user' instead.",
                    str(w[-1].message),
                )
Example #39
0
 def test_install_cached_requirements_used(self, get_cached_requirements):
     get_cached_requirements.return_value = 'my_cached_reqs'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='salt://requirements.txt')
         expected = ['pip', 'install', '--requirement', 'my_cached_reqs']
         mock.assert_called_once_with(
             expected,
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #40
0
 def test_install_extra_index_url_argument_in_resulting_command(self):
     pkg = 'pep8'
     extra_index_url = 'http://foo.tld'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, extra_index_url=extra_index_url)
         mock.assert_called_once_with(
             ['pip', 'install', '--extra-index-url', extra_index_url, pkg],
             saltenv='base',
             runas=None,
             cwd=None,
             use_vt=False,
             python_shell=False,
         )
Example #41
0
 def test_fix4361(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(requirements='requirements.txt')
         expected_cmd = [
             sys.executable, '-m', 'pip', 'install', '--requirement',
             'requirements.txt'
         ]
         mock.assert_called_with(
             expected_cmd,
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #42
0
 def test_install_no_deps_argument_in_resulting_command(self):
     pkg = 'pep8'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, no_deps=True)
         expected = [
             sys.executable, '-m', 'pip', 'install', '--no-deps', pkg
         ]
         mock.assert_called_with(
             expected,
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #43
0
    def test_install_fix_activate_env(self, mock_path):
        mock_path.is_file.return_value = True
        mock_path.isdir.return_value = True

        def join(*args):
            return '/'.join(args)
        mock_path.join = join
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('mock', bin_env='/test_env', activate=True)
            mock.assert_called_once_with(
                '. /test_env/bin/activate && /test_env/bin/pip install '
                '\'mock\'',
                env={'VIRTUAL_ENV': '/test_env'},
                runas=None,
                cwd=None)
Example #44
0
 def test_install_failed_cached_requirements(self):
     with patch('salt.modules.pip._get_cached_requirements'
                ) as get_cached_requirements:
         get_cached_requirements.return_value = False
         ret = pip.install(requirements='salt://my_test_reqs')
         self.assertEqual(False, ret['result'])
         self.assertIn('my_test_reqs', ret['comment'])
Example #45
0
 def test_install_proxy_argument_in_resulting_command(self):
     pkg = 'pep8'
     proxy = 'salt-user:salt-passwd@salt-proxy:3128'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, proxy=proxy)
         expected = [
             sys.executable, '-m', 'pip', 'install', '--proxy', proxy, pkg
         ]
         mock.assert_called_with(
             expected,
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #46
0
 def test_install_extra_index_url_argument_in_resulting_command(self):
     pkg = 'pep8'
     extra_index_url = 'http://foo.tld'
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
         pip.install(pkg, extra_index_url=extra_index_url)
         expected = [
             sys.executable, '-m', 'pip', 'install', '--extra-index-url',
             extra_index_url, pkg
         ]
         mock.assert_called_with(
             expected,
             saltenv='base',
             runas=None,
             use_vt=False,
             python_shell=False,
         )
Example #47
0
    def test_install_log_argument_in_resulting_command(self, mock_path):
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', log='/tmp/pip-install.log')
            mock.assert_called_once_with(
                'pip install --log=/tmp/pip-install.log \'pep8\'',
                saltenv='base',
                runas=None,
                cwd=None)

        # Let's fake a non-writable log file
        mock_path.exists.side_effect = IOError('Fooo!')
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(IOError,
                              pip.install,
                              'pep8',
                              log='/tmp/pip-install.log')
Example #48
0
 def test_install_log_argument_in_resulting_command(self):
     with patch('os.access') as mock_path:
         pkg = 'pep8'
         log_path = '/tmp/pip-install.log'
         mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
         with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
             pip.install(pkg, log=log_path)
             expected = [
                 sys.executable, '-m', 'pip', 'install', '--log', log_path,
                 pkg
             ]
             mock.assert_called_with(
                 expected,
                 saltenv='base',
                 runas=None,
                 use_vt=False,
                 python_shell=False,
             )
Example #49
0
    def test_install_exists_action_argument_in_resulting_command(self):
        for action in ('s', 'i', 'w', 'b'):
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
                pip.install('pep8', exists_action=action)
                mock.assert_called_once_with(
                    'pip install --exists-action={0} \'pep8\''.format(action),
                    saltenv='base',
                    runas=None,
                    cwd=None)

        # Test for invalid action
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(CommandExecutionError,
                              pip.install,
                              'pep8',
                              exists_action='d')
Example #50
0
    def test_install_pre_argument_in_resulting_command(self):
        pkg = 'pep8'
        # Lower than 1.4 versions don't end-up with `--pre` in the resulting
        # output
        mock = MagicMock(
            side_effect=[{
                'retcode': 0,
                'stdout': 'pip 1.2.0 /path/to/site-packages/pip'
            }, {
                'retcode': 0,
                'stdout': ''
            }])
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            with patch('salt.modules.pip.version',
                       MagicMock(return_value='1.3')):
                pip.install(pkg, pre_releases=True)
                expected = [sys.executable, '-m', 'pip', 'install', pkg]
                mock.assert_called_with(
                    expected,
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )

        mock_run = MagicMock(
            return_value='pip 1.4.1 /path/to/site-packages/pip')
        mock_run_all = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {
                'cmd.run': mock_run,
                'cmd.run_all': mock_run_all
        }):
            with patch('salt.modules.pip._get_pip_bin',
                       MagicMock(return_value=['pip'])):
                pip.install(pkg, pre_releases=True)
                expected = ['pip', 'install', '--pre', pkg]
                mock_run_all.assert_called_with(
                    expected,
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )
Example #51
0
    def test_install_log_argument_in_resulting_command(self, mock_path):
        pkg = 'pep8'
        log_path = '/tmp/pip-install.log'
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(pkg, log=log_path)
            mock.assert_called_once_with(
                ['pip', 'install', '--log', log_path, pkg],
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        # Let's fake a non-writable log file
        mock_path.exists.side_effect = IOError('Fooo!')
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(IOError, pip.install, pkg, log=log_path)
Example #52
0
    def test_install_venv(self, mock_path):
        mock_path.is_file.return_value = True
        mock_path.isdir.return_value = True

        pkg = 'mock'
        venv_path = '/test_env'

        def join(*args):
            return '/'.join(args)
        mock_path.join = join
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(pkg, bin_env=venv_path)
            mock.assert_called_once_with(
                [os.path.join(venv_path, 'bin', 'pip'), 'install', pkg],
                env={'VIRTUAL_ENV': '/test_env'},
                saltenv='base',
                runas=None,
                use_vt=False,
                python_shell=False,
            )
Example #53
0
    def test_install_venv(self, mock_path):
        mock_path.is_file.return_value = True
        mock_path.isdir.return_value = True

        def join(*args):
            return '/'.join(args)

        mock_path.join = join
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('mock', bin_env='/test_env')
            mock.assert_called_once_with(
                '/test_env/bin/pip install '
                '\'mock\'',
                env={'VIRTUAL_ENV': '/test_env'},
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )
Example #54
0
    def test_install_exists_action_argument_in_resulting_command(self):
        pkg = 'pep8'
        for action in ('s', 'i', 'w', 'b'):
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
                pip.install('pep8', exists_action=action)
                mock.assert_called_once_with(
                    ['pip', 'install', '--exists-action', action, pkg],
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )

        # Test for invalid action
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(CommandExecutionError,
                              pip.install,
                              pkg,
                              exists_action='d')
Example #55
0
    def test_install_pre_argument_in_resulting_command(self):
        # Lower than 1.4 versions don't end-up with `--pre` in the resulting
        # output
        mock = MagicMock(
            side_effect=[{
                'retcode': 0,
                'stdout': 'pip 1.2.0 /path/to/site-packages/pip'
            }, {
                'retcode': 0,
                'stdout': ''
            }])
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', pre_releases=True)
            mock.assert_called_with(
                'pip install \'pep8\'',
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        mock = MagicMock(
            side_effect=[{
                'retcode': 0,
                'stdout': 'pip 1.4.0 /path/to/site-packages/pip'
            }, {
                'retcode': 0,
                'stdout': ''
            }])
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', pre_releases=True)
            mock.assert_called_with(
                'pip install --pre \'pep8\'',
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )
Example #56
0
    def test_install_timeout_argument_in_resulting_command(self):
        # Passing an int
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', timeout=10)
            mock.assert_called_once_with('pip install --timeout=10 \'pep8\'',
                                         saltenv='base',
                                         runas=None,
                                         cwd=None)

        # Passing an int as a string
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install('pep8', timeout='10')
            mock.assert_called_once_with('pip install --timeout=10 \'pep8\'',
                                         saltenv='base',
                                         runas=None,
                                         cwd=None)

        # Passing a non-int to timeout
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(ValueError, pip.install, 'pep8', timeout='a')
Example #57
0
    def test_issue5940_install_multiple_pip_mirrors(self):
        '''
        test multiple pip mirrors.  This test only works with pip < 7.0.0
        '''
        with patch.object(pip, 'version', MagicMock(return_value='1.4')):
            mirrors = [
                'http://g.pypi.python.org', 'http://c.pypi.python.org',
                'http://pypi.crate.io'
            ]

            expected = [
                sys.executable, '-m', 'pip', 'install', '--use-mirrors'
            ]
            for item in mirrors:
                expected.extend(['--mirrors', item])
            expected.append('pep8')

            # Passing mirrors as a list
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
                pip.install(pkgs=['pep8'], mirrors=mirrors)
                mock.assert_called_with(
                    expected,
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )

            # Passing mirrors as a comma separated list
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
                pip.install(pkgs=['pep8'], mirrors=','.join(mirrors))
                mock.assert_called_with(
                    expected,
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )

            expected = [
                sys.executable, '-m', 'pip', 'install', '--use-mirrors',
                '--mirrors', mirrors[0], 'pep8'
            ]

            # As single string (just use the first element from mirrors)
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
                pip.install(pkgs=['pep8'], mirrors=mirrors[0])
                mock.assert_called_with(
                    expected,
                    saltenv='base',
                    runas=None,
                    use_vt=False,
                    python_shell=False,
                )
Example #58
0
    def test_install_timeout_argument_in_resulting_command(self):
        # Passing an int
        pkg = 'pep8'
        expected_prefix = ['pip', 'install', '--timeout']
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(pkg, timeout=10)
            mock.assert_called_once_with(
                expected_prefix + [10, pkg],
                saltenv='base',
                runas=None,
                use_vt=False,
                python_shell=False,
            )

        # Passing an int as a string
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(pkg, timeout='10')
            mock.assert_called_once_with(
                expected_prefix + ['10', pkg],
                saltenv='base',
                runas=None,
                use_vt=False,
                python_shell=False,
            )

        # Passing a non-int to timeout
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(
                ValueError,
                pip.install,
                pkg,
                timeout='a'
            )
Example #59
0
    def test_install_download_cache_dir_arguments_in_resulting_command(self):
        pkg = 'pep8'
        cache_dir_arg_mapping = {
            '1.5.6': '--download-cache',
            '6.0': '--cache-dir',
        }
        download_cache = '/tmp/foo'
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})

        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            for pip_version, cmd_arg in cache_dir_arg_mapping.items():
                with patch('salt.modules.pip.version',
                           MagicMock(return_value=pip_version)):
                    # test `download_cache` kwarg
                    pip.install(pkg, download_cache='/tmp/foo')
                    expected = [
                        sys.executable, '-m', 'pip', 'install', cmd_arg,
                        download_cache, pkg
                    ]
                    mock.assert_called_with(
                        expected,
                        saltenv='base',
                        runas=None,
                        use_vt=False,
                        python_shell=False,
                    )

                    # test `cache_dir` kwarg
                    pip.install(pkg, cache_dir='/tmp/foo')
                    mock.assert_called_with(
                        expected,
                        saltenv='base',
                        runas=None,
                        use_vt=False,
                        python_shell=False,
                    )
Example #60
0
    def test_install_multiple_requirements_arguments_in_resulting_command(
            self, get_cached_requirements):
        get_cached_requirements.side_effect = [
            'my_cached_reqs-1', 'my_cached_reqs-2'
        ]
        requirements = [
            'salt://requirements-1.txt', 'salt://requirements-2.txt'
        ]

        # Passing option as a list
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(requirements=requirements)
            mock.assert_called_once_with(
                'pip install '
                '--requirement=\'my_cached_reqs-1\' '
                '--requirement=\'my_cached_reqs-2\'',
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        # Passing option as a comma separated list
        get_cached_requirements.side_effect = [
            'my_cached_reqs-1', 'my_cached_reqs-2'
        ]
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(requirements=','.join(requirements))
            mock.assert_called_once_with(
                'pip install '
                '--requirement=\'my_cached_reqs-1\' '
                '--requirement=\'my_cached_reqs-2\'',
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )

        # Passing option as a single string entry
        get_cached_requirements.side_effect = ['my_cached_reqs-1']
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            pip.install(requirements=requirements[0])
            mock.assert_called_once_with(
                'pip install --requirement=\'my_cached_reqs-1\'',
                saltenv='base',
                runas=None,
                cwd=None,
                use_vt=False,
                python_shell=False,
            )