Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

team: Fix failure of creating team interface with slaves #800

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions libnmstate/appliers/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2020 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 libnmstate.schema import Team


def get_slaves_from_state(state, default=()):
ports = state.get(Team.CONFIG_SUBTREE, {}).get(Team.PORT_SUBTREE)
if ports is None:
return default
return [p[Team.Port.NAME] for p in ports]
8 changes: 8 additions & 0 deletions libnmstate/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from libnmstate.appliers import linux_bridge
from libnmstate.appliers import ovs_bridge
from libnmstate.appliers import bond
from libnmstate.appliers import team
from libnmstate.error import NmstateValueError
from libnmstate import nm
from libnmstate.schema import DNS
Expand Down Expand Up @@ -73,6 +74,13 @@ def generate_ifaces_metadata(desired_state, current_state):
get_slaves_func=linux_bridge.get_slaves_from_state,
set_metadata_func=linux_bridge.set_bridge_ports_metadata,
)
_generate_link_master_metadata(
desired_state.interfaces,
current_state.interfaces,
master_type=InterfaceType.TEAM,
get_slaves_func=team.get_slaves_from_state,
set_metadata_func=lambda *args: None,
)
_generate_dns_metadata(desired_state, current_state)
_generate_route_metadata(desired_state, current_state)
_generate_route_rule_metadata(desired_state)
Expand Down
2 changes: 2 additions & 0 deletions libnmstate/nm/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

import copy
cathay4t marked this conversation as resolved.
Show resolved Hide resolved
import json

from libnmstate.nm import connection as nm_connection
Expand Down Expand Up @@ -59,6 +60,7 @@ def create_setting(iface_state, base_con_profile):


def _convert_team_config_to_teamd_format(team_config, ifname):
team_config = copy.deepcopy(team_config)
team_config[TEAMD_JSON_DEVICE] = ifname

team_ports = team_config.get(Team.PORT_SUBTREE, ())
Expand Down
43 changes: 39 additions & 4 deletions tests/integration/team_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
#

from contextlib import contextmanager
import json
import os

import pytest

import libnmstate
from libnmstate.error import NmstateDependencyError
from libnmstate.error import NmstateLibnmError
from libnmstate.schema import Interface
from libnmstate.schema import InterfaceState
from libnmstate.schema import InterfaceType
from libnmstate.schema import Team

from .testlib import assertlib
from .testlib.cmdlib import exec_cmd
from .testlib.cmdlib import RC_SUCCESS
from .testlib.nmplugin import disable_nm_plugin


TEAM0 = "team0"
PORT1 = "eth1"
PORT2 = "eth2"


pytestmark = pytest.mark.skipif(
Expand All @@ -42,13 +48,27 @@
)


@pytest.mark.xfail(reason="https://bugzilla.redhat.com/1798947")
def test_create_team_iface():
@pytest.mark.xfail(
raises=NmstateLibnmError, reason="https://bugzilla.redhat.com/1798947"
)
def test_create_team_iface_without_slaves():
with team_interface(TEAM0) as team_state:
assertlib.assert_state(team_state)


@pytest.mark.xfail(reason="https://bugzilla.redhat.com/1798947")
@pytest.mark.xfail(
raises=NmstateLibnmError, reason="https://bugzilla.redhat.com/1798947"
)
def test_create_team_iface_with_slaves():
with team_interface(TEAM0, [PORT1, PORT2]) as team_state:
assertlib.assert_state(team_state)
assert [PORT1, PORT2] == _get_runtime_team_slaves(TEAM0)
ffmancera marked this conversation as resolved.
Show resolved Hide resolved
assertlib.assert_absent(TEAM0)


@pytest.mark.xfail(
raises=NmstateLibnmError, reason="https://bugzilla.redhat.com/1798947"
)
def test_edit_team_iface():
with team_interface(TEAM0) as team_state:
team_state[Interface.KEY][0][Team.CONFIG_SUBTREE] = {
Expand Down Expand Up @@ -78,7 +98,7 @@ def test_nm_team_plugin_missing():


@contextmanager
def team_interface(ifname):
def team_interface(ifname, slaves=None):
desired_state = {
Interface.KEY: [
{
Expand All @@ -88,6 +108,11 @@ def team_interface(ifname):
}
]
}
if slaves:
team_state = {Team.PORT_SUBTREE: []}
desired_state[Interface.KEY][0][Team.CONFIG_SUBTREE] = team_state
for slave in slaves:
team_state[Team.PORT_SUBTREE].append({Team.Port.NAME: slave})
libnmstate.apply(desired_state)
try:
yield desired_state
Expand All @@ -102,3 +127,13 @@ def team_interface(ifname):
]
}
)


def _get_runtime_team_slaves(team_iface_name):
"""
Use `teamdctl team0 state dump` to check team runtime status
"""
rc, output, _ = exec_cmd(f"teamdctl {team_iface_name} state dump".split())
assert rc == RC_SUCCESS
teamd_state = json.loads(output)
return sorted(teamd_state.get("ports", {}).keys())