Skip to content

Commit b779fcb

Browse files
committed
UT: Common(): 100% coverage
1. test_bootflash_common.py - 100% coverage of Common() in dcnm_bootflash.py - Remove unused commented imports.
1 parent 3dc8a40 commit b779fcb

File tree

3 files changed

+227
-30
lines changed

3 files changed

+227
-30
lines changed

tests/unit/modules/dcnm/dcnm_bootflash/fixtures/configs_Query.json

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Mocked playbook configurations for query-state tests.",
44
"tests/unit/modules/dcnm/dcnm_bootflash"
55
],
6-
"test_dcnm_bootflash_00100a": {
6+
"test_bootflash_common_00200a": {
77
"targets": [
88
{
99
"filepath": "bootflash:/foo.txt",
@@ -13,9 +13,87 @@
1313
"switches": [
1414
{
1515
"ip_address": "192.168.1.2"
16-
},
16+
}
17+
]
18+
},
19+
"test_bootflash_common_00210a": {
20+
"TEST_NOTES": [
21+
"Negative test.",
22+
"switches[0].ip_address is misspelled."
23+
],
24+
"targets": [
1725
{
18-
"ip_address": "192.168.1.3"
26+
"filepath": "bootflash:/foo.txt",
27+
"supervisor": "active"
28+
}
29+
],
30+
"switches": [
31+
{
32+
"ip_address_misspelled": "192.168.1.2"
33+
}
34+
]
35+
},
36+
"test_bootflash_common_00220a": {
37+
"TEST_NOTES": [
38+
"Negative test.",
39+
"switches[0].targets is not a list."
40+
],
41+
"targets": [
42+
{
43+
"filepath": "bootflash:/foo.txt",
44+
"supervisor": "active"
45+
}
46+
],
47+
"switches": [
48+
{
49+
"ip_address": "192.168.1.2",
50+
"targets": "NOT_A_LIST"
51+
}
52+
]
53+
},
54+
"test_bootflash_common_00230a": {
55+
"TEST_NOTES": [
56+
"Negative test.",
57+
"switches[0].targets.filepath is misspelled."
58+
],
59+
"targets": [
60+
{
61+
"filepath": "bootflash:/foo.txt",
62+
"supervisor": "active"
63+
}
64+
],
65+
"switches": [
66+
{
67+
"ip_address": "192.168.1.2",
68+
"targets": [
69+
{
70+
"filepath_misspelled": "bootflash:/foo.txt",
71+
"supervisor": "active"
72+
}
73+
]
74+
}
75+
]
76+
},
77+
"test_bootflash_common_00240a": {
78+
"TEST_NOTES": [
79+
"Negative test.",
80+
"switches[0].targets.supervisor is misspelled."
81+
],
82+
"targets": [
83+
{
84+
"filepath": "bootflash:/foo.txt",
85+
"supervisor": "active"
86+
}
87+
],
88+
"switches": [
89+
{
90+
"ip_address": "192.168.1.2",
91+
"targets": [
92+
{
93+
"filepath": "bootflash:/foo.txt",
94+
"supervisor_misspelled": "active"
95+
}
96+
]
1997
}
2098
]
2199
},

tests/unit/modules/dcnm/dcnm_bootflash/test_dcnm_bootflash_common.py renamed to tests/unit/modules/dcnm/dcnm_bootflash/test_bootflash_common.py

Lines changed: 142 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,14 @@
2525
__copyright__ = "Copyright (c) 2024 Cisco and/or its affiliates."
2626
__author__ = "Allen Robel"
2727

28-
# TODO: Add imports as needed
2928
import copy
29+
import inspect
3030

31-
# import inspect
3231
import pytest
3332
from ansible_collections.cisco.dcnm.plugins.modules.dcnm_bootflash import \
3433
Common
35-
# from ansible_collections.cisco.dcnm.plugins.module_utils.bootflash.bootflash_files import \
36-
# BootflashFiles
37-
# from ansible_collections.cisco.dcnm.plugins.module_utils.bootflash.convert_target_to_params import \
38-
# ConvertTargetToParams
39-
# from ansible_collections.cisco.dcnm.plugins.module_utils.common.response_handler import \
40-
# ResponseHandler
41-
# from ansible_collections.cisco.dcnm.plugins.module_utils.common.rest_send_v2 import \
42-
# RestSend
43-
# from ansible_collections.cisco.dcnm.plugins.module_utils.common.results import \
44-
# Results
45-
# from ansible_collections.cisco.dcnm.plugins.module_utils.common.sender_file import \
46-
# Sender
47-
# from ansible_collections.cisco.dcnm.plugins.module_utils.common.switch_details import \
48-
# SwitchDetails
49-
# from ansible_collections.cisco.dcnm.tests.unit.module_utils.common.common_utils import \
50-
# ResponseGenerator
51-
# from ansible_collections.cisco.dcnm.tests.unit.modules.dcnm.dcnm_bootflash.utils import (
52-
# MockAnsibleModule, configs_deleted, does_not_raise, params_deleted,
53-
# payloads_bootflash_files, responses_ep_all_switches,
54-
# responses_ep_bootflash_files, targets)
5534
from ansible_collections.cisco.dcnm.tests.unit.modules.dcnm.dcnm_bootflash.utils import (
56-
does_not_raise, params_deleted)
35+
configs_query, does_not_raise, params_deleted, params_query)
5736

5837

5938
def test_bootflash_common_00000() -> None:
@@ -292,13 +271,150 @@ def test_bootflash_common_00200() -> None:
292271
- instance.want matches expectation.
293272
- Exceptions are not not raised.
294273
"""
274+
method_name = inspect.stack()[0][3]
275+
key = f"{method_name}a"
276+
277+
params = copy.deepcopy(params_query)
278+
params["config"] = configs_query(key)
295279
with does_not_raise():
296-
instance = Common(params_deleted)
280+
instance = Common(params)
297281
instance.get_want()
298-
# print(f"instance.want: {instance.want}")
299282
assert instance.want == [
300283
{
301284
"ip_address": "192.168.1.2",
302-
"targets": [{"filepath": "bootflash:/testfile", "supervisor": "active"}],
285+
"targets": [{"filepath": "bootflash:/foo.txt", "supervisor": "active"}],
303286
}
304287
]
288+
289+
290+
def test_bootflash_common_00210() -> None:
291+
"""
292+
### Classes and Methods
293+
- Common()
294+
- get_want()
295+
296+
### Summary
297+
Verify ``get_want()`` behavior when a dictionary in the switches
298+
list is missing the ``ip_address`` parameter.
299+
300+
### Setup
301+
- ``configs_query`` contains ``switches[0].ip_address_misspelled``
302+
rather than the expected ``switches[0].ip_address``.
303+
304+
### Test
305+
- ValueError is raised.
306+
- Error message matches expectation.
307+
"""
308+
method_name = inspect.stack()[0][3]
309+
key = f"{method_name}a"
310+
311+
params = copy.deepcopy(params_query)
312+
params["config"] = configs_query(key)
313+
with does_not_raise():
314+
instance = Common(params)
315+
match = r"Common.get_want:\s+"
316+
match += r"Expected ip_address in switch dict\.\s+"
317+
match += r"Got.*ip_address_misspelled.*\."
318+
with pytest.raises(ValueError, match=match):
319+
instance.get_want()
320+
321+
322+
def test_bootflash_common_00220() -> None:
323+
"""
324+
### Classes and Methods
325+
- Common()
326+
- get_want()
327+
328+
### Summary
329+
Verify ``get_want()`` behavior when a switch in config.switches contains
330+
an invalid ``targets`` parameter value.
331+
332+
### Setup
333+
- ``configs_query`` contains a switch with local ``targets`` parameter,
334+
and that parameter is a string rather than a list, i.e.
335+
switches[0].targets = "NOT_A_LIST".
336+
337+
### Test
338+
- TypeError is raised.
339+
- Error message matches expectation.
340+
"""
341+
method_name = inspect.stack()[0][3]
342+
key = f"{method_name}a"
343+
344+
params = copy.deepcopy(params_query)
345+
params["config"] = configs_query(key)
346+
with does_not_raise():
347+
instance = Common(params)
348+
match = r"Common.get_want:\s+"
349+
match += r"Expected list of dictionaries for switch\['targets'\]\.\s+"
350+
match += r"Got str\."
351+
with pytest.raises(TypeError, match=match):
352+
instance.get_want()
353+
354+
355+
def test_bootflash_common_00230() -> None:
356+
"""
357+
### Classes and Methods
358+
- Common()
359+
- get_want()
360+
361+
### Summary
362+
Verify ``get_want()`` behavior when a switch in config.switches contains
363+
a ``targets`` parameter value that is missing the ``filepath`` key.
364+
365+
### Setup
366+
- ``configs_query`` contains a switch with local ``targets`` parameter,
367+
and that parameter is a list, but a dictionary in the list has
368+
misspelled ``filepath`` as ``filepath_misspelled`` i.e.
369+
``switches[0].targets[0].filepath_misspelled``.
370+
371+
### Test
372+
- ValueError is raised.
373+
- Error message matches expectation.
374+
"""
375+
method_name = inspect.stack()[0][3]
376+
key = f"{method_name}a"
377+
378+
params = copy.deepcopy(params_query)
379+
params["config"] = configs_query(key)
380+
with does_not_raise():
381+
instance = Common(params)
382+
match = r"Common.get_want:\s+"
383+
match += r"Expected filepath in target dict\.\s+"
384+
match += r"Got.*filepath_misspelled.*\."
385+
with pytest.raises(ValueError, match=match):
386+
instance.get_want()
387+
388+
389+
def test_bootflash_common_00240() -> None:
390+
"""
391+
### Classes and Methods
392+
- Common()
393+
- get_want()
394+
395+
### Summary
396+
Verify ``get_want()`` behavior when a switch in config.switches contains
397+
a ``targets`` parameter value that is missing the ``supervisor`` key.
398+
399+
### Setup
400+
- ``configs_query`` contains a switch with local ``targets`` parameter,
401+
and that parameter is a list of dict, but a dictionary in the list has
402+
misspelled ``supervisor`` as ``supervisor_misspelled`` i.e.
403+
``switches[0].targets[0].supervisor_misspelled``.
404+
405+
### Test
406+
- ValueError is raised.
407+
- Error message matches expectation.
408+
"""
409+
method_name = inspect.stack()[0][3]
410+
key = f"{method_name}a"
411+
412+
params = copy.deepcopy(params_query)
413+
params["config"] = configs_query(key)
414+
with does_not_raise():
415+
instance = Common(params)
416+
match = r"Common.get_want:\s+"
417+
match += r"Expected supervisor in target dict\.\s+"
418+
match += r"Got.*supervisor_misspelled.*\."
419+
with pytest.raises(ValueError, match=match):
420+
instance.get_want()

tests/unit/modules/dcnm/dcnm_bootflash/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929

3030
params_query = {
3131
"state": "query",
32-
"config": {"switches": [{"ip_address": "192.168.1.2"}]},
32+
"config": {
33+
"switches": [{"ip_address": "192.168.1.2"}],
34+
"targets": [{"filepath": "bootflash:/testfile", "supervisor": "active"}],
35+
},
3336
"check_mode": False,
3437
}
3538

0 commit comments

Comments
 (0)