Skip to content

Commit a04c331

Browse files
committed
Add tests/capture-output.sh to help with regression testing
1 parent 4e35161 commit a04c331

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

tests/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ec2-metadata regression testing
2+
3+
`capture-output.sh` runs every `ec2-metadata` subcommand and saves each output to a separate file. This lets you compare output before and after a code change to catch regressions.
4+
5+
## Usage
6+
7+
```bash
8+
# 1. Capture baseline output (before your changes)
9+
./capture-output.sh output-before
10+
11+
# 2. Make your changes to ec2-metadata
12+
13+
# 3. Capture output again
14+
./capture-output.sh output-after
15+
16+
# 4. Compare
17+
diff -r output-before output-after
18+
```
19+
20+
No diff output means the change is behavior-preserving. Any differences will show exactly which subcommand's output changed and how.
21+
22+
## Notes
23+
24+
- Must be run on an EC2 instance (requires IMDS access).
25+
- The subcommand list is derived dynamically from `ec2-metadata --help`, so new options are picked up automatically.
26+
- Individual subcommand failures are logged as warnings but don't stop the run, so you get partial results even if some metadata categories are unavailable.

tests/capture-output.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/bash
2+
# Capture ec2-metadata output for each subcommand into a directory.
3+
# Usage: ./capture-output.sh [output-dir]
4+
# default output-dir: output
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
9+
EC2_METADATA="${SCRIPT_DIR}/../ec2-metadata"
10+
OUTDIR="${1:-output}"
11+
12+
[[ -x "$EC2_METADATA" ]] || { echo "ERROR: ec2-metadata not found at $EC2_METADATA" >&2; exit 1; }
13+
14+
mapfile -t cmds < <("$EC2_METADATA" --help | grep -oP '(?<=/--)[a-z][-a-z0-9]+' | grep -v 'help\|quiet\|all\|path')
15+
if [[ ${#cmds[@]} -eq 0 ]]; then
16+
echo "ERROR: No subcommands found in --help output" >&2
17+
exit 1
18+
fi
19+
20+
mkdir -p "$OUTDIR"
21+
22+
for cmd in "${cmds[@]}"; do
23+
"$EC2_METADATA" --"$cmd" > "$OUTDIR/$cmd.txt" 2>&1 || echo "WARN: --$cmd failed" >&2
24+
done
25+
26+
"$EC2_METADATA" > "$OUTDIR/default.txt" 2>&1 || echo "WARN: default (no args) failed" >&2
27+
28+
files=("$OUTDIR"/*.txt)
29+
echo "Captured ${#files[@]} files in $OUTDIR/"

0 commit comments

Comments
 (0)