Skip to content

Commit d84cf6d

Browse files
Merge pull request #54 from fred-lefebvre/main
Some housekeeping changes and new script to make regression testing easier
2 parents fc9ff74 + a04c331 commit d84cf6d

File tree

3 files changed

+107
-52
lines changed

3 files changed

+107
-52
lines changed

ec2-metadata

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ QUIET=""
5353
function set_imds_token()
5454
{
5555
if [ -z "${IMDS_TOKEN}" ];then
56-
IMDS_TOKEN=$(curl -s -f -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 900" ${METADATA_BASEURL}/${METADATA_TOKEN_PATH})
56+
IMDS_TOKEN=$(curl -s -f -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 900" "${METADATA_BASEURL}/${METADATA_TOKEN_PATH}")
5757
if [ "${?}" -gt 0 ] || [ -z "${IMDS_TOKEN}" ]; then
5858
echo '[ERROR] Could not get IMDSv2 token. Instance Metadata might have been disabled or this is not an EC2 instance.' >&2
5959
exit 1
@@ -65,15 +65,15 @@ function set_imds_token()
6565
function get_meta()
6666
{
6767
local imds_out
68-
imds_out=$(curl -s -q -H "X-aws-ec2-metadata-token:${IMDS_TOKEN}" -f ${METADATA_BASEURL}/${METADATA_VERSION}/${1})
68+
imds_out=$(curl -s -q -H "X-aws-ec2-metadata-token:${IMDS_TOKEN}" -f "${METADATA_BASEURL}/${METADATA_VERSION}/${1}")
6969
echo -n "${imds_out}"
7070
}
7171

7272
#print standard metric
7373
function print_normal_metric() {
7474
metric_path=$2
75-
[ -z "$QUIET" ] && echo -n $1": "
76-
RESPONSE=$(get_meta ${metric_path})
75+
[ -z "$QUIET" ] && echo -n "$1: "
76+
RESPONSE=$(get_meta "${metric_path}")
7777
if [ -n "${RESPONSE}" ]; then
7878
echo "$RESPONSE"
7979
else
@@ -82,35 +82,35 @@ function print_normal_metric() {
8282
}
8383

8484
#print block-device-mapping
85-
function print_block-device-mapping()
85+
function print_block_device_mapping()
8686
{
8787
[ -z "$QUIET" ] && echo 'block-device-mapping: '
8888
x=$(get_meta meta-data/block-device-mapping/)
8989
if [ -n "${x}" ]; then
9090
for i in $x; do
91-
[ -z "$QUIET" ] && echo -ne '\t' "$i: "
92-
echo "$(get_meta meta-data/block-device-mapping/$i)"
91+
[ -z "$QUIET" ] && echo -ne '\t' "$i: "
92+
echo "$(get_meta "meta-data/block-device-mapping/$i")"
9393
done
9494
else
9595
echo not available
9696
fi
9797
}
9898

9999
#print public-keys
100-
function print_public-keys()
100+
function print_public_keys()
101101
{
102102
[ -z "$QUIET" ] && echo 'public-keys: '
103103
x=$(get_meta meta-data/public-keys/)
104104
if [ -n "${x}" ]; then
105105
for i in $x; do
106-
index=$(echo $i|cut -d = -f 1)
107-
keyname=$(echo $i|cut -d = -f 2)
108-
[ -z "$QUIET" ] && echo keyname:$keyname
109-
[ -z "$QUIET" ] && echo index:$index
110-
format=$(get_meta meta-data/public-keys/$index/)
111-
[ -z "$QUIET" ] && echo format:$format
106+
index=$(echo "$i" | cut -d = -f 1)
107+
keyname=$(echo "$i" | cut -d = -f 2)
108+
[ -z "$QUIET" ] && echo "keyname:$keyname"
109+
[ -z "$QUIET" ] && echo "index:$index"
110+
format=$(get_meta "meta-data/public-keys/$index/")
111+
[ -z "$QUIET" ] && echo "format:$format"
112112
[ -z "$QUIET" ] && echo 'key:(begins from next line)'
113-
echo "$(get_meta meta-data/public-keys/$index/$format)"
113+
echo "$(get_meta "meta-data/public-keys/$index/$format")"
114114
done
115115
else
116116
echo not available
@@ -124,8 +124,8 @@ function print_tags()
124124
x=$(get_meta meta-data/tags/instance/)
125125
if [ -n "${x}" ]; then
126126
for i in $x; do
127-
echo -n -e '\t' "$i: "
128-
echo "$(get_meta meta-data/tags/instance/$i)"
127+
echo -n -e '\t' "$i: "
128+
echo "$(get_meta "meta-data/tags/instance/$i")"
129129
done
130130
else
131131
echo not available
@@ -138,7 +138,7 @@ function print_all()
138138
print_normal_metric ami-launch-index meta-data/ami-launch-index
139139
print_normal_metric ami-manifest-path meta-data/ami-manifest-path
140140
print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids
141-
print_block-device-mapping
141+
print_block_device_mapping
142142
print_normal_metric instance-id meta-data/instance-id
143143
print_normal_metric instance-type meta-data/instance-type
144144
print_normal_metric local-hostname meta-data/local-hostname
@@ -150,7 +150,7 @@ function print_all()
150150
print_normal_metric product-codes meta-data/product-codes
151151
print_normal_metric public-hostname meta-data/public-hostname
152152
print_normal_metric public-ipv4 meta-data/public-ipv4
153-
print_public-keys
153+
print_public_keys
154154
print_normal_metric ramdisk-id /meta-data/ramdisk-id
155155
print_normal_metric reservation-id /meta-data/reservation-id
156156
print_normal_metric security-groups meta-data/security-groups
@@ -191,49 +191,49 @@ declare -a actions
191191
declare -a paths
192192
shortopts=almnbithokzPcpvuresdgR
193193
longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping
194-
instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone
195-
partition product-codes public-hostname public-ipv4 public-keys ramdisk-id
196-
reservation-id security-groups user-data tags region help all quiet path:)
194+
instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone
195+
partition product-codes public-hostname public-ipv4 public-keys ramdisk-id
196+
reservation-id security-groups user-data tags region help all quiet path:)
197197

198198
oldIFS="$IFS"
199199
IFS=,
200200
TEMP=$(getopt -o $shortopts --longoptions "${longopts[*]}" -n 'ec2-metadata' -- "$@")
201201
if [ $? -ne 0 ]; then
202-
echo 'Terminating...' >&2
203-
exit 1
202+
echo 'Terminating...' >&2
203+
exit 1
204204
fi
205205
IFS="$oldIFS"
206206

207207
eval set -- "$TEMP"
208208
unset TEMP
209209

210210
while true; do
211-
case "$1" in
212-
--help)
213-
print_help ; shift
214-
exit 0
215-
;;
216-
--quiet)
217-
QUIET=1 ; shift
218-
;;
219-
--path)
220-
actions+=("$1")
221-
paths+=("$2")
222-
shift 2
223-
;;
224-
--)
225-
shift ; break
226-
;;
227-
--?*|-?)
228-
# pass most arguments to the original action processing
229-
# code after setting options
230-
actions+=("$1"); shift
231-
;;
232-
*)
233-
echo 'Unknown error: ' "[$1]" >&2
234-
exit 1
235-
;;
236-
esac
211+
case "$1" in
212+
--help)
213+
print_help ; shift
214+
exit 0
215+
;;
216+
--quiet)
217+
QUIET=1 ; shift
218+
;;
219+
--path)
220+
actions+=("$1")
221+
paths+=("$2")
222+
shift 2
223+
;;
224+
--)
225+
shift ; break
226+
;;
227+
--?*|-?)
228+
# pass most arguments to the original action processing
229+
# code after setting options
230+
actions+=("$1"); shift
231+
;;
232+
*)
233+
echo 'Unknown error: ' "[$1]" >&2
234+
exit 1
235+
;;
236+
esac
237237
done
238238

239239
#start processing command line arguments
@@ -243,7 +243,7 @@ for action in "${actions[@]}"; do
243243
-l | --ami-launch-index ) print_normal_metric ami-launch-index meta-data/ami-launch-index ;;
244244
-m | --ami-manifest-path ) print_normal_metric ami-manifest-path meta-data/ami-manifest-path ;;
245245
-n | --ancestor-ami-ids ) print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids ;;
246-
-b | --block-device-mapping ) print_block-device-mapping ;;
246+
-b | --block-device-mapping ) print_block_device_mapping ;;
247247
-i | --instance-id ) print_normal_metric instance-id meta-data/instance-id ;;
248248
-t | --instance-type ) print_normal_metric instance-type meta-data/instance-type ;;
249249
-h | --local-hostname ) print_normal_metric local-hostname meta-data/local-hostname ;;
@@ -255,7 +255,7 @@ for action in "${actions[@]}"; do
255255
-c | --product-codes ) print_normal_metric product-codes meta-data/product-codes ;;
256256
-p | --public-hostname ) print_normal_metric public-hostname meta-data/public-hostname ;;
257257
-v | --public-ipv4 ) print_normal_metric public-ipv4 meta-data/public-ipv4 ;;
258-
-u | --public-keys ) print_public-keys ;;
258+
-u | --public-keys ) print_public_keys ;;
259259
-r | --ramdisk-id ) print_normal_metric ramdisk-id /meta-data/ramdisk-id ;;
260260
-e | --reservation-id ) print_normal_metric reservation-id /meta-data/reservation-id ;;
261261
-s | --security-groups ) print_normal_metric security-groups meta-data/security-groups ;;

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)