This directory contains unit tests for all shell scripts in the scripts/shell directory.
The test suite uses bats-core (Bash Automated Testing System) to validate the functionality and structure of security scanning scripts.
Unit tests are provided for the following scripts:
- run-trivy-scan.sh - Container and filesystem vulnerability scanning
- run-grype-scan.sh - Multi-target vulnerability scanning
- run-checkov-scan.sh - Infrastructure-as-Code security scanning
- run-clamav-scan.sh - Antivirus and malware scanning
- run-trufflehog-scan.sh - Secret detection scanning
- run-xeol-scan.sh - End-of-Life software detection
- run-sbom-scan.sh - Software Bill of Materials generation
- run-helm-build.sh - Helm chart building and validation
- run-anchore-scan.sh - Anchore security analysis (placeholder)
- run-sonar-analysis.sh - SonarQube code quality analysis
- run-target-security-scan.sh - Main orchestrator script
- scan-directory-template.sh - Shared template functions
The test framework requires bats-core to be installed. Install it using one of these methods:
sudo apt-get install batsbrew install bats-coregit clone https://github.com/bats-core/bats-core.git
cd bats-core
sudo ./install.sh /usr/localVerify installation:
bats --versionExecute all unit tests with a single command:
cd tests/shell
./run-tests.shTest a specific script:
cd tests/shell
bats test-run-trivy-scan.bats
bats test-run-grype-scan.batsYou can also filter tests using pattern matching:
bats test-run-*.batsEach test file follows this structure:
#!/usr/bin/env bats
# Unit tests for <script-name>.sh
SCRIPT_DIR="/path/to/scripts/shell"
SCRIPT_PATH="${SCRIPT_DIR}/<script-name>.sh"
@test "test description" {
# Test assertions
[ -f "$SCRIPT_PATH" ]
grep -q "pattern" "$SCRIPT_PATH"
}The unit tests verify:
-
File Existence & Permissions
- Script files exist
- Scripts are executable
-
Script Structure
- Proper shebang (
#!/bin/bash) - Required functions are defined
- Color variables are defined
- Proper shebang (
-
Dependencies
- Scripts source required templates
- Scripts call initialization functions
-
Docker Integration
- Scripts use Docker for security scanning
- Scripts check for Docker availability
-
Help Documentation
- Help flags (
-h,--help) work correctly - Help text is informative
- Help flags (
-
Tool-Specific Features
- Correct Docker images are used
- Proper scan modes are supported
- Environment variables are handled
Example output:
=========================================
Shell Script Unit Test Runner
=========================================
✅ bats is installed: Bats 1.13.0
Found 12 test files
Running all unit tests...
1..107
ok 1 run-anchore-scan.sh exists and is executable
ok 2 run-anchore-scan.sh has proper shebang
...
ok 107 scan-directory-template.sh count_scannable_files excludes .git
=========================================
✅ All tests passed!
=========================================
To add tests for a new script:
- Create a new test file:
test-<script-name>.bats - Copy the template from an existing test file
- Update the
SCRIPT_PATHvariable - Add relevant test cases
- Run tests to verify
Example test case:
@test "script-name.sh has proper shebang" {
head -n 1 "$SCRIPT_PATH" | grep -q "^#!/bin/bash"
}
@test "script-name.sh defines required functions" {
grep -q "function_name()" "$SCRIPT_PATH"
}These tests can be integrated into CI/CD pipelines:
# Example GitHub Actions workflow
- name: Run Shell Script Tests
run: |
cd tests/shell
./run-tests.shInstall bats-core as described in Prerequisites section.
Make test runner executable:
chmod +x tests/shell/run-tests.shCheck if the script being tested has changed. Update test expectations to match current script behavior.
- Total Tests: 107
- Test Files: 12
- Scripts Covered: 12
- Success Rate: 100%
When modifying shell scripts in scripts/shell/:
- Update or add corresponding tests
- Run the test suite to verify changes
- Ensure all tests pass before committing
- Update test documentation as needed