Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_lxca_cmms.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_cmms
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_cmms.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_cmms.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_cmms.execute_module", autospec=True)
66 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_cmms.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(default="cmms", choices=["cmms", "cmms_by_uuid", "cmms_by_chassis_uuid"]),
72 auth_url=dict(required=True),
73 uuid=dict(),
74 chassis=dict(),
75 )
76 _setup_conn.return_value = "Fake connection"
77 _execute_module.return_value = []
78 mod_obj = ansible_mod_cls.return_value
79 args = {
80 "auth_url": "https://10.243.30.195",
81 "login_user": "USERID",
82 "login_password": "password",
83 "command_options": "cmms",
84 }
85 mod_obj.params = args
86 lxca_cmms.main()
87 assert mock.call(argument_spec=expected_arguments_spec, supports_check_mode=False) == ansible_mod_cls.call_args
88
89 @mock.patch(
90 "ansible_collections.community.general.plugins.module_utils._lxca_common.setup_conn",
91 autospec=True,
92 )
93 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_cmms._cmms_by_uuid", autospec=True)
94 @mock.patch("ansible_collections.community.general.plugins.modules.lxca_cmms.AnsibleModule", autospec=True)
95 def test__cmms_empty_list(self, ansible_mod_cls, _get_cmms, _setup_conn, setup_module):
96 mod_obj = ansible_mod_cls.return_value
97 args = {
98 "auth_url": "https://10.243.30.195",
99 "login_user": "USERID",
100 "login_password": "password",
101 "uuid": "3C737AA5E31640CE949B10C129A8B01F",
102 "command_options": "cmms_by_uuid",
103 }
104 mod_obj.params = args
105 _setup_conn.return_value = "Fake connection"
106 empty_nodes_list = []
107 _get_cmms.return_value = empty_nodes_list
108 ret_cmms = _get_cmms(mod_obj, args)
109 assert mock.call(mod_obj, mod_obj.params) == _get_cmms.call_args
110 assert _get_cmms.return_value == ret_cmms