|
25 | 25 | __copyright__ = "Copyright (c) 2024 Cisco and/or its affiliates."
|
26 | 26 | __author__ = "Allen Robel"
|
27 | 27 |
|
28 |
| -# TODO: Add imports as needed |
29 | 28 | import copy
|
| 29 | +import inspect |
30 | 30 |
|
31 |
| -# import inspect |
32 | 31 | import pytest
|
33 | 32 | from ansible_collections.cisco.dcnm.plugins.modules.dcnm_bootflash import \
|
34 | 33 | 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) |
55 | 34 | 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) |
57 | 36 |
|
58 | 37 |
|
59 | 38 | def test_bootflash_common_00000() -> None:
|
@@ -292,13 +271,150 @@ def test_bootflash_common_00200() -> None:
|
292 | 271 | - instance.want matches expectation.
|
293 | 272 | - Exceptions are not not raised.
|
294 | 273 | """
|
| 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) |
295 | 279 | with does_not_raise():
|
296 |
| - instance = Common(params_deleted) |
| 280 | + instance = Common(params) |
297 | 281 | instance.get_want()
|
298 |
| - # print(f"instance.want: {instance.want}") |
299 | 282 | assert instance.want == [
|
300 | 283 | {
|
301 | 284 | "ip_address": "192.168.1.2",
|
302 |
| - "targets": [{"filepath": "bootflash:/testfile", "supervisor": "active"}], |
| 285 | + "targets": [{"filepath": "bootflash:/foo.txt", "supervisor": "active"}], |
303 | 286 | }
|
304 | 287 | ]
|
| 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() |
0 commit comments