def test_handler_invalid_data(self): network_id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' ip_address = '192.168.x.x' lease_remaining = 120 json_rep = jsonutils.dumps( dict(network_id=network_id, lease_remaining=lease_remaining, ip_address=ip_address)) handler = mock.Mock() mock_sock = mock.Mock() mock_sock.recv.return_value = json_rep relay = dhcp_agent.DhcpLeaseRelay(handler) with mock.patch.object(relay, '_validate_field') as validate: validate.side_effect = ValueError with mock.patch.object(dhcp_agent.LOG, 'warn') as log: relay._handler(mock_sock, mock.Mock()) mock_sock.assert_has_calls( [mock.call.recv(1024), mock.call.close()]) self.assertFalse(handler.called) self.assertTrue(log.called)
def test_handler_invalid_data(self): network_id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' ip_address = '192.168.x.x' lease_remaining = 120 json_rep = jsonutils.dumps( dict(network_id=network_id, lease_remaining=lease_remaining, ip_address=ip_address)) handler = mock.Mock() mock_sock = mock.Mock() mock_sock.recv.return_value = json_rep relay = dhcp_agent.DhcpLeaseRelay(handler) with mock.patch('quantum.openstack.common.' 'uuidutils.is_uuid_like') as validate: validate.return_value = False with mock.patch.object(dhcp_agent.LOG, 'warn') as log: relay._handler(mock_sock, mock.Mock()) mock_sock.assert_has_calls( [mock.call.recv(1024), mock.call.close()]) self.assertFalse(handler.called) self.assertTrue(log.called)
def test_init_relay_socket_path_prev_socket_exists(self): with mock.patch('os.path.exists') as exists: exists.return_value = False relay = dhcp_agent.DhcpLeaseRelay(None) self.unlink.assert_called_once_with( cfg.CONF.dhcp_lease_relay_socket) self.assertFalse(exists.called)
def test_init_relay_socket_path_no_prev_socket(self): with mock.patch('os.path.exists') as exists: exists.return_value = False self.unlink.side_effect = OSError relay = dhcp_agent.DhcpLeaseRelay(None) self.unlink.assert_called_once_with( cfg.CONF.dhcp_lease_relay_socket) exists.assert_called_once_with(cfg.CONF.dhcp_lease_relay_socket)
def test_init_relay_socket_path_prev_socket_unlink_failure(self): self.unlink.side_effect = OSError with mock.patch('os.path.exists') as exists: exists.return_value = True with self.assertRaises(OSError): relay = dhcp_agent.DhcpLeaseRelay(None) self.unlink.assert_called_once_with( cfg.CONF.dhcp_lease_relay_socket) exists.assert_called_once_with( cfg.CONF.dhcp_lease_relay_socket)
def test_start(self): with mock.patch.object(dhcp_agent, 'eventlet') as mock_eventlet: handler = mock.Mock() relay = dhcp_agent.DhcpLeaseRelay(handler) relay.start() mock_eventlet.assert_has_calls([ mock.call.listen(cfg.CONF.dhcp_lease_relay_socket, family=socket.AF_UNIX), mock.call.spawn(mock_eventlet.serve, mock.call.listen.return_value, relay._handler) ])
def test_handler_valid_data(self): network_id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' ip_address = '192.168.1.9' lease_remaining = 120 json_rep = jsonutils.dumps( dict(network_id=network_id, lease_remaining=lease_remaining, ip_address=ip_address)) handler = mock.Mock() mock_sock = mock.Mock() mock_sock.recv.return_value = json_rep relay = dhcp_agent.DhcpLeaseRelay(handler) relay._handler(mock_sock, mock.Mock()) mock_sock.assert_has_calls([mock.call.recv(1024), mock.call.close()]) handler.called_once_with(network_id, ip_address, lease_remaining)
def test_handler_other_exception(self): network_id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' ip_address = '192.168.x.x' lease_remaining = 120 json_rep = jsonutils.dumps( dict(network_id=network_id, lease_remaining=lease_remaining, ip_address=ip_address)) handler = mock.Mock() mock_sock = mock.Mock() mock_sock.recv.side_effect = Exception relay = dhcp_agent.DhcpLeaseRelay(handler) with mock.patch.object(dhcp_agent.LOG, 'exception') as log: relay._handler(mock_sock, mock.Mock()) mock_sock.assert_has_calls([mock.call.recv(1024)]) self.assertFalse(handler.called) self.assertTrue(log.called)
def test_validate_field_invalid(self): relay = dhcp_agent.DhcpLeaseRelay(None) with self.assertRaises(ValueError): retval = relay._validate_field('zz', '\d[a-f]')
def test_validate_field_valid(self): relay = dhcp_agent.DhcpLeaseRelay(None) retval = relay._validate_field('1b', '\d[a-f]') self.assertEqual(retval, '1b')