mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
virt_net: idempotency of create/stop actions (#53276)
Currently, if we try to stop or start a network two time in a row, the second call will fail. With this patch: - we don't recreate a network, if it exists - we only stop a network if it's active, and so we avoid an exception saying the network is not active * test: mock libvirt * add integration tests for virt_net * test: enable virt_net test on RedHat 7 and 8 * ci: use the unsupported alias * tests that require privileged mode are run in VM * virt_net/create raise unexpected libvirt exception * import mock from units.compat * virt_net: do not call create() on "active" network * virt_net func test: only clean up the libvirt packages * test: virt_net: don't use assert_called() * virt_net: add the destructive alias * move the test in virt_net dir * test/virt_net: clean up the network at the end
This commit is contained in:
committed by
Abhijeet Kasurde
parent
56418cc274
commit
fc3064471b
0
test/units/modules/cloud/misc/__init__.py
Normal file
0
test/units/modules/cloud/misc/__init__.py
Normal file
0
test/units/modules/cloud/misc/virt_net/__init__.py
Normal file
0
test/units/modules/cloud/misc/virt_net/__init__.py
Normal file
69
test/units/modules/cloud/misc/virt_net/conftest.py
Normal file
69
test/units/modules/cloud/misc/virt_net/conftest.py
Normal file
@@ -0,0 +1,69 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright: (c) 2019, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
import pytest
|
||||
|
||||
from ansible.modules.cloud.misc import virt_net
|
||||
|
||||
from units.compat import mock
|
||||
|
||||
|
||||
virt_net.libvirt = None
|
||||
virt_net.HAS_VIRT = True
|
||||
|
||||
|
||||
class DummyNetwork():
|
||||
def __init__(self, name, isActive=True):
|
||||
self._name = name
|
||||
self._isActive = isActive
|
||||
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
def isActive(self):
|
||||
return self._isActive
|
||||
|
||||
|
||||
class DummyLibvirtConn():
|
||||
def __init__(self):
|
||||
self._network = [
|
||||
DummyNetwork("inactive_net", isActive=False),
|
||||
DummyNetwork("active_net", isActive=True)]
|
||||
|
||||
def listNetworks(self):
|
||||
return [i.name() for i in self._network]
|
||||
|
||||
def networkLookupByName(self, name):
|
||||
for i in self._network:
|
||||
if i.name() == name:
|
||||
return i
|
||||
|
||||
def listDefinedNetworks(self):
|
||||
return []
|
||||
|
||||
|
||||
class DummyLibvirt():
|
||||
VIR_ERR_NETWORK_EXIST = 'VIR_ERR_NETWORK_EXIST'
|
||||
|
||||
@classmethod
|
||||
def open(cls, uri):
|
||||
return DummyLibvirtConn()
|
||||
|
||||
class libvirtError(Exception):
|
||||
def __init__(self):
|
||||
self.error_code
|
||||
|
||||
def get_error_code(self):
|
||||
return self.error_code
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_libvirt(monkeypatch):
|
||||
monkeypatch.setattr(virt_net, 'libvirt', DummyLibvirt)
|
||||
return DummyLibvirt
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def virt_net_obj(dummy_libvirt):
|
||||
return virt_net.VirtNetwork('qemu:///nowhere', mock.MagicMock())
|
||||
30
test/units/modules/cloud/misc/virt_net/test_virt_net.py
Normal file
30
test/units/modules/cloud/misc/virt_net/test_virt_net.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright: (c) 2019, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from units.compat import mock
|
||||
|
||||
|
||||
def test_virt_net_create_already_active(virt_net_obj, dummy_libvirt):
|
||||
virt_net_obj.conn.create = mock.Mock()
|
||||
assert virt_net_obj.create("active_net") is None
|
||||
virt_net_obj.conn.create.assert_not_called()
|
||||
|
||||
|
||||
def test_virt_net_recreate(virt_net_obj, dummy_libvirt):
|
||||
virt_net_obj.conn.create = mock.Mock()
|
||||
dummy_libvirt.libvirtError.error_code = 'VIR_ERR_NETWORK_EXIST'
|
||||
virt_net_obj.conn.create.side_effect = dummy_libvirt.libvirtError
|
||||
assert virt_net_obj.create("active_net") is None
|
||||
|
||||
|
||||
def test_virt_stop_ignore_inactive(virt_net_obj):
|
||||
virt_net_obj.conn.destroy = mock.Mock()
|
||||
virt_net_obj.stop('inactive_net')
|
||||
virt_net_obj.conn.destroy.assert_not_called()
|
||||
|
||||
|
||||
def test_virt_stop_active(virt_net_obj, monkeypatch):
|
||||
virt_net_obj.conn.destroy = mock.Mock()
|
||||
virt_net_obj.stop('active_net')
|
||||
virt_net_obj.conn.destroy.assert_called_with('active_net')
|
||||
Reference in New Issue
Block a user