Add support for vlan update on ovs bridges (#57168)

This commit adds support for vlan update on openvswitch_bridge module.
This commit is contained in:
Daniel Mellado Area 2019-06-05 16:32:15 +02:00 committed by Sam Doran
parent 72f2d05b6f
commit 091bebcbf7
3 changed files with 61 additions and 4 deletions

View file

@ -140,6 +140,12 @@ def map_obj_to_commands(want, have, module):
or want['external_ids'][k] != have['external_ids'][k]): or want['external_ids'][k] != have['external_ids'][k]):
command += " " + k + " " + v command += " " + k + " " + v
commands.append(command) commands.append(command)
if want['vlan'] and want['vlan'] != have['vlan']:
templatized_command = ("%(ovs-vsctl)s -t %(timeout)s"
" set port %(bridge)s tag=%(vlan)s")
command = templatized_command % module.params
commands.append(command)
else: else:
templatized_command = ("%(ovs-vsctl)s -t %(timeout)s add-br" templatized_command = ("%(ovs-vsctl)s -t %(timeout)s add-br"
" %(bridge)s") " %(bridge)s")
@ -169,6 +175,7 @@ def map_obj_to_commands(want, have, module):
command = templatized_command % module.params command = templatized_command % module.params
command += " " + k + " " + v command += " " + k + " " + v
commands.append(command) commands.append(command)
return commands return commands

View file

@ -11,7 +11,7 @@
- assert: - assert:
that: that:
- "result.changed == true" - result is changed
- name: Create bridge again (idempotent) - name: Create bridge again (idempotent)
openvswitch_bridge: openvswitch_bridge:
@ -20,7 +20,29 @@
- assert: - assert:
that: that:
- "result.changed == false" - result is not changed
- name: Tear down test bridge - name: Add fake bridge
openvswitch_bridge:
bridge: fake-br-test
parent: br-test
vlan: 100
register: result
- assert:
that:
- result is changed
- name: Change fake bridge vlan
openvswitch_bridge:
bridge: fake-br-test
parent: br-test
vlan: 300
register: result
- assert:
that:
- result is changed
- name: Tear down test bridges
command: ovs-vsctl del-br br-test command: ovs-vsctl del-br br-test

View file

@ -20,11 +20,24 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from units.compat.mock import patch
from ansible.modules.network.ovs import openvswitch_bridge from ansible.modules.network.ovs import openvswitch_bridge
from units.compat.mock import patch, MagicMock
from units.modules.utils import set_module_args from units.modules.utils import set_module_args
from .ovs_module import TestOpenVSwitchModule, load_fixture from .ovs_module import TestOpenVSwitchModule, load_fixture
import pytest
@pytest.fixture
def patched_openvswitch_bridge(monkeypatch):
mocked_bridge = MagicMock()
mocked_bridge.return_value = {'bridge': 'test-br2', 'parent': 'test-br',
'vlan': 200, 'fail_mode': None,
'external_ids': None, 'set': None}
monkeypatch.setattr(openvswitch_bridge, 'map_config_to_obj', mocked_bridge)
return openvswitch_bridge
test_name_side_effect_matrix = { test_name_side_effect_matrix = {
'test_openvswitch_bridge_absent_idempotent': [ 'test_openvswitch_bridge_absent_idempotent': [
(0, '', '')], (0, '', '')],
@ -51,6 +64,11 @@ test_name_side_effect_matrix = {
(0, '', ''), (0, '', ''),
(0, '', ''), (0, '', ''),
(0, '', '')], (0, '', '')],
'test_openvswitch_bridge_updates_vlan': [
(0, '', ''),
(0, '', ''),
(0, '', ''),
(0, '', '')],
'test_openvswitch_bridge_present_adds_external_id': [ 'test_openvswitch_bridge_present_adds_external_id': [
(0, 'list_br_test_br.cfg', ''), (0, 'list_br_test_br.cfg', ''),
(0, 'br_to_parent_test_br.cfg', ''), (0, 'br_to_parent_test_br.cfg', ''),
@ -155,6 +173,16 @@ class TestOpenVSwitchBridgeModule(TestOpenVSwitchModule):
self.execute_module(changed=True, commands=commands, self.execute_module(changed=True, commands=commands,
test_name='test_openvswitch_bridge_present_creates_fake_bridge') test_name='test_openvswitch_bridge_present_creates_fake_bridge')
@pytest.mark.usefixtures('patched_openvswitch_bridge')
def test_openvswitch_bridge_updates_vlan(self):
set_module_args({'state': 'present', 'bridge': 'test-br2', 'parent':
'test-br', 'vlan': 300})
commands = [
'/usr/bin/ovs-vsctl -t 5 set port test-br2 tag=300'
]
self.execute_module(changed=True, commands=commands,
test_name='test_openvswitch_bridge_updates_vlan')
def test_openvswitch_bridge_present_adds_external_id(self): def test_openvswitch_bridge_present_adds_external_id(self):
set_module_args(dict(state='present', set_module_args(dict(state='present',
bridge='test-br', bridge='test-br',