Skip to content

Commit

Permalink
Skip core pack installation in XSIAM (#27692)
Browse files Browse the repository at this point in the history
  • Loading branch information
adi88d authored Jun 26, 2023
1 parent b335807 commit 41f44fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 59 deletions.
21 changes: 1 addition & 20 deletions Tests/Marketplace/Tests/search_and_unistall_pack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,8 @@ def test_uninstall_all_packs(mocker):
assert success is True


def test_reset_base_pack_version(mocker):
"""
Given
- Base pack with different version than production.
When
- Updating Base 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)

assert success is True


@pytest.mark.parametrize('def_call, return_val', [
(script.get_all_installed_packs, None),
(script.reset_base_pack_version, False)
(script.get_all_installed_packs, None)
])
def test_exception_reset_base_pack(def_call, return_val, mocker):
"""
Expand Down
54 changes: 15 additions & 39 deletions Tests/Marketplace/search_and_uninstall_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from Tests.configure_and_test_integration_instances import CloudBuild
from Tests.scripts.utils import logging_wrapper as logging
from Tests.scripts.utils.log_util import install_logging
from Tests.Marketplace.search_and_install_packs import install_packs
from Tests.Marketplace.configure_and_install_packs import search_and_install_packs_and_their_dependencies
from time import sleep

UNREMOVABLE_PACKS = ['Base', 'CoreAlertFields', 'Core']


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,21 @@ 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)

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
_, success = search_and_install_packs_and_their_dependencies(pack_ids=UNREMOVABLE_PACKS,
client=client,
hostname=host,
install_packs_one_by_one=True)
return success


def wait_for_uninstallation_to_complete(client: demisto_client):
Expand All @@ -153,7 +129,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 +205,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

0 comments on commit 41f44fd

Please sign in to comment.