def test_persist_no_conf_success(self): ''' Tests successful add of config file when previously not one ''' with patch('salt.utils.fopen', mock_open()) as m_open: 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')
def test_persist_success(self): ''' Tests successful write to existing sysctl file ''' to_write = '#\n# Kernel sysctl configuration\n#\n' m_calls_list = [call.writelines(['net.inet.icmp.icmplim=50', '\n'])] with patch('salt.utils.fopen', mock_open(read_data=to_write)) as m_open: 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)
def test_persist_no_conf_success(): """ Tests successful add of config file when it did not already exist """ config = "/etc/sysctl.conf" isfile_mock = MagicMock( side_effect=lambda x: False if x == config else DEFAULT) with patch("salt.utils.files.fopen", mock_open()) as m_open, patch("os.path.isfile", isfile_mock): mac_sysctl.persist("net.inet.icmp.icmplim", 50, config=config) num_handles = len(m_open.filehandles) assert num_handles == 1, num_handles writes = m_open.write_calls() num_writes = len(writes) assert num_writes == 1, num_writes assert writes[0] == "#\n# Kernel sysctl configuration\n#\n", writes[0]
def test_persist_no_conf_success(self): ''' Tests successful add of config file when it did not already exist ''' config = '/etc/sysctl.conf' isfile_mock = MagicMock( side_effect=lambda x: False if x == config else DEFAULT ) with patch('salt.utils.files.fopen', mock_open()) as m_open, \ patch('os.path.isfile', isfile_mock): mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=config) # We only should have opened the one file num_handles = len(m_open.filehandles) assert num_handles == 1, num_handles writes = m_open.write_calls() # We should have called .write() only once, with the expected # content num_writes = len(writes) assert num_writes == 1, num_writes assert writes[0] == '#\n# Kernel sysctl configuration\n#\n', writes[0]
def test_persist_success(): """ Tests successful write to existing sysctl file """ config = "/etc/sysctl.conf" to_write = "#\n# Kernel sysctl configuration\n#\n" writelines_calls = [[ "#\n", "# Kernel sysctl configuration\n", "#\n", "net.inet.icmp.icmplim=50\n", ]] isfile_mock = MagicMock( side_effect=lambda x: True if x == config else DEFAULT) with patch("salt.utils.files.fopen", mock_open(read_data=to_write)) as m_open, patch( "os.path.isfile", isfile_mock): mac_sysctl.persist("net.inet.icmp.icmplim", 50, config=config) num_handles = len(m_open.filehandles) assert num_handles == 1, num_handles writes = m_open.writelines_calls() assert writes == writelines_calls, writes
def test_persist_success(self): ''' Tests successful write to existing sysctl file ''' config = '/etc/sysctl.conf' to_write = '#\n# Kernel sysctl configuration\n#\n' writelines_calls = [[ '#\n', '# Kernel sysctl configuration\n', '#\n', 'net.inet.icmp.icmplim=50\n', ]] isfile_mock = MagicMock( side_effect=lambda x: True if x == config else DEFAULT) with patch('salt.utils.files.fopen', mock_open(read_data=to_write)) as m_open, \ patch('os.path.isfile', isfile_mock): mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=config) # We only should have opened the one file num_handles = len(m_open.filehandles) assert num_handles == 1, num_handles writes = m_open.writelines_calls() assert writes == writelines_calls, writes