1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import errno
from uuid import uuid4
import pytest
from pr2test.context_manager import NDBContextManager, SpecContextManager
from utils import require_user
from pyroute2 import config
from pyroute2.ipset import IPSet, IPSetError
from pyroute2.wiset import COUNT
config.nlm_generator = True
pytest_plugins = "pytester"
@pytest.fixture
def context(request, tmpdir):
'''
This fixture is used to prepare the environment and
to clean it up after each test.
https://docs.pytest.org/en/stable/fixture.html
'''
# test stage:
#
ctx = NDBContextManager(request, tmpdir) # setup
yield ctx # execute
ctx.teardown() # cleanup
@pytest.fixture
def spec(request, tmpdir):
'''
A simple fixture with only some variables set
'''
ctx = SpecContextManager(request, tmpdir)
yield ctx
ctx.teardown()
@pytest.fixture
def ipset():
require_user('root')
sock = IPSet()
yield sock
sock.close()
@pytest.fixture
def ipset_name(ipset):
name = str(uuid4())[:16]
yield name
try:
ipset.destroy(name)
except IPSetError as e:
if e.code != errno.ENOENT:
raise
@pytest.fixture(params=(None, IPSet))
def wiset_sock(request):
if request.param is None:
yield None
else:
before_count = COUNT["count"]
with IPSet() as sock:
yield sock
assert before_count == COUNT['count']
|