Skip to content

Commit

Permalink
Merge 67c112d into 4e131a3
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalpalo committed Jun 6, 2024
2 parents 4e131a3 + 67c112d commit 057ce0b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/4284.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added a flow to upload playbook execution resport to artifacts bucket.
type: internal
pr_number: 4284
7 changes: 7 additions & 0 deletions demisto_sdk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3003,10 +3003,17 @@ def openapi_codegen(ctx, **kwargs):
@click.option(
"-k", "--api-key", help="The Demisto API key for the server", required=True
)
@click.option(
"-ab",
"--artifacts_bucket",
help="The artifacts bucket name to upload the results to",
required=True,
)
@click.option("-s", "--server", help="The server URL to connect to")
@click.option("-c", "--conf", help="Path to content conf.json file", required=True)
@click.option("-e", "--secret", help="Path to content-test-conf conf.json file")
@click.option("-n", "--nightly", type=bool, help="Run nightly tests")
@click.option("-sa", "--service_account", help="GCP service account.")
@click.option("-t", "--slack", help="The token for slack", required=True)
@click.option("-a", "--circleci", help="The token for circleci", required=True)
@click.option("-b", "--build-number", help="The build number", required=True)
Expand Down
44 changes: 42 additions & 2 deletions demisto_sdk/commands/test_content/TestContentClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import requests
from demisto_client.demisto_api import DefaultApi, Incident
from demisto_client.demisto_api.rest import ApiException
from google.cloud import storage # type: ignore[attr-defined]
from junitparser import JUnitXml, TestCase, TestSuite
from junitparser.junitparser import Failure, Result, Skipped
from packaging.version import Version
Expand Down Expand Up @@ -856,7 +857,10 @@ def __init__(self, kwargs: dict, logging_module: ParallelLoggingManager):
self.instances_ips = self._get_instances_ips()
self.filtered_tests = self._extract_filtered_tests()
self.tests_data_keeper = TestResults(
self.conf.unmockable_integrations, kwargs["artifacts_path"]
self.conf.unmockable_integrations,
kwargs["artifacts_path"],
kwargs["service_account"],
kwargs["artifacts_bucket"],
)
self.conf_unmockable_tests = self._get_unmockable_tests_from_conf()
self.unmockable_test_ids: Set[str] = set()
Expand Down Expand Up @@ -1192,7 +1196,13 @@ def _retrieve_slack_user_id(self):


class TestResults:
def __init__(self, unmockable_integrations, artifacts_path: str):
def __init__(
self,
unmockable_integrations,
artifacts_path: str,
service_account: str = None,
artifacts_bucket: str = None,
):
self.succeeded_playbooks: List[str] = []
self.failed_playbooks: Set[str] = set()
self.playbook_report: Dict[str, List[Dict[Any, Any]]] = {}
Expand All @@ -1204,6 +1214,8 @@ def __init__(self, unmockable_integrations, artifacts_path: str):
self.unmockable_integrations = unmockable_integrations
self.playbook_skipped_integration: Set[str] = set()
self.artifacts_path = Path(artifacts_path)
self.service_account = service_account
self.artifacts_bucket = artifacts_bucket

def add_proxy_related_test_data(self, proxy):
# Using multiple appends and not extend since append is guaranteed to be thread safe
Expand Down Expand Up @@ -1343,6 +1355,34 @@ def print_table(table_name: str, table_data: dict, logging_method: Callable):
table.add_row(row)
logging_method(f"{table_name}:\n{table}", real_time=True)

def upload_playbook_result_json_to_bucket(
self,
repository_name: str,
file_name,
logging_module: Union[Any, ParallelLoggingManager] = logging,
):
"""Uploads a JSON object to a specified path in the GCP bucket.
Args:
repository_name: The name of the repository within the bucket.
file_name: The desired filename for the uploaded JSON data.
logging_module: Logging module to use for upload_playbook_result_json_to_bucket.
"""
logging_module.info("Start uploading playbook results file to bucket")

storage_client = storage.Client.from_service_account_json(self.service_account)
storage_bucket = storage_client.bucket(self.artifacts_bucket)

blob = storage_bucket.blob(
f"content-playbook-reports/{repository_name}/{file_name}"
)
blob.upload_from_filename(
self.artifacts_path / "test_playbooks_report.xml",
content_type="application/xml",
)

logging_module.info("Finished uploading playbook results file to bucket")


class Integration:
def __init__(
Expand Down
6 changes: 6 additions & 0 deletions demisto_sdk/commands/test_content/execute_test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def execute_test_content(**kwargs):
build_context.isAMI, logging_manager
)
build_context.tests_data_keeper.create_result_files()

# if kwargs["nightly"]:
build_number = kwargs["build_number"]
build_context.tests_data_keeper.upload_playbook_result_json_to_bucket(
kwargs["server_type"], f"playbook_report_{build_number}", logging_manager
)
if build_context.tests_data_keeper.failed_playbooks:
logging_manager.critical(
"Some tests have failed. Not destroying instances.", real_time=True
Expand Down
4 changes: 4 additions & 0 deletions demisto_sdk/commands/test_content/tests/build_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def get_mocked_build_context(
"server_type": "XSOAR",
"artifacts_path": tmp_file,
"product_type": "xsoar",
"service_account": "test",
"artifacts_bucket": "test",
}
return BuildContext(kwargs, logging_manager)

Expand Down Expand Up @@ -295,6 +297,8 @@ def create_xsiam_build(
"xsiam_servers_api_keys_path": xsiam_api_keys_path,
"artifacts_path": tmp_file,
"product_type": "xsoar",
"service_account": "test",
"artifacts_bucket": "test",
}
return BuildContext(kwargs, logging_manager)

Expand Down
2 changes: 2 additions & 0 deletions demisto_sdk/commands/test_content/tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class Dummyconf:
"mem_check": "",
"server_version": "",
"artifacts_path": ".",
"service_account": "",
"artifacts_bucket": "",
}
mocker.patch.object(
BuildContext, "_load_conf_files", return_value=(Dummyconf(), "")
Expand Down

0 comments on commit 057ce0b

Please sign in to comment.