mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-27 10:40:22 -07:00
Refactor openvswitch unit tests (#24055)
Rather than passing a file to load fixture, build a matrix containing the run_command side_effect per test. This will allow more code-reuse for other ovs modules unit tests.
This commit is contained in:
parent
1d0a629dcc
commit
7c1b8da1a1
2 changed files with 36 additions and 12 deletions
|
@ -65,9 +65,9 @@ class AnsibleFailJson(Exception):
|
||||||
class TestOpenVSwitchModule(unittest.TestCase):
|
class TestOpenVSwitchModule(unittest.TestCase):
|
||||||
|
|
||||||
def execute_module(self, failed=False, changed=False,
|
def execute_module(self, failed=False, changed=False,
|
||||||
command=None, fixture_name=None):
|
command=None, test_name=None):
|
||||||
|
|
||||||
self.load_fixtures(fixture_name)
|
self.load_fixtures(test_name)
|
||||||
|
|
||||||
if failed:
|
if failed:
|
||||||
result = self.failed()
|
result = self.failed()
|
||||||
|
@ -108,5 +108,5 @@ class TestOpenVSwitchModule(unittest.TestCase):
|
||||||
self.assertEqual(result['changed'], changed, result)
|
self.assertEqual(result['changed'], changed, result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def load_fixtures(self, fixture_name):
|
def load_fixtures(self, test_name):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -26,6 +26,24 @@ from ansible.compat.tests.mock import patch
|
||||||
from ansible.modules.network.ovs import openvswitch_db
|
from ansible.modules.network.ovs import openvswitch_db
|
||||||
from .ovs_module import TestOpenVSwitchModule, load_fixture, set_module_args
|
from .ovs_module import TestOpenVSwitchModule, load_fixture, set_module_args
|
||||||
|
|
||||||
|
test_name_side_effect_matrix = {
|
||||||
|
'test_openvswitch_db_absent_idempotent': [
|
||||||
|
(0, 'openvswitch_db_disable_in_band_missing.cfg', None),
|
||||||
|
(0, None, None)],
|
||||||
|
'test_openvswitch_db_absent_removes_key': [
|
||||||
|
(0, 'openvswitch_db_disable_in_band_true.cfg', None),
|
||||||
|
(0, None, None)],
|
||||||
|
'test_openvswitch_db_present_idempotent': [
|
||||||
|
(0, 'openvswitch_db_disable_in_band_true.cfg', None),
|
||||||
|
(0, None, None)],
|
||||||
|
'test_openvswitch_db_present_adds_key': [
|
||||||
|
(0, 'openvswitch_db_disable_in_band_missing.cfg', None),
|
||||||
|
(0, None, None)],
|
||||||
|
'test_openvswitch_db_present_updates_key': [
|
||||||
|
(0, 'openvswitch_db_disable_in_band_true.cfg', None),
|
||||||
|
(0, None, None)],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
|
|
||||||
|
@ -43,10 +61,16 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
self.mock_run_command.stop()
|
self.mock_run_command.stop()
|
||||||
self.mock_get_bin_path.stop()
|
self.mock_get_bin_path.stop()
|
||||||
|
|
||||||
def load_fixtures(self, fixture_name):
|
def load_fixtures(self, test_name):
|
||||||
self.run_command.side_effect = [
|
test_side_effects = []
|
||||||
(0, load_fixture(fixture_name + '.cfg'), None),
|
for s in test_name_side_effect_matrix[test_name]:
|
||||||
(0, None, None)]
|
rc = s[0]
|
||||||
|
out = load_fixture(s[1]) if s[1] else None
|
||||||
|
err = s[2]
|
||||||
|
side_effect_with_fixture_loaded = (rc, out, err)
|
||||||
|
test_side_effects.append(side_effect_with_fixture_loaded)
|
||||||
|
self.run_command.side_effect = test_side_effects
|
||||||
|
|
||||||
self.get_bin_path.return_value = '/usr/bin/ovs-vsctl'
|
self.get_bin_path.return_value = '/usr/bin/ovs-vsctl'
|
||||||
|
|
||||||
def test_openvswitch_db_absent_idempotent(self):
|
def test_openvswitch_db_absent_idempotent(self):
|
||||||
|
@ -54,7 +78,7 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
table='Bridge', record='test-br',
|
table='Bridge', record='test-br',
|
||||||
col='other_config', key='disable-in-band',
|
col='other_config', key='disable-in-band',
|
||||||
value='True'))
|
value='True'))
|
||||||
self.execute_module(fixture_name='openvswitch_db_disable_in_band_missing')
|
self.execute_module(test_name='test_openvswitch_db_absent_idempotent')
|
||||||
|
|
||||||
def test_openvswitch_db_absent_removes_key(self):
|
def test_openvswitch_db_absent_removes_key(self):
|
||||||
set_module_args(dict(state='absent',
|
set_module_args(dict(state='absent',
|
||||||
|
@ -65,14 +89,14 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
changed=True,
|
changed=True,
|
||||||
command='/usr/bin/ovs-vsctl -t 5 remove Bridge test-br other_config'
|
command='/usr/bin/ovs-vsctl -t 5 remove Bridge test-br other_config'
|
||||||
' disable-in-band=True',
|
' disable-in-band=True',
|
||||||
fixture_name='openvswitch_db_disable_in_band_true')
|
test_name='test_openvswitch_db_absent_removes_key')
|
||||||
|
|
||||||
def test_openvswitch_db_present_idempotent(self):
|
def test_openvswitch_db_present_idempotent(self):
|
||||||
set_module_args(dict(state='present',
|
set_module_args(dict(state='present',
|
||||||
table='Bridge', record='test-br',
|
table='Bridge', record='test-br',
|
||||||
col='other_config', key='disable-in-band',
|
col='other_config', key='disable-in-band',
|
||||||
value='True'))
|
value='True'))
|
||||||
self.execute_module(fixture_name='openvswitch_db_disable_in_band_true')
|
self.execute_module(test_name='test_openvswitch_db_present_idempotent')
|
||||||
|
|
||||||
def test_openvswitch_db_present_adds_key(self):
|
def test_openvswitch_db_present_adds_key(self):
|
||||||
set_module_args(dict(state='present',
|
set_module_args(dict(state='present',
|
||||||
|
@ -83,7 +107,7 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
changed=True,
|
changed=True,
|
||||||
command='/usr/bin/ovs-vsctl -t 5 add Bridge test-br other_config'
|
command='/usr/bin/ovs-vsctl -t 5 add Bridge test-br other_config'
|
||||||
' disable-in-band=True',
|
' disable-in-band=True',
|
||||||
fixture_name='openvswitch_db_disable_in_band_missing')
|
test_name='test_openvswitch_db_present_adds_key')
|
||||||
|
|
||||||
def test_openvswitch_db_present_updates_key(self):
|
def test_openvswitch_db_present_updates_key(self):
|
||||||
set_module_args(dict(state='present',
|
set_module_args(dict(state='present',
|
||||||
|
@ -94,4 +118,4 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule):
|
||||||
changed=True,
|
changed=True,
|
||||||
command='/usr/bin/ovs-vsctl -t 5 set Bridge test-br other_config'
|
command='/usr/bin/ovs-vsctl -t 5 set Bridge test-br other_config'
|
||||||
':disable-in-band=False',
|
':disable-in-band=False',
|
||||||
fixture_name='openvswitch_db_disable_in_band_true')
|
test_name='test_openvswitch_db_present_updates_key')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue