From 354c909b9b45fd4459a40e4a5b67dcb7a61beb34 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Fri, 20 Dec 2019 13:45:32 +0200 Subject: [PATCH] tests: Azure - apply tags and delete by tags - change image & vm names to Composer-Test-* for consistency - tag vm with composer_test upon creation, timestamp is already present - tag blobs with composer_test upon upload - tag images with composer_test & first_seen timestamp upon creation --- tests/cleanup/remove_old_objects_azure.sh | 16 +++++++++------- tests/cli/test_build_and_deploy_azure.sh | 15 ++++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/tests/cleanup/remove_old_objects_azure.sh b/tests/cleanup/remove_old_objects_azure.sh index 4cf8e96e..46247e49 100755 --- a/tests/cleanup/remove_old_objects_azure.sh +++ b/tests/cleanup/remove_old_objects_azure.sh @@ -4,12 +4,14 @@ . /usr/share/beakerlib/beakerlib.sh -# Delete old objects based on the $TAG_NAME tag value defined in a previous execution of the script +# Delete old objects based on first_seen tag value delete_old_resources() { local resource_type="$1" - # list resources older than $TIMESTAMP based on the $TAG_NAME tag created in a previous run - rlRun -c -s 'az resource list --resource-type $resource_type --query "[?tags.$TAG_NAME < \`$TIMESTAMP\`].name" --output tsv' 0 "Get a list of $resource_type older than $TIMESTAMP" + # list composer-test resources older than $TIMESTAMP based on the first_seen tag + # timestamp tagging is done either when resources are created or by the function below + # Note: the query language here is called JMESPath + rlRun -c -s 'az resource list --resource-type $resource_type --query "[?tags.$TAG_NAME < \`$TIMESTAMP\` && tags.composer_test == \`true\`].name" --output tsv' 0 "Get a list of $resource_type older than $TIMESTAMP" resources_to_delete=$(cat $rlRun_LOG) if [ -n "$resources_to_delete" ]; then @@ -21,12 +23,12 @@ delete_old_resources() { fi } -# Find objects without the $TAG_NAME tag and create the tag with the current date/time value +# Find objects without the first_seen tag and create the tag with the current date/time value tag_new_resources() { local resource_type="$1" - # list resources without the $TAG_NAME tag - rlRun -c -s 'az resource list --resource-type $resource_type --query "[?tags.$TAG_NAME == null].name" --output tsv' 0 "Get a list of $resource_type without the $TAG_NAME tag." + # list composer-test resources without the first_seen tag + rlRun -c -s 'az resource list --resource-type $resource_type --query "[?tags.$TAG_NAME == null && tags.composer_test == \`true\`].name" --output tsv' 0 "Get a list of $resource_type without the $TAG_NAME tag." resources_without_tag=$(cat $rlRun_LOG) if [ -n "$resources_without_tag" ]; then @@ -126,7 +128,7 @@ Microsoft.Compute/images rlPhaseStartTest "Delete old blobs" # get a list of blobs older than $TIMESTAMP - rlRun -c -s 'az storage blob list --container-name $AZURE_STORAGE_CONTAINER --query "[?properties.creationTime < \`$TIMESTAMP\`].[name,properties.creationTime]" --output tsv' + rlRun -c -s 'az storage blob list --container-name $AZURE_STORAGE_CONTAINER --query "[?properties.creationTime < \`$TIMESTAMP\` && tags.composer_test == \`true\`].[name,properties.creationTime]" --output tsv' blobs_to_delete=$(cat $rlRun_LOG) if [ -n "$blobs_to_delete" ]; then diff --git a/tests/cli/test_build_and_deploy_azure.sh b/tests/cli/test_build_and_deploy_azure.sh index 201eb2ee..adb988ab 100755 --- a/tests/cli/test_build_and_deploy_azure.sh +++ b/tests/cli/test_build_and_deploy_azure.sh @@ -74,23 +74,27 @@ rlJournalStart rlPhaseStartTest "Upload image to Azure" rlRun -t -c "$CLI compose image $UUID" - IMAGE="$UUID-disk.vhd" - OS_IMAGE_NAME="Composer-$UUID-Automated-Import" + rlRun -t -c "mv $UUID-disk.vhd Composer-Test-$UUID-disk.vhd" + IMAGE="Composer-Test-$UUID-disk.vhd" + OS_IMAGE_NAME="Composer-Test-$UUID" rlRun -t -c "ansible localhost -m azure_rm_storageblob -a \ 'resource_group=$AZURE_RESOURCE_GROUP \ storage_account_name=$AZURE_STORAGE_ACCOUNT \ container=$AZURE_STORAGE_CONTAINER \ - blob=$IMAGE src=$IMAGE blob_type=page'" + blob=$IMAGE src=$IMAGE blob_type=page \ + tags={\"composer_test\":\"true\"}'" # create image from blob + now=$(date -u '+%FT%T') rlRun -t -c "ansible localhost -m azure_rm_image -a \ 'resource_group=$AZURE_RESOURCE_GROUP name=$OS_IMAGE_NAME os_type=Linux location=eastus \ - source=https://$AZURE_STORAGE_ACCOUNT.blob.core.windows.net/$AZURE_STORAGE_CONTAINER/$IMAGE'" + source=https://$AZURE_STORAGE_ACCOUNT.blob.core.windows.net/$AZURE_STORAGE_CONTAINER/$IMAGE \ + tags={\"composer_test\":\"true\",\"first_seen\":\"$now\"}'" rlPhaseEnd rlPhaseStartTest "Start VM instance" - VM_NAME="Composer-Auto-VM-$UUID" + VM_NAME="Composer-Test-VM-$UUID" SSH_KEY_DIR=`mktemp -d /tmp/composer-ssh-keys.XXXXXX` rlRun -t -c "ssh-keygen -t rsa -N '' -f $SSH_KEY_DIR/id_rsa" @@ -120,6 +124,7 @@ rlJournalStart resource_group: $AZURE_RESOURCE_GROUP tags: "first_seen": "$now" + "composer_test": "true" storage_account_name: $AZURE_STORAGE_ACCOUNT __EOF__