diff --git a/.github/workflows/py_unit_tests.yml b/.github/workflows/py_unit_tests.yml index f669c2b..5c7d2b8 100644 --- a/.github/workflows/py_unit_tests.yml +++ b/.github/workflows/py_unit_tests.yml @@ -35,6 +35,9 @@ jobs: pip install -r tests/requirements.txt - name: Run Python Unit Tests + env: + SPLUNKBASE_USERNAME_FOR_TEST: ${{ secrets.SPLUNKBASE_USERNAME }} + SPLUNKBASE_PASSWORD_FOR_TEST: ${{ secrets.SPLUNKBASE_PASSWORD }} run: pytest tests --junitxml=junit/test-results.xml --cov=src --cov-config=tests/.coveragerc --cov-report=xml - name: Adding GitHub action step summary diff --git a/src/app_inspect.py b/src/app_inspect.py index d2c340c..7fae465 100644 --- a/src/app_inspect.py +++ b/src/app_inspect.py @@ -22,17 +22,9 @@ class SplunkAppInspect: HTML_RESPONSE_URL = "{}/report".format(BASE_URL) - def __init__(self, app_build_path) -> None: - self.is_app_inspect_check = utils.str_to_boolean_default_true( - utils.get_input('is_app_inspect_check')) - utils.info("is_app_inspect_check: {}".format( - self.is_app_inspect_check)) - if not self.is_app_inspect_check: - utils.info("Ignoring App-inspect checks.") - return - - self.splunkbase_username = utils.get_input('splunkbase_username') - self.splunkbase_password = utils.get_input('splunkbase_password') + def __init__(self, app_build_path, splunkbase_username, splunkbase_password) -> None: + self.splunkbase_username = splunkbase_username + self.splunkbase_password = splunkbase_password if not self.splunkbase_username: msg = "splunkbase_username input is not provided." @@ -233,8 +225,6 @@ def _perform_ssai_inspect_check(self): def run_all_checks(self): utils.info("Running the Splunk App inspect check.") - if not self.is_app_inspect_check: - return thread_app_inspect = Thread(target=self._perform_app_inspect_check) thread_app_inspect.start() diff --git a/src/app_utilities.py b/src/app_utilities.py index f378024..8ae2bfa 100644 --- a/src/app_utilities.py +++ b/src/app_utilities.py @@ -34,16 +34,16 @@ def add_utilities(self, app_utilities): WhatsInsideTheAppUtility(self.app_read_dir, self.app_write_dir) elif utility == "logger": - LoggerUtility(self.app_read_dir, self.app_write_dir) + LoggerUtility(self.app_read_dir, self.app_write_dir).add() elif utility == "splunk_python_sdk": - SplunkPythonSDKUtility(self.app_read_dir, self.app_write_dir) + SplunkPythonSDKUtility(self.app_read_dir, self.app_write_dir).add() elif utility == "common_js_utilities": - CommonJSUtilitiesFile(self.app_read_dir, self.app_write_dir) + CommonJSUtilitiesFile(self.app_read_dir, self.app_write_dir).add() elif utility == "ucc_additional_packaging": - UCCAdditionalPackagingUtility(self.app_read_dir, self.app_write_dir) + UCCAdditionalPackagingUtility(self.app_read_dir, self.app_write_dir).add() else: utils.error("utility={} is not supported.".format(utility)) diff --git a/src/helpers/git_manager.py b/src/helpers/git_manager.py index 6dd3ce7..1e1808c 100644 --- a/src/helpers/git_manager.py +++ b/src/helpers/git_manager.py @@ -1,6 +1,5 @@ import os -import re import hashlib import helpers.github_action_utils as utils diff --git a/src/main.py b/src/main.py index b3239f8..644cd69 100644 --- a/src/main.py +++ b/src/main.py @@ -98,8 +98,21 @@ def main(): utils.info("generate_build Completed.") # Run App Inspect - SplunkAppInspect(build_path).run_all_checks() - utils.info("SplunkAppInspect Completed.") + is_app_inspect_check = utils.str_to_boolean_default_true( + utils.get_input('is_app_inspect_check') + ) + utils.info("is_app_inspect_check: {}".format(is_app_inspect_check)) + + if is_app_inspect_check: + splunkbase_username = utils.get_input('splunkbase_username') + splunkbase_password = utils.get_input('splunkbase_password') + + SplunkAppInspect(build_path, splunkbase_username, splunkbase_password).run_all_checks() + utils.info("SplunkAppInspect Completed.") + else: + utils.info("Ignoring App-inspect checks.") + return + except Exception as e: utils.error( diff --git a/tests/__init__.py b/tests/__init__.py index f53f2f7..8d8743d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -12,10 +12,40 @@ # Mock the set_env function during the test -patcher = patch("helpers.github_action_utils.set_env") -mock_set_env = patcher.start() +patcher_set_env = patch("helpers.github_action_utils.set_env") +mock_set_env = patcher_set_env.start() def mock_set_env(name, value): print(f"Mocked set_env called with args: name={name}, value={value}") os.environ[name] = value -mock_set_env.side_effect = mock_set_env \ No newline at end of file +mock_set_env.side_effect = mock_set_env + + +# Mock the _pr function from git_manager.GitHubPR during the test +patcher_github_pr = patch("helpers.git_manager.GitHubPR._pr") +mock_github_pr = patcher_github_pr.start() + +def mock_github_pr_fn(new_branch): + print(f"Mocked git_manager.GitHubPR._pr called with args: new_branch={new_branch}") +mock_github_pr.side_effect = mock_github_pr_fn + + +# Mock the _check_branch_exist function from git_manager.GitHubPR during the test +patcher_github_check_branch_exist = patch("helpers.git_manager.GitHubPR._check_branch_exist") +mock_github_check_branch_exist = patcher_github_check_branch_exist.start() + +def mock_github_check_branch_exist_fn(branch_name): + print(f"Mocked git_manager.GitHubPR._check_branch_exist called with args: new_branch={branch_name}") + if str(branch_name).endswith("_exists"): + return True + return False +mock_github_check_branch_exist.side_effect = mock_github_check_branch_exist_fn + + +# Mock the _reset_the_git_repo function from git_manager.GitHubPR during the test +patcher_github_reset_the_git_repo = patch("helpers.git_manager.GitHubPR._reset_the_git_repo") +mock_github_reset_the_git_repo = patcher_github_reset_the_git_repo.start() + +def mock_github_reset_the_git_repo_fn(): + print("Mocked git_manager.GitHubPR._reset_the_git_repo called.") +mock_github_reset_the_git_repo.side_effect = mock_github_reset_the_git_repo_fn diff --git a/tests/test_app_inspect.py b/tests/test_app_inspect.py index a26940e..702dc1d 100644 --- a/tests/test_app_inspect.py +++ b/tests/test_app_inspect.py @@ -4,26 +4,23 @@ from .helper import setup_action_yml from main import main -import app_inspect +SPLUNKBASE_USERNAME_FOR_TEST = os.environ["SPLUNKBASE_USERNAME_FOR_TEST"] +SPLUNKBASE_PASSWORD_FOR_TEST = os.environ["SPLUNKBASE_PASSWORD_FOR_TEST"] + + class TestAppInspect(unittest.TestCase): def test_app_inspect_failure_integration(self): - with setup_action_yml("repo_1_regular_build", app_dir="my_app_1", is_app_inspect_check="true", splunkbase_username="VatsalJagani", splunkbase_password="g9@PDP5Ktby%Q8"): + with setup_action_yml("repo_1_regular_build", app_dir="my_app_1", is_app_inspect_check="true", splunkbase_username=SPLUNKBASE_USERNAME_FOR_TEST, splunkbase_password=SPLUNKBASE_PASSWORD_FOR_TEST): with pytest.raises(SystemExit) as pytest_wrapped_e: main() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 5 - def test_app_inspect_failure_unit(self): - with self.assertRaisesRegex(Exception, "All status \\[app-inspect, cloud-checks, self-service-checks\\].*Failure.*"): - build_path = os.path.join(os.path.dirname(__file__), "test_app_builds_for_app_inspect", "my_app_1_1_1_2_1.tgz") - app_inspect.SplunkAppInspect(build_path).run_all_checks() - - - def test_app_inspect_success_unit(self): - build_path = os.path.join(os.path.dirname(__file__), "test_app_builds_for_app_inspect", "TA-defender-atp-status-check_1_1_0_1.tgz") - app_inspect.SplunkAppInspect(build_path).run_all_checks() + def test_app_inspect_success_integration(self): + with setup_action_yml("app_inspect_pass", app_dir=".", to_make_permission_changes="true", is_app_inspect_check="true", splunkbase_username=SPLUNKBASE_USERNAME_FOR_TEST, splunkbase_password=SPLUNKBASE_PASSWORD_FOR_TEST): + main()