tests, integ: Extract iface-up fixtures out of conftest

The existing eth{1,2}_up fixtures serve the integration tests with the
majority of tests. There are however serveral tests which use unique
interfaces (e.g. dhcp tests) which need similar fixtures.

In order to share the functionality, the fixtures implementation have
been extracted to a shared location (testlib) and are aimed to be used
in the following patches by other more focused scoped fixtures.

Signed-off-by: Edward Haas <edwardh@redhat.com>
This commit is contained in:
Edward Haas 2019-07-28 20:09:10 +03:00
parent 6d85b859d3
commit d866ed3b35
2 changed files with 62 additions and 30 deletions

View File

@ -23,8 +23,7 @@ import pytest
import libnmstate
from .testlib import statelib
from .testlib.statelib import INTERFACES
from .testlib import ifacelib
@pytest.fixture(scope='session', autouse=True)
@ -38,48 +37,25 @@ def logging_setup():
@pytest.fixture(scope='session', autouse=True)
def ethx_init(preserve_old_config):
""" Remove any existing definitions on the ethX interfaces. """
_set_eth_admin_state('eth1', 'down')
_set_eth_admin_state('eth2', 'down')
ifacelib.ifaces_init('eth1', 'eth2')
@pytest.fixture(scope='function')
def eth1_up():
_set_eth_admin_state('eth1', 'up')
try:
yield statelib.show_only(('eth1',))
finally:
_set_eth_admin_state('eth1', 'down')
with ifacelib.iface_up('eth1') as ifstate:
yield ifstate
@pytest.fixture(scope='function')
def eth2_up():
_set_eth_admin_state('eth2', 'up')
try:
yield statelib.show_only(('eth2',))
finally:
_set_eth_admin_state('eth2', 'down')
with ifacelib.iface_up('eth2') as ifstate:
yield ifstate
port0_up = eth1_up
port1_up = eth2_up
def _set_eth_admin_state(ifname, state):
current_state = statelib.show_only((ifname,))
iface_current_state, = current_state[INTERFACES]
if iface_current_state['state'] != state or state == 'down':
desired_state = {
INTERFACES: [
{
'name': iface_current_state['name'],
'type': iface_current_state['type'],
'state': state,
}
]
}
libnmstate.apply(desired_state)
@pytest.fixture(scope='session', autouse=True)
def preserve_old_config():
old_state = libnmstate.show()

View File

@ -0,0 +1,56 @@
#
# Copyright (c) 2019 Red Hat, Inc.
#
# This file is part of nmstate
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from contextlib import contextmanager
import libnmstate
from libnmstate import schema
from . import statelib
def ifaces_init(*ifnames):
""" Remove any existing definitions on the interfaces. """
for ifname in ifnames:
_set_eth_admin_state(ifname, schema.InterfaceState.DOWN)
@contextmanager
def iface_up(ifname):
_set_eth_admin_state(ifname, schema.InterfaceState.UP)
try:
yield statelib.show_only((ifname,))
finally:
_set_eth_admin_state(ifname, schema.InterfaceState.DOWN)
def _set_eth_admin_state(ifname, state):
current_state = statelib.show_only((ifname,))
current_ifstate, = current_state[schema.Interface.KEY]
iface_current_admin_state = current_ifstate[schema.Interface.STATE]
if (
iface_current_admin_state != state
or state == schema.InterfaceState.DOWN
):
desired_state = {
schema.Interface.KEY: [
{schema.Interface.NAME: ifname, schema.Interface.STATE: state}
]
}
libnmstate.apply(desired_state)