From 091bebcbf724d934a97812713507085e14ec3b18 Mon Sep 17 00:00:00 2001 From: Daniel Mellado Area Date: Wed, 5 Jun 2019 16:32:15 +0200 Subject: [PATCH] Add support for vlan update on ovs bridges (#57168) This commit adds support for vlan update on openvswitch_bridge module. --- .../modules/network/ovs/openvswitch_bridge.py | 7 +++++ .../openvswitch_bridge/tests/basic.yaml | 28 +++++++++++++++-- .../network/ovs/test_openvswitch_bridge.py | 30 ++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/network/ovs/openvswitch_bridge.py b/lib/ansible/modules/network/ovs/openvswitch_bridge.py index b66941c3701..8a2d7667d7c 100644 --- a/lib/ansible/modules/network/ovs/openvswitch_bridge.py +++ b/lib/ansible/modules/network/ovs/openvswitch_bridge.py @@ -140,6 +140,12 @@ def map_obj_to_commands(want, have, module): or want['external_ids'][k] != have['external_ids'][k]): command += " " + k + " " + v 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: templatized_command = ("%(ovs-vsctl)s -t %(timeout)s add-br" " %(bridge)s") @@ -169,6 +175,7 @@ def map_obj_to_commands(want, have, module): command = templatized_command % module.params command += " " + k + " " + v commands.append(command) + return commands diff --git a/test/integration/targets/openvswitch_bridge/tests/basic.yaml b/test/integration/targets/openvswitch_bridge/tests/basic.yaml index 7cdeeda1555..4d33f604f4d 100644 --- a/test/integration/targets/openvswitch_bridge/tests/basic.yaml +++ b/test/integration/targets/openvswitch_bridge/tests/basic.yaml @@ -11,7 +11,7 @@ - assert: that: - - "result.changed == true" + - result is changed - name: Create bridge again (idempotent) openvswitch_bridge: @@ -20,7 +20,29 @@ - assert: 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 diff --git a/test/units/modules/network/ovs/test_openvswitch_bridge.py b/test/units/modules/network/ovs/test_openvswitch_bridge.py index 2bd52b529d5..e1e1046c216 100644 --- a/test/units/modules/network/ovs/test_openvswitch_bridge.py +++ b/test/units/modules/network/ovs/test_openvswitch_bridge.py @@ -20,11 +20,24 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from units.compat.mock import patch from ansible.modules.network.ovs import openvswitch_bridge +from units.compat.mock import patch, MagicMock from units.modules.utils import set_module_args 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_openvswitch_bridge_absent_idempotent': [ (0, '', '')], @@ -51,6 +64,11 @@ test_name_side_effect_matrix = { (0, '', ''), (0, '', ''), (0, '', '')], + 'test_openvswitch_bridge_updates_vlan': [ + (0, '', ''), + (0, '', ''), + (0, '', ''), + (0, '', '')], 'test_openvswitch_bridge_present_adds_external_id': [ (0, 'list_br_test_br.cfg', ''), (0, 'br_to_parent_test_br.cfg', ''), @@ -155,6 +173,16 @@ class TestOpenVSwitchBridgeModule(TestOpenVSwitchModule): self.execute_module(changed=True, commands=commands, 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): set_module_args(dict(state='present', bridge='test-br',