-
Notifications
You must be signed in to change notification settings - Fork 875
chore(provisioner/terraform): minimize testdata diff #16908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It was hard to deduce whether or not changes in our terraform testdata are relevant or not, so we now have a rudimentary filter for randomly generated values that aren't relevant for the testdata.
162fbc3
to
fea7f9e
Compare
minimize_diff() { | ||
for f in *.tf*.json; do | ||
declare -A deleted=() | ||
declare -a sed_args=() | ||
while read -r line; do | ||
# Deleted line (previous value). | ||
if [[ $line = -\ * ]]; then | ||
key="${line#*\"}" | ||
key="${key%%\"*}" | ||
value="${line#*: }" | ||
value="${value#*\"}" | ||
value="${value%\"*}" | ||
declare deleted["$key"]="$value" | ||
# Added line (new value). | ||
elif [[ $line = +\ * ]]; then | ||
key="${line#*\"}" | ||
key="${key%%\"*}" | ||
value="${line#*: }" | ||
value="${value#*\"}" | ||
value="${value%\"*}" | ||
# Matched key, restore the value. | ||
if [[ -v deleted["$key"] ]]; then | ||
sed_args+=(-e "s|${value}|${deleted["$key"]}|g") | ||
unset deleted["$key"] | ||
fi | ||
fi | ||
if [[ ${#sed_args[@]} -gt 0 ]]; then | ||
# Handle macOS compat. | ||
if sed -h 2>&1 | grep -q "\[-i extension\]"; then | ||
sed -i '' "${sed_args[@]}" "$f" | ||
else | ||
sed -i'' "${sed_args[@]}" "$f" | ||
fi | ||
fi | ||
done < <( | ||
# Filter out known keys with autogenerated values. | ||
git diff -- "$f" | | ||
grep -E "\"(terraform_version|id|agent_id|resource_id|token|random|timestamp)\":" | ||
) | ||
done | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be performed by jq
using walk
or similar?
jq 'walk(if type == "object" then with_entries(select(.key | test("(terraform_version|id|agent_id|resource_id|token|random|timestamp)") | not)) else . end)'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would give you the current value, but not the previous (or whether or not it changed) 🤔.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stamp 👍
It was hard to deduce whether or not changes in our terraform testdata
are relevant or not, so we now have a rudimentary filter for randomly
generated values that aren't relevant for the testdata.
With this change we could technically run this in CI as well, I believe.