Skip to content

Commit

Permalink
dhcp: add EnumType compat for py311, linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
etene committed Jan 29, 2025
1 parent 68925d8 commit 9fa4d33
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
5 changes: 3 additions & 2 deletions pyroute2/compat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'''Compatibility with older but supported Python versions'''

try:
from enum import StrEnum
from enum import EnumType, StrEnum # noqa: F401
except ImportError:
# StrEnum appeared in python 3.11

# EnumMeta was renamed to EnumType
from enum import Enum
from enum import EnumMeta as EnumType # noqa: F401

class StrEnum(str, Enum):
'''Same as enum, but members are also strings.'''
Expand Down
4 changes: 3 additions & 1 deletion pyroute2/dhcp/enums/dhcp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from enum import EnumType, IntEnum
from enum import IntEnum

from pyroute2.compat import EnumType


class MessageType(IntEnum):
Expand Down
14 changes: 9 additions & 5 deletions tests/test_linux/fixtures/dhcp_servers/mock.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio

import pytest
from fixtures.pcap_files import PcapFile

from pyroute2.dhcp.dhcp4socket import AsyncDHCP4Socket
from pyroute2.dhcp.messages import SentDHCPMessage
from fixtures.pcap_files import PcapFile


class MockDHCPServerFixture:
Expand All @@ -15,6 +16,7 @@ class MockDHCPServerFixture:
The requests made by the client will be stored in `decoded_requests`.
'''

def __init__(self, responses: list[bytes]):
self.responses: list[bytes] = responses
self.requests: list[bytes] = []
Expand All @@ -34,14 +36,16 @@ async def sock_recv(self, sock, size: int) -> bytes:


@pytest.fixture
def mock_dhcp_server(pcap: PcapFile,
monkeypatch: pytest.MonkeyPatch,
) -> MockDHCPServerFixture:
def mock_dhcp_server(
pcap: PcapFile, monkeypatch: pytest.MonkeyPatch
) -> MockDHCPServerFixture:
'''Monkey patches the client to respond to requests with pcap data.
The `pcap` fixture is used which means the pcap file must be named
after the test.
'''
responder = MockDHCPServerFixture(responses=pcap)
monkeypatch.setattr('pyroute2.dhcp.dhcp4socket.AsyncDHCP4Socket.loop', responder)
monkeypatch.setattr(
'pyroute2.dhcp.dhcp4socket.AsyncDHCP4Socket.loop', responder
)
return responder
18 changes: 9 additions & 9 deletions tests/test_linux/test_dhcp/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@

import pytest
from pyroute2.dhcp.client import AsyncDHCPClient, ClientConfig
from fixtures.dhcp_servers.mock import MockDHCPServerFixture
from fixtures.interfaces import VethPair

from pyroute2.dhcp.client import AsyncDHCPClient, ClientConfig
from pyroute2.dhcp.enums import bootp, dhcp
from pyroute2.dhcp.fsm import State
from fixtures.dhcp_servers.mock import MockDHCPServerFixture




@pytest.mark.asyncio
async def test_get_and_renew_lease(mock_dhcp_server: MockDHCPServerFixture,
veth_pair: VethPair,
):
async def test_get_and_renew_lease(
mock_dhcp_server: MockDHCPServerFixture, veth_pair: VethPair
):
'''A lease is obtained with a 1s renewing time, the client renews it.
The test pcap file contains the OFFER & the 2 ACKs.
Expand Down Expand Up @@ -45,7 +43,9 @@ async def test_get_and_renew_lease(mock_dhcp_server: MockDHCPServerFixture,
# all bootp ip addr fields are left blank
assert all([discover.dhcp[f'{x}iaddr'] == '0.0.0.0' for x in 'cysg'])
# the requested parameters match those in the client config
assert discover.dhcp['options']['parameter_list'] == list(cfg.requested_parameters)
assert discover.dhcp['options']['parameter_list'] == list(
cfg.requested_parameters
)

# The pcap contains an offer in response to the discover.
# The client sends a request for that offer:
Expand Down

0 comments on commit 9fa4d33

Please sign in to comment.