diff --git a/tests/test_core/conftest.py b/tests/test_core/conftest.py index 39f04877b..69ca5c2c6 100644 --- a/tests/test_core/conftest.py +++ b/tests/test_core/conftest.py @@ -1,5 +1,4 @@ import errno -import os import pytest import pytest_asyncio @@ -9,64 +8,6 @@ from pyroute2.common import uifname -class AsyncIPRouteContext(AsyncIPRoute): - def __init__(self, *argv, **kwarg): - self.remove_netns_on_exit = False - self.registry_ifname = set() - if kwarg.get('netns') is True: - kwarg['netns'] = uifname() - kwarg['flags'] = os.O_CREAT - self.remove_netns_on_exit = True - super().__init__(*argv, **kwarg) - - def register_temporary_ifname(self, ifname=None): - ifname = ifname if ifname is not None else uifname() - self.registry_ifname.add(ifname) - return ifname - - async def close(self, *argv, **kwarg): - for ifname in self.registry_ifname: - try: - await self.link('del', ifname=ifname) - except NetlinkError as e: - if e.code != errno.ENODEV: - raise - await super().close(*argv, **kwarg) - if self.remove_netns_on_exit: - netns.remove(self.status['netns']) - - -class SyncIPRouteContext(IPRoute): - def __init__(self, *argv, **kwarg): - self.remove_netns_on_exit = False - self.registry_ifname = set() - if kwarg.get('netns') is True: - kwarg['netns'] = uifname() - kwarg['flags'] = os.O_CREAT - self.remove_netns_on_exit = True - super().__init__(*argv, **kwarg) - - def register_temporary_ifname(self, ifname=None): - ifname = ifname if ifname is not None else uifname() - self.registry_ifname.add(ifname) - return ifname - - def register_temporary_netns(self, netns=None): - netns = netns if netns is not None else uifname() - self.registry_netns.add(netns) - - def close(self, *argv, **kwarg): - for ifname in self.registry_ifname: - try: - self.link('del', ifname=ifname) - except NetlinkError as e: - if e.code != errno.ENODEV: - raise - super().close(*argv, **kwarg) - if self.remove_netns_on_exit: - netns.remove(self.status['netns']) - - @pytest_asyncio.fixture async def p9(request, tmpdir): ctx = AsyncPlan9Context() @@ -75,15 +16,66 @@ async def p9(request, tmpdir): 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(request, tmpdir): +async def async_ipr(nsname, request): kwarg = getattr(request, 'param', {}) - async with AsyncIPRouteContext(**kwarg) as ctx: + async with AsyncIPRoute(netns=nsname, **kwarg) as ctx: yield ctx @pytest.fixture -def sync_ipr(request, tmpdir): +def sync_ipr(nsname, request): kwarg = getattr(request, 'param', {}) - with SyncIPRouteContext(**kwarg) as ctx: + with IPRoute(netns=nsname, **kwarg) as ctx: yield ctx diff --git a/tests/test_core/test_ipr/test_addr_async.py b/tests/test_core/test_ipr/test_addr_async.py index 1f5ef93f2..0c003a2c6 100644 --- a/tests/test_core/test_ipr/test_addr_async.py +++ b/tests/test_core/test_ipr/test_addr_async.py @@ -1,7 +1,7 @@ import re import pytest -from net_tools import address_exists, interface_exists +from net_tools import address_exists ip4v6 = re.compile('^[.:0-9a-f]*$') @@ -17,18 +17,9 @@ async def test_addr_dump(async_ipr): assert prefixlen > 0 -async def util_link_add(async_ipr): - ifname = async_ipr.register_temporary_ifname() - await async_ipr.link('add', ifname=ifname, kind='dummy', state='up') - assert interface_exists(ifname) - (link,) = await async_ipr.link('get', ifname=ifname) - return ifname, link.get('index') - - @pytest.mark.asyncio -async def test_addr_add(async_ipr): - ifname, index = await util_link_add(async_ipr) +async def test_addr_add(async_ipr, link, nsname): await async_ipr.addr( - 'add', index=index, address='192.168.145.150', prefixlen=24 + 'add', index=link.get('index'), address='192.168.145.150', prefixlen=24 ) - assert address_exists('192.168.145.150', ifname) + assert address_exists('192.168.145.150', link.get('ifname'), netns=nsname) diff --git a/tests/test_core/test_ipr/test_link_async.py b/tests/test_core/test_ipr/test_link_async.py index b1fd1e074..5c3222ff5 100644 --- a/tests/test_core/test_ipr/test_link_async.py +++ b/tests/test_core/test_ipr/test_link_async.py @@ -9,21 +9,15 @@ async def test_link_dump(async_ipr): assert 1 < len(link.get('ifname')) < 16 -async def util_link_add(async_ipr): - ifname = async_ipr.register_temporary_ifname() - await async_ipr.link('add', ifname=ifname, kind='dummy', state='up') - assert interface_exists(ifname) - return ifname - - @pytest.mark.asyncio -async def test_link_add(async_ipr): - await util_link_add(async_ipr) +async def test_link_add(async_ipr, tmp_link, nsname): + await async_ipr.link('add', ifname=tmp_link, kind='dummy', state='up') + assert interface_exists(tmp_link, netns=nsname) @pytest.mark.asyncio -async def test_link_get(async_ipr): - ifname = await util_link_add(async_ipr) +async def test_link_get(async_ipr, link): + ifname = link.get('ifname') (link,) = await async_ipr.link('get', ifname=ifname) assert link.get('state') == 'up' assert link.get('index') > 1 @@ -32,15 +26,15 @@ async def test_link_get(async_ipr): @pytest.mark.asyncio -async def test_link_del_by_index(async_ipr): - ifname = await util_link_add(async_ipr) +async def test_link_del_by_index(async_ipr, link, nsname): + ifname = link.get('ifname') (link,) = await async_ipr.link('get', ifname=ifname) await async_ipr.link('del', index=link['index']) - assert not interface_exists(ifname) + assert not interface_exists(ifname, netns=nsname) @pytest.mark.asyncio -async def test_link_del_by_name(async_ipr): - ifname = await util_link_add(async_ipr) +async def test_link_del_by_name(async_ipr, link, nsname): + ifname = link.get('ifname') await async_ipr.link('del', ifname=ifname) - assert not interface_exists(ifname) + assert not interface_exists(ifname, netns=nsname) diff --git a/tests/test_core/test_ipr/test_link_sync.py b/tests/test_core/test_ipr/test_link_sync.py index e53d9a280..6b2f46486 100644 --- a/tests/test_core/test_ipr/test_link_sync.py +++ b/tests/test_core/test_ipr/test_link_sync.py @@ -7,19 +7,13 @@ def test_link_dump(sync_ipr): assert 1 < len(link.get('ifname')) < 16 -def util_link_add(sync_ipr): - ifname = sync_ipr.register_temporary_ifname() - sync_ipr.link('add', ifname=ifname, kind='dummy', state='up') - assert interface_exists(ifname) - return ifname +def test_link_add(sync_ipr, tmp_link, nsname): + sync_ipr.link('add', ifname=tmp_link, kind='dummy', state='up') + assert interface_exists(tmp_link, netns=nsname) -def test_link_add(sync_ipr): - util_link_add(sync_ipr) - - -def test_link_get(sync_ipr): - ifname = util_link_add(sync_ipr) +def test_link_get(sync_ipr, link): + ifname = link.get('ifname') (link,) = sync_ipr.link('get', ifname=ifname) assert link.get('state') == 'up' assert link.get('index') > 1 @@ -27,14 +21,14 @@ def test_link_get(sync_ipr): assert link.get(('linkinfo', 'kind')) == 'dummy' -def test_link_del_by_index(sync_ipr): - ifname = util_link_add(sync_ipr) +def test_link_del_by_index(sync_ipr, link, nsname): + ifname = link.get('ifname') (link,) = sync_ipr.link('get', ifname=ifname) sync_ipr.link('del', index=link['index']) - assert not interface_exists(ifname) + assert not interface_exists(ifname, netns=nsname) -def test_link_del_by_name(sync_ipr): - ifname = util_link_add(sync_ipr) +def test_link_del_by_name(sync_ipr, link, nsname): + ifname = link.get('ifname') sync_ipr.link('del', ifname=ifname) - assert not interface_exists(ifname) + assert not interface_exists(ifname, netns=nsname) diff --git a/tests/test_core/test_ipr/test_route_dump_async.py b/tests/test_core/test_ipr/test_route_dump_async.py index fec54892c..a831c4b24 100644 --- a/tests/test_core/test_ipr/test_route_dump_async.py +++ b/tests/test_core/test_ipr/test_route_dump_async.py @@ -22,44 +22,38 @@ ''' -@pytest.mark.parametrize('async_ipr', [{'netns': True}], indirect=True) @pytest.mark.asyncio -async def test_load(async_ipr): - netns = async_ipr.status['netns'] - await async_ipr.link('set', index=1, state='up') - assert address_exists('127.0.0.1', ifname='lo', netns=netns) - assert not route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert not route_exists(dst='10.1.3.0/24', table=100, netns=netns) +async def test_load(async_ipr, nsname): + assert address_exists('127.0.0.1', ifname='lo', netns=nsname) + assert not route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert not route_exists(dst='10.1.3.0/24', table=100, netns=nsname) fd = io.BytesIO() fd.write(load_dump(test_dump_data)) fd.seek(0) await async_ipr.route_load(fd) - assert route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert route_exists(dst='10.1.3.0/24', table=100, netns=nsname) -@pytest.mark.parametrize('async_ipr', [{'netns': True}], indirect=True) @pytest.mark.asyncio -async def test_loads(async_ipr): - netns = async_ipr.status['netns'] - await async_ipr.link('set', index=1, state='up') - assert address_exists('127.0.0.1', ifname='lo', netns=netns) - assert not route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert not route_exists(dst='10.1.3.0/24', table=100, netns=netns) +async def test_loads(async_ipr, nsname): + assert address_exists('127.0.0.1', ifname='lo', netns=nsname) + assert not route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert not route_exists(dst='10.1.3.0/24', table=100, netns=nsname) await async_ipr.route_loads(load_dump(test_dump_data)) - assert route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert route_exists(dst='10.1.3.0/24', table=100, netns=nsname) @pytest.mark.parametrize( 'family,target_tables,target_families,fmt,offset', [ - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'iproute2', 4), - (AF_INET, {254, 255}, {AF_INET}, 'iproute2', 4), - (AF_INET6, {254, 255}, {AF_INET6}, 'iproute2', 4), - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'raw', 0), - (AF_INET, {254, 255}, {AF_INET}, 'raw', 0), - (AF_INET6, {254, 255}, {AF_INET6}, 'raw', 0), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'iproute2', 4), + (AF_INET, {255}, {AF_INET}, 'iproute2', 4), + (AF_INET6, {255}, {AF_INET6}, 'iproute2', 4), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'raw', 0), + (AF_INET, {255}, {AF_INET}, 'raw', 0), + (AF_INET6, {255}, {AF_INET6}, 'raw', 0), ], ids=( 'iproute2/AF_UNSPEC', @@ -81,19 +75,19 @@ async def test_dump( for route in async_ipr.marshal.parse(fd.getvalue()[offset:]): tables.add(route.get('table')) families.add(route.get('family')) - assert tables >= target_tables + assert tables == target_tables assert families == target_families @pytest.mark.parametrize( 'family,target_tables,target_families,fmt,offset', [ - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'iproute2', 4), - (AF_INET, {254, 255}, {AF_INET}, 'iproute2', 4), - (AF_INET6, {254, 255}, {AF_INET6}, 'iproute2', 4), - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'raw', 0), - (AF_INET, {254, 255}, {AF_INET}, 'raw', 0), - (AF_INET6, {254, 255}, {AF_INET6}, 'raw', 0), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'iproute2', 4), + (AF_INET, {255}, {AF_INET}, 'iproute2', 4), + (AF_INET6, {255}, {AF_INET6}, 'iproute2', 4), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'raw', 0), + (AF_INET, {255}, {AF_INET}, 'raw', 0), + (AF_INET6, {255}, {AF_INET6}, 'raw', 0), ], ids=( 'iproute2/AF_UNSPEC', @@ -114,5 +108,5 @@ async def test_dumps( for route in async_ipr.marshal.parse(data[offset:]): tables.add(route.get('table')) families.add(route.get('family')) - assert tables >= target_tables + assert tables == target_tables assert families == target_families diff --git a/tests/test_core/test_ipr/test_route_dump_sync.py b/tests/test_core/test_ipr/test_route_dump_sync.py index 89da3a030..13480eb8f 100644 --- a/tests/test_core/test_ipr/test_route_dump_sync.py +++ b/tests/test_core/test_ipr/test_route_dump_sync.py @@ -22,42 +22,38 @@ ''' -@pytest.mark.parametrize('sync_ipr', [{'netns': True}], indirect=True) -def test_load(sync_ipr): - netns = sync_ipr.status['netns'] +def test_load(sync_ipr, nsname): sync_ipr.link('set', index=1, state='up') - assert address_exists('127.0.0.1', ifname='lo', netns=netns) - assert not route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert not route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert address_exists('127.0.0.1', ifname='lo', netns=nsname) + assert not route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert not route_exists(dst='10.1.3.0/24', table=100, netns=nsname) fd = io.BytesIO() fd.write(load_dump(test_dump_data)) fd.seek(0) sync_ipr.route_load(fd) - assert route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert route_exists(dst='10.1.3.0/24', table=100, netns=nsname) -@pytest.mark.parametrize('sync_ipr', [{'netns': True}], indirect=True) -def test_loads(sync_ipr): - netns = sync_ipr.status['netns'] +def test_loads(sync_ipr, nsname): sync_ipr.link('set', index=1, state='up') - assert address_exists('127.0.0.1', ifname='lo', netns=netns) - assert not route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert not route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert address_exists('127.0.0.1', ifname='lo', netns=nsname) + assert not route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert not route_exists(dst='10.1.3.0/24', table=100, netns=nsname) sync_ipr.route_loads(load_dump(test_dump_data)) - assert route_exists(dst='10.1.2.0/24', table=100, netns=netns) - assert route_exists(dst='10.1.3.0/24', table=100, netns=netns) + assert route_exists(dst='10.1.2.0/24', table=100, netns=nsname) + assert route_exists(dst='10.1.3.0/24', table=100, netns=nsname) @pytest.mark.parametrize( 'family,target_tables,target_families,fmt,offset', [ - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'iproute2', 4), - (AF_INET, {254, 255}, {AF_INET}, 'iproute2', 4), - (AF_INET6, {254, 255}, {AF_INET6}, 'iproute2', 4), - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'raw', 0), - (AF_INET, {254, 255}, {AF_INET}, 'raw', 0), - (AF_INET6, {254, 255}, {AF_INET6}, 'raw', 0), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'iproute2', 4), + (AF_INET, {255}, {AF_INET}, 'iproute2', 4), + (AF_INET6, {255}, {AF_INET6}, 'iproute2', 4), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'raw', 0), + (AF_INET, {255}, {AF_INET}, 'raw', 0), + (AF_INET6, {255}, {AF_INET6}, 'raw', 0), ], ids=( 'iproute2/AF_UNSPEC', @@ -76,19 +72,19 @@ def test_dump(sync_ipr, family, target_tables, target_families, fmt, offset): for route in sync_ipr.marshal.parse(fd.getvalue()[offset:]): tables.add(route.get('table')) families.add(route.get('family')) - assert tables >= target_tables + assert tables == target_tables assert families == target_families @pytest.mark.parametrize( 'family,target_tables,target_families,fmt,offset', [ - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'iproute2', 4), - (AF_INET, {254, 255}, {AF_INET}, 'iproute2', 4), - (AF_INET6, {254, 255}, {AF_INET6}, 'iproute2', 4), - (AF_UNSPEC, {254, 255}, {AF_INET, AF_INET6}, 'raw', 0), - (AF_INET, {254, 255}, {AF_INET}, 'raw', 0), - (AF_INET6, {254, 255}, {AF_INET6}, 'raw', 0), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'iproute2', 4), + (AF_INET, {255}, {AF_INET}, 'iproute2', 4), + (AF_INET6, {255}, {AF_INET6}, 'iproute2', 4), + (AF_UNSPEC, {255}, {AF_INET, AF_INET6}, 'raw', 0), + (AF_INET, {255}, {AF_INET}, 'raw', 0), + (AF_INET6, {255}, {AF_INET6}, 'raw', 0), ], ids=( 'iproute2/AF_UNSPEC', @@ -106,5 +102,5 @@ def test_dumps(sync_ipr, family, target_tables, target_families, fmt, offset): for route in sync_ipr.marshal.parse(data[offset:]): tables.add(route.get('table')) families.add(route.get('family')) - assert tables >= target_tables + assert tables == target_tables assert families == target_families diff --git a/tests/test_core/test_ipr/test_rule_async.py b/tests/test_core/test_ipr/test_rule_async.py index fe1aa9c56..a5ea6a66f 100644 --- a/tests/test_core/test_ipr/test_rule_async.py +++ b/tests/test_core/test_ipr/test_rule_async.py @@ -15,15 +15,12 @@ ], ) @pytest.mark.parametrize( - 'async_ipr', - [{'netns': True, 'ext_ack': True, 'strict_check': True}], - indirect=True, + 'async_ipr', [{'ext_ack': True, 'strict_check': True}], indirect=True ) @pytest.mark.asyncio -async def test_rule_strict_src(async_ipr, priority, spec): - netns = async_ipr.status['netns'] +async def test_rule_strict_src(async_ipr, priority, spec, nsname): await async_ipr.rule('add', priority=priority, **spec) - assert rule_exists(priority=priority, netns=netns) + assert rule_exists(priority=priority, netns=nsname) @pytest.mark.parametrize( @@ -39,12 +36,10 @@ async def test_rule_strict_src(async_ipr, priority, spec): (20107, AF_INET6, {'table': 5192, 'dst': 'fd00::', 'dst_len': 8}), ], ) -@pytest.mark.parametrize('async_ipr', [{'netns': True}], indirect=True) @pytest.mark.asyncio -async def test_rule_add_del(async_ipr, priority, proto, spec): - netns = async_ipr.status['netns'] +async def test_rule_add_del(async_ipr, priority, proto, spec, nsname): await async_ipr.rule('add', priority=priority, **spec) - assert rule_exists(priority=priority, proto=proto, netns=netns) + assert rule_exists(priority=priority, proto=proto, netns=nsname) assert ( len( [ @@ -58,5 +53,5 @@ async def test_rule_add_del(async_ipr, priority, proto, spec): ) await async_ipr.rule('del', priority=priority, **spec) assert not rule_exists( - priority=priority, proto=proto, netns=netns, timeout=0.1 + priority=priority, proto=proto, netns=nsname, timeout=0.1 ) diff --git a/tests/test_core/test_ipr/test_rule_sync.py b/tests/test_core/test_ipr/test_rule_sync.py index 71e6f49e0..011dd7066 100644 --- a/tests/test_core/test_ipr/test_rule_sync.py +++ b/tests/test_core/test_ipr/test_rule_sync.py @@ -15,14 +15,11 @@ ], ) @pytest.mark.parametrize( - 'sync_ipr', - [{'netns': True, 'ext_ack': True, 'strict_check': True}], - indirect=True, + 'sync_ipr', [{'ext_ack': True, 'strict_check': True}], indirect=True ) -def test_rule_strict_src(sync_ipr, priority, spec): - netns = sync_ipr.status['netns'] +def test_rule_strict_src(sync_ipr, priority, spec, nsname): sync_ipr.rule('add', priority=priority, **spec) - assert rule_exists(priority=priority, netns=netns) + assert rule_exists(priority=priority, netns=nsname) @pytest.mark.parametrize( @@ -38,15 +35,13 @@ def test_rule_strict_src(sync_ipr, priority, spec): (20107, AF_INET6, {'table': 5192, 'dst': 'fd00::', 'dst_len': 8}), ], ) -@pytest.mark.parametrize('sync_ipr', [{'netns': True}], indirect=True) -def test_rule_add_del(sync_ipr, priority, proto, spec): - netns = sync_ipr.status['netns'] +def test_rule_add_del(sync_ipr, priority, proto, spec, nsname): sync_ipr.rule('add', priority=priority, **spec) - assert rule_exists(priority=priority, proto=proto, netns=netns) + assert rule_exists(priority=priority, proto=proto, netns=nsname) assert ( len([x for x in sync_ipr.rule('dump', priority=priority, **spec)]) == 1 ) sync_ipr.rule('del', priority=priority, **spec) assert not rule_exists( - priority=priority, proto=proto, netns=netns, timeout=0.1 + priority=priority, proto=proto, netns=nsname, timeout=0.1 ) diff --git a/tests/test_core/test_ipr/test_tc_async.py b/tests/test_core/test_ipr/test_tc_async.py index 0d209667d..b4e72d342 100644 --- a/tests/test_core/test_ipr/test_tc_async.py +++ b/tests/test_core/test_ipr/test_tc_async.py @@ -1,27 +1,12 @@ import pytest -from net_tools import ( - class_exists, - filter_exists, - interface_exists, - qdisc_exists, -) +from net_tools import class_exists, filter_exists, qdisc_exists from pyroute2 import protocols -async def util_link_add(async_ipr): - ifname = async_ipr.register_temporary_ifname() - await async_ipr.link('add', ifname=ifname, kind='dummy', state='up') - assert interface_exists(ifname) - (link,) = await async_ipr.link('get', ifname=ifname) - index = link['index'] - return ifname, index - - @pytest.mark.asyncio -async def test_tc_get_qdiscs(async_ipr): +async def test_tc_get_qdiscs(async_ipr, ifname, index, nsname): root_handle = '1:' - ifname, index = await util_link_add(async_ipr) await async_ipr.tc( 'add', 'tbf', @@ -31,19 +16,20 @@ async def test_tc_get_qdiscs(async_ipr): burst=256, latency=1, ) - assert qdisc_exists(ifname=ifname, handle=root_handle, rate=256) + assert qdisc_exists( + ifname=ifname, handle=root_handle, rate=256, netns=nsname + ) assert len([x async for x in await async_ipr.get_qdiscs(index=index)]) == 1 await async_ipr.tc('del', index=index, handle=root_handle, root=True) assert not qdisc_exists( - ifname=ifname, handle=root_handle, rate=256, timeout=0.1 + ifname=ifname, handle=root_handle, rate=256, timeout=0.1, netns=nsname ) @pytest.mark.asyncio -async def test_tc_htb(async_ipr): +async def test_tc_htb(async_ipr, ifname, index, nsname): root_handle = '1:' root_options_default = '0x200000' - ifname, index = await util_link_add(async_ipr) await async_ipr.tc( 'add', 'htb', @@ -52,7 +38,10 @@ async def test_tc_htb(async_ipr): default=int(root_options_default, 16), ) assert qdisc_exists( - ifname=ifname, handle=root_handle, default=root_options_default + ifname=ifname, + handle=root_handle, + default=root_options_default, + netns=nsname, ) await async_ipr.tc( @@ -64,7 +53,9 @@ async def test_tc_htb(async_ipr): rate='256kbit', burst=1024 * 6, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname + ) await async_ipr.tc( 'add-class', @@ -76,7 +67,9 @@ async def test_tc_htb(async_ipr): burst=1024 * 6, prio=1, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:10', parent='1:1') + assert class_exists( + ifname=ifname, kind='htb', handle='1:10', parent='1:1', netns=nsname + ) await async_ipr.tc( 'add-class', @@ -88,7 +81,9 @@ async def test_tc_htb(async_ipr): burst=1024 * 6, prio=2, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:20', parent='1:1') + assert class_exists( + ifname=ifname, kind='htb', handle='1:20', parent='1:1', netns=nsname + ) await async_ipr.tc( 'add-filter', @@ -108,6 +103,7 @@ async def test_tc_htb(async_ipr): protocol='ip', match_value="6000000", match_mask="ff000000", + netns=nsname, ) await async_ipr.tc( @@ -128,40 +124,79 @@ async def test_tc_htb(async_ipr): protocol='ip', match_value='10000000', match_mask='ff000000', + netns=nsname, ) # complementary delete commands await async_ipr.tc('del-filter', index=index, handle='0:0', parent='1:0') assert not filter_exists( - ifname=ifname, kind='u32', parent='1:0', timeout=0.1 + ifname=ifname, kind='u32', parent='1:0', timeout=0.1, netns=nsname ) await async_ipr.tc('del-class', index=index, handle='1:20', parent='1:1') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:10', parent='1:1', netns=nsname + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname ) - assert class_exists(ifname=ifname, kind='htb', handle='1:10', parent='1:1') - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) await async_ipr.tc('del-class', index=index, handle='1:10', parent='1:1') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:10', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:10', + parent='1:1', + timeout=0.1, + netns=nsname, + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname ) - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) await async_ipr.tc('del-class', index=index, handle='1:1', parent='1:0') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:10', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:10', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:1', root=True, timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:1', + root=True, + timeout=0.1, + netns=nsname, ) await async_ipr.tc('del', index=index, handle=root_handle, root=True) - assert not qdisc_exists(ifname=ifname, handle=root_handle, timeout=0.1) + assert not qdisc_exists( + ifname=ifname, handle=root_handle, timeout=0.1, netns=nsname + ) diff --git a/tests/test_core/test_ipr/test_tc_sync.py b/tests/test_core/test_ipr/test_tc_sync.py index db024786c..80e5b413b 100644 --- a/tests/test_core/test_ipr/test_tc_sync.py +++ b/tests/test_core/test_ipr/test_tc_sync.py @@ -1,25 +1,10 @@ -from net_tools import ( - class_exists, - filter_exists, - interface_exists, - qdisc_exists, -) +from net_tools import class_exists, filter_exists, qdisc_exists from pyroute2 import protocols -def util_link_add(sync_ipr): - ifname = sync_ipr.register_temporary_ifname() - sync_ipr.link('add', ifname=ifname, kind='dummy', state='up') - assert interface_exists(ifname) - (link,) = sync_ipr.link('get', ifname=ifname) - index = link['index'] - return ifname, index - - -def test_tc_get_qdiscs(sync_ipr): +def test_tc_get_qdiscs(sync_ipr, ifname, index, nsname): root_handle = '1:' - ifname, index = util_link_add(sync_ipr) sync_ipr.tc( 'add', 'tbf', @@ -29,18 +14,19 @@ def test_tc_get_qdiscs(sync_ipr): burst=256, latency=1, ) - assert qdisc_exists(ifname=ifname, handle=root_handle, rate=256) + assert qdisc_exists( + ifname=ifname, handle=root_handle, rate=256, netns=nsname + ) assert len([x for x in sync_ipr.get_qdiscs(index=index)]) == 1 sync_ipr.tc('del', index=index, handle=root_handle, root=True) assert not qdisc_exists( - ifname=ifname, handle=root_handle, rate=256, timeout=0.1 + ifname=ifname, handle=root_handle, rate=256, timeout=0.1, netns=nsname ) -def test_tc_htb(sync_ipr): +def test_tc_htb(sync_ipr, ifname, index, nsname): root_handle = '1:' root_options_default = '0x200000' - ifname, index = util_link_add(sync_ipr) sync_ipr.tc( 'add', 'htb', @@ -49,7 +35,10 @@ def test_tc_htb(sync_ipr): default=int(root_options_default, 16), ) assert qdisc_exists( - ifname=ifname, handle=root_handle, default=root_options_default + ifname=ifname, + handle=root_handle, + default=root_options_default, + netns=nsname, ) sync_ipr.tc( @@ -61,7 +50,9 @@ def test_tc_htb(sync_ipr): rate='256kbit', burst=1024 * 6, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname + ) sync_ipr.tc( 'add-class', @@ -73,7 +64,9 @@ def test_tc_htb(sync_ipr): burst=1024 * 6, prio=1, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:10', parent='1:1') + assert class_exists( + ifname=ifname, kind='htb', handle='1:10', parent='1:1', netns=nsname + ) sync_ipr.tc( 'add-class', @@ -85,7 +78,9 @@ def test_tc_htb(sync_ipr): burst=1024 * 6, prio=2, ) - assert class_exists(ifname=ifname, kind='htb', handle='1:20', parent='1:1') + assert class_exists( + ifname=ifname, kind='htb', handle='1:20', parent='1:1', netns=nsname + ) sync_ipr.tc( 'add-filter', @@ -105,6 +100,7 @@ def test_tc_htb(sync_ipr): protocol='ip', match_value="6000000", match_mask="ff000000", + netns=nsname, ) sync_ipr.tc( @@ -125,40 +121,79 @@ def test_tc_htb(sync_ipr): protocol='ip', match_value='10000000', match_mask='ff000000', + netns=nsname, ) # complementary delete commands sync_ipr.tc('del-filter', index=index, handle='0:0', parent='1:0') assert not filter_exists( - ifname=ifname, kind='u32', parent='1:0', timeout=0.1 + ifname=ifname, kind='u32', parent='1:0', timeout=0.1, netns=nsname ) sync_ipr.tc('del-class', index=index, handle='1:20', parent='1:1') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:10', parent='1:1', netns=nsname + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname ) - assert class_exists(ifname=ifname, kind='htb', handle='1:10', parent='1:1') - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) sync_ipr.tc('del-class', index=index, handle='1:10', parent='1:1') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:10', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:10', + parent='1:1', + timeout=0.1, + netns=nsname, + ) + assert class_exists( + ifname=ifname, kind='htb', handle='1:1', root=True, netns=nsname ) - assert class_exists(ifname=ifname, kind='htb', handle='1:1', root=True) sync_ipr.tc('del-class', index=index, handle='1:1', parent='1:0') assert not class_exists( - ifname=ifname, kind='htb', handle='1:20', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:20', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:10', parent='1:1', timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:10', + parent='1:1', + timeout=0.1, + netns=nsname, ) assert not class_exists( - ifname=ifname, kind='htb', handle='1:1', root=True, timeout=0.1 + ifname=ifname, + kind='htb', + handle='1:1', + root=True, + timeout=0.1, + netns=nsname, ) sync_ipr.tc('del', index=index, handle=root_handle, root=True) - assert not qdisc_exists(ifname=ifname, handle=root_handle, timeout=0.1) + assert not qdisc_exists( + ifname=ifname, handle=root_handle, timeout=0.1, netns=nsname + )