Skip to content

Commit

Permalink
Add unit tests for the cassandra/schema/create.sh script (#6594)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Subpart of the #6582 

## Description of the changes
- created unit tests for the create.sh file , to test for various cases
such as presence of Mode parameter , output for specific parameters

## How was this change tested?
- 

## Checklist
- [X] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [X] I have signed all commits
- [X] I have added unit tests for the new functionality
- [ ] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `npm run lint` and `npm run test`

---------

Signed-off-by: asim <aktech701@gmail.com>
  • Loading branch information
asimchoudhary authored Jan 25, 2025
1 parent 5c9f16d commit c2ea27a
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-lint-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:

- name: Run unit tests for scripts
run: |
SHUNIT2=.tools/shunit2 bash scripts/utils/compute-tags.test.sh
SHUNIT2=.tools/shunit2 bash scripts/utils/run-tests.sh
binary-size-check:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -183,4 +183,4 @@ jobs:
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 #v4.2.0
with:
path: ./jaeger_binary_size.txt
key: jaeger_binary_size_${{ github.run_id }}
key: jaeger_binary_size_${{ github.run_id }}
86 changes: 86 additions & 0 deletions plugin/storage/cassandra/schema/create.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
# Copyright (c) 2025 The Jaeger Authors.
# SPDX-License-Identifier: Apache-2.0

# This script uses https://github.com/kward/shunit2 to run unit tests.
# The path to this repo must be provided via SHUNIT2 env var.

SHUNIT2="${SHUNIT2:?'expecting SHUNIT2 env var pointing to a dir with https://github.com/kward/shunit2 clone'}"


createScript="$(dirname $0)/create.sh"


unset MODE
unset DATACENTER
unset KEYSPACE
unset REPLICATION
unset REPLICATION_FACTOR
unset TRACE_TTL
unset DEPENDENCIES_TTL
unset COMPACTION_WINDOW
unset VERSION

testRequireMode() {
err=$(bash "$createScript" 2>&1)
assertContains "$err" "missing MODE parameter"
}

testInvalidMode() {
err=$(MODE=invalid bash "$createScript" 2>&1)
assertContains "$err" "invalid MODE=invalid, expecting 'prod' or 'test'"
}

testProdModeRequiresDatacenter() {
err=$(MODE=prod bash "$createScript" 2>&1)
assertContains "$err" "missing DATACENTER parameter for prod mode"
}

testProdModeWithDatacenter() {
out=$(MODE=prod DATACENTER=dc1 bash "$createScript" 2>&1)
assertContains "$out" "mode = prod"
assertContains "$out" "datacenter = dc1"
assertContains "$out" "replication = {'class': 'NetworkTopologyStrategy', 'dc1': '2' }"
}

testTestMode() {
out=$(MODE=test bash "$createScript" 2>&1)
assertContains "$out" "mode = test"
assertContains "$out" "datacenter = test"
assertContains "$out" "replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}"
}

testCustomTTL() {
out=$(MODE=test TRACE_TTL=86400 DEPENDENCIES_TTL=172800 bash "$createScript" 2>&1)
assertContains "$out" "trace_ttl = 86400"
assertContains "$out" "dependencies_ttl = 172800"
}

testInvalidKeyspace() {
err=$(MODE=test KEYSPACE=invalid-keyspace bash "$createScript" 2>&1)
assertContains "$err" "invalid characters in KEYSPACE"
}

testValidKeyspace() {
out=$(MODE=test KEYSPACE=valid_keyspace_123 bash "$createScript" 2>&1)
assertContains "$out" "keyspace = valid_keyspace_123"
}

testCustomCompactionWindow() {
out=$(MODE=test COMPACTION_WINDOW=24h bash "$createScript" 2>&1)
assertContains "$out" "compaction_window_size = 24"
assertContains "$out" "compaction_window_unit = HOURS"
}

testInvalidCompactionWindow() {
err=$(MODE=test COMPACTION_WINDOW=24x bash "$createScript" 2>&1)
assertContains "$err" "Invalid compaction window size format"
}

testCustomVersion() {
out=$(MODE=test VERSION=3 bash "$createScript" 2>&1)
assertContains "$out" "v003.cql.tmpl"
}


source "${SHUNIT2}/shunit2"
71 changes: 71 additions & 0 deletions scripts/utils/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

# Copyright (c) 2025 The Jaeger Authors.
# SPDX-License-Identifier: Apache-2.0


UTILS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$UTILS_DIR/../.."

# Define list of test files explicitly here , to be dynamic to the location of the test file
TEST_FILES=(
"$UTILS_DIR/compute-tags.test.sh"
"$REPO_ROOT/plugin/storage/cassandra/schema/create.test.sh"
)

run_test_file() {
local test_file="$1"
if [ ! -f "$test_file" ]; then
echo "Error: Test file not found: $test_file"
return 1
fi

echo "Running tests from: $test_file"

export SHUNIT2="${SHUNIT2:?'SHUNIT2 environment variable must be set'}"

bash "$test_file"
local result=$?
echo "Test file $test_file completed with status: $result"
return $result
}

main() {

if [ ! -f "${SHUNIT2}/shunit2" ]; then
echo "Error: shunit2 not found at ${SHUNIT2}/shunit2"
exit 1
fi
local failed=0
local total=0
local passed=0
local failed_tests=()

# Run all test files
for test_file in "${TEST_FILES[@]}"; do
((total++))
if ! run_test_file "$test_file"; then
failed=1
failed_tests+=("$test_file")
else
((passed++))
fi
done

echo "-------------------"
echo "Test Summary:"
echo "Total: $total"
echo "Passed: $passed"
echo "Failed: $((total - passed))"

if [ ${#failed_tests[@]} -gt 0 ]; then
echo "Failed tests:"
for test in "${failed_tests[@]}"; do
echo " - $(basename "$test")"
done
fi

exit $failed
}

main

0 comments on commit c2ea27a

Please sign in to comment.