Added VLAN configuration. (#35343)

Fixes GH 22147

Signed-off-by: Amol Kahat <akahat@redhat.com>
This commit is contained in:
Amol Kahat 2018-01-26 21:51:59 +05:30 committed by Adam Miller
parent 1f1e5c11a9
commit e8633b7a22
2 changed files with 98 additions and 3 deletions

View file

@ -1049,12 +1049,60 @@ class Nmcli(object):
def create_connection_vlan(self): def create_connection_vlan(self):
cmd = [self.nmcli_bin] cmd = [self.nmcli_bin]
# format for creating ethernet interface cmd.append('con')
cmd.append('add')
cmd.append('type')
cmd.append('vlan')
cmd.append('con-name')
if self.conn_name is not None:
cmd.append(self.conn_name)
elif self.ifname is not None:
cmd.append(self.ifname)
else:
cmd.append('vlan%s' % self.vlanid)
cmd.append('ifname')
if self.ifname is not None:
cmd.append(self.ifname)
elif self.conn_name is not None:
cmd.append(self.conn_name)
else:
cmd.append('vlan%s' % self.vlanid)
params = {'dev': self.vlandev,
'id': self.vlanid,
'ip4': self.ip4,
'gw4': self.gw4,
'ip6': self.ip6,
'gw6': self.gw6,
'autoconnect': self.bool_to_string(self.autoconnect)
}
for k, v in params.items():
cmd.extend([k, v])
return cmd return cmd
def modify_connection_vlan(self): def modify_connection_vlan(self):
cmd = [self.nmcli_bin] cmd = [self.nmcli_bin]
# format for modifying ethernet interface cmd.append('con')
cmd.append('mod')
cmd.append('con-name')
params = {'vlan.parent': self.vlandev,
'vlan.id': self.vlanid,
'ipv4.address': self.ip4,
'ipv4.geteway': self.gw4,
'ipv4.dns': self.dns4,
'ipv6.address': self.ip6,
'ipv6.gateway': self.gw6,
'ipv6.dns': self.dns6,
'autoconnect': self.bool_to_string(self.autoconnect)
}
for k, v in params.items():
cmd.extend([k, v])
return cmd return cmd
def create_connection(self): def create_connection(self):

View file

@ -1,9 +1,10 @@
# Copyright: (c) 2017 Ansible Project # Copyright: (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
import pytest
import json import json
import pytest
from ansible.modules.net_tools import nmcli from ansible.modules.net_tools import nmcli
pytestmark = pytest.mark.usefixtures('patch_ansible_module') pytestmark = pytest.mark.usefixtures('patch_ansible_module')
@ -118,6 +119,18 @@ TESTCASE_BRIDGE_SLAVE = [
} }
] ]
TESTCASE_VLAN = [
{
'type': 'vlan',
'conn_name': 'non_existent_nw_device',
'ifname': 'vlan_not_exists',
'ip4': '10.10.10.10',
'gw4': '10.10.10.1',
'state': 'present',
'_ansible_check_mode': False,
}
]
def mocker_set(mocker, connection_exists=False): def mocker_set(mocker, connection_exists=False):
""" """
@ -360,3 +373,37 @@ def test_mod_bridge_slave(mocked_generic_connection_modify):
for param in ['bridge-port.path-cost', '100']: for param in ['bridge-port.path-cost', '100']:
assert param in args[0] assert param in args[0]
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module'])
def test_create_vlan_con(mocked_generic_connection_create):
"""
Test if Bridge_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]
for param in ['vlan']:
assert param in args[0]
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module'])
def test_mod_vlan_conn(mocked_generic_connection_modify):
"""
Test if Bridge_slave modified
"""
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]
for param in ['vlan.id']:
assert param in args[0]