@@ -60,6 +60,10 @@ function verify_arn_exists() {
6060 aws ec2 describe-volumes --region " $check_region " --volume-ids " $resource_id " --filters " Name=status,Values=available,in-use" & > /dev/null
6161 return $?
6262 ;;
63+ snapshot)
64+ aws ec2 describe-snapshots --region " $check_region " --snapshot-ids " $resource_id " & > /dev/null
65+ return $?
66+ ;;
6367 security-group)
6468 aws ec2 describe-security-groups --region " $check_region " --group-ids " $resource_id " & > /dev/null
6569 return $?
239243# Combine all results into a single TAGGED_RESOURCES variable
240244# Merge ResourceTagMappingList arrays from all responses and remove duplicates by ResourceARN
241245if [[ ${# TAGGED_RESOURCES_LIST[@]} -gt 0 ]]; then
242- TAGGED_RESOURCES=$( jq -n --argjson results " $( printf ' %s\n' " ${TAGGED_RESOURCES_LIST[@]} " | jq -s ' .' ) " \
243- ' {ResourceTagMappingList: ($results | map(.ResourceTagMappingList) | flatten | unique_by(.ResourceARN))}' )
246+ TAGGED_RESOURCES=$( printf ' %s\n' " ${TAGGED_RESOURCES_LIST[@]} " | jq -s ' {ResourceTagMappingList: (map(.ResourceTagMappingList) | flatten | unique_by(.ResourceARN))}' )
244247else
245248 TAGGED_RESOURCES=' {"ResourceTagMappingList":[]}'
246249fi
250253run_command " aws iam list-users --query 'Users[?starts_with(UserName, \` $CLUSTER_NAME \` )].Arn'" " IAM_USERS"
251254
252255# Combine tagged resources and IAM users into a single array of ARNs
253- LEAKED_ARNS=$( jq -n --argjson tagged " $TAGGED_RESOURCES " --argjson iam " $IAM_USERS " ' $tagged. ResourceTagMappingList | map(.ResourceARN) + $iam ' )
256+ LEAKED_ARNS=$( printf ' %s\n ' " $TAGGED_RESOURCES " " $IAM_USERS " | jq -s ' ((.[0]. ResourceTagMappingList // []) | map(.ResourceARN)) + (.[1] // []) ' )
254257
255258echo " Confirming that the ARNs we discovered have not actually been deleted..."
256259VERIFIED_ARNS=()
@@ -290,21 +293,53 @@ DNS_RECORD_COUNT=$(echo "${DNS_RECORDS:-[]}" | jq 'length')
290293
291294TOTAL_LEAKED=$(( RESOURCE_COUNT + DNS_RECORD_COUNT))
292295
293- if [[ " $TOTAL_LEAKED " -gt 0 ]] ; then
294- echo " " >&2
295- echo " Test Failed: Found $TOTAL_LEAKED leaked resources ( $RESOURCE_COUNT ARNs, $DNS_RECORD_COUNT DNS records) " >&2
296+ function xmlescape() {
297+ echo -n " $1 " | sed ' s/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/ ' " ' " ' /\'/g '
298+ }
296299
300+ function generate_failure_report() {
301+ echo " Found $TOTAL_LEAKED leaked resources ($RESOURCE_COUNT ARNs, $DNS_RECORD_COUNT DNS records)"
297302 if [[ " $RESOURCE_COUNT " -gt 0 ]]; then
298- echo " Leaked ARNs:" >&2
299- echo " $LEAKED_ARNS " | jq -r ' .[]' >&2
303+ echo " Leaked ARNs:"
304+ echo " $LEAKED_ARNS " | jq -r ' .[]'
300305 fi
301-
302306 if [[ " $DNS_RECORD_COUNT " -gt 0 ]]; then
303- echo " " >&2
304- echo " Leaked DNS Records:" >&2
305- echo " $DNS_RECORDS " | jq -r ' .[] | .Name' | sed ' s/\\052/*/g' >&2
307+ echo " Leaked DNS Records:"
308+ echo " $DNS_RECORDS " | jq -r ' .[] | .Name' | sed ' s/\\052/*/g'
306309 fi
310+ }
307311
312+ function createDeprovisionJunit() {
313+ local testcase_xml
314+ local failures=0
315+ if [[ " $TOTAL_LEAKED " -gt 0 ]]; then
316+ failures=$(( failures+ 1 ))
317+ local failure_message=" Found $TOTAL_LEAKED leaked resources"
318+ local failure_output
319+ failure_output=$( generate_failure_report)
320+ testcase_xml=$( cat << INNER_EOF
321+ <testcase name="destroy should succeed">
322+ <failure message="${failure_message} ">$( xmlescape " ${failure_output} " ) </failure>
323+ </testcase>
324+ INNER_EOF
325+ )
326+ else
327+ testcase_xml=' <testcase name="destroy should succeed"/>'
328+ fi
329+
330+ cat > " ${ARTIFACT_DIR} /junit_deprovision.xml" << EOF
331+ <testsuite name="cluster install" tests="1" failures="${failures} ">
332+ ${testcase_xml}
333+ </testsuite>
334+ EOF
335+ }
336+
337+ createDeprovisionJunit
338+
339+ if [[ " $TOTAL_LEAKED " -gt 0 ]]; then
340+ echo " " >&2
341+ echo " Test Failed:" >&2
342+ generate_failure_report >&2
308343 exit 1
309344else
310345 echo " No leaked resources found"
0 commit comments