Skip to content

Commit

Permalink
Add unit tests using shunit2
Browse files Browse the repository at this point in the history
  • Loading branch information
hailihu committed Nov 27, 2024
1 parent a1df749 commit a21b75d
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Unit Tests

permissions:
contents: read

on: [push]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout shell test framework shUnit2
uses: actions/checkout@v4
with:
repository: kward/shunit2
path: ${{github.workspace}}/tests/shunit2
fetch-depth: 1
- name: Run unit tests
shell: bash
run: |
export PATH="tests/shunit2:$PATH"
tests/unit_test.sh
115 changes: 115 additions & 0 deletions tests/unit_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env bash
# file: tests/unit_test.sh


test_urlencode() {
result=`urlencode "/test/a/b/c"`
expected="%2Ftest%2Fa%2Fb%2Fc"
assertEquals \
"the result of url_encode() was wrong" \
"${expected}" "${result}"
}


test_pathtype() {
result=`pathtype "/test/a/b/c"`
expected='curl "${curl_authorization[@]}" \
"${curl_options_no_errors[@]}" \
-X GET "$api/namespace/$path" \
| jq -r .fileType'
assertEquals \
"the result of pathtype() was wrong" \
"${expected}" "${result}"
}


test_create_path() {
# Check error handling when incorrect path is given
counter=0
( create_path "/test/a" true >${stdoutF} 2>${stderrF} )
result=$?
assertFalse "expecting return code of 1 (false)" ${result}
grep "ERROR: Unable to create dirs. Check the specified path" "${stderrF}" >/dev/null
assertTrue "STDERR message incorrect" $?

# Check error handling when parent does not exist
( create_path "/test/a" false >${stdoutF} 2>${stderrF} )
result=$?
assertFalse "expecting return code of 1 (false)" ${result}
grep "ERROR: parent dir '/test' does not exist. To recursivly create dirs, add --recursive" "${stderrF}" >/dev/null
assertTrue 'STDERR message incorrect' $?

# Check error handling when max number of directories is exceeded
counter=10
( create_path "/test/a" true >${stdoutF} 2>${stderrF} )
result=$?
assertFalse "expecting return code of 1 (false)" ${result}
grep "ERROR: max number of directories that can be created at once is 10" "${stderrF}" >/dev/null
assertTrue "STDERR message incorrect" $?
}


test_get_pnfsid() {
result=`get_pnfsid "/test/a/b/c"`
expected='curl "${curl_authorization[@]}" \
"${curl_options_no_errors[@]}" \
-X GET "$api/namespace/$path" \
| jq -r .pnfsId'
assertEquals \
"the result of get_pnfsid() was wrong" \
"${expected}" "${result}"
}


test_is_online() {
result=`is_online "/test/a/b/c"`
expected='curl "${curl_authorization[@]}" \
"${curl_options_no_errors[@]}" \
-X GET "$api/namespace/$path?locality=true&qos=true" \
| jq -r ".fileLocality" \
| grep --silent "ONLINE"'
assertEquals \
"the result of is_online() was wrong" \
"${expected}" "${result}"
}


test_get_subdirs() {
result=`get_subdirs "/test/a/b/c"`
expected='curl "${curl_authorization[@]}" \
"${curl_options_common[@]}" \
-X GET "$api/namespace/$path?children=true" \
| jq -r "$str"'
assertEquals \
"the result of get_subdirs() was wrong" \
"${expected}" "${result}"
}


test_get_files_in_dir() {
result=`get_files_in_dir "/test/a/b/c"`
expected='curl "${curl_authorization[@]}" \
"${curl_options_common[@]}" \
-X GET "$api/namespace/$path?children=true" \
| jq -r "$str"'
assertEquals \
"the result of get_files_in_dir() was wrong" \
"${expected}" "${result}"
}


oneTimeSetUp() {
outputDir="${SHUNIT_TMPDIR}/output"
mkdir "${outputDir}"
stdoutF="${outputDir}/stdout"
stderrF="${outputDir}/stderr"
debug=false
dry_run=true

# Load functions to test
. ada/ada_functions.inc
}


# Load and run shunit2
. shunit2

0 comments on commit a21b75d

Please sign in to comment.