Skip to content

Commit

Permalink
Fixed app-build test-cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
VatsalJagani committed Mar 30, 2024
1 parent 6b147dd commit 8b45dc8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
23 changes: 8 additions & 15 deletions DEV_README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# README for Developers
# README for Developers of this action

* Fork the project and submit a Pull Request if you would like to contribute to the project.

* Automated tests for the GitHub action present in the separate repo here [splunk-app-action-test-app](https://github.com/VatsalJagani/splunk-app-action-test-app)


## How to Run Locally
```
python3 src/main.py local_test
```


# Notes for author
## How to generate tag

* Create a new tag and push the tag from git:
```
Expand All @@ -33,13 +27,12 @@ python3 src/main.py local_test
```


* Run below command to install all node JS dependencies.
* `npm install`

* Run below command if you make any changes to JS file before committing the code to repo.
* `ncc build index.js --license LICENSE`
* This do not require if there is change in python file or action.yml file.

## How to run python test-cases locally
```
python -m pip install --upgrade pip
python -m pip install -r tests/requirements.txt
pytest tests --junitxml=junit/test-results.xml --cov=src --cov-config=tests/.coveragerc --cov-report=xml
```


## Note from actions/upload-artifact - Zipped Artifact Downloads
Expand Down
63 changes: 39 additions & 24 deletions tests/test_build_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def get_file_permissions(filepath):
return file_permissions


def remove_ds_store_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file == ".DS_Store":
os.remove(os.path.join(root, file))


class TestAppBuild(unittest.TestCase):

def extract_app_build(self, tgz_file):
Expand All @@ -70,10 +77,11 @@ def extract_app_build(self, tgz_file):
with tarfile.open(tgz_file, 'r:gz') as tar:
tar.extractall(extract_dir)

remove_ds_store_files(extract_dir)

# Initialize counters
file_count = 0
folder_count = 0
all_files = []
all_folders = []
is_root = True

# Walk through the extracted directory
Expand All @@ -82,19 +90,20 @@ def extract_app_build(self, tgz_file):
is_root = False
continue

folder_count += len(dirs)
file_count += len(files)

# Print relative paths of files
for file in files:
relative_path = os.path.relpath(os.path.join(root, file), extract_dir)
# print("DEBUG: File:", relative_path)
all_files.append(relative_path)

# print("DEBUG: Total Files:", file_count)
# print("DEBUG: Total Folders:", folder_count)
# print(f"All Files: {all_files}")
return file_count, folder_count, all_files
for dir in dirs:
relative_path = os.path.relpath(os.path.join(root, dir), extract_dir)
all_folders.append(relative_path)

print("DEBUG: Total Files:", len(all_files))
print("DEBUG: Total Folders:", len(all_folders))
print(f"DEBUG: All Files: {all_files}")
print(f"DEBUG: All Folders: {all_folders}")
return len(all_files), len(all_folders), all_files, all_folders

finally:
# Cleanup: Remove the temporary extraction directory
Expand All @@ -115,7 +124,7 @@ def test_build_1_regular(self):
app_build_name = "my_app_1_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

file_count, folder_count, all_files = self.extract_app_build(app_build_name)
file_count, folder_count, all_files, _ = self.extract_app_build(app_build_name)
assert folder_count == 7
assert file_count == 10
assert "my_app_1/static/appIconAlt.png" in all_files
Expand All @@ -130,7 +139,7 @@ def test_build_repo_root_as_app_dir(self):
app_build_name = "my_app_1_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

file_count, folder_count, all_files = self.extract_app_build(app_build_name)
file_count, folder_count, all_files, _ = self.extract_app_build(app_build_name)
assert folder_count == 7
assert file_count == 10
assert "my_app_1/static/appIconAlt.png" in all_files
Expand All @@ -151,9 +160,7 @@ def test_ucc_build_1_regular(self):
app_build_name = app_build_files[0]
assert os.path.isfile(app_build_name)

file_count, folder_count, all_files = self.extract_app_build(app_build_name)
assert folder_count == 57
assert file_count == 362
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_ucc_1/default/app.conf" in all_files
assert "my_app_ucc_1/appserver/static/js/build/globalConfig.json" in all_files
assert "my_app_ucc_1/lib/splunklib/client.py" in all_files
Expand All @@ -164,6 +171,11 @@ def test_ucc_build_1_regular(self):
assert all("my_app_ucc_1/globalConfig.json" not in s for s in all_files), "'globalConfig.json' file shouldn't be in the root of the App."
assert all("additional_packaging.py" not in s for s in all_files), "'additional_packaging.py' file shouldn't be part of the App build."

assert "my_app_ucc_1/lib/splunklib/modularinput" in all_folders
assert "my_app_ucc_1/lib/solnlib" in all_folders
assert "my_app_ucc_1/appserver/static/js/build" in all_folders
assert "my_app_ucc_1/lib/splunktaucclib/common" in all_folders


def test_ucc_build_repo_root_as_app_dir(self):
with setup_action_yml("repo_ucc_2_repo_root_as_app_dir", app_dir=".", use_ucc_gen="true", is_app_inspect_check="false"):
Expand All @@ -178,9 +190,7 @@ def test_ucc_build_repo_root_as_app_dir(self):
app_build_name = app_build_files[0]
assert os.path.isfile(app_build_name)

file_count, folder_count, all_files = self.extract_app_build(app_build_name)
assert folder_count == 57
assert file_count == 362
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_ucc_1/default/app.conf" in all_files
assert "my_app_ucc_1/appserver/static/js/build/globalConfig.json" in all_files
assert "my_app_ucc_1/lib/splunklib/client.py" in all_files
Expand All @@ -191,6 +201,11 @@ def test_ucc_build_repo_root_as_app_dir(self):
assert all("my_app_ucc_1/globalConfig.json" not in s for s in all_files), "'globalConfig.json' file shouldn't be in the root of the App."
assert all("additional_packaging.py" not in s for s in all_files), "'additional_packaging.py' file shouldn't be part of the App build."

assert "my_app_ucc_1/lib/splunklib/modularinput" in all_folders
assert "my_app_ucc_1/lib/solnlib" in all_folders
assert "my_app_ucc_1/appserver/static/js/build" in all_folders
assert "my_app_ucc_1/lib/splunktaucclib/common" in all_folders


def test_file_permission_check_no_change(self):
with setup_action_yml("repo_file_permission", app_dir="my_app_2", is_app_inspect_check="false"):
Expand All @@ -199,7 +214,7 @@ def test_file_permission_check_no_change(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

_, _, all_files = self.extract_app_build(app_build_name)
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand All @@ -217,7 +232,7 @@ def test_file_auto_change_permission(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

file_count, folder_count, all_files = self.extract_app_build(app_build_name)
file_count, folder_count, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand All @@ -235,7 +250,7 @@ def test_file_permission_check_no_change_2(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

_, _, all_files = self.extract_app_build(app_build_name)
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand All @@ -253,7 +268,7 @@ def test_file_auto_change_permission_2(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

_, _, all_files = self.extract_app_build(app_build_name)
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand All @@ -279,7 +294,7 @@ def test_file_permission_change_via_user_commands(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

_, _, all_files = self.extract_app_build(app_build_name)
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand All @@ -305,7 +320,7 @@ def test_file_permission_change_via_user_commands_root_dir(self):
app_build_name = "my_app_2_1_1_2_1.tgz"
assert os.path.isfile(app_build_name)

_, _, all_files = self.extract_app_build(app_build_name)
_, _, all_files, all_folders = self.extract_app_build(app_build_name)
assert "my_app_2/bin/file1.sh" in all_files
assert get_file_permissions("my_app_2/bin/file1.sh") == "rwxr-xr-x"
assert "my_app_2/bin/file2.sh" in all_files
Expand Down

0 comments on commit 8b45dc8

Please sign in to comment.