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

Skip core pack installation in XSIAM #27692

Merged
merged 10 commits into from
Jun 26, 2023
12 changes: 8 additions & 4 deletions Tests/Marketplace/Tests/search_and_unistall_pack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
def mocked_generic_request_func(self, path: str, method, body=None, accept=None, _request_timeout=None):
if path == '/contentpacks/marketplace/Base':
return MOCK_BASE_SEARCH_RESULTS, 200, None
if path == '/contentpacks/marketplace/Core':
return MOCK_BASE_SEARCH_RESULTS, 200, None
if path == '/contentpacks/marketplace/CoreAlertFields':
return MOCK_BASE_SEARCH_RESULTS, 200, None
elif path == '/contentpacks/metadata/installed':
return MOCK_PACKS_INSTALLATED_RESULT, 200, None
elif path == '/contentpacks/installed/delete':
Expand Down Expand Up @@ -97,27 +101,27 @@ def test_uninstall_all_packs(mocker):
assert success is True


def test_reset_base_pack_version(mocker):
def test_reset_core_pack_version(mocker):
"""
Given
- Base pack with different version than production.
When
- Updating Base pack to prod version.
- Updating core pack to prod version.
Then
- Ensure the pack version gets reset.
"""
client = MockClient()
client.api_client.configuration.host = 'https://api-someurl.com/' # disable-secrets-detection
mocker.patch.object(demisto_client, 'generic_request_func', side_effect=mocked_generic_request_func)
mocker.patch('Tests.Marketplace.search_and_uninstall_pack.install_packs', return_value=True)
success = script.reset_base_pack_version(client)
success = script.reset_core_pack_version(client)

assert success is True


@pytest.mark.parametrize('def_call, return_val', [
(script.get_all_installed_packs, None),
(script.reset_base_pack_version, False)
(script.reset_core_pack_version, False)
])
def test_exception_reset_base_pack(def_call, return_val, mocker):
"""
Expand Down
85 changes: 48 additions & 37 deletions Tests/Marketplace/search_and_uninstall_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from Tests.Marketplace.search_and_install_packs import install_packs
from time import sleep

UNREMOVABLE_PACKS = ['Base', 'CoreAlertFields', 'Core']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there mandatory dependencies for these packs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for Core:
image
and for CoreAlertFields:
image



def get_all_installed_packs(client: demisto_client):
"""
Expand All @@ -35,8 +37,9 @@ def get_all_installed_packs(client: demisto_client):
installed_packs_ids_str = ', '.join(installed_packs_ids)
logging.debug(
f'The following packs are currently installed from a previous build run:\n{installed_packs_ids_str}')
if 'Base' in installed_packs_ids:
installed_packs_ids.remove('Base')
for pack in UNREMOVABLE_PACKS:
if pack in installed_packs_ids:
installed_packs_ids.remove(pack)
return installed_packs_ids
else:
result_object = ast.literal_eval(response_data)
Expand Down Expand Up @@ -92,48 +95,56 @@ def uninstall_all_packs(client: demisto_client, hostname):
return True


def reset_base_pack_version(client: demisto_client):
def reset_core_pack_version(client: demisto_client):
"""
Resets base pack version to prod version.
Resets core pack version to prod version.

Args:
client (demisto_client): The client to connect to.


"""
host = client.api_client.configuration.host.replace('https://api-', 'https://') # disable-secrets-detection
try:
# make the search request
response_data, status_code, _ = demisto_client.generic_request_func(client,
path='/contentpacks/marketplace/Base',
method='GET',
accept='application/json',
_request_timeout=None)
if 200 <= status_code < 300:
result_object = ast.literal_eval(response_data)

if result_object and result_object.get('currentVersion'):
logging.debug('Found Base pack in bucket!')

pack_data = {
'id': result_object.get('id'),
'version': result_object.get('currentVersion')
}
# install latest version of Base pack
logging.info(f'updating base pack to version {result_object.get("currentVersion")}')
return install_packs(client, host, [pack_data], False)

is_succeed = True

for pack_id in UNREMOVABLE_PACKS:

try:
# make the search request
response_data, status_code, _ = demisto_client.generic_request_func(client,
path=f'/contentpacks/marketplace/{pack_id}',
method='GET',
accept='application/json',
_request_timeout=None)
if 200 <= status_code < 300:
result_object = ast.literal_eval(response_data)

if result_object and result_object.get('currentVersion'):
logging.debug(f'Found {pack_id} pack in bucket!')

pack_data = {
'id': result_object.get('id'),
'version': result_object.get('currentVersion')
}
# install latest version of the pack
logging.info(f'updating {pack_id} pack to version {result_object.get("currentVersion")}')
installation_status = install_packs(client, host, [pack_data], False)
if not installation_status:
is_succeed = False

else:
raise Exception(f'Did not find {pack_id} pack')
else:
raise Exception('Did not find Base pack')
else:
result_object = ast.literal_eval(response_data)
msg = result_object.get('message', '')
err_msg = f'Search request for base pack, failed with status code ' \
f'{status_code}\n{msg}'
raise Exception(err_msg)
except Exception:
logging.exception('Search request Base pack has failed.')
return False
result_object = ast.literal_eval(response_data)
msg = result_object.get('message', '')
err_msg = f'Search request for {pack_id} pack, failed with status code ' \
f'{status_code}\n{msg}'
raise Exception(err_msg)
except Exception:
logging.exception(f'Search request {pack_id} pack has failed.')
is_succeed = False

return is_succeed


def wait_for_uninstallation_to_complete(client: demisto_client):
Expand All @@ -153,7 +164,7 @@ def wait_for_uninstallation_to_complete(client: demisto_client):
installed_packs_amount_history, failed_uninstall_attempt_count = len(installed_packs), 0
# new calculation for num of retries
retries = math.ceil(len(installed_packs) / 2)
while len(installed_packs) > 1:
while len(installed_packs) > len(UNREMOVABLE_PACKS):
if retry > retries:
raise Exception('Waiting time for packs to be uninstalled has passed, there are still installed '
'packs. Aborting.')
Expand Down Expand Up @@ -229,7 +240,7 @@ def main():
# We are syncing marketplace since we are copying production bucket to build bucket and if packs were configured
# in earlier builds they will appear in the bucket as it is cached.
sync_marketplace(client=client)
success = reset_base_pack_version(client) and uninstall_all_packs(client,
success = reset_core_pack_version(client) and uninstall_all_packs(client,
host) and wait_for_uninstallation_to_complete(
client)
sync_marketplace(client=client)
Expand Down