desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Tests adding of config file failure'
def test_persist_no_conf_failure(self):
with patch('salt.utils.files.fopen', mock_open()) as m_open: with patch('os.path.isfile', MagicMock(return_value=False)): m_open.side_effect = IOError(13, 'Permission denied', '/file') self.assertRaises(CommandExecutionError, mac_sysctl.persist, 'net.inet.icmp.icmplim', 50, config=None)
'Tests successful add of config file when previously not one'
def test_persist_no_conf_success(self):
with patch('salt.utils.files.fopen', mock_open()) as m_open: with patch('os.path.isfile', MagicMock(return_value=False)): mac_sysctl.persist('net.inet.icmp.icmplim', 50) helper_open = m_open() helper_open.write.assert_called_once_with('#\n# Kernel sysctl configuration\n#\n')
'Tests successful write to existing sysctl file'
def test_persist_success(self):
to_write = '#\n# Kernel sysctl configuration\n#\n' m_calls_list = [call.writelines(['net.inet.icmp.icmplim=50', '\n'])] with patch('salt.utils.files.fopen', mock_open(read_data=to_write)) as m_open: with patch('os.path.isfile', MagicMock(return_value=True)): mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=to_write) helper_open = m_open() calls_list = helper_open.method_calls self.assertEqual(calls_list, m_calls_list)
'Test installing a certificate into the macOS keychain'
def test_install_cert(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): keychain.install('/path/to/cert.p12', 'passw0rd') mock.assert_called_once_with('security import /path/to/cert.p12 -P passw0rd -k /Library/Keychains/System.keychain')
'Test installing a certificate into the macOS keychain with extras'
def test_install_cert_extras(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): with patch('salt.modules.mac_keychain.unlock_keychain') as unlock_mock: keychain.install('/path/to/cert.p12', 'passw0rd', '/path/to/chain', allow_any=True, keychain_password='passw0rd1') unlock_mock.assert_called_once_with('/path/to/chain', 'passw0rd1') mock.assert_called_once_with('security import /path/to/cert.p12 -P passw0rd -k /path/to/chain -A')
'Test uninstalling a certificate from the macOS keychain'
def test_uninstall_cert(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): keychain.uninstall('/path/to/cert.p12', 'passw0rd') mock.assert_called_once_with('security delete-certificate -c "/path/to/cert.p12" passw0rd')
'Test listing available certificates in a keychain'
def test_list_certs(self):
expected = ['com.apple.systemdefault', 'com.apple.kerberos.kdc'] mock = MagicMock(return_value='"com.apple.systemdefault"\n"com.apple.kerberos.kdc"') with patch.dict(keychain.__salt__, {'cmd.run': mock}): out = keychain.list_certs('/path/to/cert.p12') mock.assert_called_once_with('security find-certificate -a /path/to/cert.p12 | grep -o "alis".*\\" | grep -o \'\\"[-A-Za-z0-9.:() ]*\\"\'', python_shell=True) self.assertEqual(out, expected)
'Test getting the friendly name of a certificate'
def test_get_friendly_name(self):
expected = 'ID Installer Salt' mock = MagicMock(return_value='friendlyName: ID Installer Salt') with patch.dict(keychain.__salt__, {'cmd.run': mock}): out = keychain.get_friendly_name('/path/to/cert.p12', 'passw0rd') mock.assert_called_once_with('openssl pkcs12 -in /path/to/cert.p12 -passin pass:passw0rd -info -nodes -nokeys 2> /dev/null | grep friendlyName:', python_shell=True) self.assertEqual(out, expected)
'Test getting the default keychain'
def test_get_default_keychain(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): keychain.get_default_keychain('frank', 'system') mock.assert_called_once_with('security default-keychain -d system', runas='frank')
'Test setting the default keychain'
def test_set_default_keychain(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): keychain.set_default_keychain('/path/to/chain.keychain', 'system', 'frank') mock.assert_called_once_with('security default-keychain -d system -s /path/to/chain.keychain', runas='frank')
'Test unlocking the keychain'
def test_unlock_keychain(self):
mock = MagicMock() with patch.dict(keychain.__salt__, {'cmd.run': mock}): keychain.unlock_keychain('/path/to/chain.keychain', 'passw0rd') mock.assert_called_once_with('security unlock-keychain -p passw0rd /path/to/chain.keychain')
'test to show installed version of dnsmasq.'
def test_version(self):
mock = MagicMock(return_value='A B C') with patch.dict(dnsmasq.__salt__, {'cmd.run': mock}): self.assertEqual(dnsmasq.version(), 'C')
'Test to Show installed version of dnsmasq and compile options.'
def test_fullversion(self):
mock = MagicMock(return_value='A B C\nD E F G H I') with patch.dict(dnsmasq.__salt__, {'cmd.run': mock}): self.assertDictEqual(dnsmasq.fullversion(), {'version': 'C', 'compile options': ['G', 'H', 'I']})
'test to show installed version of dnsmasq.'
def test_set_config(self):
mock = MagicMock(return_value={'conf-dir': 'A'}) with patch.object(dnsmasq, 'get_config', mock): mock = MagicMock(return_value=['.', '~', 'bak', '#']) with patch.object(os, 'listdir', mock): self.assertDictEqual(dnsmasq.set_config(), {})
'Test that the kwargs returned from running the set_config function do not contain the __pub that may have been passed through in **kwargs.'
def test_set_config_filter_pub_kwargs(self):
with patch('salt.modules.dnsmasq.get_config', MagicMock(return_value={'conf-dir': 'A'})): mock_domain = 'local' mock_address = '/some-test-address.local/8.8.4.4' with patch.dict(dnsmasq.__salt__, {'file.append': MagicMock()}): ret = dnsmasq.set_config(follow=False, domain=mock_domain, address=mock_address, __pub_pid=8184, __pub_jid=20161101194639387946L, __pub_tgt='salt-call') self.assertEqual(ret, {'domain': mock_domain, 'address': mock_address})
'test to dumps all options from the config file.'
def test_get_config(self):
mock = MagicMock(return_value={'conf-dir': 'A'}) with patch.object(dnsmasq, 'get_config', mock): mock = MagicMock(return_value=['.', '~', 'bak', '#']) with patch.object(os, 'listdir', mock): self.assertDictEqual(dnsmasq.get_config(), {'conf-dir': 'A'})
'Tests that a CommandExecutionError is when a filename that doesn\'t exist is passed in.'
def test_parse_dnsmasq_no_file(self):
self.assertRaises(CommandExecutionError, dnsmasq._parse_dnamasq, 'filename')
'test for generic function for parsing dnsmasq files including includes.'
def test_parse_dnamasq(self):
with patch('os.path.isfile', MagicMock(return_value=True)): text_file_data = '\n'.join(['line here', 'second line', 'A=B', '#']) with patch('salt.utils.files.fopen', mock_open(read_data=text_file_data), create=True) as m: m.return_value.__iter__.return_value = text_file_data.splitlines() self.assertDictEqual(dnsmasq._parse_dnamasq('filename'), {'A': 'B', 'unparsed': ['line here', 'second line']})
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_get_coredump_network_config_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.get_coredump_network_config, HOST, USER, PASSWORD, esxi_hosts='foo')
'Tests error message returned with list of esxi_hosts.'
def test_get_coredump_network_config_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'Error': ERROR}}, vsphere.get_coredump_network_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_get_coredump_network_config_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'Coredump Config': {}}}, vsphere.get_coredump_network_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_get_coredump_network_config_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): self.assertEqual({HOST: {'Error': ERROR}}, vsphere.get_coredump_network_config(HOST, USER, PASSWORD))
'Tests successful function return for a single ESXi host.'
def test_get_coredump_network_config_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})): self.assertEqual({HOST: {'Coredump Config': {}}}, vsphere.get_coredump_network_config(HOST, USER, PASSWORD))
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_coredump_network_enable_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.coredump_network_enable, HOST, USER, PASSWORD, True, esxi_hosts='foo')
'Tests error message returned with list of esxi_hosts.'
def test_coredump_network_enable_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'Error': ERROR}}, vsphere.coredump_network_enable(HOST, USER, PASSWORD, True, esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_coredump_network_enable_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})): enabled = True host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'Coredump Enabled': enabled}}, vsphere.coredump_network_enable(HOST, USER, PASSWORD, enabled, esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_coredump_network_enable_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): self.assertEqual({HOST: {'Error': ERROR}}, vsphere.coredump_network_enable(HOST, USER, PASSWORD, True))
'Tests successful function return for a single ESXi host.'
def test_coredump_network_enable_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})): enabled = True self.assertEqual({HOST: {'Coredump Enabled': enabled}}, vsphere.coredump_network_enable(HOST, USER, PASSWORD, enabled))
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_set_coredump_network_config_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.set_coredump_network_config, HOST, USER, PASSWORD, 'loghost', 'foo', esxi_hosts='bar')
'Tests error message returned with list of esxi_hosts.'
def test_set_coredump_network_config_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'retcode': 1, 'success': False}}, vsphere.set_coredump_network_config(HOST, USER, PASSWORD, 'dump-ip.test.com', esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_set_coredump_network_config_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'retcode': 0, 'success': True}}, vsphere.set_coredump_network_config(HOST, USER, PASSWORD, 'dump-ip.test.com', esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_set_coredump_network_config_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1})): self.assertEqual({HOST: {'retcode': 1, 'success': False}}, vsphere.set_coredump_network_config(HOST, USER, PASSWORD, 'dump-ip.test.com'))
'Tests successful function return for a single ESXi host.'
def test_set_coredump_network_config_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})): self.assertEqual({HOST: {'retcode': 0, 'success': True}}, vsphere.set_coredump_network_config(HOST, USER, PASSWORD, 'dump-ip.test.com'))
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_get_firewall_status_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.get_firewall_status, HOST, USER, PASSWORD, esxi_hosts='foo')
'Tests error message returned with list of esxi_hosts.'
def test_get_firewall_status_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'success': False, 'Error': ERROR, 'rulesets': None}}, vsphere.get_firewall_status(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_get_firewall_status_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'rulesets': {}, 'success': True}}, vsphere.get_firewall_status(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_get_firewall_status_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): self.assertEqual({HOST: {'success': False, 'Error': ERROR, 'rulesets': None}}, vsphere.get_firewall_status(HOST, USER, PASSWORD))
'Tests successful function return for a single ESXi host.'
def test_get_firewall_status_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): self.assertEqual({HOST: {'rulesets': {}, 'success': True}}, vsphere.get_firewall_status(HOST, USER, PASSWORD))
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_enable_firewall_ruleset_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.enable_firewall_ruleset, HOST, USER, PASSWORD, 'foo', 'bar', esxi_hosts='baz')
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_syslog_service_reload_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.syslog_service_reload, HOST, USER, PASSWORD, esxi_hosts='foo')
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list, but we don\'t enter the \'loghost\'/firewall loop.'
def test_set_syslog_config_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.set_syslog_config, HOST, USER, PASSWORD, 'foo', 'bar', esxi_hosts='baz')
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list, and we enter the \'loghost\'/firewall loop.'
def test_set_syslog_config_esxi_hosts_not_list_firewall(self):
self.assertRaises(CommandExecutionError, vsphere.set_syslog_config, HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True, esxi_hosts='bar')
'Tests error message returned with list of esxi_hosts with \'loghost\' as syslog_config.'
def test_set_syslog_config_host_list_firewall_bad_retcode(self):
with patch('salt.modules.vsphere.enable_firewall_ruleset', MagicMock(return_value={'host_1.foo.com': {'retcode': 1, 'stdout': ERROR}})): with patch('salt.modules.vsphere._set_syslog_config_helper', MagicMock(return_value={})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'enable_firewall': {'message': ERROR, 'success': False}}}, vsphere.set_syslog_config(HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True, esxi_hosts=[host_1]))
'Tests successful function return with list of esxi_hosts with \'loghost\' as syslog_config.'
def test_set_syslog_config_host_list_firewall_success(self):
with patch('salt.modules.vsphere.enable_firewall_ruleset', MagicMock(return_value={'host_1.foo.com': {'retcode': 0}})): with patch('salt.modules.vsphere._set_syslog_config_helper', MagicMock(return_value={})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'enable_firewall': {'success': True}}}, vsphere.set_syslog_config(HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True, esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host with \'loghost\' as syslog_config.'
def test_set_syslog_config_firewall_bad_retcode(self):
with patch('salt.modules.vsphere.enable_firewall_ruleset', MagicMock(return_value={HOST: {'retcode': 1, 'stdout': ERROR}})): with patch('salt.modules.vsphere._set_syslog_config_helper', MagicMock(return_value={})): self.assertEqual({HOST: {'enable_firewall': {'message': ERROR, 'success': False}}}, vsphere.set_syslog_config(HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True))
'Tests successful function return for a single ESXi host with \'loghost\' as syslog_config.'
def test_set_syslog_config_firewall_success(self):
with patch('salt.modules.vsphere.enable_firewall_ruleset', MagicMock(return_value={HOST: {'retcode': 0}})): with patch('salt.modules.vsphere._set_syslog_config_helper', MagicMock(return_value={})): self.assertEqual({HOST: {'enable_firewall': {'success': True}}}, vsphere.set_syslog_config(HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True))
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_get_syslog_config_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.get_syslog_config, HOST, USER, PASSWORD, esxi_hosts='foo')
'Tests error message returned with list of esxi_hosts.'
def test_get_syslog_config_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'message': ERROR, 'success': False}}, vsphere.get_syslog_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_get_syslog_config_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'success': True}}, vsphere.get_syslog_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_get_syslog_config_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): self.assertEqual({HOST: {'message': ERROR, 'success': False}}, vsphere.get_syslog_config(HOST, USER, PASSWORD))
'Tests successful function return for a single ESXi host.'
def test_get_syslog_config_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): self.assertEqual({HOST: {'success': True}}, vsphere.get_syslog_config(HOST, USER, PASSWORD))
'Tests CommandExecutionError is raised when a syslog_config parameter is missing.'
def test_reset_syslog_config_no_syslog_config(self):
self.assertRaises(CommandExecutionError, vsphere.reset_syslog_config, HOST, USER, PASSWORD)
'Tests CommandExecutionError is raised when esxi_hosts is provided, but is not a list.'
def test_reset_syslog_config_esxi_hosts_not_list(self):
self.assertRaises(CommandExecutionError, vsphere.reset_syslog_config, HOST, USER, PASSWORD, syslog_config='test', esxi_hosts='foo')
'Tests error message returned when an invalid syslog_config parameter is provided.'
def test_reset_syslog_config_invalid_config_param(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={})): error = 'Invalid syslog configuration parameter' self.assertEqual({HOST: {'success': False, 'test': {'message': error, 'success': False}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='test'))
'Tests error message returned with list of esxi_hosts.'
def test_reset_syslog_config_host_list_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'success': False, 'logdir': {'message': ERROR, 'success': False}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='logdir', esxi_hosts=[host_1]))
'Tests successful function return when an esxi_host is provided.'
def test_reset_syslog_config_host_list_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): host_1 = 'host_1.foo.com' self.assertEqual({host_1: {'success': True, 'loghost': {'success': True}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='loghost', esxi_hosts=[host_1]))
'Tests error message given for a single ESXi host.'
def test_reset_syslog_config_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): self.assertEqual({HOST: {'success': False, 'logdir-unique': {'message': ERROR, 'success': False}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='logdir-unique'))
'Tests successful function return for a single ESXi host.'
def test_reset_syslog_config_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): self.assertEqual({HOST: {'success': True, 'default-rotate': {'success': True}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='default-rotate'))
'Tests successful function return for a single ESXi host when passing in multiple syslog_config values.'
def test_reset_syslog_config_success_multiple_configs(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): self.assertEqual({HOST: {'success': True, 'default-size': {'success': True}, 'default-timeout': {'success': True}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='default-size,default-timeout'))
'Tests successful function return for a single ESXi host when passing in multiple syslog_config values.'
def test_reset_syslog_config_success_all_configs(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})): self.assertEqual({HOST: {'success': True, 'logdir': {'success': True}, 'loghost': {'success': True}, 'default-rotate': {'success': True}, 'default-size': {'success': True}, 'default-timeout': {'success': True}, 'logdir-unique': {'success': True}}}, vsphere.reset_syslog_config(HOST, USER, PASSWORD, syslog_config='all'))
'Tests function returns False when an invalid syslog config is passed.'
def test_reset_syslog_config_params_no_valid_reset(self):
valid_resets = ['hello', 'world'] config = 'foo' ret = {'success': False, config: {'success': False, 'message': 'Invalid syslog configuration parameter'}} self.assertEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD, 'cmd', config, valid_resets))
'Tests function returns False when the esxxli function returns an unsuccessful retcode.'
def test_reset_syslog_config_params_error(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): valid_resets = ['hello', 'world'] error_dict = {'success': False, 'message': ERROR} ret = {'success': False, 'hello': error_dict, 'world': error_dict} self.assertDictEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD, 'cmd', valid_resets, valid_resets))
'Tests function returns True when the esxxli function returns a successful retcode.'
def test_reset_syslog_config_params_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})): valid_resets = ['hello', 'world'] ret = {'success': True, 'hello': {'success': True}, 'world': {'success': True}} self.assertDictEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD, 'cmd', valid_resets, valid_resets))
'Tests function returns False when an invalid syslog config is passed.'
def test_set_syslog_config_helper_no_valid_reset(self):
config = 'foo' ret = {'success': False, 'message': "'{0}' is not a valid config variable.".format(config)} self.assertEqual(ret, vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'bar'))
'Tests function returns False when the esxcli function returns an unsuccessful retcode.'
def test_set_syslog_config_helper_bad_retcode(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})): config = 'default-rotate' self.assertEqual({config: {'success': False, 'message': ERROR}}, vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'foo'))
'Tests successful function return.'
def test_set_syslog_config_helper_success(self):
with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})): config = 'logdir' self.assertEqual({config: {'success': True}}, vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'foo'))
'Test if it return raw configs for all interfaces.'
def test_raw_interface_configs(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertEqual(win_ip.raw_interface_configs(), ETHERNET_CONFIG)
'Test if it return configs for all interfaces.'
def test_get_all_interfaces(self):
ret = {'Ethernet': {'DHCP enabled': 'Yes', 'DNS servers configured through DHCP': ['1.2.3.4'], 'Default Gateway': '1.2.3.1', 'Gateway Metric': '0', 'InterfaceMetric': '20', 'Register with which suffix': 'Primary only', 'WINS servers configured through DHCP': ['None'], 'ip_addrs': [{'IP Address': '1.2.3.74', 'Netmask': '255.255.255.0', 'Subnet': '1.2.3.0/24'}]}} mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual(win_ip.get_all_interfaces(), ret)
'Test if it return the configuration of a network interface.'
def test_get_interface(self):
ret = {'DHCP enabled': 'Yes', 'DNS servers configured through DHCP': ['1.2.3.4'], 'Default Gateway': '1.2.3.1', 'Gateway Metric': '0', 'InterfaceMetric': '20', 'Register with which suffix': 'Primary only', 'WINS servers configured through DHCP': ['None'], 'ip_addrs': [{'IP Address': '1.2.3.74', 'Netmask': '255.255.255.0', 'Subnet': '1.2.3.0/24'}]} mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual(win_ip.get_interface('Ethernet'), ret)
'Test if it returns `True` if interface is enabled, otherwise `False`.'
def test_is_enabled(self):
mock_cmd = MagicMock(side_effect=[ETHERNET_ENABLE, '']) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(win_ip.is_enabled('Ethernet')) self.assertRaises(CommandExecutionError, win_ip.is_enabled, 'Ethernet')
'Test if it returns `True` if interface is disabled, otherwise `False`.'
def test_is_disabled(self):
mock_cmd = MagicMock(return_value=ETHERNET_ENABLE) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertFalse(win_ip.is_disabled('Ethernet'))
'Test if it enable an interface.'
def test_enable(self):
mock_cmd = MagicMock(return_value=ETHERNET_ENABLE) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(win_ip.enable('Ethernet')) mock_cmd = MagicMock(return_value='Connect state: Disconnected') with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertFalse(win_ip.enable('Ethernet'))
'Test if it disable an interface.'
def test_disable(self):
mock_cmd = MagicMock(return_value=ETHERNET_ENABLE) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertFalse(win_ip.disable('Ethernet')) mock_cmd = MagicMock(return_value='Connect state: Disconnected') with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(win_ip.disable('Ethernet'))
'Test if it disable an interface.'
def test_get_subnet_length(self):
self.assertEqual(win_ip.get_subnet_length('255.255.255.0'), 24) self.assertRaises(SaltInvocationError, win_ip.get_subnet_length, '255.255.0')
'Test if it set static IP configuration on a Windows NIC.'
def test_set_static_ip(self):
self.assertRaises(SaltInvocationError, win_ip.set_static_ip, 'Local Area Connection', '10.1.2/24') mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) mock_all = MagicMock(return_value={'retcode': 1, 'stderr': 'Error'}) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd, 'cmd.run_all': mock_all}): self.assertRaises(CommandExecutionError, win_ip.set_static_ip, 'Ethernet', '1.2.3.74/24', append=True) self.assertRaises(CommandExecutionError, win_ip.set_static_ip, 'Ethernet', '1.2.3.74/24') mock_all = MagicMock(return_value={'retcode': 0}) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd, 'cmd.run_all': mock_all}): self.assertDictEqual(win_ip.set_static_ip('Local Area Connection', '1.2.3.74/24'), {}) self.assertDictEqual(win_ip.set_static_ip('Ethernet', '1.2.3.74/24'), {'Address Info': {'IP Address': '1.2.3.74', 'Netmask': '255.255.255.0', 'Subnet': '1.2.3.0/24'}})
'Test if it set Windows NIC to get IP from DHCP.'
def test_set_dhcp_ip(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual(win_ip.set_dhcp_ip('Ethernet'), {'DHCP enabled': 'Yes', 'Interface': 'Ethernet'})
'Test if it set static DNS configuration on a Windows NIC.'
def test_set_static_dns(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) 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'})
'Test if it set DNS source to DHCP on Windows.'
def test_set_dhcp_dns(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual(win_ip.set_dhcp_dns('Ethernet'), {'DNS Server': 'DHCP', 'Interface': 'Ethernet'})
'Test if it set both IP Address and DNS to DHCP.'
def test_set_dhcp_all(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertDictEqual(win_ip.set_dhcp_all('Ethernet'), {'Interface': 'Ethernet', 'DNS Server': 'DHCP', 'DHCP enabled': 'Yes'})
'Test if it set DNS source to DHCP on Windows.'
def test_get_default_gateway(self):
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG) with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}): self.assertEqual(win_ip.get_default_gateway(), '1.2.3.1')
'Test to tests to the CPU performance of minions.'
def test_cpu(self):
with patch.dict(sysbench.__salt__, {'cmd.run': MagicMock(return_value={'A': 'a'})}): with patch.object(sysbench, '_parser', return_value={'A': 'a'}): self.assertEqual(sysbench.cpu(), {'Prime numbers limit: 500': {'A': 'a'}, 'Prime numbers limit: 5000': {'A': 'a'}, 'Prime numbers limit: 2500': {'A': 'a'}, 'Prime numbers limit: 1000': {'A': 'a'}})
'Test to this tests the performance of the processor\'s scheduler'
def test_threads(self):
with patch.dict(sysbench.__salt__, {'cmd.run': MagicMock(return_value={'A': 'a'})}): with patch.object(sysbench, '_parser', return_value={'A': 'a'}): self.assertEqual(sysbench.threads(), {'Yields: 500 Locks: 8': {'A': 'a'}, 'Yields: 200 Locks: 4': {'A': 'a'}, 'Yields: 1000 Locks: 16': {'A': 'a'}, 'Yields: 100 Locks: 2': {'A': 'a'}})
'Test to tests the implementation of mutex'
def test_mutex(self):
with patch.dict(sysbench.__salt__, {'cmd.run': MagicMock(return_value={'A': 'a'})}): with patch.object(sysbench, '_parser', return_value={'A': 'a'}): self.assertEqual(sysbench.mutex(), {'Mutex: 1000 Locks: 25000 Loops: 10000': {'A': 'a'}, 'Mutex: 50 Locks: 10000 Loops: 2500': {'A': 'a'}, 'Mutex: 1000 Locks: 10000 Loops: 5000': {'A': 'a'}, 'Mutex: 500 Locks: 50000 Loops: 5000': {'A': 'a'}, 'Mutex: 500 Locks: 25000 Loops: 2500': {'A': 'a'}, 'Mutex: 500 Locks: 10000 Loops: 10000': {'A': 'a'}, 'Mutex: 50 Locks: 50000 Loops: 10000': {'A': 'a'}, 'Mutex: 1000 Locks: 50000 Loops: 2500': {'A': 'a'}, 'Mutex: 50 Locks: 25000 Loops: 5000': {'A': 'a'}})
'Test to this tests the memory for read and write operations.'
def test_memory(self):
with patch.dict(sysbench.__salt__, {'cmd.run': MagicMock(return_value={'A': 'a'})}): with patch.object(sysbench, '_parser', return_value={'A': 'a'}): self.assertEqual(sysbench.memory(), {'Operation: read Scope: local': {'A': 'a'}, 'Operation: write Scope: local': {'A': 'a'}, 'Operation: read Scope: global': {'A': 'a'}, 'Operation: write Scope: global': {'A': 'a'}})
'Test to this tests for the file read and write operations'
def test_fileio(self):
with patch.dict(sysbench.__salt__, {'cmd.run': MagicMock(return_value={'A': 'a'})}): with patch.object(sysbench, '_parser', return_value={'A': 'a'}): self.assertEqual(sysbench.fileio(), {'Mode: seqrd': {'A': 'a'}, 'Mode: seqwr': {'A': 'a'}, 'Mode: rndrd': {'A': 'a'}, 'Mode: rndwr': {'A': 'a'}, 'Mode: seqrewr': {'A': 'a'}, 'Mode: rndrw': {'A': 'a'}})
'Test to ping'
def test_ping(self):
self.assertTrue(sysbench.ping())
'Test if it shows status of the DRBD devices'
def test_overview(self):
ret = {'connection state': 'True', 'device': 'Stack', 'fs': 'None', 'local disk state': 'UpToDate', 'local role': 'master', 'minor number': 'Salt', 'mountpoint': 'True', 'partner disk state': 'UpToDate', 'partner role': 'minion', 'percent': '888', 'remains': '666', 'total size': '50', 'used': '50'} mock = MagicMock(return_value='Salt:Stack True master/minion UpToDate/UpToDate True None 50 50 666 888') with patch.dict(drbd.__salt__, {'cmd.run': mock}): self.assertDictEqual(drbd.overview(), ret) ret = {'connection state': 'True', 'device': 'Stack', 'local disk state': 'UpToDate', 'local role': 'master', 'minor number': 'Salt', 'partner disk state': 'partner', 'partner role': 'minion', 'synched': '5050', 'synchronisation: ': 'syncbar'} mock = MagicMock(return_value='Salt:Stack True master/minion UpToDate/partner syncbar None 50 50') with patch.dict(drbd.__salt__, {'cmd.run': mock}): self.assertDictEqual(drbd.overview(), ret)
'Test if it get current system keyboard setting'
def test_get_sys(self):
mock = MagicMock(return_value='X11 Layout=us') with patch.dict(keyboard.__grains__, {'os_family': 'RedHat'}): with patch.dict(keyboard.__salt__, {'cmd.run': mock}): self.assertEqual(keyboard.get_sys(), 'us')
'Test if it set current system keyboard setting'
def test_set_sys(self):
mock = MagicMock(return_value='us') with patch.dict(keyboard.__grains__, {'os_family': 'RedHat'}): with patch.dict(keyboard.__salt__, {'cmd.run': mock}): with patch.dict(keyboard.__salt__, {'file.sed': MagicMock()}): self.assertEqual(keyboard.set_sys('us'), 'us')
'Test if it get current X keyboard setting'
def test_get_x(self):
mock = MagicMock(return_value='layout: us') with patch.dict(keyboard.__salt__, {'cmd.run': mock}): self.assertEqual(keyboard.get_x(), 'us')
'Test if it set current X keyboard setting'
def test_set_x(self):
mock = MagicMock(return_value='us') with patch.dict(keyboard.__salt__, {'cmd.run': mock}): self.assertEqual(keyboard.set_x('us'), 'us')
'If cache contains already a directory, do not raise an exception.'
def test_cache_skips_makedirs_on_race_condition(self):
with patch('os.path.isfile', (lambda prm: False)): for exists in range(2): with patch('os.makedirs', self._fake_makedir()): with Client(self.opts)._cache_loc('testfile') as c_ref_itr: assert (c_ref_itr == '/__test__/files/base/testfile')
'If makedirs raises other than EEXIST errno, an exception should be raised.'
def test_cache_raises_exception_on_non_eexist_ioerror(self):
with patch('os.path.isfile', (lambda prm: False)): with patch('os.makedirs', self._fake_makedir(num=errno.EROFS)): with self.assertRaises(OSError): with Client(self.opts)._cache_loc('testfile') as c_ref_itr: assert (c_ref_itr == '/__test__/files/base/testfile')
'test _get_gpg_exec'
def test__get_gpg_exec(self):
gpg_exec = '/bin/gpg' with patch('salt.utils.path.which', MagicMock(return_value=gpg_exec)): self.assertEqual(gpg._get_gpg_exec(), gpg_exec) with patch('salt.utils.path.which', MagicMock(return_value=False)): self.assertRaises(SaltRenderError, gpg._get_gpg_exec)
'test _decrypt_ciphertext'
def test__decrypt_ciphertext(self):
key_dir = '/etc/salt/gpgkeys' secret = 'Use more salt.' crypted = '!@#$%^&*()_+' class GPGDecrypt(object, ): def communicate(self, *args, **kwargs): return [secret, None] class GPGNotDecrypt(object, ): def communicate(self, *args, **kwargs): return [None, 'decrypt error'] with patch('salt.renderers.gpg._get_key_dir', MagicMock(return_value=key_dir)): with patch('salt.utils.path.which', MagicMock()): with patch('salt.renderers.gpg.Popen', MagicMock(return_value=GPGDecrypt())): self.assertEqual(gpg._decrypt_ciphertext(crypted), secret) with patch('salt.renderers.gpg.Popen', MagicMock(return_value=GPGNotDecrypt())): self.assertEqual(gpg._decrypt_ciphertext(crypted), crypted)
'test _decrypt_object'
def test__decrypt_object(self):
secret = 'Use more salt.' crypted = '-----BEGIN PGP MESSAGE-----!@#$%^&*()_+' secret_map = {'secret': secret} crypted_map = {'secret': crypted} secret_list = [secret] crypted_list = [crypted] with patch('salt.renderers.gpg._decrypt_ciphertext', MagicMock(return_value=secret)): self.assertEqual(gpg._decrypt_object(secret), secret) self.assertEqual(gpg._decrypt_object(crypted), secret) self.assertEqual(gpg._decrypt_object(crypted_map), secret_map) self.assertEqual(gpg._decrypt_object(crypted_list), secret_list) self.assertEqual(gpg._decrypt_object(None), None)
'test render'
def test_render(self):
key_dir = '/etc/salt/gpgkeys' secret = 'Use more salt.' crypted = '-----BEGIN PGP MESSAGE-----!@#$%^&*()_+' with patch('salt.renderers.gpg._get_gpg_exec', MagicMock(return_value=True)): with patch('salt.renderers.gpg._get_key_dir', MagicMock(return_value=key_dir)): with patch('salt.renderers.gpg._decrypt_object', MagicMock(return_value=secret)): self.assertEqual(gpg.render(crypted), secret)
'We don\'t want to check in another .git dir into GH because that just gets messy. Instead, we\'ll create a temporary repo on the fly for the tests to examine.'
def setUp(self):
if (not gitfs.__virtual__()): self.skipTest('GitFS could not be loaded. Skipping GitFS tests!') self.integration_base_files = os.path.join(FILES, 'file', 'base') try: shutil.copytree(self.integration_base_files, (self.tmp_repo_dir + '/')) except OSError: pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) if ('USERNAME' not in os.environ): try: os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name except AttributeError: log.error("Unable to get effective username, falling back to 'root'.") os.environ['USERNAME'] = 'root' repo.index.add([x for x in os.listdir(self.tmp_repo_dir) if (x != '.git')]) repo.index.commit('Test') gitfs.update()
'Remove the temporary git repository and gitfs cache directory to ensure a clean environment for each test.'
def tearDown(self):
shutil.rmtree(self.tmp_repo_dir) shutil.rmtree(self.tmp_cachedir) shutil.rmtree(self.tmp_sock_dir) del self.tmp_repo_dir del self.tmp_cachedir del self.tmp_sock_dir del self.integration_base_files
'Test that different maps are indeed reported different'
def test_diff_with_diffent_keys(self):
map1 = {'file1': 1234} map2 = {'file2': 1234} assert (fileserver.diff_mtime_map(map1, map2) is True)