Skip to content

Commit

Permalink
Added test-cases for app-inspect.
Browse files Browse the repository at this point in the history
  • Loading branch information
VatsalJagani committed Mar 13, 2024
1 parent 6417a6c commit 0ebef3d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/py_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions src/app_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/app_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
1 change: 0 additions & 1 deletion src/helpers/git_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import os
import re
import hashlib
import helpers.github_action_utils as utils

Expand Down
17 changes: 15 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 33 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
19 changes: 8 additions & 11 deletions tests/test_app_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0ebef3d

Please sign in to comment.