mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
* Refactor Proxmox modules to use `module_utils`.
* Fix tests.
* Rename `node_check`.
* Add `ignore_missing` to `get_vm`.
* Refactor `proxmox` module.
* Add changelog entry.
* Add `choose_first_if_multiple` parameter for deprecation.
(cherry picked from commit a61bdbadd5
)
Co-authored-by: Markus Reiter <me@reitermark.us>
This commit is contained in:
parent
fab30c5e55
commit
a678029bd2
10 changed files with 849 additions and 1072 deletions
|
@ -1,3 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
# -*- 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 __future__ import (absolute_import, division, print_function)
|
||||
|
@ -6,8 +8,9 @@ __metaclass__ = type
|
|||
import json
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import MagicMock
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import MagicMock, patch
|
||||
from ansible_collections.community.general.plugins.modules.cloud.misc import proxmox_snap
|
||||
import ansible_collections.community.general.plugins.module_utils.proxmox as proxmox_utils
|
||||
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
|
||||
|
||||
|
||||
|
@ -32,8 +35,8 @@ def get_resources(type):
|
|||
"status": "running"}]
|
||||
|
||||
|
||||
def fake_api(api_host, api_user, api_password, validate_certs):
|
||||
r = MagicMock()
|
||||
def fake_api(mocker):
|
||||
r = mocker.MagicMock()
|
||||
r.cluster.resources.get = MagicMock(side_effect=get_resources)
|
||||
return r
|
||||
|
||||
|
@ -48,7 +51,8 @@ def test_proxmox_snap_without_argument(capfd):
|
|||
assert json.loads(out)['failed']
|
||||
|
||||
|
||||
def test_create_snapshot_check_mode(capfd, mocker):
|
||||
@patch('ansible_collections.community.general.plugins.module_utils.proxmox.ProxmoxAnsible._connect')
|
||||
def test_create_snapshot_check_mode(connect_mock, capfd, mocker):
|
||||
set_module_args({"hostname": "test-lxc",
|
||||
"api_user": "root@pam",
|
||||
"api_password": "secret",
|
||||
|
@ -58,8 +62,8 @@ def test_create_snapshot_check_mode(capfd, mocker):
|
|||
"timeout": "1",
|
||||
"force": True,
|
||||
"_ansible_check_mode": True})
|
||||
proxmox_snap.HAS_PROXMOXER = True
|
||||
proxmox_snap.setup_api = mocker.MagicMock(side_effect=fake_api)
|
||||
proxmox_utils.HAS_PROXMOXER = True
|
||||
connect_mock.side_effect = lambda: fake_api(mocker)
|
||||
with pytest.raises(SystemExit) as results:
|
||||
proxmox_snap.main()
|
||||
|
||||
|
@ -68,7 +72,8 @@ def test_create_snapshot_check_mode(capfd, mocker):
|
|||
assert not json.loads(out)['changed']
|
||||
|
||||
|
||||
def test_remove_snapshot_check_mode(capfd, mocker):
|
||||
@patch('ansible_collections.community.general.plugins.module_utils.proxmox.ProxmoxAnsible._connect')
|
||||
def test_remove_snapshot_check_mode(connect_mock, capfd, mocker):
|
||||
set_module_args({"hostname": "test-lxc",
|
||||
"api_user": "root@pam",
|
||||
"api_password": "secret",
|
||||
|
@ -78,8 +83,8 @@ def test_remove_snapshot_check_mode(capfd, mocker):
|
|||
"timeout": "1",
|
||||
"force": True,
|
||||
"_ansible_check_mode": True})
|
||||
proxmox_snap.HAS_PROXMOXER = True
|
||||
proxmox_snap.setup_api = mocker.MagicMock(side_effect=fake_api)
|
||||
proxmox_utils.HAS_PROXMOXER = True
|
||||
connect_mock.side_effect = lambda: fake_api(mocker)
|
||||
with pytest.raises(SystemExit) as results:
|
||||
proxmox_snap.main()
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright: (c) 2021, Andreas Botzner (@paginabianca) <andreas at botzner dot com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
#
|
||||
|
@ -130,6 +131,14 @@ def test_without_required_parameters(connect_mock, capfd, mocker):
|
|||
assert json.loads(out)['failed']
|
||||
|
||||
|
||||
def mock_api_tasks_response(mocker):
|
||||
m = mocker.MagicMock()
|
||||
g = mocker.MagicMock()
|
||||
m.nodes = mocker.MagicMock(return_value=g)
|
||||
g.tasks.get = mocker.MagicMock(return_value=TASKS)
|
||||
return m
|
||||
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.module_utils.proxmox.ProxmoxAnsible._connect')
|
||||
def test_get_tasks(connect_mock, capfd, mocker):
|
||||
set_module_args({'api_host': 'proxmoxhost',
|
||||
|
@ -137,14 +146,7 @@ def test_get_tasks(connect_mock, capfd, mocker):
|
|||
'api_password': 'supersecret',
|
||||
'node': NODE})
|
||||
|
||||
def f():
|
||||
m = mocker.MagicMock()
|
||||
g = mocker.MagicMock()
|
||||
m.nodes = mocker.MagicMock(return_value=g)
|
||||
g.tasks.get = mocker.MagicMock(return_value=TASKS)
|
||||
return m
|
||||
|
||||
connect_mock.side_effect = f
|
||||
connect_mock.side_effect = lambda: mock_api_tasks_response(mocker)
|
||||
proxmox_utils.HAS_PROXMOXER = True
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
|
@ -163,14 +165,7 @@ def test_get_single_task(connect_mock, capfd, mocker):
|
|||
'node': NODE,
|
||||
'task': TASK_UPID})
|
||||
|
||||
def f():
|
||||
m = mocker.MagicMock()
|
||||
g = mocker.MagicMock()
|
||||
m.nodes = mocker.MagicMock(return_value=g)
|
||||
g.tasks.get = mocker.MagicMock(return_value=TASKS)
|
||||
return m
|
||||
|
||||
connect_mock.side_effect = f
|
||||
connect_mock.side_effect = lambda: mock_api_tasks_response(mocker)
|
||||
proxmox_utils.HAS_PROXMOXER = True
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
|
@ -190,14 +185,7 @@ def test_get_non_existent_task(connect_mock, capfd, mocker):
|
|||
'node': NODE,
|
||||
'task': 'UPID:nonexistent'})
|
||||
|
||||
def f():
|
||||
m = mocker.MagicMock()
|
||||
g = mocker.MagicMock()
|
||||
m.nodes = mocker.MagicMock(return_value=g)
|
||||
g.tasks.get = mocker.MagicMock(return_value=TASKS)
|
||||
return m
|
||||
|
||||
connect_mock.side_effect = f
|
||||
connect_mock.side_effect = lambda: mock_api_tasks_response(mocker)
|
||||
proxmox_utils.HAS_PROXMOXER = True
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue