Skip to content

Commit d8e1000

Browse files
committed
feat(test/compare.sh): Add verification
Signed-off-by: Oliver Gondža <ogondza@gmail.com>
1 parent 25ff78f commit d8e1000

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ Note: most of the resource outputs are given in 3 file types: `.json`, `.yaml`,
184184

185185
## Testing
186186

187-
You can run the script locally from your workstation.
188-
To do that you need an OpenShift cluster and you will have to install the Red Hat GitOps Operator.
189-
Then you can run the script like this:
187+
To do that you need an OpenShift cluster, and you will have to install the Red Hat GitOps Operator.
188+
Then you can test how your changes affects gathered data:
190189

191190
```shell
192-
chmod +x ./gather_gitops.sh
193-
./gather_gitops.sh --base-collection-path .
191+
# You may need to create the repository on quay.io manually to make sure it is public
192+
make REGISTRY_USERNAME=my-non-production-org CONTAINER_IMAGE_TAG="$(git rev-parse HEAD)" push
193+
# Note some differences are expected, like few lines in rapidly populated logs
194+
./test/compare.sh registry.redhat.io/openshift-gitops-1/must-gather-rhel8:"$SOME_OLD_VERSION" quay.io/my-non-production-org/gitops-must-gather:"$(git rev-parse HEAD)"
194195
```
195196

196197
Last but not least, please make sure you run `make lint` before pushing new changes.

test/compare.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
# https://github.com/olivergondza/bash-strict-mode
3+
set -eEuo pipefail
4+
trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
5+
6+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
7+
8+
function main() {
9+
if [[ $# -ne 2 ]]; then
10+
echo >&2 "Usage: $0 [IMAGE_A] [IMAGE_B]"
11+
exit 1
12+
fi
13+
local img_a=$1
14+
local img_b=$2
15+
16+
inv_a="$(mktemp -d gitops-must-gather-A-XXXX)"
17+
inv_b="$(mktemp -d gitops-must-gather-B-XXXX)"
18+
trap "rm -rf '${inv_a}' '${inv_b}'" EXIT
19+
20+
gather "$img_a" "$inv_a"
21+
gather "$img_b" "$inv_b"
22+
23+
diff --color=auto --recursive \
24+
--ignore-matching-lines="resourceVersion: " \
25+
"${inv_a}" "${inv_b}"
26+
}
27+
28+
function gather() {
29+
image=$1
30+
dir=$2
31+
32+
if ! oc adm must-gather --image="$image" --dest-dir="${dir}" 2>&1 | tee "${dir}/oc-adm-output.log"; then
33+
echo >&2 "Failed gathering for $image"
34+
return 1
35+
fi
36+
37+
sanitize "$image" "$dir"
38+
}
39+
40+
function sanitize() {
41+
image=$1
42+
dir=$2
43+
44+
# Unify names of the directories its name is based on image name
45+
mv "$dir"/*-sha256-* "$dir/__RESOURCES__"
46+
47+
# In log files, drop image name, generated resource names, timestamp, line numbers, and transfer metrics
48+
sed -i -r \
49+
-e "s~${image}~__IMAGE_TAG__~g" \
50+
-e "s~must-gather-[a-z0-9]{5}~must-gather-XXXXX~g" \
51+
-e 's~[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+Z~__TIMESTAMP__~g' \
52+
-e 's~gather_gitops:[0-9]+~gather_gitops:LL~g' \
53+
-e '/total size is .* speedup is .*/d' \
54+
-e '/sent .* received .* bytes\/sec/d' \
55+
"$dir/oc-adm-output.log" "$dir/must-gather.logs" "$dir/__RESOURCES__/gather.logs" "$dir/__RESOURCES__/gather_gitops.log"
56+
57+
# Timestamps are not going to match, just test there is the same number of them
58+
wc -l < "$dir/timestamp" > "$dir/timestamp"
59+
wc -l < "$dir/__RESOURCES__/timestamp" > "$dir/__RESOURCES__/timestamp"
60+
}
61+
62+
main "$@"

0 commit comments

Comments
 (0)