Skip to content

Commit

Permalink
Add support for vlan update on ovs bridges (#57168)
Browse files Browse the repository at this point in the history
This commit adds support for vlan update on openvswitch_bridge module.
  • Loading branch information
danielmellado authored and samdoran committed Jun 5, 2019
1 parent 72f2d05 commit 091bebc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/ansible/modules/network/ovs/openvswitch_bridge.py
Expand Up @@ -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")
Expand Down Expand Up @@ -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


Expand Down
28 changes: 25 additions & 3 deletions test/integration/targets/openvswitch_bridge/tests/basic.yaml
Expand Up @@ -11,7 +11,7 @@

- assert:
that:
- "result.changed == true"
- result is changed

- name: Create bridge again (idempotent)
openvswitch_bridge:
Expand All @@ -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
30 changes: 29 additions & 1 deletion test/units/modules/network/ovs/test_openvswitch_bridge.py
Expand Up @@ -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, '', '')],
Expand All @@ -51,6 +64,11 @@
(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', ''),
Expand Down Expand Up @@ -155,6 +173,16 @@ def test_openvswitch_bridge_present_creates_fake_bridge(self):
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',
Expand Down

0 comments on commit 091bebc

Please sign in to comment.