Ejemplo n.º 1
0
    def test_installed_pkg_version_succeeds(self, _mod_run_check_mock):
        '''
            Test installing a PKG file where the version number matches the current installed version
        '''
        expected = {
            'changes': {},
            'comment': 'Version already matches .*5\\.1\\.[0-9]',
            'name': '/path/to/file.pkg',
            'result': True
        }

        installed_mock = MagicMock(return_value=['com.apple.id', 'some.other.id'])
        get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
        install_mock = MagicMock(return_value={'retcode': 0})
        cmd_mock = MagicMock(return_value='Version of this: 5.1.9')
        _mod_run_check_mock.return_value = True

        with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock,
                                              'macpackage.get_pkg_id': get_pkg_id_mock,
                                              'macpackage.install': install_mock,
                                              'cmd.run': cmd_mock}):
            out = macpackage.installed('/path/to/file.pkg', version_check=r'/usr/bin/runme --version=.*5\.1\.[0-9]')
            cmd_mock.assert_called_once_with('/usr/bin/runme --version', output_loglevel="quiet", ignore_retcode=True)
            assert not installed_mock.called
            assert not get_pkg_id_mock.called
            assert not install_mock.called
            self.assertEqual(out, expected)
Ejemplo n.º 2
0
    def test_installed(self):
        gems = {'foo': ['1.0'], 'bar': ['2.0']}
        gem_list = MagicMock(return_value=gems)
        gem_install_succeeds = MagicMock(return_value=True)
        gem_install_fails = MagicMock(return_value=False)

        with patch.dict(gem.__salt__, {'gem.list': gem_list}):
            with patch.dict(gem.__salt__,
                            {'gem.install': gem_install_succeeds}):
                ret = gem.installed('foo')
                self.assertEqual(True, ret['result'])
                ret = gem.installed('quux')
                self.assertEqual(True, ret['result'])
                gem_install_succeeds.assert_called_once_with(
                    'quux', pre_releases=False, ruby=None, runas=None,
                    version=None, proxy=None, rdoc=False, source=None,
                    ri=False, gem_bin=None
                )

            with patch.dict(gem.__salt__,
                            {'gem.install': gem_install_fails}):
                ret = gem.installed('quux')
                self.assertEqual(False, ret['result'])
                gem_install_fails.assert_called_once_with(
                    'quux', pre_releases=False, ruby=None, runas=None,
                    version=None, proxy=None, rdoc=False, source=None,
                    ri=False, gem_bin=None
                )
Ejemplo n.º 3
0
    def test_add_serial_missing(self):
        '''
            Test adding a certificate to specified certificate store when the file doesn't exist
        '''
        expected = {
            'changes': {},
            'comment': 'Certificate file not found.',
            'name': '/path/to/cert.cer',
            'result': False
        }

        cache_mock = MagicMock(return_value=False)
        get_cert_serial_mock = MagicMock(return_value='ABCDEF')
        get_store_serials_mock = MagicMock(return_value=['123456'])
        add_mock = MagicMock(return_value='Added successfully')
        with patch.dict(certutil.__salt__, {'cp.cache_file': cache_mock,
                                            'certutil.get_cert_serial': get_cert_serial_mock,
                                            'certutil.get_stored_cert_serials': get_store_serials_mock,
                                            'certutil.add_store': add_mock}):
            out = certutil.add_store('/path/to/cert.cer', 'TrustedPublisher')
            cache_mock.assert_called_once_with('/path/to/cert.cer', 'base')
            assert not get_cert_serial_mock.called
            assert not get_store_serials_mock.called
            assert not add_mock.called
            self.assertEqual(expected, out)
Ejemplo n.º 4
0
    def run_contents_pillar(self, pillar_value, expected):
        returner = MagicMock(return_value=None)

        filestate.__salt__ = {"file.manage_file": returner}

        path = "/tmp/foo"
        pillar_path = "foo:bar"

        # the values don't matter here
        filestate.__salt__["config.manage_mode"] = MagicMock()
        filestate.__salt__["file.source_list"] = MagicMock(return_value=[None, None])
        filestate.__salt__["file.get_managed"] = MagicMock(return_value=[None, None, None])

        # pillar.get should return the pillar_value
        pillar_mock = MagicMock(return_value=pillar_value)
        filestate.__salt__["pillar.get"] = pillar_mock

        ret = filestate.managed(path, contents_pillar=pillar_path)

        # make sure the pillar_mock is called with the given path
        pillar_mock.assert_called_once_with(pillar_path)

        # make sure no errors are returned
        self.assertEqual(None, ret)

        # make sure the value is correct
        self.assertEqual(expected, returner.call_args[0][-2])
Ejemplo n.º 5
0
    def test_installed_dmg(self, _mod_run_check_mock):
        '''
            Test installing a DMG file
        '''
        expected = {
            'changes': {'installed': ['some.other.id']},
            'comment': '/path/to/file.dmg installed',
            'name': '/path/to/file.dmg',
            'result': True
        }

        mount_mock = MagicMock(return_value=['success', '/tmp/dmg-X'])
        unmount_mock = MagicMock()
        installed_mock = MagicMock(return_value=['com.apple.id'])
        get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
        install_mock = MagicMock(return_value={'retcode': 0})
        _mod_run_check_mock.return_value = True

        with patch.dict(macpackage.__salt__, {'macpackage.mount': mount_mock,
                                              'macpackage.unmount': unmount_mock,
                                              'macpackage.installed_pkgs': installed_mock,
                                              'macpackage.get_pkg_id': get_pkg_id_mock,
                                              'macpackage.install': install_mock}):
            out = macpackage.installed('/path/to/file.dmg', dmg=True)
            mount_mock.assert_called_once_with('/path/to/file.dmg')
            unmount_mock.assert_called_once_with('/tmp/dmg-X')
            installed_mock.assert_called_once_with()
            get_pkg_id_mock.assert_called_once_with('/tmp/dmg-X/*.pkg')
            install_mock.assert_called_once_with('/tmp/dmg-X/*.pkg', 'LocalSystem', False, False)
            self.assertEqual(out, expected)
Ejemplo n.º 6
0
    def test_uninstall_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.uninstall('pep8', timeout=10)
            mock.assert_called_once_with(
                'pip uninstall -y --timeout=10 pep8',
                saltenv='base',
                runas=None,
                cwd=None,
                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.uninstall('pep8', timeout='10')
            mock.assert_called_once_with(
                'pip uninstall -y --timeout=10 pep8',
                saltenv='base',
                runas=None,
                cwd=None,
                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.uninstall,
                'pep8',
                timeout='a'
            )
Ejemplo n.º 7
0
    def test_set_proxy_windows_no_ftp(self):
        '''
            Test to make sure that we correctly set the proxy info
            on Windows
        '''
        proxy.__grains__['os'] = 'Windows'
        mock_reg = MagicMock()
        mock_cmd = MagicMock()

        with patch.dict(proxy.__salt__, {'reg.set_value': mock_reg, 'cmd.run': mock_cmd}):
            out = proxy.set_proxy_win('192.168.0.1', 3128, types=['http', 'https'],
                                      bypass_hosts=['.moo.com', '.salt.com'])

            calls = [
                call('HKEY_CURRENT_USER',
                     'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
                     'ProxyServer',
                     'http=192.168.0.1:3128;https=192.168.0.1:3128;'),
                call('HKEY_CURRENT_USER',
                     'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
                     'ProxyEnable',
                     1,
                     vtype='REG_DWORD'),
                call('HKEY_CURRENT_USER',
                     'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
                     'ProxyOverride',
                     '<local>;.moo.com;.salt.com')
            ]
            mock_reg.assert_has_calls(calls)
            mock_cmd.assert_called_once_with('netsh winhttp import proxy source=ie')
            self.assertTrue(out)
Ejemplo n.º 8
0
    def test_unzip(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip(
                '/tmp/salt.{{grains.id}}.zip',
                '/tmp/dest',
                excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                template='jinja'
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                template='jinja'
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip(
                '/tmp/salt.{{grains.id}}.zip',
                '/tmp/dest',
                excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                template='jinja'
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                template='jinja'
            )
Ejemplo n.º 9
0
 def test__rvm(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
         rvm._rvm('install', '1.9.3')
         mock.assert_called_once_with(
             '/usr/local/rvm/bin/rvm install 1.9.3', runas=None
         )
Ejemplo n.º 10
0
    def test_rar(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.rar(
                '/tmp/rarfile.rar',
                '/tmp/sourcefile1,/tmp/sourcefile2'
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'rar a -idp /tmp/rarfile.rar '
                '/tmp/sourcefile1 /tmp/sourcefile2',
                template=None
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.rar(
                '/tmp/rarfile.rar',
                ['/tmp/sourcefile1', '/tmp/sourcefile2']
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'rar a -idp /tmp/rarfile.rar '
                '/tmp/sourcefile1 /tmp/sourcefile2',
                template=None
            )
Ejemplo n.º 11
0
    def test_rar(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.rar(
                '/tmp/rarfile.rar',
                '/tmp/sourcefile1,/tmp/sourcefile2'
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                ['rar', 'a', '-idp', '/tmp/rarfile.rar',
                 '/tmp/sourcefile1', '/tmp/sourcefile2'],
                runas=None, python_shell=False, template=None, cwd=None
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.rar(
                '/tmp/rarfile.rar',
                ['/tmp/sourcefile1', '/tmp/sourcefile2']
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                ['rar', 'a', '-idp', '/tmp/rarfile.rar',
                 '/tmp/sourcefile1', '/tmp/sourcefile2'],
                runas=None, python_shell=False, template=None, cwd=None
            )
Ejemplo n.º 12
0
    def test_package_removed_removed(self):
        '''
            Test removing a package already removed
        '''
        expected = {
            'comment': "The package Pack2 is already removed",
            'changes': {},
            'name': 'Pack2',
            'result': True}

        mock_removed = MagicMock(
            side_effect=[['Pack1'], ['Pack1']])
        mock_remove = MagicMock()
        mock_info = MagicMock(
            return_value={'Package Identity': 'Pack2'})

        with patch.dict(
            dism.__salt__, {'dism.installed_packages': mock_removed,
                            'dism.remove_package': mock_remove,
                            'dism.package_info': mock_info}):

            out = dism.package_removed('Pack2')

            mock_removed.assert_called_once_with()
            assert not mock_remove.called
            self.assertEqual(out, expected)
Ejemplo n.º 13
0
    def test_capability_installed_failure(self):
        '''
            Test installing a capability which fails with DISM
        '''
        expected = {
            'comment': "Failed to install Capa2: Failed",
            'changes': {},
            'name': 'Capa2',
            'result': False}

        mock_installed = MagicMock(
            side_effect=[['Capa1'], ['Capa1']])
        mock_add = MagicMock(
            return_value={'retcode': 67, 'stdout': 'Failed'})

        with patch.dict(
            dism.__salt__, {'dism.installed_capabilities': mock_installed,
                            'dism.add_capability': mock_add}):
            with patch.dict(dism.__opts__, {'test': False}):

                out = dism.capability_installed('Capa2', 'somewhere', True)

                mock_installed.assert_called_with()
                mock_add.assert_called_once_with(
                    'Capa2', 'somewhere', True, None, False)
                self.assertEqual(out, expected)
Ejemplo n.º 14
0
    def test_package_installed(self):
        '''
            Test installing a package with DISM
        '''
        expected = {
            'comment': "Installed Pack2",
            'changes': {'package': {'new': 'Pack2'},
                        'retcode': 0},
            'name': 'Pack2',
            'result': True}

        mock_installed = MagicMock(
            side_effect=[['Pack1'], ['Pack1', 'Pack2']])
        mock_add = MagicMock(
            return_value={'retcode': 0})
        mock_info = MagicMock(
            return_value={'Package Identity': 'Pack2'})

        with patch.dict(
            dism.__salt__, {'dism.installed_packages': mock_installed,
                            'dism.add_package': mock_add,
                            'dism.package_info': mock_info}):
            with patch.dict(dism.__opts__, {'test': False}):

                out = dism.package_installed('Pack2')

                mock_installed.assert_called_with()
                mock_add.assert_called_once_with(
                    'Pack2', False, False, None, False)
                self.assertEqual(out, expected)
Ejemplo n.º 15
0
    def test_package_removed_failure(self):
        '''
            Test removing a package which fails with DISM
        '''
        expected = {
            'comment': "Failed to remove Pack2: Failed",
            'changes': {},
            'name': 'Pack2',
            'result': False}

        mock_removed = MagicMock(
            side_effect=[['Pack1', 'Pack2'], ['Pack1', 'Pack2']])
        mock_remove = MagicMock(
            return_value={'retcode': 67, 'stdout': 'Failed'})
        mock_info = MagicMock(
            return_value={'Package Identity': 'Pack2'})

        with patch.dict(
            dism.__salt__, {'dism.installed_packages': mock_removed,
                            'dism.remove_package': mock_remove,
                            'dism.package_info': mock_info}):
            with patch.dict(dism.__opts__, {'test': False}):

                out = dism.package_removed('Pack2')

                mock_removed.assert_called_with()
                mock_remove.assert_called_once_with(
                    'Pack2', None, False)
                self.assertEqual(out, expected)
Ejemplo n.º 16
0
 def test_clear_argument(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
         virtualenv_mod.create('/tmp/foo', clear=True)
         mock.assert_called_once_with(
             'virtualenv --clear /tmp/foo', runas=None
         )
Ejemplo n.º 17
0
    def test_zip(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.zip_(
                '/tmp/salt.{{grains.id}}.zip',
                '/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                template='jinja', cwd=None
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                ['zip', '-r', '/tmp/salt.{{grains.id}}.zip',
                 '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                 python_shell=False, template='jinja', cwd=None
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.zip_(
                '/tmp/salt.{{grains.id}}.zip',
                ['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                template='jinja', cwd=None
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                ['zip', '-r', '/tmp/salt.{{grains.id}}.zip',
                 '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                python_shell=False, template='jinja', cwd=None
            )
Ejemplo n.º 18
0
    def test_issue_6030_deprecated_never_download(self):
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})

        with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
            virtualenv_mod.create(
                '/tmp/foo', never_download=True
            )
            mock.assert_called_once_with(
                'virtualenv --never-download /tmp/foo',
                runas=None
            )

        with TestsLoggingHandler() as handler:
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            # Let's fake a higher virtualenv version
            virtualenv_mock = MagicMock()
            virtualenv_mock.__version__ = '1.10rc1'
            with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
                with patch.dict('sys.modules',
                                {'virtualenv': virtualenv_mock}):
                    virtualenv_mod.create(
                        '/tmp/foo', never_download=True
                    )
                    mock.assert_called_once_with('virtualenv /tmp/foo',
                                                 runas=None)

                # Are we logging the deprecation information?
                self.assertIn(
                    'INFO:The virtualenv \'--never-download\' option has been '
                    'deprecated in virtualenv(>=1.10), as such, the '
                    '\'never_download\' option to `virtualenv.create()` has '
                    'also been deprecated and it\'s not necessary anymore.',
                    handler.messages
                )
Ejemplo n.º 19
0
 def test_get_managed_object_name(self):
     mock_get_managed_object_name = MagicMock()
     with patch('salt.utils.vmware.get_managed_object_name',
                mock_get_managed_object_name):
         vmware.create_cluster(self.mock_dc, 'fake_cluster',
                               self.mock_cluster_spec)
     mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
Ejemplo n.º 20
0
    def test_uninstall_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.uninstall(pkg, log=log_path)
            mock.assert_called_once_with(
                ['pip', 'uninstall', '-y', '--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.uninstall,
                pkg,
                log=log_path
            )
Ejemplo n.º 21
0
 def test_wipe(self):
     mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
     with patch.dict(blockdev.__salt__, {'cmd.run_all': mock}):
         blockdev.wipe('/dev/sda')
         mock.assert_called_once_with(
             'wipefs /dev/sda'
         )
Ejemplo n.º 22
0
    def test_tar(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.tar(
                'zcvf', 'foo.tar',
                ['/tmp/something-to-compress-1',
                 '/tmp/something-to-compress-2'],
                cwd=None, template=None
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'tar -zcvf foo.tar /tmp/something-to-compress-1 '
                '/tmp/something-to-compress-2',
                cwd=None,
                template=None
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.tar(
                'zcvf', 'foo.tar',
                '/tmp/something-to-compress-1,/tmp/something-to-compress-2',
                cwd=None, template=None
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'tar -zcvf foo.tar /tmp/something-to-compress-1 '
                '/tmp/something-to-compress-2',
                cwd=None,
                template=None
            )
Ejemplo n.º 23
0
    def run_contents_pillar(self, pillar_value, expected):
        def returner(contents, *args, **kwargs):
            returner.returned = (contents, args, kwargs)
        returner.returned = None

        filestate.__salt__ = {
            'file.manage_file': returner
        }

        path = '/tmp/foo'
        pillar_path = 'foo:bar'

        # the values don't matter here
        filestate.__salt__['config.manage_mode'] = MagicMock()
        filestate.__salt__['file.source_list'] = MagicMock(return_value=[None, None])
        filestate.__salt__['file.get_managed'] = MagicMock(return_value=[None, None, None])

        # pillar.get should return the pillar_value
        pillar_mock = MagicMock(return_value=pillar_value)
        filestate.__salt__['pillar.get'] = pillar_mock

        ret = filestate.managed(path, contents_pillar=pillar_path)

        # make sure the pillar_mock is called with the given path
        pillar_mock.assert_called_once_with(pillar_path)

        # make sure no errors are returned
        self.assertEquals(None, ret)

        # make sure the value is correct
        self.assertEquals(expected, returner.returned[1][-1])
Ejemplo n.º 24
0
    def test_installed_activate_fail(self):
        '''
            Test activating the given product key when the install fails
        '''
        expected = {
            'changes': {},
            'comment': 'Unable to activate the given product key.',
            'name': 'AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE',
            'result': False
        }

        info = {
            'description': 'Prof',
            'licensed': False,
            'name': 'Win7',
            'partial_key': 'ABCDE'
        }

        info_mock = MagicMock(return_value=info)
        install_mock = MagicMock()
        activate_mock = MagicMock(return_value='Failed to activate')
        with patch.dict(license.__salt__, {'license.info': info_mock,
                                           'license.install': install_mock,
                                           'license.activate': activate_mock}):
            out = license.activate('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
            info_mock.assert_called_once_with()
            assert not install_mock.called
            activate_mock.assert_called_once_with()
            self.assertEqual(out, expected)
Ejemplo n.º 25
0
    def test_set_proxy_windows(self):
        '''
            Test to make sure we can set the proxy settings on Windows
        '''
        proxy.__grains__['os'] = 'Windows'
        expected = {
            'changes': {},
            'comment': 'Proxy settings updated correctly',
            'name': '192.168.0.1',
            'result': True
        }

        set_proxy_mock = MagicMock(return_value=True)
        patches = {
            'proxy.get_proxy_win': MagicMock(return_value={}),
            'proxy.get_proxy_bypass': MagicMock(return_value=[]),
            'proxy.set_proxy_win': set_proxy_mock,
        }

        with patch.dict(proxy.__salt__, patches):
            out = proxy.managed('192.168.0.1', '3128', user='******', password='******',
                                bypass_domains=['salt.com', 'test.com'])

            set_proxy_mock.assert_called_once_with('192.168.0.1', '3128', ['http', 'https', 'ftp'],
                                                   ['salt.com', 'test.com'])
            self.assertEqual(out, expected)
Ejemplo n.º 26
0
    def test_installed_activated(self):
        '''
            Test activating the given product key when its already activated
        '''
        expected = {
            'changes': {},
            'comment': 'Windows is already activated.',
            'name': 'AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE',
            'result': True
        }

        info = {
            'description': 'Prof',
            'licensed': True,
            'name': 'Win7',
            'partial_key': 'ABCDE'
        }

        info_mock = MagicMock(return_value=info)
        install_mock = MagicMock(return_value='Installed successfully')
        activate_mock = MagicMock(return_value='Activated successfully')
        with patch.dict(license.__salt__, {'license.info': info_mock,
                                           'license.install': install_mock,
                                           'license.activate': activate_mock}):
            out = license.activate('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
            info_mock.assert_called_once_with()
            assert not install_mock.called
            assert not activate_mock.called
            self.assertEqual(out, expected)
Ejemplo n.º 27
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',
                saltenv='base',
                runas=None,
                cwd=None,
                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(
                'pip install '
                '--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
                '--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
                saltenv='base',
                runas=None,
                cwd=None,
                python_shell=False,
            )
Ejemplo n.º 28
0
    def test_unrar(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unrar(
                '/tmp/rarfile.rar',
                '/home/strongbad/',
                excludes='file_1,file_2'
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unrar x -idp /tmp/rarfile.rar '
                '-x file_1 -x file_2 /home/strongbad/',
                template=None
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unrar(
                '/tmp/rarfile.rar',
                '/home/strongbad/',
                excludes=['file_1', 'file_2']
            )
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unrar x -idp /tmp/rarfile.rar '
                '-x file_1 -x file_2 /home/strongbad/',
                template=None
            )
Ejemplo n.º 29
0
    def test_freeze_command(self):
        eggs = [
            'M2Crypto==0.21.1',
            '-e [email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8#egg=SaltTesting-dev',
            'bbfreeze==1.1.0',
            'bbfreeze-loader==1.1.0',
            'pycrypto==2.6'
        ]
        mock = MagicMock(
            return_value={
                'retcode': 0,
                'stdout': '\n'.join(eggs)
            }
        )
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            ret = pip.freeze()
            mock.assert_called_once_with(
                'pip freeze',
                runas=None,
                cwd=None,
                python_shell=False,
            )
            self.assertEqual(ret, eggs)

        # Non zero returncode raises exception?
        mock = MagicMock(return_value={'retcode': 1, 'stderr': 'CABOOOOMMM!'})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
            self.assertRaises(
                CommandExecutionError,
                pip.freeze,
            )
Ejemplo n.º 30
0
    def test_get_pkg_id_with_files(self, pkg_id_pkginfo_mock):
        '''
            Test getting a the id for a package
        '''
        expected = ['com.apple.this']
        cmd_mock = MagicMock(side_effect=[
            '/path/to/PackageInfo\n/path/to/some/other/fake/PackageInfo',
            '',
            ''
        ])
        pkg_id_pkginfo_mock.side_effect = [['com.apple.this'], []]
        temp_mock = MagicMock(return_value='/tmp/dmg-ABCDEF')
        remove_mock = MagicMock()

        with patch.dict(macpackage.__salt__, {'cmd.run': cmd_mock,
                                              'temp.dir': temp_mock,
                                              'file.remove': remove_mock}):
            out = macpackage.get_pkg_id('/path/to/file.pkg')

            temp_mock.assert_called_once_with(prefix='pkg-')
            cmd_calls = [
                call('xar -t -f /path/to/file.pkg | grep PackageInfo', python_shell=True, output_loglevel='quiet'),
                call('xar -x -f /path/to/file.pkg /path/to/PackageInfo /path/to/some/other/fake/PackageInfo',
                     cwd='/tmp/dmg-ABCDEF', output_loglevel='quiet')
            ]
            cmd_mock.assert_has_calls(cmd_calls)

            pkg_id_pkginfo_calls = [
                call('/path/to/PackageInfo'),
                call('/path/to/some/other/fake/PackageInfo')
            ]
            pkg_id_pkginfo_mock.assert_has_calls(pkg_id_pkginfo_calls)
            remove_mock.assert_called_once_with('/tmp/dmg-ABCDEF')

            self.assertEqual(out, expected)
Ejemplo n.º 31
0
 def test_no_cached_service_instance_same_host_on_proxy(self):
     # Service instance is uncached when using class default mock objs
     mock_get_si = MagicMock()
     with patch('salt.utils.vmware._get_service_instance', mock_get_si):
         salt.utils.vmware.get_service_instance(host='fake_host',
                                                username='******',
                                                password='******',
                                                protocol='fake_protocol',
                                                port=1,
                                                mechanism='fake_mechanism',
                                                principal='fake_principal',
                                                domain='fake_domain')
         mock_get_si.assert_called_once_with(
             'fake_host', 'fake_username', 'fake_password', 'fake_protocol',
             1, 'fake_mechanism', 'fake_principal', 'fake_domain')
Ejemplo n.º 32
0
class CreateDatacenterTestCase(TestCase):
    '''Tests for salt.utils.vmware.create_datacenter'''
    def setUp(self):
        self.mock_si = MagicMock()
        self.mock_dc = MagicMock()
        self.mock_create_datacenter = MagicMock(return_value=self.mock_dc)
        self.mock_root_folder = MagicMock(
            CreateDatacenter=self.mock_create_datacenter)

    def test_get_root_folder(self):
        mock_get_root_folder = MagicMock()
        with patch('salt.utils.vmware.get_root_folder', mock_get_root_folder):
            vmware.create_datacenter(self.mock_si, 'fake_dc')
        mock_get_root_folder.assert_called_once_with(self.mock_si)

    def test_create_datacenter_call(self):
        with patch('salt.utils.vmware.get_root_folder',
                   MagicMock(return_value=self.mock_root_folder)):
            vmware.create_datacenter(self.mock_si, 'fake_dc')
        self.mock_create_datacenter.assert_called_once_with('fake_dc')

    def test_create_datacenter_raise_vim_fault(self):
        exc = vim.VimFault()
        exc.msg = 'VimFault msg'
        self.mock_root_folder = MagicMock(CreateDatacenter=MagicMock(
            side_effect=exc))
        with patch('salt.utils.vmware.get_root_folder',
                   MagicMock(return_value=self.mock_root_folder)):
            with self.assertRaises(VMwareApiError) as excinfo:
                vmware.create_datacenter(self.mock_si, 'fake_dc')
        self.assertEqual(excinfo.exception.strerror, 'VimFault msg')

    def test_create_datacenter_runtime_fault(self):
        exc = vmodl.RuntimeFault()
        exc.msg = 'RuntimeFault msg'
        self.mock_root_folder = MagicMock(CreateDatacenter=MagicMock(
            side_effect=exc))
        with patch('salt.utils.vmware.get_root_folder',
                   MagicMock(return_value=self.mock_root_folder)):
            with self.assertRaises(VMwareRuntimeError) as excinfo:
                vmware.create_datacenter(self.mock_si, 'fake_dc')
        self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')

    def test_datastore_successfully_created(self):
        with patch('salt.utils.vmware.get_root_folder',
                   MagicMock(return_value=self.mock_root_folder)):
            res = vmware.create_datacenter(self.mock_si, 'fake_dc')
        self.assertEqual(res, self.mock_dc)
Ejemplo n.º 33
0
    def test_one_elem_multiple_properties(self):
        obj_mock = MagicMock()

        # property1  mock
        prop_set_obj1_mock = MagicMock()
        prop_set_obj1_name_prop = PropertyMock(return_value='prop_name1')
        prop_set_obj1_val_prop = PropertyMock(return_value='prop_value1')
        type(prop_set_obj1_mock).name = prop_set_obj1_name_prop
        type(prop_set_obj1_mock).val = prop_set_obj1_val_prop

        # property2  mock
        prop_set_obj2_mock = MagicMock()
        prop_set_obj2_name_prop = PropertyMock(return_value='prop_name2')
        prop_set_obj2_val_prop = PropertyMock(return_value='prop_value2')
        type(prop_set_obj2_mock).name = prop_set_obj2_name_prop
        type(prop_set_obj2_mock).val = prop_set_obj2_val_prop

        # obj.propSet
        propSet_prop = PropertyMock(return_value=[prop_set_obj1_mock,
                                                  prop_set_obj2_mock])
        type(obj_mock).propSet = propSet_prop

        # obj.obj
        inner_obj_mock = MagicMock()
        obj_prop = PropertyMock(return_value=inner_obj_mock)
        type(obj_mock).obj = obj_prop

        get_content = MagicMock(return_value=[obj_mock])
        with patch('salt.utils.vmware.get_content', get_content):
            ret = salt.utils.vmware.get_mors_with_properties(
                self.si, self.obj_type, self.prop_list,
                self.container_ref, self.traversal_spec)
        get_content.assert_called_once_with(
            self.si, self.obj_type,
            property_list=self.prop_list,
            container_ref=self.container_ref,
            traversal_spec=self.traversal_spec,
            local_properties=False)
        self.assertEqual(propSet_prop.call_count, 1)
        self.assertEqual(prop_set_obj1_name_prop.call_count, 1)
        self.assertEqual(prop_set_obj1_val_prop.call_count, 1)
        self.assertEqual(prop_set_obj2_name_prop.call_count, 1)
        self.assertEqual(prop_set_obj2_val_prop.call_count, 1)
        self.assertEqual(obj_prop.call_count, 1)
        self.assertEqual(len(ret), 1)
        self.assertDictEqual(ret[0], {'prop_name1': 'prop_value1',
                                      'prop_name2': 'prop_value2',
                                      'object': inner_obj_mock})
Ejemplo n.º 34
0
    def test_unzip(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip('/tmp/salt.{{grains.id}}.zip',
                                '/tmp/dest',
                                excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                                template='jinja')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with([
                'unzip', '/tmp/salt.{{grains.id}}.zip', '-d', '/tmp/dest',
                '-x', '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'
            ],
                                         python_shell=False,
                                         template='jinja')

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip('/tmp/salt.{{grains.id}}.zip',
                                '/tmp/dest',
                                excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                                template='jinja')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with([
                'unzip', '/tmp/salt.{{grains.id}}.zip', '-d', '/tmp/dest',
                '-x', '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'
            ],
                                         python_shell=False,
                                         template='jinja')

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip('/tmp/salt.{{grains.id}}.zip',
                                '/tmp/dest',
                                excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                                template='jinja',
                                options='fo')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with([
                'unzip', '-fo', '/tmp/salt.{{grains.id}}.zip', '-d',
                '/tmp/dest', '-x', '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'
            ],
                                         python_shell=False,
                                         template='jinja')

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.unzip('/tmp/salt.{{grains.id}}.zip',
                                '/tmp/dest',
                                excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                                template='jinja',
                                options='fo')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with([
                'unzip', '-fo', '/tmp/salt.{{grains.id}}.zip', '-d',
                '/tmp/dest', '-x', '/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'
            ],
                                         python_shell=False,
                                         template='jinja')
Ejemplo n.º 35
0
    def test_cmd_unzip(self):
        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.cmd_unzip_('/tmp/salt.{{grains.id}}.zip',
                                     '/tmp/dest',
                                     excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                                     template='jinja',
                                     runas=None)
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                runas=None,
                template='jinja')

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.cmd_unzip_(
                '/tmp/salt.{{grains.id}}.zip',
                '/tmp/dest',
                excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                template='jinja')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                runas=None,
                template='jinja')

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.cmd_unzip_('/tmp/salt.{{grains.id}}.zip',
                                     '/tmp/dest',
                                     excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
                                     template='jinja',
                                     options='fo')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip -fo /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                runas=None,
                template='jinja',
            )

        mock = MagicMock(return_value='salt')
        with patch.dict(archive.__salt__, {'cmd.run': mock}):
            ret = archive.cmd_unzip_(
                '/tmp/salt.{{grains.id}}.zip',
                '/tmp/dest',
                excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
                template='jinja',
                options='fo')
            self.assertEqual(['salt'], ret)
            mock.assert_called_once_with(
                'unzip -fo /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
                '-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
                runas=None,
                template='jinja')
Ejemplo n.º 36
0
    def test_absent_already(self):
        '''
            Test ensuring non-existent defaults value is absent
        '''
        expected = {
            'changes': {},
            'comment': 'com.apple.something Key is already absent',
            'name': 'Key',
            'result': True
        }

        mock = MagicMock(return_value={'retcode': 1})
        with patch.dict(macdefaults.__salt__, {'macdefaults.delete': mock}):
            out = macdefaults.absent('Key', 'com.apple.something')
            mock.assert_called_once_with('com.apple.something', 'Key', None)
            self.assertEqual(out, expected)
Ejemplo n.º 37
0
 def test_command_with_kwargs(self):
     mock = MagicMock()
     with patch.dict(django.__salt__,
                     {'cmd.run': mock}):
         django.command(
             'settings.py',
             'runserver',
             None,
             None,
             database='something'
         )
         mock.assert_called_once_with(
             'django-admin.py runserver --settings=settings.py '
             '--database=something',
             env=None
         )
Ejemplo n.º 38
0
    def test_get_ftp_proxy_osx(self):
        '''
            Test to make sure that we correctly get the current proxy info
            on OSX
        '''
        proxy.__grains__['os'] = 'Darwin'
        mock = MagicMock(
            return_value=
            'Enabled: Yes\nServer: 192.168.0.1\nPort: 3128\nAuthenticated Proxy Enabled: 0'
        )
        expected = {'enabled': True, 'server': '192.168.0.1', 'port': '3128'}

        with patch.dict(proxy.__salt__, {'cmd.run': mock}):
            out = proxy.get_ftp_proxy()
            mock.assert_called_once_with('networksetup -getftpproxy Ethernet')
            self.assertEqual(expected, out)
Ejemplo n.º 39
0
 def test__check_and_install_ruby(self):
     mock_check_rvm = MagicMock(return_value={
         'changes': {},
         'result': True
     })
     mock_check_ruby = MagicMock(return_value={
         'changes': {},
         'result': False
     })
     mock_install_ruby = MagicMock(return_value='')
     with patch.object(rvm, '_check_rvm', new=mock_check_rvm):
         with patch.object(rvm, '_check_ruby', new=mock_check_ruby):
             with patch.dict(rvm.__salt__,
                             {'rvm.install_ruby': mock_install_ruby}):
                 rvm._check_and_install_ruby({'changes': {}}, '1.9.3')
     mock_install_ruby.assert_called_once_with('1.9.3', runas=None)
Ejemplo n.º 40
0
    def test_revert_snapshot(self):
        '''
        Test parallels.revert_snapshot
        '''
        name = 'macvm'
        snap_name = 'k-bar'
        snap_id = 'c2eab062-a635-4ccd-b9ae-998370f898b5'

        mock_snap_name = MagicMock(return_value=snap_id)
        with patch.object(parallels, '_validate_snap_name', mock_snap_name):
            mock_delete = MagicMock(return_value='')
            with patch.object(parallels, 'prlctl', mock_delete):
                parallels.revert_snapshot(name, snap_name)
                mock_delete.assert_called_once_with('snapshot-switch',
                                                    [name, '--id', snap_id],
                                                    runas=None)
Ejemplo n.º 41
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)
Ejemplo n.º 42
0
 def test_delete_rule_icmp4_any(self):
     '''
     Test if it deletes a new firewall rule
     '''
     mock_cmd = MagicMock(return_value='Ok.')
     with patch.dict(win_firewall.__salt__, {'cmd.run': mock_cmd}):
         self.assertTrue(
             win_firewall.delete_rule("test",
                                      "1",
                                      protocol='icmpv4:any,any'))
         mock_cmd.assert_called_once_with([
             'netsh', 'advfirewall', 'firewall', 'delete', 'rule',
             'name=test', 'protocol=icmpv4:any,any', 'dir=in',
             'remoteip=any'
         ],
                                          python_shell=False)
Ejemplo n.º 43
0
 def test_create(self):
     mock = MagicMock(return_value='salt')
     with patch.dict(mdadm.__salt__, {'cmd.run': mock}):
         ret = mdadm.create('/dev/md0',
                            5,
                            devices=['/dev/sdb1', '/dev/sdc1', '/dev/sdd1'],
                            test_mode=False,
                            force=True,
                            chunk=256)
         self.assertEqual('salt', ret)
         mock.assert_called_once_with([
             'mdadm', '-C', '/dev/md0', '-R', '-v', '--chunk', '256',
             '--force', '-l', '5', '-e', 'default', '-n', '3', '/dev/sdb1',
             '/dev/sdc1', '/dev/sdd1'
         ],
                                      python_shell=False)
Ejemplo n.º 44
0
    def test_sspi_get_token_error(self):
        mock_token = MagicMock(side_effect=Exception('Exception'))

        with patch('salt.utils.vmware.get_gssapi_token', mock_token):
            with self.assertRaises(excs.VMwareConnectionError) as excinfo:
                salt.utils.vmware._get_service_instance(
                    host='fake_host.fqdn',
                    username='******',
                    password='******',
                    protocol='fake_protocol',
                    port=1,
                    mechanism='sspi',
                    principal='fake_principal',
                    domain='fake_domain')
            mock_token.assert_called_once_with('fake_principal',
                                               'fake_host.fqdn', 'fake_domain')
            self.assertEqual('Exception', excinfo.exception.strerror)
Ejemplo n.º 45
0
 def test_create(self):
     mock = MagicMock(return_value='salt')
     with patch.dict(mdadm.__salt__, {'cmd.run': mock}):
         ret = mdadm.create(
                 '/dev/md0', 5,
                 ['/dev/sdb1',
                  '/dev/sdc1',
                  '/dev/sdd1'],
                 test_mode=False,
                 force=True,
                 chunk=256
         )
         self.assertEqual('salt', ret)
         mock.assert_called_once_with(
                 'mdadm -C /dev/md0 -v --chunk=256 --force -l 5 -n 3 '
                 '/dev/sdb1 /dev/sdc1 /dev/sdd1'
         )
    def test_issue_6029_deprecated_distribute(self):
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})

        with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
            virtualenv_mod._install_script = MagicMock(
                return_value={
                    'retcode': 0,
                    'stdout': 'Installed script!',
                    'stderr': ''
                }
            )
            virtualenv_mod.create(
                '/tmp/foo', system_site_packages=True, distribute=True
            )
            mock.assert_called_once_with(
                ['virtualenv', '--distribute', '--system-site-packages', '/tmp/foo'],
                runas=None,
                python_shell=False
            )

        with TestsLoggingHandler() as handler:
            # Let's fake a higher virtualenv version
            virtualenv_mock = MagicMock()
            virtualenv_mock.__version__ = '1.10rc1'
            mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
            with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
                with patch.dict('sys.modules',
                                {'virtualenv': virtualenv_mock}):
                    virtualenv_mod.create(
                        '/tmp/foo', system_site_packages=True, distribute=True
                    )
                    mock.assert_called_once_with(
                        ['virtualenv', '--system-site-packages', '/tmp/foo'],
                        runas=None,
                        python_shell=False
                    )

                # Are we logging the deprecation information?
                self.assertIn(
                    'INFO:The virtualenv \'--distribute\' option has been '
                    'deprecated in virtualenv(>=1.10), as such, the '
                    '\'distribute\' option to `virtualenv.create()` has '
                    'also been deprecated and it\'s not necessary anymore.',
                    handler.messages
                )
Ejemplo n.º 47
0
 def test_first_attempt_successful_connection(self):
     mock_sc = MagicMock()
     with patch('salt.utils.vmware.SmartConnect', mock_sc):
         salt.utils.vmware._get_service_instance(host='fake_host.fqdn',
                                                 username='******',
                                                 password='******',
                                                 protocol='fake_protocol',
                                                 port=1,
                                                 mechanism='sspi',
                                                 principal='fake_principal',
                                                 domain='fake_domain')
         mock_sc.assert_called_once_with(host='fake_host.fqdn',
                                         user='******',
                                         pwd='fake_password',
                                         protocol='fake_protocol',
                                         port=1,
                                         b64token='fake_token',
                                         mechanism='sspi')
Ejemplo n.º 48
0
    def test__gem(self):
        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        with patch.dict(gem.__salt__, {
                'rvm.is_installed': MagicMock(return_value=False),
                'cmd.run_all': mock
        }):
            gem._gem('install rails')
            mock.assert_called_once_with('gem install rails', runas=None)

        mock = MagicMock(return_value=None)
        with patch.dict(gem.__salt__, {
                'rvm.is_installed': MagicMock(return_value=True),
                'rvm.do': mock
        }):
            gem._gem('install rails', ruby='1.9.3')
            mock.assert_called_once_with('1.9.3',
                                         'gem install rails',
                                         runas=None)
Ejemplo n.º 49
0
 def test_userpass_mechanism_no_domain(self):
     mock_sc = MagicMock()
     with patch('salt.utils.vmware.SmartConnect', mock_sc):
         salt.utils.vmware._get_service_instance(host='fake_host.fqdn',
                                                 username='******',
                                                 password='******',
                                                 protocol='fake_protocol',
                                                 port=1,
                                                 mechanism='userpass',
                                                 principal='fake principal',
                                                 domain=None)
         mock_sc.assert_called_once_with(host='fake_host.fqdn',
                                         user='******',
                                         pwd='fake_password',
                                         protocol='fake_protocol',
                                         port=1,
                                         b64token=None,
                                         mechanism='userpass')
Ejemplo n.º 50
0
    def test_uninstall_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.uninstall('pep8', log='/tmp/pip-install.log')
            mock.assert_called_once_with(
                'pip uninstall -y --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.uninstall,
                              'pep8',
                              log='/tmp/pip-install.log')
Ejemplo n.º 51
0
    def test_set_http_proxy_osx(self):
        '''
            Test to make sure that we correctly set the proxy info
            on OSX
        '''
        proxy.__grains__['os'] = 'Darwin'
        mock = MagicMock()

        with patch.dict(proxy.__salt__, {'cmd.run': mock}):
            out = proxy.set_http_proxy('192.168.0.1',
                                       3128,
                                       'frank',
                                       'badpassw0rd',
                                       bypass_hosts='.moo.com,.salt.com')
            mock.assert_called_once_with(
                'networksetup -setwebproxy Ethernet 192.168.0.1 3128 On frank badpassw0rd'
            )
            self.assertTrue(out)
Ejemplo n.º 52
0
    def test_stop(self):
        '''
        Test parallels.stop
        '''
        name = 'macvm'
        runas = 'macdev'

        # Validate stop
        mock_stop = MagicMock()
        with patch.object(parallels, 'prlctl', mock_stop):
            parallels.stop(name, runas=runas)
            mock_stop.assert_called_once_with('stop', [name], runas=runas)

        # Validate immediate stop
        mock_kill = MagicMock()
        with patch.object(parallels, 'prlctl', mock_kill):
            parallels.stop(name, kill=True, runas=runas)
            mock_kill.assert_called_once_with('stop', [name, '--kill'], runas=runas)
Ejemplo n.º 53
0
    def test_absent_deleting_existing(self):
        '''
            Test removing an existing value
        '''
        expected = {
            'changes': {
                'absent': 'com.apple.something Key is now absent'
            },
            'comment': '',
            'name': 'Key',
            'result': True
        }

        mock = MagicMock(return_value={'retcode': 0})
        with patch.dict(macdefaults.__salt__, {'macdefaults.delete': mock}):
            out = macdefaults.absent('Key', 'com.apple.something')
            mock.assert_called_once_with('com.apple.something', 'Key', None)
            self.assertEqual(out, expected)
Ejemplo n.º 54
0
 def test_command_with_args(self):
     mock = MagicMock()
     with patch.dict(django.__salt__,
                     {'cmd.run': mock}):
         django.command(
             'settings.py',
             'runserver',
             None,
             None,
             None,
             'noinput',
             'somethingelse'
         )
         mock.assert_called_once_with(
             'django-admin.py runserver --settings=settings.py '
             '--noinput --somethingelse',
             env=None
         )
Ejemplo n.º 55
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')
Ejemplo n.º 56
0
    def test_prlctl(self):
        '''
        Test parallels.prlctl
        '''
        runas = 'macdev'

        # Validate 'prlctl user list'
        user_cmd = ['prlctl', 'user', 'list']
        user_fcn = MagicMock()
        with patch.dict(parallels.__salt__, {'cmd.run': user_fcn}):
            parallels.prlctl('user', 'list', runas=runas)
            user_fcn.assert_called_once_with(user_cmd, runas=runas)

        # Validate 'prlctl exec macvm uname'
        exec_cmd = ['prlctl', 'exec', 'macvm', 'uname']
        exec_fcn = MagicMock()
        with patch.dict(parallels.__salt__, {'cmd.run': exec_fcn}):
            parallels.prlctl('exec', 'macvm uname', runas=runas)
            exec_fcn.assert_called_once_with(exec_cmd, runas=runas)
Ejemplo n.º 57
0
 def test_get_ftp_proxy_windows(self):
     '''
         Test to make sure that we correctly get the current proxy info
         on Windows
     '''
     proxy.__grains__['os'] = 'Windows'
     result = {
         'vdata':
         'http=192.168.0.1:3128;https=192.168.0.2:3128;ftp=192.168.0.3:3128'
     }
     mock = MagicMock(return_value=result)
     expected = {'server': '192.168.0.3', 'port': '3128'}
     with patch.dict(proxy.__salt__, {'reg.read_value': mock}):
         out = proxy.get_ftp_proxy()
         mock.assert_called_once_with(
             'HKEY_CURRENT_USER',
             'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
             'ProxyServer')
         self.assertEqual(expected, out)
Ejemplo n.º 58
0
    def test_list_vms(self):
        '''
        Test parallels.list_vms
        '''
        runas = 'macdev'

        # Validate a simple list
        mock_plain = MagicMock()
        with patch.object(parallels, 'prlctl', mock_plain):
            parallels.list_vms(runas=runas)
            mock_plain.assert_called_once_with('list', [], runas=runas)

        # Validate listing a single VM
        mock_name = MagicMock()
        with patch.object(parallels, 'prlctl', mock_name):
            parallels.list_vms(name='macvm', runas=runas)
            mock_name.assert_called_once_with('list',
                                              ['macvm'],
                                              runas=runas)

        # Validate listing templates
        mock_templ = MagicMock()
        with patch.object(parallels, 'prlctl', mock_templ):
            parallels.list_vms(template=True, runas=runas)
            mock_templ.assert_called_once_with('list',
                                               ['--template'],
                                               runas=runas)

        # Validate listing extra info
        mock_info = MagicMock()
        with patch.object(parallels, 'prlctl', mock_info):
            parallels.list_vms(info=True, runas=runas)
            mock_info.assert_called_once_with('list',
                                              ['--info'],
                                              runas=runas)

        # Validate listing with extra options
        mock_complex = MagicMock()
        with patch.object(parallels, 'prlctl', mock_complex):
            parallels.list_vms(args=' -o uuid,status', all=True, runas=runas)
            mock_complex.assert_called_once_with('list',
                                                 ['-o', 'uuid,status', '--all'],
                                                 runas=runas)
Ejemplo n.º 59
0
    def test_gemset_present(self):
        with patch.object(rvm, '_check_rvm') as mock_method:
            mock_method.return_value = {'result': True, 'changes': {}}
            gems = ['global', 'foo', 'bar']
            gemset_list = MagicMock(return_value=gems)
            gemset_create = MagicMock(return_value=True)
            check_ruby = MagicMock(
                return_value={'result': False, 'changes': {}})
            with patch.object(rvm, '_check_ruby', new=check_ruby):
                with patch.dict(rvm.__salt__,
                                {'rvm.gemset_list': gemset_list,
                                 'rvm.gemset_create': gemset_create}):
                    ret = rvm.gemset_present('foo')
                    self.assertEqual(True, ret['result'])

                    ret = rvm.gemset_present('quux')
                    self.assertEqual(True, ret['result'])
                    gemset_create.assert_called_once_with(
                        'default', 'quux', runas=None)
Ejemplo n.º 60
0
    def test_get_mpkg_ids(self, get_pkg_id_mock):
        '''
            Test getting the ids of a mpkg file
        '''
        expected = ['com.apple.this', 'com.salt.other']
        mock = MagicMock(
            return_value='/tmp/dmg-X/file.pkg\n/tmp/dmg-X/other.pkg')
        get_pkg_id_mock.side_effect = [['com.apple.this'], ['com.salt.other']]

        with patch.dict(macpackage.__salt__, {'cmd.run': mock}):
            out = macpackage.get_mpkg_ids('/path/to/file.mpkg')

            mock.assert_called_once_with('find /path/to -name *.pkg',
                                         python_shell=True)

            calls = [call('/tmp/dmg-X/file.pkg'), call('/tmp/dmg-X/other.pkg')]
            get_pkg_id_mock.assert_has_calls(calls)

            self.assertEqual(out, expected)