Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_lxca_nodes.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import json
8 from unittest import mock
9
10 import pytest
11
12 from ansible_collections.community.general.plugins.modules import lxca_nodes
13
14
15 @pytest.fixture(scope="module")
16 @mock.patch(
17 "ansible_collections.community.general.plugins.module_utils._lxca_common.close_conn",
18 autospec=True,
19 )
20 def setup_module(close_conn):
21 close_conn.return_value = True
22
23
24 class TestMyModule:
25 @pytest.mark.parametrize(
26 "patch_ansible_module",
27 [
28 {},
29 {
30 "auth_url": "https://10.240.14.195",
31 "login_user": "USERID",
32 },
33 {
34 "auth_url": "https://10.240.14.195",
35 "login_password": "Password",
36 },
37 {
38 "login_user": "USERID",
39 "login_password": "Password",
40 },
41 ],
42 indirect=["patch_ansible_module"],
43 )
44 @pytest.mark.usefixtures("patch_ansible_module")
45 @mock.patch(
46 "ansible_collections.community.general.plugins.module_utils._lxca_common.setup_conn",
47 autospec=True,
48 )
49 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_nodes.execute_module", autospec=True)
50 def test_without_required_parameters(self, _setup_conn, _execute_module, mocker, capfd, setup_module):
51 """Failure must occurs when all parameters are missing"""
52 with pytest.raises(SystemExit):
53 _setup_conn.return_value = "Fake connection"
54 _execute_module.return_value = "Fake execution"
55 lxca_nodes.main()
56 out, err = capfd.readouterr()
57 results = json.loads(out)
58 assert results["failed"]
59 assert "missing required arguments" in results["msg"]
60
61 @mock.patch(
62 "ansible_collections.community.general.plugins.module_utils._lxca_common.setup_conn",
63 autospec=True,
64 )
65 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_nodes.execute_module", autospec=True)
66 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_nodes.AnsibleModule", autospec=True)
67 def test__argument_spec(self, ansible_mod_cls, _execute_module, _setup_conn, setup_module):
68 expected_arguments_spec = dict(
69 login_user=dict(required=True),
70 login_password=dict(required=True, no_log=True),
71 command_options=dict(
72 default="nodes",
73 choices=[
74 "nodes",
75 "nodes_by_uuid",
76 "nodes_by_chassis_uuid",
77 "nodes_status_managed",
78 "nodes_status_unmanaged",
79 ],
80 ),
81 auth_url=dict(required=True),
82 uuid=dict(),
83 chassis=dict(),
84 )
85 _setup_conn.return_value = "Fake connection"
86 _execute_module.return_value = []
87 mod_obj = ansible_mod_cls.return_value
88 args = {
89 "auth_url": "https://10.243.30.195",
90 "login_user": "USERID",
91 "login_password": "password",
92 "command_options": "nodes",
93 }
94 mod_obj.params = args
95 lxca_nodes.main()
96 assert mock.call(argument_spec=expected_arguments_spec, supports_check_mode=False) == ansible_mod_cls.call_args
97
98 @mock.patch(
99 "ansible_collections.community.general.plugins.module_utils._lxca_common.setup_conn",
100 autospec=True,
101 )
102 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_nodes._nodes_by_uuid", autospec=True)
103 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_nodes.AnsibleModule", autospec=True)
104 def test__nodes_empty_list(self, ansible_mod_cls, _get_nodes, _setup_conn, setup_module):
105 mod_obj = ansible_mod_cls.return_value
106 args = {
107 "auth_url": "https://10.243.30.195",
108 "login_user": "USERID",
109 "login_password": "password",
110 "uuid": "3C737AA5E31640CE949B10C129A8B01F",
111 "command_options": "nodes_by_uuid",
112 }
113 mod_obj.params = args
114 _setup_conn.return_value = "Fake connection"
115 empty_nodes_list = []
116 _get_nodes.return_value = empty_nodes_list
117 ret_nodes = _get_nodes(mod_obj, args)
118 assert mock.call(mod_obj, mod_obj.params) == _get_nodes.call_args
119 assert _get_nodes.return_value == ret_nodes