1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[nmcli] add connection.slave-type for teamed devices (#2827)

* [nmcli] add connection.slave-type for teamed devices

* [nmcli] add fragment with changes for #2827

* [nmcli] add tests for network team

* [nmcli] fix testing

Co-authored-by: Oriol MULA VALLS <oriol.mula@lxp.lu>
This commit is contained in:
omula 2021-06-28 20:46:44 +02:00 committed by GitHub
parent debb15efbe
commit 2d1527a564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- nmcli - fixes team-slave configuration by adding connection.slave-type (https://github.com/ansible-collections/community.general/issues/766).

View file

@ -833,6 +833,10 @@ class Nmcli(object):
'bridge-port.hairpin-mode': self.hairpin, 'bridge-port.hairpin-mode': self.hairpin,
'bridge-port.priority': self.slavepriority, 'bridge-port.priority': self.slavepriority,
}) })
elif self.type == 'team-slave':
options.update({
'connection.slave-type': 'team',
})
elif self.tunnel_conn_type: elif self.tunnel_conn_type:
options.update({ options.update({
'ip-tunnel.local': self.ip_tunnel_local, 'ip-tunnel.local': self.ip_tunnel_local,

View file

@ -257,6 +257,50 @@ bridge-port.hairpin-mode: yes
bridge-port.priority: 32 bridge-port.priority: 32
""" """
TESTCASE_TEAM = [
{
'type': 'team',
'conn_name': 'non_existent_nw_device',
'ifname': 'team0_non_existant',
'state': 'present',
'_ansible_check_mode': False,
}
]
TESTCASE_TEAM_SHOW_OUTPUT = """\
connection.id: non_existent_nw_device
connection.interface-name: team0_non_existant
connection.autoconnect: yes
connection.type: team
ipv4.ignore-auto-dns: no
ipv4.ignore-auto-routes: no
ipv4.never-default: no
ipv4.may-fail: yes
ipv6.method: auto
ipv6.ignore-auto-dns: no
ipv6.ignore-auto-routes: no
"""
TESTCASE_TEAM_SLAVE = [
{
'type': 'team-slave',
'conn_name': 'non_existent_nw_slaved_device',
'ifname': 'generic_slaved_non_existant',
'master': 'team0_non_existant',
'state': 'present',
'_ansible_check_mode': False,
}
]
TESTCASE_TEAM_SLAVE_SHOW_OUTPUT = """\
connection.id: non_existent_nw_slaved_device
connection.interface-name: generic_slaved_non_existant
connection.autoconnect: yes
connection.master: team0_non_existant
connection.slave-type: team
802-3-ethernet.mtu: auto
"""
TESTCASE_VLAN = [ TESTCASE_VLAN = [
{ {
'type': 'vlan', 'type': 'vlan',
@ -495,6 +539,20 @@ def mocked_bridge_slave_unchanged(mocker):
execute_return=(0, TESTCASE_BRIDGE_SLAVE_SHOW_OUTPUT, "")) execute_return=(0, TESTCASE_BRIDGE_SLAVE_SHOW_OUTPUT, ""))
@pytest.fixture
def mocked_team_connection_unchanged(mocker):
mocker_set(mocker,
connection_exists=True,
execute_return=(0, TESTCASE_TEAM_SHOW_OUTPUT, ""))
@pytest.fixture
def mocked_team_slave_connection_unchanged(mocker):
mocker_set(mocker,
connection_exists=True,
execute_return=(0, TESTCASE_TEAM_SLAVE_SHOW_OUTPUT, ""))
@pytest.fixture @pytest.fixture
def mocked_vlan_connection_unchanged(mocker): def mocked_vlan_connection_unchanged(mocker):
mocker_set(mocker, mocker_set(mocker,
@ -952,6 +1010,93 @@ def test_bridge_slave_unchanged(mocked_bridge_slave_unchanged, capfd):
assert not results['changed'] assert not results['changed']
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_TEAM, indirect=['patch_ansible_module'])
def test_team_connection_create(mocked_generic_connection_create, capfd):
"""
Test : Team connection created
"""
with pytest.raises(SystemExit):
nmcli.main()
assert nmcli.Nmcli.execute_command.call_count == 1
arg_list = nmcli.Nmcli.execute_command.call_args_list
args, kwargs = arg_list[0]
assert args[0][0] == '/usr/bin/nmcli'
assert args[0][1] == 'con'
assert args[0][2] == 'add'
assert args[0][3] == 'type'
assert args[0][4] == 'team'
assert args[0][5] == 'con-name'
assert args[0][6] == 'non_existent_nw_device'
for param in ['connection.autoconnect', 'connection.interface-name', 'team0_non_existant']:
assert param in args[0]
out, err = capfd.readouterr()
results = json.loads(out)
assert not results.get('failed')
assert results['changed']
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_TEAM, indirect=['patch_ansible_module'])
def test_team_connection_unchanged(mocked_team_connection_unchanged, capfd):
"""
Test : Team connection unchanged
"""
with pytest.raises(SystemExit):
nmcli.main()
out, err = capfd.readouterr()
results = json.loads(out)
assert not results.get('failed')
assert not results['changed']
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_TEAM_SLAVE, indirect=['patch_ansible_module'])
def test_create_team_slave(mocked_generic_connection_create, capfd):
"""
Test if Team_slave created
"""
with pytest.raises(SystemExit):
nmcli.main()
assert nmcli.Nmcli.execute_command.call_count == 1
arg_list = nmcli.Nmcli.execute_command.call_args_list
args, kwargs = arg_list[0]
assert args[0][0] == '/usr/bin/nmcli'
assert args[0][1] == 'con'
assert args[0][2] == 'add'
assert args[0][3] == 'type'
assert args[0][4] == 'team-slave'
assert args[0][5] == 'con-name'
assert args[0][6] == 'non_existent_nw_slaved_device'
for param in ['connection.autoconnect', 'connection.interface-name', 'connection.master', 'team0_non_existant', 'connection.slave-type']:
assert param in args[0]
out, err = capfd.readouterr()
results = json.loads(out)
assert not results.get('failed')
assert results['changed']
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_TEAM_SLAVE, indirect=['patch_ansible_module'])
def test_team_slave_connection_unchanged(mocked_team_slave_connection_unchanged, capfd):
"""
Test : Team slave connection unchanged
"""
with pytest.raises(SystemExit):
nmcli.main()
out, err = capfd.readouterr()
results = json.loads(out)
assert not results.get('failed')
assert not results['changed']
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module']) @pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module'])
def test_create_vlan_con(mocked_generic_connection_create, capfd): def test_create_vlan_con(mocked_generic_connection_create, capfd):
""" """