Skip to content

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

Merged
merged 6 commits into from
Mar 13, 2025

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Mar 13, 2025

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.

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.
@mafredri mafredri self-assigned this Mar 13, 2025
@mafredri mafredri requested review from johnstcn and mtojek March 13, 2025 15:57
@mafredri mafredri force-pushed the mafredri/tf-testdata-minimize-diff branch from 162fbc3 to fea7f9e Compare March 13, 2025 16:14
Comment on lines 26 to 66
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
}
Copy link
Member

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)'

Copy link
Member Author

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) 🤔.

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stamp 👍

@mafredri mafredri merged commit a1f5468 into main Mar 13, 2025
30 checks passed
@mafredri mafredri deleted the mafredri/tf-testdata-minimize-diff branch March 13, 2025 18:13
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants