Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/173 use proper retry #183

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mgappa
Copy link

@mgappa mgappa commented Jan 6, 2025

Description

To use less memory consumption, we must, first of all, add some tests on the current code and, second of all, apply some refactoring on the retry part. On this PR, we add:

  • Test the CWPP compute_execute method, and fix unit.py test file
  • Refactor the retry method by using the combination of Retry class from urlib3 and Session request library
  • Add coverage and responses libraries for test purposes to get the code coverage and simplify the unit test writing

Motivation and Context

This PR is the first step for the issue #173

How Has This Been Tested?

To test the code, you must install the dependencies in the requirements_test.txt file, with the pip install -r requirements_test.txt command. Then, you can execute all tests with the following command coverage run -m unittest discover -v -s "./tests" -p "test*.py". Finally, you can look at the coverage result by taping the command coverage report -m or if you are looking for a nicer presentation, you can tape the command coverage html

Screenshots (if appropriate)

Coverage on the main branch:

$ coverage run -m unittest discover -v -s "./tests" -p "*.py"
unit (unittest.loader._FailedTest) ... ERROR

======================================================================
ERROR: unit (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: unit
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/local/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/workspaces/new-prismacloud-api-python/tests/unit.py", line 5, in <module>
    import mock
ModuleNotFoundError: No module named 'mock'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

Coverage on the mgappa:refactor/173_use_proper_retry branch:

$ coverage run -m unittest discover -v -s "./tests" -p "test*.py"
test_excute_compute_nominal_for_tag_update (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_using_force_parameter (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_without_force_parameter (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_failed_because_bad_status_code (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_nominal_for_credentials_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_nominal_for_hosts_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_retry_failed_for_credentials_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_retry_for_credentials_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_with_an_expire_token_for_credentials_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_with_csv_content_type (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_execute_compute_without_token_for_credentials_list (test_cwpp_cwpp.TestCasePrismaCloudAPICWPPMixin) ... ok
test_pc_api_configure (test_unit.TestPrismaCloudAPI) ... ok
test_pc_api_current_user (test_unit.TestPrismaCloudAPI) ... ok

----------------------------------------------------------------------
Ran 13 tests in 0.672s

OK

Report:

$ coverage report -m
Name                                       Stmts   Miss  Cover   Missing
------------------------------------------------------------------------
prismacloud/api/__init__.py                   11      1    91%   14
prismacloud/api/cspm/__init__.py               7      0   100%
prismacloud/api/cspm/_endpoints.py           267    162    39%   39, 42, 45, 48, 51, 69, 72, 75-76, 79, 82-83, 86, 89, 92, 105, 108-112, 115-116, 119, 132, 135, 138, 141, 154, 157, 170, 173, 189-190, 193-194, 207, 210, 213, 216, 219, 222, 235, 238, 241, 244, 247, 262, 265, 268, 271, 275, 278, 293, 296, 299, 302, 305, 308, 311, 314, 327, 330, 333, 336, 339, 355, 358, 374, 377, 390-391, 394, 397-411, 426, 429, 432, 435, 438, 453, 456, 469, 472, 475, 488, 491, 494, 509, 512, 515, 518, 539-552, 555-558, 561-577, 580-593, 596-597, 600-601, 604, 617, 633, 636, 639, 642, 655, 658, 661, 675, 678, 691-692, 705
prismacloud/api/cspm/_extended.py             57     53     7%   11-45, 48-67
prismacloud/api/cspm/cspm.py                 120     58    52%   34-38, 44, 46, 49-69, 77, 84, 91, 104-108, 111, 113, 115, 123-127, 129-136, 140-144, 155-156, 162-163
prismacloud/api/cwpp/__init__.py              24      0   100%
prismacloud/api/cwpp/_audits.py               25     17    32%   11-12, 19-26, 31-33, 39, 66-67, 72-73
prismacloud/api/cwpp/_cloud.py                13      6    54%   9, 14, 17, 20, 23, 26
prismacloud/api/cwpp/_collections.py          11      5    55%   9, 12, 18, 21, 24
prismacloud/api/cwpp/_containers.py            6      4    33%   9-13
prismacloud/api/cwpp/_credentials.py          10      4    60%   12, 15, 21, 26
prismacloud/api/cwpp/_defenders.py             7      4    43%   9-10, 13-14
prismacloud/api/cwpp/_feeds.py                 5      2    60%   11, 29
prismacloud/api/cwpp/_hosts.py                13      8    38%   10-11, 14-15, 18-19, 22-23
prismacloud/api/cwpp/_images.py                9      6    33%   9-13, 16-17
prismacloud/api/cwpp/_logs.py                 13      8    38%   11-12, 15-16, 19-20, 23-24
prismacloud/api/cwpp/_policies.py             11      5    55%   9, 12, 17, 20, 23
prismacloud/api/cwpp/_registry.py             15     10    33%   9-13, 16-17, 20-21, 24-25
prismacloud/api/cwpp/_scans.py                 6      4    33%   9-13
prismacloud/api/cwpp/_serverless.py           13      8    38%   6-7, 11-12, 16-17, 21-22
prismacloud/api/cwpp/_settings.py             17      8    53%   9, 12, 18, 21, 27, 30, 36, 39
prismacloud/api/cwpp/_stats.py                27     13    52%   8, 12, 15, 20, 25, 30, 34, 37, 41, 44, 48, 51, 56
prismacloud/api/cwpp/_status.py                5      2    60%   7, 10
prismacloud/api/cwpp/_tags.py                 19     12    37%   9-10, 13-14, 17-18, 21-22, 25-26, 29-30
prismacloud/api/cwpp/_vms.py                   4      2    50%   11-13
prismacloud/api/cwpp/cwpp.py                  83     13    84%   17-21, 71, 88, 98-103, 127-128, 135
prismacloud/api/pc_lib_api.py                 62      3    95%   56, 87, 93
prismacloud/api/pc_lib_utility.py            286    224    22%   23-27, 43-48, 54-114, 121-167, 173-211, 216-228, 234-254, 260-268, 275, 293-301, 307-312, 318-323, 328-336, 341-352, 358-364, 370-377, 383-389, 395-402, 408-414, 420-427, 433
prismacloud/api/pccs/__init__.py              14      0   100%
prismacloud/api/pccs/_checkov_version.py       4      2    50%   9-10
prismacloud/api/pccs/_code_policies.py         5      3    40%   9-14
prismacloud/api/pccs/_errors.py               17      8    53%   9, 12, 15, 18, 21, 24, 27, 30
prismacloud/api/pccs/_fixes.py                 5      2    60%   9, 12
prismacloud/api/pccs/_packages.py              3      1    67%   9
prismacloud/api/pccs/_repositories.py         11      5    55%   9, 12, 15, 18, 21
prismacloud/api/pccs/_rules.py                 9      4    56%   9, 12, 15, 18
prismacloud/api/pccs/_scans.py                 3      1    67%   9
prismacloud/api/pccs/_suppressions.py         11      5    55%   9, 12, 15, 18, 21
prismacloud/api/pccs/pccs.py                  71     64    10%   13-88, 94
prismacloud/api/version.py                     1      0   100%
tests/data.py                                  5      0   100%
tests/test_cwpp_cwpp.py                       95      0   100%
tests/test_unit.py                            15      0   100%
------------------------------------------------------------------------
TOTAL                                       1415    737    48%

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

mgappa added 6 commits January 3, 2025 14:57
* Fix unit test by using full function/method/object path
* Rename unit to test_unit python file
* Move data information into a specific python file
* Add some basic test by using responses library, that permit to simplify
the request mock. We add test before the retry change to keep all basic
fonctionnalities
* Retry class permit to retry requests and to use requests session
* Add retry_number property on PrismaCloudAPI used by Retry class
* Update test_execute_compute_retry_failed_for_credentials_list test
to use retry_number instead of retry_wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant