Skip to content

Commit a2c7f9f

Browse files
[PR #11235/fb2f34ba backport][stable-12] Stop re-defining the argument spec in unit tests (#11239)
Stop re-defining the argument spec in unit tests (#11235) * Stop re-defining the argument spec in unit tests. * Shut up linter. (cherry picked from commit fb2f34b) Co-authored-by: Felix Fontein <[email protected]>
1 parent 3033dfa commit a2c7f9f

File tree

6 files changed

+35
-274
lines changed

6 files changed

+35
-274
lines changed

plugins/modules/archive.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def get_archive(module):
589589
return TarArchive(module)
590590

591591

592-
def main():
592+
def create_module() -> AnsibleModule:
593593
module = AnsibleModule(
594594
argument_spec=dict(
595595
path=dict(type="list", elements="path", required=True),
@@ -603,6 +603,11 @@ def main():
603603
add_file_common_args=True,
604604
supports_check_mode=True,
605605
)
606+
return module
607+
608+
609+
def main():
610+
module = create_module()
606611

607612
check_mode = module.check_mode
608613

plugins/modules/java_keystore.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def create_file(content):
510510
return tmpfile
511511

512512

513-
def main():
513+
def create_module() -> AnsibleModule:
514514
choose_between = (["certificate", "certificate_path"], ["private_key", "private_key_path"])
515515

516516
module = AnsibleModule(
@@ -533,6 +533,11 @@ def main():
533533
add_file_common_args=True,
534534
)
535535
module.run_command_environ_update = dict(LANG="C", LC_ALL="C", LC_MESSAGES="C")
536+
return module
537+
538+
539+
def main():
540+
module = create_module()
536541

537542
result = dict()
538543
jks = JavaKeystore(module)

plugins/modules/nmcli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,8 +2675,7 @@ def is_connection_changed(self):
26752675
return self._compare_conn_params(self.show_connection(), options)
26762676

26772677

2678-
def main():
2679-
# Parsing argument file
2678+
def create_module() -> AnsibleModule:
26802679
module = AnsibleModule(
26812680
argument_spec=dict(
26822681
ignore_unsupported_suboptions=dict(type="bool", default=False),
@@ -2870,6 +2869,12 @@ def main():
28702869
supports_check_mode=True,
28712870
)
28722871
module.run_command_environ_update = dict(LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C")
2872+
return module
2873+
2874+
2875+
def main():
2876+
# Parsing argument file
2877+
module = create_module()
28732878

28742879
nmcli = Nmcli(module)
28752880

tests/unit/plugins/modules/test_archive.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import pytest
88
from unittest.mock import Mock, patch
99

10-
from ansible.module_utils.basic import AnsibleModule
1110
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
1211
ModuleTestCase,
1312
set_module_args,
1413
)
15-
from ansible_collections.community.general.plugins.modules.archive import get_archive, common_path
14+
from ansible_collections.community.general.plugins.modules.archive import get_archive, common_path, create_module
1615

1716

1817
class TestArchive(ModuleTestCase):
@@ -27,19 +26,7 @@ def tearDown(self):
2726

2827
def test_archive_removal_safety(self):
2928
with set_module_args(dict(path=["/foo", "/bar", "/baz"], dest="/foo/destination.tgz", remove=True)):
30-
module = AnsibleModule(
31-
argument_spec=dict(
32-
path=dict(type="list", elements="path", required=True),
33-
format=dict(type="str", default="gz", choices=["bz2", "gz", "tar", "xz", "zip"]),
34-
dest=dict(type="path"),
35-
exclude_path=dict(type="list", elements="path", default=[]),
36-
exclusion_patterns=dict(type="list", elements="path"),
37-
force_archive=dict(type="bool", default=False),
38-
remove=dict(type="bool", default=False),
39-
),
40-
add_file_common_args=True,
41-
supports_check_mode=True,
42-
)
29+
module = create_module()
4330

4431
self.os_path_isdir.side_effect = [True, False, False, True]
4532

tests/unit/plugins/modules/test_java_keystore.py

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,8 @@
1313
ModuleTestCase,
1414
set_module_args,
1515
)
16-
from ansible.module_utils.basic import AnsibleModule
17-
from ansible_collections.community.general.plugins.modules.java_keystore import JavaKeystore
18-
19-
20-
module_argument_spec = dict(
21-
name=dict(type="str", required=True),
22-
dest=dict(type="path", required=True),
23-
certificate=dict(type="str", no_log=True),
24-
certificate_path=dict(type="path"),
25-
private_key=dict(type="str", no_log=True),
26-
private_key_path=dict(type="path", no_log=False),
27-
private_key_passphrase=dict(type="str", no_log=True),
28-
password=dict(type="str", required=True, no_log=True),
29-
ssl_backend=dict(type="str", default="openssl", choices=["openssl", "cryptography"]),
30-
keystore_type=dict(type="str", choices=["jks", "pkcs12"]),
31-
force=dict(type="bool", default=False),
32-
)
33-
module_supports_check_mode = True
34-
module_choose_between = (["certificate", "certificate_path"], ["private_key", "private_key_path"])
16+
from ansible.module_utils.basic import AnsibleModule # noqa: F401 # pylint: disable=unused-import
17+
from ansible_collections.community.general.plugins.modules.java_keystore import JavaKeystore, create_module
3518

3619

3720
class TestCreateJavaKeystore(ModuleTestCase):
@@ -96,12 +79,7 @@ def test_create_jks_success(self):
9679
password="changeit",
9780
)
9881
):
99-
module = AnsibleModule(
100-
argument_spec=module_argument_spec,
101-
supports_check_mode=module_supports_check_mode,
102-
mutually_exclusive=module_choose_between,
103-
required_one_of=module_choose_between,
104-
)
82+
module = create_module()
10583

10684
with patch("os.remove", return_value=True):
10785
self.create_path.side_effect = ["/tmp/tmpgrzm2ah7"]
@@ -139,12 +117,7 @@ def test_create_jks_keypass_fail_export_pkcs12(self):
139117
password="changeit",
140118
)
141119
):
142-
module = AnsibleModule(
143-
argument_spec=module_argument_spec,
144-
supports_check_mode=module_supports_check_mode,
145-
mutually_exclusive=module_choose_between,
146-
required_one_of=module_choose_between,
147-
)
120+
module = create_module()
148121

149122
module.exit_json = Mock()
150123
module.fail_json = Mock()
@@ -189,12 +162,7 @@ def test_create_jks_fail_export_pkcs12(self):
189162
password="changeit",
190163
)
191164
):
192-
module = AnsibleModule(
193-
argument_spec=module_argument_spec,
194-
supports_check_mode=module_supports_check_mode,
195-
mutually_exclusive=module_choose_between,
196-
required_one_of=module_choose_between,
197-
)
165+
module = create_module()
198166

199167
module.exit_json = Mock()
200168
module.fail_json = Mock()
@@ -237,12 +205,7 @@ def test_create_jks_fail_import_key(self):
237205
password="changeit",
238206
)
239207
):
240-
module = AnsibleModule(
241-
argument_spec=module_argument_spec,
242-
supports_check_mode=module_supports_check_mode,
243-
mutually_exclusive=module_choose_between,
244-
required_one_of=module_choose_between,
245-
)
208+
module = create_module()
246209

247210
module.exit_json = Mock()
248211
module.fail_json = Mock()
@@ -315,12 +278,7 @@ def test_cert_unchanged_same_fingerprint(self):
315278
password="changeit",
316279
)
317280
):
318-
module = AnsibleModule(
319-
argument_spec=module_argument_spec,
320-
supports_check_mode=module_supports_check_mode,
321-
mutually_exclusive=module_choose_between,
322-
required_one_of=module_choose_between,
323-
)
281+
module = create_module()
324282

325283
with patch("os.remove", return_value=True):
326284
self.create_file.side_effect = ["/tmp/placeholder", ""]
@@ -341,12 +299,7 @@ def test_cert_changed_fingerprint_mismatch(self):
341299
password="changeit",
342300
)
343301
):
344-
module = AnsibleModule(
345-
argument_spec=module_argument_spec,
346-
supports_check_mode=module_supports_check_mode,
347-
mutually_exclusive=module_choose_between,
348-
required_one_of=module_choose_between,
349-
)
302+
module = create_module()
350303

351304
with patch("os.remove", return_value=True):
352305
self.create_file.side_effect = ["/tmp/placeholder", ""]
@@ -367,12 +320,7 @@ def test_cert_changed_alias_does_not_exist(self):
367320
password="changeit",
368321
)
369322
):
370-
module = AnsibleModule(
371-
argument_spec=module_argument_spec,
372-
supports_check_mode=module_supports_check_mode,
373-
mutually_exclusive=module_choose_between,
374-
required_one_of=module_choose_between,
375-
)
323+
module = create_module()
376324

377325
with patch("os.remove", return_value=True):
378326
self.create_file.side_effect = ["/tmp/placeholder", ""]
@@ -395,12 +343,7 @@ def test_cert_changed_password_mismatch(self):
395343
password="changeit",
396344
)
397345
):
398-
module = AnsibleModule(
399-
argument_spec=module_argument_spec,
400-
supports_check_mode=module_supports_check_mode,
401-
mutually_exclusive=module_choose_between,
402-
required_one_of=module_choose_between,
403-
)
346+
module = create_module()
404347

405348
with patch("os.remove", return_value=True):
406349
self.create_file.side_effect = ["/tmp/placeholder", ""]
@@ -423,12 +366,7 @@ def test_cert_changed_fail_read_cert(self):
423366
password="changeit",
424367
)
425368
):
426-
module = AnsibleModule(
427-
argument_spec=module_argument_spec,
428-
supports_check_mode=module_supports_check_mode,
429-
mutually_exclusive=module_choose_between,
430-
required_one_of=module_choose_between,
431-
)
369+
module = create_module()
432370

433371
module.exit_json = Mock()
434372
module.fail_json = Mock()
@@ -457,12 +395,7 @@ def test_cert_changed_fail_read_keystore(self):
457395
password="changeit",
458396
)
459397
):
460-
module = AnsibleModule(
461-
argument_spec=module_argument_spec,
462-
supports_check_mode=module_supports_check_mode,
463-
mutually_exclusive=module_choose_between,
464-
required_one_of=module_choose_between,
465-
)
398+
module = create_module()
466399

467400
module.exit_json = Mock()
468401
module.fail_json = Mock(return_value=True)

0 commit comments

Comments
 (0)