def test_write_to_ports_io_error(mock_module): instance = mock_module.return_value with mock.patch('__builtin__.open') as mock_open: mock_open.side_effect = IOError('permission denied') cl_ports.write_to_ports_conf(instance) _msg = 'Failed to write to /etc/cumulus/ports.conf: permission denied' instance.fail_json.assert_called_with(msg=_msg)
def test_write_to_ports_conf(mock_module): """ test writing to ports.conf file """ test_port_conf = '/tmp/ports.conf' if os.path.exists(test_port_conf): os.remove(test_port_conf) instance = mock_module.return_value old_ports_value = cl_ports.PORTS_CONF cl_ports.PORTS_CONF = test_port_conf instance.ports_conf_hash = {1: '40G', 10: '10G', 22: '4x10G', 2: '40/4'} cl_ports.write_to_ports_conf(instance) result = open(test_port_conf, 'r').readlines() assert_equals(result[0].strip(), '# Managed By Ansible') result.pop(0) assert_equals(map(lambda x: x.strip(), result), ['1=40G', '2=40/4', '10=10G', '22=4x10G']) # comment this out to troubleshoot ports.conf printout os.unlink(test_port_conf) cl_ports.PORTS_CONF = old_ports_value