def test_feature_installed(self): ''' Test installing a feature with DISM ''' expected = { 'comment': "Installed Feat2", 'changes': {'feature': {'new': 'Feat2'}, 'retcode': 0}, 'name': 'Feat2', 'result': True} mock_installed = MagicMock( side_effect=[['Feat1'], ['Feat1', 'Feat2']]) mock_add = MagicMock( return_value={'retcode': 0}) with patch.dict( dism.__salt__, {'dism.installed_features': mock_installed, 'dism.add_feature': mock_add}): with patch.dict(dism.__opts__, {'test': False}): out = dism.feature_installed('Feat2') mock_installed.assert_called_with() mock_add.assert_called_once_with( 'Feat2', None, None, False, False, None, False) self.assertEqual(out, expected)
def test_get_virtualenv_version_from_shell(self): with ForceImportErrorOn("virtualenv"): # ----- virtualenv binary not available -------------------------> mock = MagicMock(return_value={"retcode": 0, "stdout": ""}) with patch.dict(virtualenv_mod.__salt__, {"cmd.run_all": mock}): self.assertRaises(CommandExecutionError, virtualenv_mod.create, "/tmp/foo") # <---- virtualenv binary not available -------------------------- # ----- virtualenv binary present but > 0 exit code -------------> mock = MagicMock( side_effect=[{"retcode": 1, "stdout": "", "stderr": "This is an error"}, {"retcode": 0, "stdout": ""}] ) with patch.dict(virtualenv_mod.__salt__, {"cmd.run_all": mock}): self.assertRaises(CommandExecutionError, virtualenv_mod.create, "/tmp/foo", venv_bin="virtualenv") # <---- virtualenv binary present but > 0 exit code -------------- # ----- virtualenv binary returns 1.9.1 as its version ---------> mock = MagicMock(side_effect=[{"retcode": 0, "stdout": "1.9.1"}, {"retcode": 0, "stdout": ""}]) with patch.dict(virtualenv_mod.__salt__, {"cmd.run_all": mock}): virtualenv_mod.create("/tmp/foo", never_download=True) mock.assert_called_with(["virtualenv", "--never-download", "/tmp/foo"], runas=None, python_shell=False) # <---- virtualenv binary returns 1.9.1 as its version ---------- # ----- virtualenv binary returns 1.10rc1 as its version -------> mock = MagicMock(side_effect=[{"retcode": 0, "stdout": "1.10rc1"}, {"retcode": 0, "stdout": ""}]) with patch.dict(virtualenv_mod.__salt__, {"cmd.run_all": mock}): virtualenv_mod.create("/tmp/foo", never_download=True) mock.assert_called_with(["virtualenv", "/tmp/foo"], runas=None, python_shell=False)
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') mock.assert_called_with( ['pip', 'install', cmd_arg, download_cache, pkg], 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( ['pip', 'install', cmd_arg, download_cache, pkg], saltenv='base', runas=None, use_vt=False, python_shell=False, )
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}): with patch('os.path.exists'): 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)
def test_feature_removed_failure(self): ''' Test removing a feature which fails with DISM ''' expected = { 'comment': "Failed to remove Feat2: Failed", 'changes': {}, 'name': 'Feat2', 'result': False } mock_removed = MagicMock( side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']]) mock_remove = MagicMock(return_value={ 'retcode': 67, 'stdout': 'Failed' }) with patch.dict( dism.__salt__, { 'dism.installed_features': mock_removed, 'dism.remove_feature': mock_remove }): with patch.dict(dism.__opts__, {'test': False}): out = dism.feature_removed('Feat2') mock_removed.assert_called_with() mock_remove.assert_called_once_with('Feat2', False, None, False) self.assertEqual(out, expected)
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}): with patch('os.path.exists'): out = dism.package_removed('Pack2') mock_removed.assert_called_with() mock_remove.assert_called_once_with('Pack2', None, False) self.assertEqual(out, expected)
def test_list_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( side_effect=[{"retcode": 0, "stdout": "pip MOCKED_VERSION"}, {"retcode": 0, "stdout": "\n".join(eggs)}] ) with patch.dict(pip.__salt__, {"cmd.run_all": mock}): ret = pip.list_() mock.assert_called_with("pip freeze", runas=None, cwd=None) self.assertEqual( ret, { "SaltTesting-dev": "[email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8", "M2Crypto": "0.21.1", "bbfreeze-loader": "1.1.0", "bbfreeze": "1.1.0", "pip": "MOCKED_VERSION", "pycrypto": "2.6", }, ) # 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.list_)
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)
def test_capability_installed(self): ''' Test capability installed state ''' expected = { 'comment': "Installed Capa2", 'changes': {'capability': {'new': 'Capa2'}, 'retcode': 0}, 'name': 'Capa2', 'result': True} mock_installed = MagicMock( side_effect=[['Capa1'], ['Capa1', 'Capa2']]) mock_add = MagicMock( return_value={'retcode': 0}) 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)
def test_feature_removed_failure(self): ''' Test removing a feature which fails with DISM ''' expected = { 'comment': "Failed to remove Feat2: Failed", 'changes': {}, 'name': 'Feat2', 'result': False} mock_removed = MagicMock( side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']]) mock_remove = MagicMock( return_value={'retcode': 67, 'stdout': 'Failed'}) with patch.dict( dism.__salt__, {'dism.installed_features': mock_removed, 'dism.remove_feature': mock_remove}): with patch.dict(dism.__opts__, {'test': False}): out = dism.feature_removed('Feat2') mock_removed.assert_called_with() mock_remove.assert_called_once_with( 'Feat2', False, None, False) self.assertEqual(out, expected)
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) 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\'', saltenv='base', runas=None, cwd=None)
def test_list_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(side_effect=[{ 'retcode': 0, 'stdout': 'pip MOCKED_VERSION' }, { 'retcode': 0, 'stdout': '\n'.join(eggs) }]) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): ret = pip.list_() mock.assert_called_with('pip freeze', runas=None, cwd=None) self.assertEqual( ret, { 'SaltTesting-dev': '[email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8', 'M2Crypto': '0.21.1', 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.0', 'pip': 'MOCKED_VERSION', 'pycrypto': '2.6' }) # 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.list_, )
def test_list_command_with_prefix(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}): with patch('salt.modules.pip.version', MagicMock(return_value='6.1.1')): ret = pip.list_(prefix='bb') mock.assert_called_with( ['pip', 'freeze'], cwd=None, runas=None, python_shell=False, use_vt=False, ) self.assertEqual(ret, { 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.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, 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, python_shell=False, )
def test_list_command_with_prefix(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.list_(prefix='bb') mock.assert_called_with( 'pip freeze', runas=None, cwd=None, python_shell=False, ) self.assertEqual( ret, { 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.0', } )
def test_feature_installed(self): ''' Test installing a feature with DISM ''' expected = { 'comment': "Installed Feat2", 'changes': { 'feature': { 'new': 'Feat2' }, 'retcode': 0 }, 'name': 'Feat2', 'result': True } mock_installed = MagicMock(side_effect=[['Feat1'], ['Feat1', 'Feat2']]) mock_add = MagicMock(return_value={'retcode': 0}) with patch.dict( dism.__salt__, { 'dism.installed_features': mock_installed, 'dism.add_feature': mock_add }): with patch.dict(dism.__opts__, {'test': False}): out = dism.feature_installed('Feat2') mock_installed.assert_called_with() mock_add.assert_called_once_with('Feat2', None, None, False, False, None, False) self.assertEqual(out, expected)
def test_get_virtualenv_version_from_shell(self): with ForceImportErrorOn('virtualenv'): # ----- virtualenv binary not available -------------------------> mock = MagicMock(return_value={'retcode': 0, 'stdout': ''}) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): self.assertRaises( CommandExecutionError, virtualenv_mod.create, '/tmp/foo', ) # <---- virtualenv binary not available -------------------------- # ----- virtualenv binary present but > 0 exit code -------------> mock = MagicMock(side_effect=[{ 'retcode': 1, 'stdout': '', 'stderr': 'This is an error' }, { 'retcode': 0, 'stdout': '' }]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): self.assertRaises( CommandExecutionError, virtualenv_mod.create, '/tmp/foo', venv_bin='virtualenv', ) # <---- virtualenv binary present but > 0 exit code -------------- # ----- virtualenv binary returns 1.9.1 as its version ---------> mock = MagicMock(side_effect=[{ 'retcode': 0, 'stdout': '1.9.1' }, { 'retcode': 0, 'stdout': '' }]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): virtualenv_mod.create('/tmp/foo', never_download=True) mock.assert_called_with( ['virtualenv', '--never-download', '/tmp/foo'], runas=None, python_shell=False) # <---- virtualenv binary returns 1.9.1 as its version ---------- # ----- virtualenv binary returns 1.10rc1 as its version -------> mock = MagicMock(side_effect=[{ 'retcode': 0, 'stdout': '1.10rc1' }, { 'retcode': 0, 'stdout': '' }]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): virtualenv_mod.create('/tmp/foo', never_download=True) mock.assert_called_with(['virtualenv', '/tmp/foo'], runas=None, python_shell=False)
def test_capability_installed(self): ''' Test capability installed state ''' expected = { 'comment': "Installed Capa2", 'changes': { 'capability': { 'new': 'Capa2' }, 'retcode': 0 }, 'name': 'Capa2', 'result': True } mock_installed = MagicMock(side_effect=[['Capa1'], ['Capa1', 'Capa2']]) mock_add = MagicMock(return_value={'retcode': 0}) 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)
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)
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)
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)
def test_get_saved_rules(self): ''' Test if it return a data structure of the rules in the conf file ''' mock = MagicMock(return_value=False) with patch.object(iptables, '_parse_conf', mock): self.assertFalse(iptables.get_saved_rules()) mock.assert_called_with(conf_file=None, family='ipv4')
def test_get_rules(self): ''' Test if it return a data structure of the current, in-memory rules ''' mock = MagicMock(return_value=False) with patch.object(iptables, '_parse_conf', mock): self.assertFalse(iptables.get_rules()) mock.assert_called_with(in_mem=True, family='ipv4')
def test_get_rules(self): ''' Test if it return a data structure of the current, in-memory rules ''' mock = MagicMock(return_value=False) with patch.object(iptables, '_parse_conf', mock): self.assertFalse(iptables.get_rules()) mock.assert_called_with(in_mem=True, family='ipv4')
def test_get_saved_rules(self): ''' Test if it return a data structure of the rules in the conf file ''' mock = MagicMock(return_value=False) with patch.object(iptables, '_parse_conf', mock): self.assertFalse(iptables.get_saved_rules()) mock.assert_called_with(conf_file=None, family='ipv4')
def test_extracted_tar(self): ''' archive.extracted tar options ''' source = 'file.tar.gz' tmp_dir = os.path.join(tempfile.gettempdir(), 'test_archive', '') test_tar_opts = [ '--no-anchored foo', 'v -p --opt', '-v -p', '--long-opt -z', 'z -v -weird-long-opt arg', ] ret_tar_opts = [ ['tar', 'x', '--no-anchored', 'foo', '-f'], ['tar', 'xv', '-p', '--opt', '-f'], ['tar', 'x', '-v', '-p', '-f'], ['tar', 'x', '--long-opt', '-z', '-f'], ['tar', 'xz', '-v', '-weird-long-opt', 'arg', '-f'], ] mock_true = MagicMock(return_value=True) mock_false = MagicMock(return_value=False) ret = { 'stdout': ['saltines', 'cheese'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337' } mock_run = MagicMock(return_value=ret) mock_source_list = MagicMock(return_value=source) with patch('os.path.exists', mock_true): with patch.dict(archive.__opts__, { 'test': False, 'cachedir': tmp_dir }): with patch.dict( archive.__salt__, { 'file.directory_exists': mock_false, 'file.file_exists': mock_false, 'file.makedirs': mock_true, 'cmd.run_all': mock_run, 'file.source_list': mock_source_list }): filename = os.path.join( tmp_dir, 'files/test/_tmp_test_archive_.tar') for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts): ret = archive.extracted(tmp_dir, source, 'tar', tar_options=test_opts) ret_opts.append(filename) mock_run.assert_called_with(ret_opts, cwd=tmp_dir, python_shell=False)
def test_get_virtualenv_version_from_shell(self): with ForceImportErrorOn('virtualenv'): # ----- virtualenv binary not available -------------------------> mock = MagicMock(return_value={'retcode': 0, 'stdout': ''}) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): self.assertRaises( CommandExecutionError, virtualenv_mod.create, '/tmp/foo', ) # <---- virtualenv binary not available -------------------------- # ----- virtualenv binary present but > 0 exit code -------------> mock = MagicMock(side_effect=[ {'retcode': 1, 'stdout': '', 'stderr': 'This is an error'}, {'retcode': 0, 'stdout': ''} ]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): self.assertRaises( CommandExecutionError, virtualenv_mod.create, '/tmp/foo', venv_bin='virtualenv', ) # <---- virtualenv binary present but > 0 exit code -------------- # ----- virtualenv binary returns 1.9.1 as its version ---------> mock = MagicMock(side_effect=[ {'retcode': 0, 'stdout': '1.9.1'}, {'retcode': 0, 'stdout': ''} ]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): virtualenv_mod.create( '/tmp/foo', never_download=True ) mock.assert_called_with( ['virtualenv', '--never-download', '/tmp/foo'], runas=None, python_shell=False ) # <---- virtualenv binary returns 1.9.1 as its version ---------- # ----- virtualenv binary returns 1.10rc1 as its version -------> mock = MagicMock(side_effect=[ {'retcode': 0, 'stdout': '1.10rc1'}, {'retcode': 0, 'stdout': ''} ]) with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}): virtualenv_mod.create( '/tmp/foo', never_download=True ) mock.assert_called_with( ['virtualenv', '/tmp/foo'], runas=None, python_shell=False )
def test_extracted_tar(self): ''' archive.extracted tar options ''' source = '/tmp/foo.tar.gz' tmp_dir = '/tmp/test_extracted_tar' test_tar_opts = [ '--no-anchored foo', 'v -p --opt', '-v -p', '--long-opt -z', 'z -v -weird-long-opt arg', ] ret_tar_opts = [ ['tar', 'x', '--no-anchored', 'foo', '-f'], ['tar', 'xv', '-p', '--opt', '-f'], ['tar', 'x', '-v', '-p', '-f'], ['tar', 'x', '--long-opt', '-z', '-f'], ['tar', 'xz', '-v', '-weird-long-opt', 'arg', '-f'], ] mock_true = MagicMock(return_value=True) mock_false = MagicMock(return_value=False) ret = {'stdout': ['cheese', 'ham', 'saltines'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337'} mock_run = MagicMock(return_value=ret) mock_source_list = MagicMock(return_value=(source, None)) state_single_mock = MagicMock(return_value={'local': {'result': True}}) list_mock = MagicMock(return_value={ 'dirs': [], 'files': ['cheese', 'saltines'], 'links': ['ham'], 'top_level_dirs': [], 'top_level_files': ['cheese', 'saltines'], 'top_level_links': ['ham'], }) isfile_mock = MagicMock(side_effect=_isfile_side_effect) with patch.dict(archive.__opts__, {'test': False, 'cachedir': tmp_dir}): with patch.dict(archive.__salt__, {'file.directory_exists': mock_false, 'file.file_exists': mock_false, 'state.single': state_single_mock, 'file.makedirs': mock_true, 'cmd.run_all': mock_run, 'archive.list': list_mock, 'file.source_list': mock_source_list}): with patch.object(os.path, 'isfile', isfile_mock): for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts): ret = archive.extracted(tmp_dir, source, options=test_opts, enforce_toplevel=False) ret_opts.append(source) mock_run.assert_called_with(ret_opts, cwd=tmp_dir + os.sep, python_shell=False)
def test_extracted_tar(self): ''' archive.extracted tar options ''' source = '/tmp/file.tar.gz' tmp_dir = os.path.join(tempfile.gettempdir(), 'test_archive', '') test_tar_opts = [ '--no-anchored foo', 'v -p --opt', '-v -p', '--long-opt -z', 'z -v -weird-long-opt arg', ] ret_tar_opts = [ ['tar', 'x', '--no-anchored', 'foo', '-f'], ['tar', 'xv', '-p', '--opt', '-f'], ['tar', 'x', '-v', '-p', '-f'], ['tar', 'x', '--long-opt', '-z', '-f'], ['tar', 'xz', '-v', '-weird-long-opt', 'arg', '-f'], ] mock_true = MagicMock(return_value=True) mock_false = MagicMock(return_value=False) ret = {'stdout': ['saltines', 'cheese'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337'} mock_run = MagicMock(return_value=ret) mock_source_list = MagicMock(return_value=(source, None)) state_single_mock = MagicMock(return_value={'local': {'result': True}}) list_mock = MagicMock(return_value={ 'dirs': [], 'files': ['saltines', 'cheese'], 'top_level_dirs': [], 'top_level_files': ['saltines', 'cheese'], }) with patch.dict(archive.__opts__, {'test': False, 'cachedir': tmp_dir}): with patch.dict(archive.__salt__, {'file.directory_exists': mock_false, 'file.file_exists': mock_false, 'state.single': state_single_mock, 'file.makedirs': mock_true, 'cmd.run_all': mock_run, 'archive.list': list_mock, 'file.source_list': mock_source_list}): filename = os.path.join( tmp_dir, 'files/test/_tmp_file.tar.gz' ) for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts): ret = archive.extracted(tmp_dir, source, options=test_opts, enforce_toplevel=False) ret_opts.append(filename) mock_run.assert_called_with(ret_opts, cwd=tmp_dir, python_shell=False)
def test_list_installed_command(self): # Given r_output = "com.apple.pkg.iTunes" # When mock_cmd = MagicMock(return_value=r_output) with patch.dict(darwin_pkgutil.__salt__, {'cmd.run_stdout': mock_cmd}): output = darwin_pkgutil.list_() # Then mock_cmd.assert_called_with("/usr/sbin/pkgutil --pkgs")
def test_list_installed_command(self): # Given r_output = "com.apple.pkg.iTunes" # When mock_cmd = MagicMock(return_value=r_output) with patch.dict(mac_pkgutil.__salt__, {'cmd.run_stdout': mock_cmd}): output = mac_pkgutil.list_() # Then mock_cmd.assert_called_with(['/usr/sbin/pkgutil', '--pkgs'], python_shell=False)
def test_list_command_with_prefix(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.list_(prefix="bb") mock.assert_called_with("pip freeze", runas=None, cwd=None) self.assertEqual(ret, {"bbfreeze-loader": "1.1.0", "bbfreeze": "1.1.0"})
def test_extracted_tar(self): ''' archive.extracted tar options ''' source = 'file.tar.gz' tmp_dir = os.path.join(tempfile.gettempdir(), 'test_archive', '') test_tar_opts = [ '--no-anchored foo', 'v -p --opt', '-v -p', '--long-opt -z', 'z -v -weird-long-opt arg', ] ret_tar_opts = [ ['tar', 'x', '--no-anchored', 'foo', '-f'], ['tar', 'xv', '-p', '--opt', '-f'], ['tar', 'x', '-v', '-p', '-f'], ['tar', 'x', '--long-opt', '-z', '-f'], ['tar', 'xz', '-v', '-weird-long-opt', 'arg', '-f'], ] mock_true = MagicMock(return_value=True) mock_false = MagicMock(return_value=False) ret = {'stdout': ['saltines', 'cheese'], 'stderr': 'biscuits', 'retcode': '31337', 'pid': '1337'} mock_run = MagicMock(return_value=ret) with patch('os.path.exists', mock_true): with patch.dict(archive.__opts__, {'test': False, 'cachedir': tmp_dir}): with patch.dict(archive.__salt__, {'file.directory_exists': mock_false, 'file.file_exists': mock_false, 'file.makedirs': mock_true, 'cmd.run_all': mock_run}): if HAS_PWD: running_as = pwd.getpwuid(os.getuid()).pw_name else: running_as = 'root' filename = os.path.join( tmp_dir, 'files/test/_tmp{0}_test_archive_.tar'.format( '' if running_as == 'root' else '_{0}'.format(running_as) ) ) for test_opts, ret_opts in zip(test_tar_opts, ret_tar_opts): ret = archive.extracted(tmp_dir, source, 'tar', tar_options=test_opts) ret_opts.append(filename) mock_run.assert_called_with(ret_opts, cwd=tmp_dir, python_shell=False)
def test_list_installed_command(self): # Given r_output = "com.apple.pkg.iTunes" # When mock_cmd = MagicMock(return_value=r_output) with patch.dict(mac_pkgutil.__salt__, {'cmd.run_stdout': mock_cmd}): output = mac_pkgutil.list_() # Then mock_cmd.assert_called_with( ['/usr/sbin/pkgutil', '--pkgs'], python_shell=False )
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) 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'", saltenv="base", runas=None, cwd=None)
def test_list_command_with_all(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', 'pip==9.0.1', 'pycrypto==2.6', 'setuptools==20.10.1' ] # N.B.: this is deliberately different from the "output" of pip freeze. # This is to demonstrate that the version reported comes from freeze # instead of from the pip.version function. mock_version = '9.0.0' mock = MagicMock(return_value={ 'retcode': 0, 'stdout': '\n'.join(eggs) }) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): with patch('salt.modules.pip.version', MagicMock(return_value=mock_version)): ret = pip.list_() mock.assert_called_with( ['pip', 'freeze', '--all'], cwd=None, runas=None, python_shell=False, use_vt=False, ) self.assertEqual( ret, { 'SaltTesting-dev': '[email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8', 'M2Crypto': '0.21.1', 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.0', 'pip': '9.0.1', 'pycrypto': '2.6', 'setuptools': '20.10.1' }) # Non zero returncode raises exception? mock = MagicMock(return_value={'retcode': 1, 'stderr': 'CABOOOOMMM!'}) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): with patch('salt.modules.pip.version', MagicMock(return_value='6.1.1')): self.assertRaises( CommandExecutionError, pip.list_, )
def test_list_pkgs_as_list(self): ''' Test if it lists the packages currently installed in a dict ''' cmdmock = MagicMock(return_value='A 1.0\nB 2.0') sortmock = MagicMock() stringifymock = MagicMock() mock_ret = {'A': ['1.0'], 'B': ['2.0']} with patch.dict(pacman.__salt__, { 'cmd.run': cmdmock, 'pkg_resource.add_pkg': lambda pkgs, name, version: pkgs.setdefault(name, []).append(version), 'pkg_resource.sort_pkglist': sortmock, 'pkg_resource.stringify': stringifymock }): self.assertDictEqual(pacman.list_pkgs(True), mock_ret) sortmock.assert_called_with(mock_ret) stringifymock.assert_not_called()
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) mock.assert_called_with( ['pip', 'install', pkg], saltenv='base', runas=None, cwd=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) mock_run_all.assert_called_with( ['pip', 'install', '--pre', pkg], saltenv='base', runas=None, cwd=None, use_vt=False, python_shell=False, )
def test_list_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_version = '6.1.1' mock = MagicMock(return_value={ 'retcode': 0, 'stdout': '\n'.join(eggs) }) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): with patch('salt.modules.pip.version', MagicMock(return_value=mock_version)): ret = pip.list_() mock.assert_called_with( ['pip', 'freeze'], cwd=None, runas=None, python_shell=False, use_vt=False, ) self.assertEqual( ret, { 'SaltTesting-dev': '[email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8', 'M2Crypto': '0.21.1', 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.0', 'pip': mock_version, 'pycrypto': '2.6' }) # Non zero returncode raises exception? mock = MagicMock(return_value={'retcode': 1, 'stderr': 'CABOOOOMMM!'}) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): with patch('salt.modules.pip.version', MagicMock(return_value='6.1.1')): self.assertRaises( CommandExecutionError, pip.list_, )
def test_list_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( side_effect=[ {'retcode': 0, 'stdout': 'pip MOCKED_VERSION'}, {'retcode': 0, 'stdout': '\n'.join(eggs)} ] ) with patch.dict(pip.__salt__, {'cmd.run_all': mock}): ret = pip.list_() mock.assert_called_with( 'pip freeze', runas=None, cwd=None, python_shell=False, ) self.assertEqual( ret, { 'SaltTesting-dev': '[email protected]:s0undt3ch/salt-testing.git@9ed81aa2f918d59d3706e56b18f0782d1ea43bf8', 'M2Crypto': '0.21.1', 'bbfreeze-loader': '1.1.0', 'bbfreeze': '1.1.0', 'pip': 'MOCKED_VERSION', 'pycrypto': '2.6' } ) # 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.list_, )
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) mock.assert_called_with( ['pip', 'install', pkg], saltenv='base', runas=None, cwd=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) mock_run_all.assert_called_with( ['pip', 'install', '--pre', pkg], saltenv='base', runas=None, cwd=None, use_vt=False, python_shell=False, )
def test_list_pkgs_as_list(self): ''' Test if it lists the packages currently installed in a dict ''' cmdmock = MagicMock(return_value='A 1.0\nB 2.0') sortmock = MagicMock() stringifymock = MagicMock() mock_ret = {'A': ['1.0'], 'B': ['2.0']} with patch.dict( pacman.__salt__, { 'cmd.run': cmdmock, 'pkg_resource.add_pkg': lambda pkgs, name, version: pkgs.setdefault(name, []). append(version), 'pkg_resource.sort_pkglist': sortmock, 'pkg_resource.stringify': stringifymock }): self.assertDictEqual(pacman.list_pkgs(True), mock_ret) sortmock.assert_called_with(mock_ret) self.assertTrue(stringifymock.call_count == 0)