Testing Azure Examples

How much effort does it take to run automated tests on the Azure Examples GitHub repository? Or to ask the question differently, "How do you test the Azure Examples GitHub repository?"

Since the repository is a collection of canned examples that make it possible for you to get up and running with Azure quickly I needed a way to validate that the content written is still working. In comes the `GitHub Workflow Generator` (currently part of this repository).

So what is this generator?

Well, lets take one of the simplest Azure Examples in the repository. The example where we create an Azure Resource Group. The markdown file prior to automation looks like the snippet below:

            
## Create the Resource Group

To setup the environment variables needed to create the Resource Group execute
the command lines below:

```shell
  export RESOURCE_GROUP=azure-examples
  export REGION=westus2
```

To create the Resource Group use the following command line:

```shell
  az group create --name $RESOURCE_GROUP --location $REGION
```

        

So how do we automate this? The snippet below puts the automation in the SAME markdown file and voila you can now use the generator to generate the automated testing to validate the 'Create an Azure Resource Group' example automatically.


## Create the Resource Group

To setup the environment variables needed to create the Resource Group execute
the command lines below:

<!-- workflow.cron(0 1 * * 0) -->

<!-- workflow.skip() -->
```shell
  export RESOURCE_GROUP=azure-examples
  export REGION=westus2
```

<!-- workflow.run()
if [[ -z $RESOURCE_GROUP ]]; then
  export RESOURCE_GROUP=azure-examples-$RANDOM
fi
export REGION=westus2
  -->

To create the Resource Group use the following command line:

```shell
  az group create --name $RESOURCE_GROUP --location $REGION
```

<!-- workflow.run()
  export DELETE_AFTER=$(( `date +%s` + 7200))
  az group update --name $RESOURCE_GROUP --set tags.'DeleteAfter'="$DELETE_AFTER" 
  -->

<!-- workflow.directOnly()
export RESULT=$(az group show --name $RESOURCE_GROUP --output tsv --query properties.provisioningState)
az group delete --name $RESOURCE_GROUP --yes || true
if [[ "$RESULT" != Succeeded ]]; then
  exit 1
fi
  -->

        

The workflow.cron makes the workflow run on a cron schedule, the workflow.skip block omits the following markdown snippets from the workflow, the workflow.run block is always included and the workflow.directOnly is used to denote a snippet that should only be included in the workflow when used directly.

The generator is setup to regenerate the automated tests automatically. For reference the command line that invokes the generator is:


 java -jar gwg.jar --baseDirectory ../../.. --outputDirectory ../../../.github/workflows

        

There is a lot more to the generator, but this is a quick overview. If you are interested drop me a note at mriem (at) manorrock.com.

Enjoy!

Posted December 24th, 2020

Up