-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: move fixtures to the source tree as a module
- Loading branch information
Showing
13 changed files
with
387 additions
and
220 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import errno | ||
from collections import namedtuple | ||
from collections.abc import AsyncGenerator, Generator | ||
from typing import Union | ||
|
||
import pytest | ||
import pytest_asyncio | ||
|
||
from pyroute2 import netns | ||
from pyroute2.common import uifname | ||
from pyroute2.iproute.linux import AsyncIPRoute, IPRoute | ||
from pyroute2.netlink.exceptions import NetlinkError | ||
|
||
TestInterface = namedtuple('TestInterface', ('index', 'ifname')) | ||
|
||
|
||
@pytest.fixture | ||
def nsname() -> Generator[str]: | ||
'''Create a unique network namespace and yield its name. | ||
Remove the netns on exit. | ||
''' | ||
nsname = uifname() | ||
netns.create(nsname) | ||
with IPRoute(netns=nsname) as ipr: | ||
ipr.link('set', index=1, state='up') | ||
ipr.poll(ipr.addr, 'dump', address='127.0.0.1', timeout=5) | ||
yield nsname | ||
try: | ||
netns.remove(nsname) | ||
except Exception: | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def test_link(nsname: str) -> Generator[TestInterface]: | ||
ifname = uifname() | ||
with IPRoute(netns=nsname) as ipr: | ||
ipr.link('add', ifname=ifname, kind='dummy', state='up') | ||
(link,) = ipr.poll(ipr.link, 'dump', ifname=ifname, timeout=5) | ||
yield TestInterface(index=link.get('index'), ifname=link.get('ifname')) | ||
try: | ||
ipr.link('del', index=link.get('index')) | ||
except NetlinkError as e: | ||
if e.code != errno.ENODEV: | ||
raise | ||
|
||
|
||
@pytest.fixture | ||
def test_link_index(test_link: TestInterface) -> Generator[int]: | ||
yield test_link.index | ||
|
||
|
||
@pytest.fixture | ||
def test_link_ifname(test_link: TestInterface) -> Generator[str]: | ||
yield test_link.ifname | ||
|
||
|
||
@pytest.fixture | ||
def tmp_link_ifname(nsname: str) -> Generator[str]: | ||
ifname = uifname() | ||
with IPRoute(netns=nsname) as ipr: | ||
yield ifname | ||
try: | ||
(link,) = ipr.link('get', ifname=ifname) | ||
ipr.link('del', index=link.get('index')) | ||
except NetlinkError as e: | ||
if e.code != errno.ENODEV: | ||
raise | ||
|
||
|
||
class TestContext: | ||
def __init__( | ||
self, ipr: Union[IPRoute, AsyncIPRoute], test_link: TestInterface | ||
): | ||
self.ipr = ipr | ||
self.test_link = test_link | ||
self.netns = self.ipr.status['netns'] | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def async_ipr( | ||
request, nsname: str, test_link: TestInterface | ||
) -> AsyncGenerator[AsyncIPRoute]: | ||
kwarg = getattr(request, 'param', {}) | ||
async with AsyncIPRoute(netns=nsname, **kwarg) as ipr: | ||
yield ipr | ||
|
||
|
||
@pytest.fixture | ||
def sync_ipr( | ||
request, nsname: str, test_link: TestInterface | ||
) -> Generator[IPRoute]: | ||
kwarg = getattr(request, 'param', {}) | ||
with IPRoute(netns=nsname, **kwarg) as ipr: | ||
yield ipr | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def async_context( | ||
async_ipr: AsyncIPRoute, test_link: TestInterface | ||
) -> AsyncGenerator[TestContext]: | ||
yield TestContext(async_ipr, test_link) | ||
|
||
|
||
@pytest.fixture | ||
def sync_context( | ||
sync_ipr: IPRoute, test_link: TestInterface | ||
) -> Generator[TestContext]: | ||
yield TestContext(sync_ipr, test_link) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,25 @@ | ||
import errno | ||
|
||
import pytest | ||
import pytest_asyncio | ||
from pr2test.plan9 import AsyncPlan9Context | ||
|
||
from pyroute2 import AsyncIPRoute, IPRoute, NetlinkError, netns | ||
from pyroute2.common import uifname | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def p9(request, tmpdir): | ||
ctx = AsyncPlan9Context() | ||
await ctx.ensure_session() | ||
yield ctx | ||
await ctx.close() | ||
|
||
|
||
@pytest.fixture | ||
def nsname(): | ||
ns = uifname() | ||
netns.create(ns) | ||
with IPRoute(netns=ns) as ipr: | ||
ipr.link('set', index=1, state='up') | ||
ipr.poll(ipr.addr, 'dump', address='127.0.0.1', timeout=5) | ||
yield ns | ||
try: | ||
netns.delete(ns) | ||
except Exception: | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def link(nsname): | ||
ifname = uifname() | ||
with IPRoute(netns=nsname) as ipr: | ||
ipr.link('add', ifname=ifname, kind='dummy', state='up') | ||
(link,) = ipr.poll(ipr.link, 'dump', ifname=ifname, timeout=5) | ||
yield link | ||
try: | ||
ipr.link('del', index=link.get('index')) | ||
except NetlinkError as e: | ||
if e.code != errno.ENODEV: | ||
raise | ||
|
||
|
||
@pytest.fixture | ||
def index(link): | ||
yield link.get('index') | ||
|
||
|
||
@pytest.fixture | ||
def ifname(link): | ||
yield link.get('ifname') | ||
|
||
|
||
@pytest.fixture | ||
def tmp_link(nsname): | ||
ifname = uifname() | ||
with IPRoute(netns=nsname) as ipr: | ||
yield ifname | ||
try: | ||
(link,) = ipr.link('get', ifname=ifname) | ||
ipr.link('del', index=link.get('index')) | ||
except NetlinkError as e: | ||
if e.code != errno.ENODEV: | ||
raise | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def async_ipr(nsname, request): | ||
kwarg = getattr(request, 'param', {}) | ||
async with AsyncIPRoute(netns=nsname, **kwarg) as ctx: | ||
yield ctx | ||
|
||
|
||
@pytest.fixture | ||
def sync_ipr(nsname, request): | ||
kwarg = getattr(request, 'param', {}) | ||
with IPRoute(netns=nsname, **kwarg) as ctx: | ||
yield ctx | ||
from pyroute2.fixtures.iproute import ( | ||
async_context, | ||
async_ipr, | ||
nsname, | ||
sync_context, | ||
sync_ipr, | ||
test_link, | ||
test_link_ifname, | ||
test_link_index, | ||
tmp_link_ifname, | ||
) | ||
from pyroute2.fixtures.plan9 import async_p9_context | ||
|
||
fixtures = [ | ||
async_p9_context, | ||
async_context, | ||
async_ipr, | ||
nsname, | ||
sync_context, | ||
sync_ipr, | ||
test_link, | ||
test_link_index, | ||
test_link_ifname, | ||
tmp_link_ifname, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.