Skip to content

Commit

Permalink
Merge pull request #28 from VatsalJagani/test-cases
Browse files Browse the repository at this point in the history
Test cases
  • Loading branch information
VatsalJagani authored Mar 8, 2024
2 parents 82805f6 + c3f4f6a commit 186dbe9
Show file tree
Hide file tree
Showing 32 changed files with 534 additions and 32 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/py_unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This is a basic workflow to run automated test-cases on GitHub App Action
name: Running Python Unit Tests for Splunk-App-Action

# Controls when the action will run. Triggers the workflow on push or pull request
on:
push:
branches:
- 'develop'

pull_request:
branches:
- '*'
- '*/*'
- '**'

workflow_dispatch:

jobs:
python-unit-tests:
name: "Running Python Unit Test Cases"
runs-on: "ubuntu-latest"

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4

- name: Display Python version
run: python -c "import sys; print(sys.version)"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r tests/requirements.txt
- name: Run Python Unit Tests
run: pytest tests --junitxml=junit/test-results.xml --cov=src --cov-config=tests/.coveragerc --cov-report=xml

- name: Linting with Ruff
run: ruff --output-format=github .
continue-on-error: true # need to remove this later on to catch the errors anf fail the workflow on linting errors

- name: Adding GitHub action step summary
uses: VatsalJagani/[email protected]
with:
pytest_results_file: "junit/test-results.xml"
pytest_cov_file: "coverage.xml"
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
*.pyc
__pycache__
src/utilities/logger/props.conf_temp
tests2/**
tests2
.coverage
coverage.xml
.pytest_cache
htmlcov
junit
6 changes: 0 additions & 6 deletions src/helpers/global_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class GlobalVariables:
APP_BUILD_NUMBER = None
APP_BUILD_NUMBER_ENCODED = None

BUILD_REPO_DIR_NAME = "repodir_for_build"
BUILD_REPO_DIR_PATH = None

# TODO - validate and remove variables which are unused

@staticmethod
def initiate(app_dir_name, repodir_name=None, root_dir_path=None) -> None:
Expand All @@ -31,8 +27,6 @@ def initiate(app_dir_name, repodir_name=None, root_dir_path=None) -> None:
GlobalVariables.APP_DIR_NAME = app_dir_name
GlobalVariables.ORIGINAL_APP_DIR_PATH = os.path.join(GlobalVariables.ORIGINAL_REPO_DIR_PATH, GlobalVariables.APP_DIR_NAME)

GlobalVariables.BUILD_REPO_DIR_PATH = os.path.join(GlobalVariables.ROOT_DIR_PATH, GlobalVariables.BUILD_REPO_DIR_NAME)


@staticmethod
def set_app_package_id(app_package_id) -> None:
Expand Down
38 changes: 16 additions & 22 deletions src/helpers/splunk_app_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@


def fetch_app_package_id_from_global_config_json(global_config_file_path):
utils.info("Fetching app package id from globalConfig.json file.")
utils.info("Fetching app_package_id from globalConfig.json file.")

_app_package_id = None
try:
with open(global_config_file_path, 'r') as f:
global_config = json.loads(f.read())
_app_package_id = global_config["meta"]["name"]

with open(global_config_file_path, 'r') as f:
global_config = json.loads(f.read())

_app_package_id = global_config["meta"]["name"]

if not _app_package_id:
raise Exception("Unable to fetch the app package id from globalConfig.json file")
except Exception as e:
utils.error(f"Exception while fetching app_package_id from globalConfig.json file. exception={e}")
raise Exception("Unable to fetch the app_package_id from globalConfig.json file.")

return _app_package_id


def fetch_app_version_from_global_config_json(global_config_file_path):
utils.info("Fetching app version number from globalConfig.json file.")

_app_version = None
utils.info("Fetching app_version_number from globalConfig.json file.")

with open(global_config_file_path, 'r') as f:
global_config = json.loads(f.read())
try:
with open(global_config_file_path, 'r') as f:
global_config = json.loads(f.read())
_app_version = global_config["meta"]["version"]

_app_version = global_config["meta"]["version"]

if not _app_version:
raise Exception("Unable to fetch the app package id from globalConfig.json file")
except Exception as e:
utils.error(f"Exception while fetching app_version_number from globalConfig.json file. exception={e}")
raise Exception("Unable to fetch the app_version_number from globalConfig.json file.")

return _app_version


def _read_app_conf(app_dir_path):
return SplunkConfigParser(os.path.join(app_build_dir, 'default', 'app.conf'))


def fetch_app_package_id_from_app_conf(app_conf_file_path, app_dir_input):
app_config = SplunkConfigParser(app_conf_file_path)

Expand Down
11 changes: 8 additions & 3 deletions src/helpers/splunk_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
MULTI_LINE_CHAR = '\\'
COMMENT_CHAR = '#'
FILE_SECTION = '___FILE___'
GLOBAL_SETTING = '___FILE___'
DEFAULT_SETTING = '___FILE___'
NEW_LINE_CHAR = '\n'


Expand Down Expand Up @@ -167,6 +169,8 @@ def read(self, encoding=None):
with open(self.file_path, 'r', encoding=encoding) as file:
content = file.read()
self._parse(content)
else:
raise Exception("Splunk Conf File Not Found.")


def _parse(self, content):
Expand Down Expand Up @@ -286,14 +290,14 @@ def __len__(self):
def __iter__(self):
return iter(self._content)

def items(self):
return self._content.items()
# def items(self):
# return self._content.items()


def merge(self, second_conf_parser, to_merge_pre_stanza_comments=True, to_merge_file_level_parameters=False):
is_changed = False

for stanza, options in second_conf_parser.items():
for stanza, options in second_conf_parser._content.items():
if stanza not in self._content:
self._content[stanza] = _SplunkStanzaOptions()
is_changed = True
Expand All @@ -308,6 +312,7 @@ def merge(self, second_conf_parser, to_merge_pre_stanza_comments=True, to_merge_


def __str__(self) -> str:
# NOTE - Do not change the code of this function as this might fail the working or even fail some test-cases
content_str = ''
for section, options in self._content.items():
content_str += options.as_string(section)
Expand Down
6 changes: 6 additions & 0 deletions tests/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
omit =
# omit extra utilities files
utilities/common_splunk_js_utilities/splunk_common_js_v_utilities.js
utilities/logger/logger_manager.py
utilities/ucc_additional_packaging/additional_packaging.py
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest
pytest-cov
ruff
Empty file.
1 change: 1 addition & 0 deletions tests/splunk_config_files/empty_config_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# just a comment in the file
11 changes: 11 additions & 0 deletions tests/splunk_config_files/global_settings_present.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# global comment-1

GLOBAL_OPTION1 = GLOBAL_VALUE1
GLOBAL_OPTION2 = GLOBAL_VALUE2

# global comment-2

[STANZA1]
# comment in stanza1
OPTION1 = VALUE1
5 changes: 5 additions & 0 deletions tests/splunk_config_files/invalid_config.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hello
MY VALUE

[STANZA]
OPTION = VALUE
7 changes: 7 additions & 0 deletions tests/splunk_config_files/merge_additional_comment_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[STANZA1]
# This is my Stanza1
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
7 changes: 7 additions & 0 deletions tests/splunk_config_files/merge_additional_comment_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[STANZA1]
# This is my Stanza1 - Comment Changed
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/splunk_config_files/merge_empty_with_comment_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# hello just comment from file-1
1 change: 1 addition & 0 deletions tests/splunk_config_files/merge_empty_with_comment_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# hello just comment from file-2
11 changes: 11 additions & 0 deletions tests/splunk_config_files/merge_file1_is_super_set_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

[STANZA1]
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
OPTION2_1 = VALUE2_1

[STANZA3]
OPTION3 = VALUE3
OPTION_3_1 = VALUE_3_1
6 changes: 6 additions & 0 deletions tests/splunk_config_files/merge_file1_is_super_set_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[STANZA2]
OPTION2 = VALUE2

[STANZA3]
OPTION3 = VALUE3
6 changes: 6 additions & 0 deletions tests/splunk_config_files/merge_new_comment_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[STANZA1]
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
7 changes: 7 additions & 0 deletions tests/splunk_config_files/merge_new_comment_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[STANZA1]
# This is my Stanza1 info
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
3 changes: 3 additions & 0 deletions tests/splunk_config_files/merge_new_stanza_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[STANZA1]
OPTION1 = VALUE1
6 changes: 6 additions & 0 deletions tests/splunk_config_files/merge_new_stanza_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[STANZA1]
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
6 changes: 6 additions & 0 deletions tests/splunk_config_files/merge_same_file_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[STANZA1]
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
6 changes: 6 additions & 0 deletions tests/splunk_config_files/merge_same_file_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[STANZA1]
OPTION1 = VALUE1

[STANZA2]
OPTION2 = VALUE2
3 changes: 3 additions & 0 deletions tests/splunk_config_files/merge_same_stanza_update_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[STANZA1]
OPTION1 = VALUE1
3 changes: 3 additions & 0 deletions tests/splunk_config_files/merge_same_stanza_update_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[STANZA1]
OPTION1 = NEW_VALUE1
10 changes: 10 additions & 0 deletions tests/splunk_config_files/parse_1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[STANZA1]
OPTION1 = VALUE1
OPTION2 = VALUE2

[STANZA2]
# comment in stanza2
OPTION3 = VALUE3

# Comment after stanza
# This is a comment
10 changes: 10 additions & 0 deletions tests/splunk_config_files/parse_2_multiline_value.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[STANZA1]
OPTION1 = VALUE1\
VALUE2\
WITH\
NEWLINES
# above option1 is multi-valued
OPTION2 = VALUE2

[STANZA2]
OPTION3 = VALUE3
2 changes: 2 additions & 0 deletions tests/splunk_config_files/read_config_file.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[abc]
xyz = pqrst1
Loading

0 comments on commit 186dbe9

Please sign in to comment.