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

[xsoar-8 coverage] - use poll functions from SDK clients #31144

Merged
merged 18 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions Tests/tests_e2e/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,6 @@ def save_playbook(xsoar_client: XsoarClient, playbook_path: str, playbook_id: st
logging.info(f'Deleted playbook {playbook_id}')


@retry(times=10, delay=30)
def is_playbook_state_as_expected(client: XsoarClient, incident_id: str, expected_states: set[str]) -> bool:
"""
Validates whether playbook has reached into an expected state

Args:
client (XsoarClient): xsoar client (saas/on-prem/xsiam).
incident_id (dict): the incident ID that the playbook is attached to.
expected_states (Set[str]): the expected states that the playbook can reach which are valid

Returns:
bool: True if the playbook reached to the expected state, raises exception if not.
"""
playbook_status_raw_response = client.get_playbook_state(incident_id)
_playbook_status = playbook_status_raw_response.get("state", "").lower()
if _playbook_status in expected_states:
return True
playbook_id = playbook_status_raw_response.get("playbookId")
if _playbook_status == "failed":
playbook_failure_reason = client.get_incident_playbook_failure(incident_id)
raise Exception(f'playbook {playbook_id} failed because of {playbook_failure_reason} and its state is {_playbook_status}')
raise Exception(f'the status of the playbook {playbook_id} is {_playbook_status}')


def get_integration_instance_name(integration_params: dict, default: str) -> str:
"""
Gets an instance name for the integration.
Expand All @@ -163,36 +139,6 @@ def get_integration_instance_name(integration_params: dict, default: str) -> str
)


@retry(times=30, delay=5)
def is_incident_state_as_expected(client: XsoarClient, incident_id: str, expected_state: str = "closed") -> bool:
"""
Validates whether an incident has reached into an expected state

Args:
client (XsoarClient): xsoar client (saas/on-prem/xsiam).
incident_id (dict): the incident ID
expected_state (str): the expected state that the incident should be

Returns:
bool: True if the playbook reached to the expected state, raises exception if not.
"""
incident_status = {
0: "new", # pending
1: "in_progress", # active
2: "closed", # done
3: "acknowledged" # archived
}

incident_response = client.search_incidents(incident_ids=incident_id)
# status 2 means the incident is closed.
incident = incident_response["data"][0]
incident_status = incident_status.get(incident.get("status"))
if incident_status == expected_state:
return True
incident_name = incident.get("name")
raise Exception(f'incident {incident_name} status is {incident_status} and is not in state {expected_state}')


@contextmanager
def get_fetched_incident(
client: XsoarClient,
Expand Down
9 changes: 4 additions & 5 deletions Tests/tests_e2e/content/xsoar_saas/test_e2e_xsoar_saas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
get_integration_instance_name,
get_fetched_incident,
save_integration_instance,
is_incident_state_as_expected,
is_playbook_state_as_expected,
save_incident,
save_playbook,
save_indicators
)
from demisto_sdk.commands.common.constants import InvestigationPlaybookState, IncidentState

install_logging('e2e-xsoar-saas.log', logger=logging)

Expand Down Expand Up @@ -171,8 +170,8 @@ def test_slack_ask(request: SubRequest, xsoar_saas_client: XsoarSaasClient):
playbook_name=playbook_id_name
), save_incident(xsoar_saas_client, playbook_id=playbook_id_name) as incident_response:
# make sure the playbook finished successfully
assert is_playbook_state_as_expected(
xsoar_saas_client, incident_id=incident_response.id, expected_states={"completed"}
assert xsoar_saas_client.poll_playbook_state(
incident_response.id, expected_states=(InvestigationPlaybookState.COMPLETED,)
)

context = xsoar_saas_client.get_investigation_context(incident_response.investigation_id)
Expand Down Expand Up @@ -227,4 +226,4 @@ def test_qradar_mirroring(request: SubRequest, xsoar_saas_client: XsoarSaasClien
assert context.get("QRadar", {}).get("Offense", {}).get("Status") == "CLOSED"

# make sure the incident gets closed after closing it in Qradar
assert is_incident_state_as_expected(xsoar_saas_client, incident_id, expected_state="closed")
assert xsoar_saas_client.poll_incident_state(incident_id, expected_states=(IncidentState.CLOSED,), timeout=300)
Loading