def test_get_all_proxies_windows(self): ''' Test to make sure that we correctly get the current proxy info on Windows ''' results = [{'vdata': 'http=192.168.0.1:3128;https=192.168.0.2:3128;ftp=192.168.0.3:3128'}, {'vdata': 1}] mock = MagicMock(side_effect=results) expected = {'enabled': True, 'http': {'server': '192.168.0.1', 'port': '3128'}, 'https': {'server': '192.168.0.2', 'port': '3128'}, 'ftp': {'server': '192.168.0.3', 'port': '3128'}} calls = [ call(hive='HKEY_CURRENT_USER', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', vname='ProxyServer'), call(hive='HKEY_CURRENT_USER', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', vname='ProxyEnable')] with patch.dict(proxy.__grains__, {'os': 'Windows'}): with patch.dict(proxy.__salt__, {'reg.read_value': mock}): out = proxy.get_proxy_win() mock.assert_has_calls(calls) self.assertEqual(expected, out)
def test_set_proxy_windows_no_ftp(self): ''' Test to make sure that we correctly set the proxy info on Windows ''' calls = [ call(hive='HKEY_CURRENT_USER', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', vname='ProxyServer', vdata='http=192.168.0.1:3128;https=192.168.0.1:3128;'), call(hive='HKEY_CURRENT_USER', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', vname='ProxyEnable', vdata=1, vtype='REG_DWORD'), call(hive='HKEY_CURRENT_USER', key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', vname='ProxyOverride', vdata='<local>;.moo.com;.salt.com')] mock_reg = MagicMock() mock_cmd = MagicMock() with patch.dict(proxy.__grains__, {'os': 'Windows'}): with patch.dict(proxy.__salt__, {'reg.set_value': mock_reg, 'cmd.run': mock_cmd}): out = proxy.set_proxy_win(server='192.168.0.1', port=3128, types=['http', 'https'], bypass_hosts=['.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)
def test_cloud_grains_present_gcp(self): ''' Test to check cloud_grains_present for gcp ''' ret = {'name': 'name', 'changes': {'cloud_provider': 'google-cloud-platform', 'gcp_instance_id': 'm_id', 'gcp_instance_name': 'm_name'}, 'result': True, 'comment': 'Cloud grains set'} mock_detect_cloud = MagicMock(return_value='google-cloud-platform') mock_set_grains = MagicMock() mock_http_query = MagicMock(side_effect=[{'body': 'm_id'}, {'body': 'm_name'}]) with patch.dict(crmshmod.__salt__, {'crm.detect_cloud': mock_detect_cloud, 'grains.set': mock_set_grains, 'http.query': mock_http_query}): assert crmshmod.cloud_grains_present(name='name') == ret mock_detect_cloud.assert_called_once_with() mock_http_query.assert_has_calls([ mock.call( url='http://metadata.google.internal/computeMetadata/v1/instance/id', header_dict={"Metadata-Flavor": "Google"}), mock.call( url='http://metadata.google.internal/computeMetadata/v1/instance/name', header_dict={"Metadata-Flavor": "Google"}) ]) mock_set_grains.assert_has_calls([ mock.call('cloud_provider', 'google-cloud-platform'), mock.call('gcp_instance_id', 'm_id'), mock.call('gcp_instance_name', 'm_name') ])
def test_installed_invalid_params(self): ''' Test to check installed when install fails ''' ret = { 'name': 'prd', 'changes': {}, 'result': False, 'comment': 'If config_file is not provided ' 'system_user_password and sapadm_password are mandatory' } mock_installed = MagicMock(return_value=False) mock_remove = MagicMock() with patch.dict(hanamod.__salt__, { 'hana.is_installed': mock_installed, 'file.remove': mock_remove }): assert hanamod.installed('prd', '00', 'pass', '/software', 'root', 'pass') == ret mock_remove.assert_has_calls([ mock.call(hanamod.TMP_CONFIG_FILE), mock.call('{}.xml'.format(hanamod.TMP_CONFIG_FILE)) ])
def test_set_static_dns(self): ''' Test if it set static DNS configuration on a Windows NIC. ''' mock_cmd = MagicMock() with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual( win_ip.set_static_dns('Ethernet', '192.168.1.252', '192.168.1.253'), { 'DNS Server': ('192.168.1.252', '192.168.1.253'), 'Interface': 'Ethernet' }) mock_cmd.assert_has_calls([ call([ 'netsh', 'interface', 'ip', 'set', 'dns', 'name=Ethernet', 'source=static', 'address=192.168.1.252', 'register=primary' ], python_shell=False), call([ 'netsh', 'interface', 'ip', 'add', 'dns', 'name=Ethernet', 'address=192.168.1.253', 'index=2' ], python_shell=False) ])
def test_get_fmri_single_named(): """ Test getting the frmi for a specific package - a single package pkg://solaris/compress/zip 3.0-11.4.0.0.1.14.0:20180814T153154Z """ install_target = "compress/zip" pkg_info_out = [ "pkg://solaris/compress/[email protected]:20180814T153154Z " " i--", ] run_stdout_mock = MagicMock(return_value="\n".join(pkg_info_out)) patches = { "cmd.run_stdout": run_stdout_mock, } with patch.dict(solarisipspkg.__salt__, patches): with patch("salt.modules.solarisipspkg.list_pkgs", ListPackages(1)): added = solarisipspkg.get_fmri(install_target, refresh=False) expected = ["pkg://solaris/compress/zip"] assert added == expected expected_calls = [ call(["/bin/pkg", "list", "-aHv", install_target], ), ] run_stdout_mock.assert_has_calls(expected_calls, any_order=True) assert run_stdout_mock.call_count == 1
def test_user_chpass(self): ''' Test changing a MySQL user password in mysql exec module ''' connect_mock = MagicMock() with patch.object(mysql, '_connect', connect_mock): with patch.object(mysql, 'version', return_value='8.0.10'): with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): mysql.user_chpass('testuser', password='******') calls = ( call().cursor().execute( 'UPDATE mysql.user SET Password=PASSWORD(%(password)s) WHERE User=%(user)s AND Host = %(host)s;', { 'password': '******', 'user': '******', 'host': 'localhost', }), call().cursor().execute('FLUSH PRIVILEGES;'), ) connect_mock.assert_has_calls(calls, any_order=True) connect_mock = MagicMock() with patch.object(mysql, '_connect', connect_mock): with patch.object(mysql, 'version', return_value='8.0.11'): with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): mysql.user_chpass('testuser', password='******') calls = ( call().cursor().execute( "ALTER USER 'testuser'@'localhost' IDENTIFIED BY 'BLUECOW';" ), call().cursor().execute('FLUSH PRIVILEGES;'), ) connect_mock.assert_has_calls(calls, any_order=True)
def _runner(self, expected_ret, test=False, check=False, add=False, add_assertion=False): mock_check = MagicMock(return_value=check) mock_add = MagicMock(return_value=add) with patch.dict(ipset.__opts__, {"test": test}): with patch.dict(ipset.__salt__, { "ipset.check": mock_check, "ipset.add": mock_add }): actual_ret = ipset.present(self.fake_name, self.fake_entries, set_name=self.fake_name) mock_check.assert_has_calls( [call(self.fake_name, e, "ipv4") for e in self.fake_entries], any_order=True) if add_assertion: expected_calls = [ call(self.fake_name, e, "ipv4", set_name=self.fake_name) for e in self.fake_entries ] if add is not True: # if the add fails, then it will only get called once. expected_calls = expected_calls[:1] mock_add.assert_has_calls(expected_calls, any_order=True) else: self.assertTrue(mock_add.call_count == 0) self.assertDictEqual(actual_ret, expected_ret)
def _runner( self, expected_ret, test=False, check=False, delete=False, delete_assertion=False, ): mock_check = MagicMock(return_value=check) mock_delete = MagicMock(return_value=delete) with patch.dict(ipset.__opts__, {"test": test}): with patch.dict(ipset.__salt__, { "ipset.check": mock_check, "ipset.delete": mock_delete }): actual_ret = ipset.absent(self.fake_name, self.fake_entries, set_name=self.fake_name) mock_check.assert_has_calls( [call(self.fake_name, e, "ipv4") for e in self.fake_entries], any_order=True) if delete_assertion: expected_calls = [ call(self.fake_name, e, "ipv4", set_name=self.fake_name) for e in self.fake_entries ] if delete is not True: expected_calls = expected_calls[:1] mock_delete.assert_has_calls(expected_calls, any_order=True) else: self.assertTrue(mock_delete.call_count == 0) self.assertDictEqual(actual_ret, expected_ret)
def test_set_corosync_unicast(self, mock_authkey, mock_add, mock_set): ''' Test _set_corosync_unicast ''' mock_cmd_run = MagicMock() mock_file_line = MagicMock() with patch.dict(crmshmod.__salt__, { 'cmd.run': mock_cmd_run, 'file.line': mock_file_line }): crmshmod._set_corosync_unicast('1.0.1.0', 'node') mock_file_line.assert_called_once_with( path='/etc/corosync/corosync.conf', match='.*mcastaddr:.*', mode='delete') mock_cmd_run.assert_has_calls([ mock.call('{crm_command} cluster stop'.format( crm_command=crmshmod.CRM_COMMAND), raise_err=True), mock.call('{crm_command} cluster start'.format( crm_command=crmshmod.CRM_COMMAND), raise_err=True) ]) mock_set.assert_called_once_with('totem.transport', 'udpu') mock_add.assert_called_once_with('1.0.1.0', 'node') mock_authkey.assert_called_once_with()
def test_rsc_defaults_present(self): ''' Test to check rsc_defaults_present ''' ret = { 'name': 'name', 'changes': { 'data1': 'value1', 'data2': 'value2' }, 'result': True, 'comment': 'Cluster rsc_defaults configured' } mock_status = MagicMock(return_value=0) mock_configure_rsc_defaults = MagicMock() with patch.dict( crmshmod.__salt__, { 'crm.status': mock_status, 'crm.configure_rsc_defaults': mock_configure_rsc_defaults }): assert crmshmod.cluster_rsc_defaults_present(name='name', rsc_defaults={ 'data1': 'value1', 'data2': 'value2' }) == ret mock_status.assert_called_once_with() mock_configure_rsc_defaults.assert_has_calls([ mock.call(option='data1', value='value1'), mock.call(option='data2', value='value2') ])
def test_installed_error(self): ''' Test to check installed when install fails ''' ret = { 'name': 'prd', 'changes': { 'config_file': 'new' }, 'result': False, 'comment': 'hana command error' } mock_installed = MagicMock(return_value=False) mock_create = MagicMock(return_value='hana_created.conf') mock_update = MagicMock(return_value='hana_updated.conf') mock_install = MagicMock( side_effect=exceptions.CommandExecutionError('hana command error')) mock_remove = MagicMock() with patch.dict( hanamod.__salt__, { 'hana.is_installed': mock_installed, 'hana.create_conf_file': mock_create, 'hana.update_conf_file': mock_update, 'hana.install': mock_install, 'file.remove': mock_remove }): assert hanamod.installed('prd', '00', 'pass', '/software', 'root', 'pass', sapadm_password='******', system_user_password='******') == ret mock_create.assert_called_once_with( software_path='/software', conf_file=hanamod.TMP_CONFIG_FILE, root_user='******', root_password='******') mock_update.assert_called_once_with( conf_file='hana_created.conf', sid='PRD', number='00', password='******', root_user='******', root_password='******', sapadm_password='******', system_user_password='******') mock_install.assert_called_once_with(software_path='/software', conf_file='hana_updated.conf', root_user='******', root_password='******') mock_remove.assert_has_calls([ mock.call(hanamod.TMP_CONFIG_FILE), mock.call('{}.xml'.format(hanamod.TMP_CONFIG_FILE)) ])
def test_install_pkgs(self): """ Test package install behavior for the following conditions: - only base package name is given ('png') - a flavor is specified ('vim--gtk2') - a branch is specified ('ruby%2.3') """ parsed_targets = ( { "vim--gtk2": None, "png": None, "ruby%2.3": None }, "repository", ) cmd_out = { "retcode": 0, "stdout": "quirks-2.241 signed on 2016-07-26T16:56:10Z", "stderr": "", } run_all_mock = MagicMock(return_value=cmd_out) patches = { "cmd.run_all": run_all_mock, "pkg_resource.parse_targets": MagicMock(return_value=parsed_targets), "pkg_resource.stringify": MagicMock(), "pkg_resource.sort_pkglist": MagicMock(), } with patch.dict(openbsdpkg.__salt__, patches): with patch("salt.modules.openbsdpkg.list_pkgs", ListPackages()): added = openbsdpkg.install() expected = { "png": { "new": "1.6.23", "old": "" }, "ruby": { "new": "2.3.1p1", "old": "" }, } self.assertDictEqual(added, expected) expected_calls = [ call("pkg_add -x -I png--%", output_loglevel="trace", python_shell=False), call("pkg_add -x -I ruby--%2.3", output_loglevel="trace", python_shell=False), call("pkg_add -x -I vim--gtk2%", output_loglevel="trace", python_shell=False), ] run_all_mock.assert_has_calls(expected_calls, any_order=True) self.assertEqual(run_all_mock.call_count, 3)
def test_get_slave_status_bad_server(self): ''' Test get_slave_status in the mysql execution module, simulating a broken server ''' connect_mock = MagicMock(return_value=None) mysql._connect = connect_mock with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): rslt = mysql.get_slave_status() connect_mock.assert_has_calls([call()]) self.assertEqual(rslt, [])
def _test_call(self, function, expected_sql, *args, **kwargs): connect_mock = MagicMock() mysql._connect = connect_mock with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): function(*args, **kwargs) if isinstance(expected_sql, dict): calls = call().cursor().execute('{0}'.format(expected_sql['sql']), expected_sql['sql_args']) else: calls = call().cursor().execute('{0}'.format(expected_sql)) connect_mock.assert_has_calls((calls,), True)
def test_get_slave_status_bad_server(): """ Test get_slave_status in the mysql execution module, simulating a broken server """ connect_mock = MagicMock(return_value=None) with patch.object(mysql, "_connect", connect_mock): with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): rslt = mysql.get_slave_status() connect_mock.assert_has_calls([call()]) assert rslt == []
def test_install_dict_pkgs_with_version(): """ Test installing a list of packages in a dict with versions """ install_target = [ { "tree": "1.7.0-11.4.0.0.1.14.0:20180814T163602Z" }, { "xclock": "1.0.7-11.4.0.0.1.14.0:20180814T173537Z" }, ] cmd_out = { "retcode": 0, "stdout": "", "stderr": "", } run_all_mock = MagicMock(return_value=cmd_out) patches = { "cmd.run_all": run_all_mock, "solarisipspkg.is_installed": MagicMock(return_value=False), "cmd.retcode": MagicMock(return_value=False), } with patch.dict(solarisipspkg.__salt__, patches): with patch("salt.modules.solarisipspkg.list_pkgs", ListPackagesDict()), patch( "salt.modules.solarisipspkg.is_installed", MagicMock(return_value=False)): added = solarisipspkg.install(pkgs=install_target, refresh=False) expected = { "pkg://solaris/file/tree": { "new": "1.7.0-11.4.0.0.1.14.0:20180814T163602Z", "old": "", }, "pkg://solaris/x11/xclock": { "new": "1.0.7-11.4.0.0.1.14.0:20180814T173537Z", "old": "", }, } assert added == expected list_first = (tuple(install_target[0].keys())[0] + "@1.7.0-11.4.0.0.1.14.0:20180814T163602Z") list_second = (tuple(install_target[1].keys())[0] + "@1.0.7-11.4.0.0.1.14.0:20180814T173537Z") expected_calls = [ call( ["pkg", "install", "-v", "--accept", list_first, list_second], output_loglevel="trace", ), ] run_all_mock.assert_has_calls(expected_calls, any_order=True) assert run_all_mock.call_count == 1
def test_set_proxy_macos(self): ''' Test to make sure we can set the proxy settings on macOS ''' with patch.dict(proxy.__grains__, {'os': 'Darwin'}): expected = {'changes': { 'new': [ {'port': '3128', 'server': '192.168.0.1', 'service': 'http', 'user': '******'}, {'port': '3128', 'server': '192.168.0.1', 'service': 'https', 'user': '******'}, {'port': '3128', 'server': '192.168.0.1', 'service': 'ftp', 'user': '******'}, {'bypass_domains': ['salt.com', 'test.com']}] }, 'comment': 'http proxy settings updated correctly\nhttps proxy settings updated correctly\nftp proxy ' 'settings updated correctly\nProxy bypass domains updated correctly\n', 'name': '192.168.0.1', 'result': True} set_proxy_mock = MagicMock(return_value=True) patches = { 'proxy.get_http_proxy': MagicMock(return_value={}), 'proxy.get_https_proxy': MagicMock(return_value={}), 'proxy.get_ftp_proxy': MagicMock(return_value={}), 'proxy.get_proxy_bypass': MagicMock(return_value=[]), 'proxy.set_http_proxy': set_proxy_mock, 'proxy.set_https_proxy': set_proxy_mock, 'proxy.set_ftp_proxy': set_proxy_mock, 'proxy.set_proxy_bypass': 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']) if six.PY3: # Sorting is different in Py3 out['changes']['new'][-1]['bypass_domains'] = sorted(out['changes']['new'][-1]['bypass_domains']) calls = [ call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call(['salt.com', 'test.com'], 'Ethernet') ] set_proxy_mock.assert_has_calls(calls) self.assertEqual(out, expected)
def test_install_pkgs(self): ''' Test package install behavior for the following conditions: - only base package name is given ('png') - a flavor is specified ('vim--gtk2') - a branch is specified ('ruby%2.3') ''' parsed_targets = ({ 'vim--gtk2': None, 'png': None, 'ruby%2.3': None }, "repository") cmd_out = { 'retcode': 0, 'stdout': 'quirks-2.241 signed on 2016-07-26T16:56:10Z', 'stderr': '' } run_all_mock = MagicMock(return_value=cmd_out) patches = { 'cmd.run_all': run_all_mock, 'pkg_resource.parse_targets': MagicMock(return_value=parsed_targets), 'pkg_resource.stringify': MagicMock(), 'pkg_resource.sort_pkglist': MagicMock(), } with patch.dict(openbsdpkg.__salt__, patches): with patch('salt.modules.openbsdpkg.list_pkgs', ListPackages()): added = openbsdpkg.install() expected = { 'png': { 'new': '1.6.23', 'old': '' }, 'ruby': { 'new': '2.3.1p1', 'old': '' } } self.assertDictEqual(added, expected) expected_calls = [ call('pkg_add -x -I png--%', output_loglevel='trace', python_shell=False), call('pkg_add -x -I ruby--%2.3', output_loglevel='trace', python_shell=False), call('pkg_add -x -I vim--gtk2%', output_loglevel='trace', python_shell=False), ] run_all_mock.assert_has_calls(expected_calls, any_order=True) self.assertEqual(run_all_mock.call_count, 3)
def test_third_attempt_successful_connection(self): with patch('ssl.SSLContext', MagicMock()), \ patch('ssl._create_unverified_context', MagicMock()): exc = vim.fault.HostConnectFault() exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]' exc2 = Exception('certificate verify failed') mock_sc = MagicMock(side_effect=[exc, exc2, None]) mock_ssl_unverif = MagicMock() mock_ssl_context = MagicMock() with patch('salt.utils.vmware.SmartConnect', mock_sc): with patch('ssl._create_unverified_context', mock_ssl_unverif): with patch('ssl.SSLContext', mock_ssl_context): 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_ssl_context.assert_called_once_with( ssl.PROTOCOL_TLSv1) mock_ssl_unverif.assert_called_once_with() calls = [ call(host='fake_host.fqdn', user='******', pwd='fake_password', protocol='fake_protocol', port=1, b64token='fake_token', mechanism='sspi'), call(host='fake_host.fqdn', user='******', pwd='fake_password', protocol='fake_protocol', port=1, sslContext=mock_ssl_unverif.return_value, b64token='fake_token', mechanism='sspi'), call(host='fake_host.fqdn', user='******', pwd='fake_password', protocol='fake_protocol', port=1, sslContext=mock_ssl_context.return_value, b64token='fake_token', mechanism='sspi'), ] mock_sc.assert_has_calls(calls)
def test_install_single_named_package(): """ Test installing a single package - a single package SUNWbashS from current drive """ install_target = "SUNWbashS" parsed_targets = ( { install_target: None }, "repository", ) cmd_out = { "retcode": 0, "stdout": "", "stderr": "", } run_all_mock = MagicMock(return_value=cmd_out) patches = { "cmd.run_all": run_all_mock, "pkg_resource.parse_targets": MagicMock(return_value=parsed_targets), "pkg_resource.stringify": MagicMock(), "pkg_resource.sort_pkglist": MagicMock(), "cmd.retcode": MagicMock(return_value=False), } with patch.dict(solarispkg.__salt__, patches): with patch("salt.modules.solarispkg.list_pkgs", ListPackages()): added = solarispkg.install( install_target, sources=[{ install_target: "tests/pytest/unit/module/sol10_pkg/bashs" }], refresh=False, ) expected = { "SUNWbashS": { "new": "11.10.0,REV=2005.01.08.01.09", "old": "" }, } assert added == expected expected_calls = [ call( ["/usr/sbin/pkgadd", "-n", "-a", ANY, "-d", install_target, "all"], output_loglevel="trace", python_shell=False, ), ] run_all_mock.assert_has_calls(expected_calls, any_order=True) assert run_all_mock.call_count == 1
def test_get_pkg_id_with_files(): """ Test getting a the id for a package """ with patch( "salt.modules.macpackage._get_pkg_id_from_pkginfo" ) as pkg_id_pkginfo_mock: 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") assert out == expected
def _test_call(function, expected_sql, *args, **kwargs): connect_mock = MagicMock() with patch.object(mysql, "_connect", connect_mock): with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): function(*args, **kwargs) if isinstance(expected_sql, dict): calls = (call().cursor().execute( "{}".format(expected_sql["sql"]), expected_sql["sql_args"])) else: calls = call().cursor().execute("{}".format(expected_sql)) connect_mock.assert_has_calls((calls, ), True)
def test_db_installed_correct(self): ''' Test to check installed when it is installed correctly ''' ret = { 'name': '192.168.10.15:30015', 'changes': { 'host': '192.168.10.15:30015' }, 'result': True, 'comment': 'Netweaver DB instance installed' } mock_db_installed = MagicMock(side_effect=[False, True]) mock_attach = MagicMock(return_value='192.168.15.1') mock_setup_cwd = MagicMock(return_value='/tmp_nw') mock_install = MagicMock() with patch.dict( netweavermod.__salt__, { 'netweaver.is_db_installed': mock_db_installed, 'netweaver.attach_virtual_host': mock_attach, 'netweaver.setup_cwd': mock_setup_cwd, 'netweaver.install': mock_install }): assert netweavermod.db_installed('192.168.10.15', 30015, 'SAPABAP1', 'schema_pass', '/software', 'root', 'pass', 'config_file', 'vhost', 'eth1', 'productID') == ret mock_db_installed.assert_has_calls([ mock.call(host='192.168.10.15', port=30015, schema_name='SAPABAP1', schema_password='******'), mock.call(host='192.168.10.15', port=30015, schema_name='SAPABAP1', schema_password='******'), ]) mock_attach.assert_called_once_with(virtual_host='vhost', virtual_host_interface='eth1') mock_setup_cwd.assert_called_once_with(software_path='/software', cwd='/tmp/swpm_unattended', additional_dvds=None) mock_install.assert_called_once_with(software_path='/software', virtual_host='vhost', product_id='productID', conf_file='config_file', root_user='******', root_password='******', cwd='/tmp_nw')
def test_installed_config_file(self): ''' Test to check installed when config file is imported ''' ret = { 'name': 'prd', 'changes': { 'sid': 'prd', 'config_file': 'hana.conf' }, 'result': True, 'comment': 'HANA installed' } mock_installed = MagicMock(return_value=False) mock_cp = MagicMock() mock_update = MagicMock(return_value='hana_updated.conf') mock_install = MagicMock() mock_remove = MagicMock() with patch.dict( hanamod.__salt__, { 'hana.is_installed': mock_installed, 'cp.get_file': mock_cp, 'hana.update_conf_file': mock_update, 'hana.install': mock_install, 'file.remove': mock_remove }): assert hanamod.installed('prd', '00', 'pass', '/software', 'root', 'pass', config_file='hana.conf', extra_parameters=[{ 'hostname': 'hana01' }]) == ret mock_cp.assert_called_once_with(path='hana.conf', dest=hanamod.TMP_CONFIG_FILE) mock_update.assert_called_once_with( conf_file=hanamod.TMP_CONFIG_FILE, extra_parameters={u'hostname': u'hana01'}) mock_install.assert_called_once_with(software_path='/software', conf_file='hana_updated.conf', root_user='******', root_password='******') mock_remove.assert_has_calls([ mock.call(hanamod.TMP_CONFIG_FILE), mock.call('{}.xml'.format(hanamod.TMP_CONFIG_FILE)) ])
def test_installed_not_installed(self, mock_get): ''' Test to check installed when the installation fails ''' ret = { 'name': 'prd', 'changes': {}, 'result': False, 'comment': 'Netweaver was not installed' } mock_installed = MagicMock(side_effect=[False, False]) mock_get.return_value = '' mock_attach = MagicMock(return_value='192.168.15.1') mock_setup_cwd = MagicMock(return_value='/tmp_nw') mock_install = MagicMock() with patch.dict( netweavermod.__salt__, { 'netweaver.is_installed': mock_installed, 'netweaver.attach_virtual_host': mock_attach, 'netweaver.setup_cwd': mock_setup_cwd, 'netweaver.install': mock_install }): assert netweavermod.installed('prd', '00', 'pass', '/software', 'root', 'pass', 'config_file', 'vhost', 'eth1', 'productID') == ret mock_installed.assert_has_calls([ mock.call(sid='prd', inst='00', password='******', sap_instance=''), mock.call(sid='prd', inst='00', password='******', sap_instance=''), ]) mock_get.assert_called_once_with('productID') mock_attach.assert_called_once_with(virtual_host='vhost', virtual_host_interface='eth1', virtual_host_mask=24) mock_setup_cwd.assert_called_once_with(software_path='/software', cwd='/tmp/swpm_unattended', additional_dvds=None) mock_install.assert_called_once_with(software_path='/software', virtual_host='vhost', product_id='productID', conf_file='config_file', root_user='******', root_password='******', cwd='/tmp_nw')
def test_set_hibernate_timeout_scheme(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock() mock.side_effect = [self.query_ouput] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): powercfg.set_hibernate_timeout(0, "dc", scheme="SCHEME_MIN") calls = [ call('powercfg /setdcvalueindex SCHEME_MIN SUB_SLEEP HIBERNATEIDLE 0', python_shell=False) ] mock.assert_has_calls(calls)
def test_get_all_proxies_windows(): """ Test to make sure that we correctly get the current proxy info on Windows """ results = [ { "vdata": ("http=192.168.0.1:3128;https=192.168.0.2:3128;ftp=192.168.0.3:3128" ) }, { "vdata": 1 }, ] mock = MagicMock(side_effect=results) expected = { "enabled": True, "http": { "server": "192.168.0.1", "port": "3128" }, "https": { "server": "192.168.0.2", "port": "3128" }, "ftp": { "server": "192.168.0.3", "port": "3128" }, } calls = [ call( hive="HKEY_CURRENT_USER", key= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", vname="ProxyServer", ), call( hive="HKEY_CURRENT_USER", key= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", vname="ProxyEnable", ), ] with patch.dict(proxy.__grains__, {"os": "Windows"}), patch.dict(proxy.__utils__, {"reg.read_value": mock}): out = proxy.get_proxy_win() mock.assert_has_calls(calls) assert expected == out
def test_traversal_spec(self): mock_traversal_spec = MagicMock(return_value='traversal_spec') with patch( 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec', mock_traversal_spec): vmware.get_dvss(self.mock_dc_ref) mock_traversal_spec.assert_has_calls([ call(path='childEntity', skip=False, type=vim.Folder), call(path='networkFolder', skip=True, type=vim.Datacenter, selectSet=['traversal_spec']) ])
def test_set_hibernate_timeout(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock() mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): powercfg.set_hibernate_timeout(0, "dc") calls = [ call('powercfg /getactivescheme', python_shell=False), call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE 0', python_shell=False) ] mock.assert_has_calls(calls)