Exercise - Author and run custom tests with the test toolkit

Completed

As a member of a product team, you know it's important to be able to implement some team or domain-specific rules. A way to do so is to implement the rules as tests. You can then run these tests by using the test toolkit.

Author and run a custom test

You'll author a custom test and use the test toolkit tool to run it. You'll also correct the deployment template to ensure that the test passes. The custom test will look to verify that all parameters follow a naming rule. This rule is a domain-specific requirement on the product the team where you're working.

We recommend that you have two text editors open for this exercise:

  • Author a custom test. Locate the path of the subdirectory arm-ttk/testcases/deploymentTemplate/ of the test toolkit's installation directory. From here, you'll run Visual Studio Code, where you'll create and edit a custom test.
  • Author a template file and run tests. You select the location that you want for this path. We recommend that you start an instance of Visual Studio Code from this path so you can easily edit the azuredeploy.json file when asked. Start an integrated terminal with this Visual Studio Code instance to make it easy to run tests.

Create the template file

Choose a directory and create a file called azuredeploy.json.

Warning

Ensure that the selected directory is empty with no subdirectories.

Give it the following content:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "resources": []
}

Create the custom test

  1. Open Visual Studio Code and go to your installation directory for the test toolkit. Place yourself in the subdirectory arm-ttk/testcases/deploymentTemplate. Run the following command:

    code .
    

    Note

    Open Visual Studio Code manually and open the directory if Visual Studio Code isn't on the path.

  2. Create a custom test file called Custom-ParameterNaming.test.ps1. Give the file the following content:

    param(
    [Parameter(Mandatory=$false,Position=0)] #not mandatory for case of an empty resource array
    [PSObject]
    $MainTemplateResources
    )
    
    Write-Error "To be implemented"
    

    Leave the text editor open. You'll edit this file later.

Run the custom test

Run the custom test by following these steps:

  1. Open a new terminal window or reuse the old one.

  2. Go to the directory where you created azuredeploy.json. Run the following command to start Visual Studio Code:

    code .
    

    Note

    Open Visual Studio Code manually and open the template directory if Visual Studio Code isn't on the path.

  3. From Visual Studio Code, open the integrated terminal by selecting Terminal > New Terminal from the top menu. Run the following command in the terminal to start a PowerShell shell:

    pwsh
    

    You see output that looks similar to this:

    PowerShell 7.0.3
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    https://aka.ms/powershell
    Type 'help' to get help.
    
  4. Run Import-Module in the terminal.

    Note

    Before you import the module, replace path/to/arm-ttk/arm-ttk.psd1 with the path to the downloaded test toolkit.

    Import-Module path/to/arm-ttk/arm-ttk.psd1
    

    Tip

    If you downloaded or cloned the tool to your Downloads directory, the path would look something like this: /Users/<user>/Downloads/arm-ttk/arm-ttk/arm-ttk.psd1.

    You're now ready to use the tool. As long as you're in the same PowerShell session, there's no need to run the import command again.

  5. Run Test-AzTemplate in the terminal, to start a test run:

    Test-AzTemplate -TemplatePath .
    

    Your output resembles the following. Notice the highlighted lines show your test:

    Validating custom\azuredeploy.json
      JSONFiles Should Be Valid
        [+] JSONFiles Should Be Valid (56 ms)
    Pass  : 1
    Fail  : 0
    Total : 1
    
    
    
      adminUsername Should Not Be A Literal
        [+] adminUsername Should Not Be A Literal (68 ms)
      apiVersions Should Be Recent In Reference Functions
        [+] apiVersions Should Be Recent In Reference Functions (203 ms)
      apiVersions Should Be Recent
        [+] apiVersions Should Be Recent (137 ms)
      artifacts parameter
        [+] artifacts parameter (145 ms)
      CommandToExecute Must Use ProtectedSettings For Secrets
        [+] CommandToExecute Must Use ProtectedSettings For Secrets (171 ms)
      deploymentTemplate
        [-] Custom ParameterNaming (354 ms)
            To be implemented
    
      DependsOn Best Practices
        [+] DependsOn Best Practices (152 ms)
      Deployment Resources Must Not Be Debug
        [+] Deployment Resources Must Not Be Debug (152 ms)
      DeploymentTemplate Must Not Contain Hardcoded Uri
        [+] DeploymentTemplate Must Not Contain Hardcoded Uri (185 ms)
      DeploymentTemplate Schema Is Correct
        [+] DeploymentTemplate Schema Is Correct (197 ms)
      Dynamic Variable References Should Not Use Concat
        [+] Dynamic Variable References Should Not Use Concat (157 ms)
      IDs Should Be Derived From ResourceIDs
        [+] IDs Should Be Derived From ResourceIDs (69 ms)
      Location Should Not Be Hardcoded
        [+] Location Should Not Be Hardcoded (260 ms)
      ManagedIdentityExtension must not be used
        [+] ManagedIdentityExtension must not be used (70 ms)
      Min And Max Value Are Numbers
        [+] Min And Max Value Are Numbers (213 ms)
      Outputs Must Not Contain Secrets
        [+] Outputs Must Not Contain Secrets (76 ms)
      Parameter Types Should Be Consistent
        [+] Parameter Types Should Be Consistent (68 ms)
      Parameters Must Be Referenced
        [+] Parameters Must Be Referenced (93 ms)
      Password params must be secure
        [+] Password params must be secure (111 ms)
      providers apiVersions Is Not Permitted
        [+] providers apiVersions Is Not Permitted (68 ms)
      ResourceIds should not contain
        [+] ResourceIds should not contain (210 ms)
      Resources Should Have Location
        [+] Resources Should Have Location (113 ms)
      Resources Should Not Be Ambiguous
        [+] Resources Should Not Be Ambiguous (147 ms)
      Secure Params In Nested Deployments
        [+] Secure Params In Nested Deployments (242 ms)
      Secure String Parameters Cannot Have Default
        [+] Secure String Parameters Cannot Have Default (129 ms)
      Template Should Not Contain Blanks
        [+] Template Should Not Contain Blanks (201 ms)
      URIs Should Be Properly Constructed
        [+] URIs Should Be Properly Constructed (180 ms)
      Variables Must Be Referenced
        [+] Variables Must Be Referenced (132 ms)
      Virtual Machines Should Not Be Preview
        [+] Virtual Machines Should Not Be Preview (91 ms)
      VM Images Should Use Latest Version
        [+] VM Images Should Use Latest Version (114 ms)
      VM Size Should Be A Parameter
        [+] VM Size Should Be A Parameter (130 ms)
    Pass  : 31
    Fail  : 1
    Total : 32
    

    Now that you've found the test, leave this terminal window open. You'll reuse it later.

Refactor the custom test

Now you'll give the custom test a proper implementation.

  1. Go back to the text editor that holds the file Custom-ParameterNaming.test.ps1.

    Note

    If you accidentally closed Visual Studio Code, go to the subdirectory arm-ttk/testcases/deploymentTemplate and open Custom-ParameterNaming.test.ps1.

  2. Replace the file's content with the following code:

    <#
    .Synopsis
     Ensures that all parameters adheres to a naming standard
    .Description
     All parameters should start with the company specific prefix 'tailwind'
    #>
    param(
       # The Template Object
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateObject,
    
       # The Template JSON Text
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateText
    )
    
    foreach ($parameter in $TemplateObject.parameters.psobject.properties) {
      # If the parameter name starts with tailwind, then the parameter is correctly named
      if ($parameter.Name -notmatch 'tailwind*') {
         Write-Error "Parameter '$($parameter.Name)' must start with prefix 'tailwind'" -TargetObject $parameter
      }
    }
    

    The preceding code iterates through all the parameters. It inspects the name attribute and checks whether the name starts with the prefix tailwind. If the inspected parameter doesn't match the naming rule, the code then invokes the Write-Error cmdlet with a suitable error message.

Update the template file

You'll now add a parameter to the template file.

  1. Select the text editor that contains azuredeploy.json and change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The preceding template content defines a parameter named location that doesn't fulfill the naming rule, because it lacks the tailwind prefix.

Rerun the test toolkit

You have a custom test written at this point. However, your template file's naming doesn't fulfill the requirement. So you expect the upcoming test run to fail. Ensure that's the case by taking the following step.

Use the existing Visual Studio Code integrated terminal window where PowerShell has been started and the test toolkit has been imported.

In Visual Studio Code, run Test-AzTemplate from the integrated terminal:

Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming

The preceding command is run with the parameter -Test, which takes a test name as input. You've provided Custom-ParameterNaming as a parameter, which means only your newly developed test will be run.

Tip

Using the -Test parameter is a good practice when you're developing a test because it limits what's being run and the size of the terminal output.

This command results in the following output:

Validating custom\azuredeploy.json
 deploymentTemplate
   [-] Custom ParameterNaming (2ms)
       Parameter 'location' must start with prefix 'tailwind'

The result indicates that your test works. Let's ensure that's the case by altering the deployment file.

Correct the template file

At this point, you want to verify the correctness of your custom test by changing the template file to adhere to the rules that the custom test laid out.

  1. In the same Visual Studio Code instance that shows the azuredeploy.json file, change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "tailwindLocation": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The parameter named location has been renamed to tailwindLocation. In theory, this parameter should now pass the test. Let's verify.

  2. Continue with the same Visual Studio Code instance and run Test-AzTemplate in the integrated terminal:

    Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming
    

    Your output now looks like the following:

    Validating custom\azuredeploy.json
      deploymentTemplate
        [+] Custom ParameterNaming (2 ms)
    

Success! You've implemented and run a custom test. You've also corrected a deployment template to match the test's condition.

Author and run a custom test

You'll author a custom test and use the test toolkit tool to run it. You'll also correct the deployment template to ensure that the test passes. The custom test will look to verify that all parameters follow a naming rule. This rule is a domain-specific requirement on the product the team where you're working.

We recommend that you have two text editors open for this exercise:

  • Author a custom test. Locate the path of the subdirectory arm-ttk/testcases/deploymentTemplate/ of the test toolkit's installation directory. From here, you'll run Visual Studio Code, where you'll create and edit a custom test.
  • Author a template file and run tests. You select the location that you want for this path. We recommend that you start an instance of Visual Studio Code from this path so you can easily edit the azuredeploy.json file when asked. Start an integrated terminal with this Visual Studio Code instance to make it easy to run tests.

Create the template file

Choose a directory and create a file called azuredeploy.json.

Warning

Ensure that the selected directory is empty with no subdirectories.

Give it the following content:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "resources": []
}

Create the custom test

  1. Open a terminal. Go to your installation directory for the test toolkit. Place yourself in the subdirectory arm-ttk/testcases/deploymentTemplate. Run the following command:

    code .
    

    Note

    Open Visual Studio Code manually and open the directory if Visual Studio Code isn't on the path.

  2. Create a custom file called Custom-ParameterNaming.test.ps1. Give the file the following content:

    param(
    [Parameter(Mandatory=$false,Position=0)] #not mandatory for case of an empty resource array
    [PSObject]
    $MainTemplateResources
    )
    
    Write-Error "To be implemented"
    

    Leave the text editor open. You'll edit this file later.

Run the custom test

Run the custom test by following these steps:

  1. Open a new terminal window or reuse the old one.

  2. Go to the directory where you created azuredeploy.json. Run the following command to start Visual Studio Code:

    code .
    

    Note

    Open Visual Studio Code manually and open the template directory if Visual Studio Code isn't on the path.

  3. From Visual Code, open the integrated terminal by selecting Terminal > New Terminal from the top menu. Run the following command in the terminal to start a PowerShell shell:

    pwsh
    

    You see output that looks similar to this:

    PowerShell 7.0.3
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    https://aka.ms/powershell
    Type 'help' to get help.
    
  4. Run Import-Module in the terminal.

    Note

    Before you import the module, replace path/to/arm-ttk/arm-ttk.psd1 with the path to the downloaded test toolkit.

    Import-Module path/to/arm-ttk/arm-ttk.psd1
    

    Tip

    If you downloaded or cloned the tool to your Downloads directory, the path would look something like this: /Users/<user>/Downloads/arm-ttk/arm-ttk/arm-ttk.psd1.

    You're now ready to use the tool. As long as you're in the same PowerShell session, there's no need to run the import command again.

  5. Run Test-AzTemplate in the terminal:

    Test-AzTemplate -TemplatePath .
    

    Your output resembles the following. Notice the highlighted lines show your test:

    Validating custom\azuredeploy.json
      JSONFiles Should Be Valid
        [+] JSONFiles Should Be Valid (56 ms)
    Pass  : 1
    Fail  : 0
    Total : 1
    
    
    
      adminUsername Should Not Be A Literal
        [+] adminUsername Should Not Be A Literal (68 ms)
      apiVersions Should Be Recent In Reference Functions
        [+] apiVersions Should Be Recent In Reference Functions (203 ms)
      apiVersions Should Be Recent
        [+] apiVersions Should Be Recent (137 ms)
      artifacts parameter
        [+] artifacts parameter (145 ms)
      CommandToExecute Must Use ProtectedSettings For Secrets
        [+] CommandToExecute Must Use ProtectedSettings For Secrets (171 ms)
      deploymentTemplate
        [-] Custom ParameterNaming (354 ms)
            To be implemented
    
      DependsOn Best Practices
        [+] DependsOn Best Practices (152 ms)
      Deployment Resources Must Not Be Debug
        [+] Deployment Resources Must Not Be Debug (152 ms)
      DeploymentTemplate Must Not Contain Hardcoded Uri
        [+] DeploymentTemplate Must Not Contain Hardcoded Uri (185 ms)
      DeploymentTemplate Schema Is Correct
        [+] DeploymentTemplate Schema Is Correct (197 ms)
      Dynamic Variable References Should Not Use Concat
        [+] Dynamic Variable References Should Not Use Concat (157 ms)
      IDs Should Be Derived From ResourceIDs
        [+] IDs Should Be Derived From ResourceIDs (69 ms)
      Location Should Not Be Hardcoded
        [+] Location Should Not Be Hardcoded (260 ms)
      ManagedIdentityExtension must not be used
        [+] ManagedIdentityExtension must not be used (70 ms)
      Min And Max Value Are Numbers
        [+] Min And Max Value Are Numbers (213 ms)
      Outputs Must Not Contain Secrets
        [+] Outputs Must Not Contain Secrets (76 ms)
      Parameter Types Should Be Consistent
        [+] Parameter Types Should Be Consistent (68 ms)
      Parameters Must Be Referenced
        [+] Parameters Must Be Referenced (93 ms)
      Password params must be secure
        [+] Password params must be secure (111 ms)
      providers apiVersions Is Not Permitted
        [+] providers apiVersions Is Not Permitted (68 ms)
      ResourceIds should not contain
        [+] ResourceIds should not contain (210 ms)
      Resources Should Have Location
        [+] Resources Should Have Location (113 ms)
      Resources Should Not Be Ambiguous
        [+] Resources Should Not Be Ambiguous (147 ms)
      Secure Params In Nested Deployments
        [+] Secure Params In Nested Deployments (242 ms)
      Secure String Parameters Cannot Have Default
        [+] Secure String Parameters Cannot Have Default (129 ms)
      Template Should Not Contain Blanks
        [+] Template Should Not Contain Blanks (201 ms)
      URIs Should Be Properly Constructed
        [+] URIs Should Be Properly Constructed (180 ms)
      Variables Must Be Referenced
        [+] Variables Must Be Referenced (132 ms)
      Virtual Machines Should Not Be Preview
        [+] Virtual Machines Should Not Be Preview (91 ms)
      VM Images Should Use Latest Version
        [+] VM Images Should Use Latest Version (114 ms)
      VM Size Should Be A Parameter
        [+] VM Size Should Be A Parameter (130 ms)
    Pass  : 31
    Fail  : 1
    Total : 32
    

    Now that you've found the test, leave this terminal window open. You'll reuse it later.

Refactor the custom test

Now you'll give the custom test a proper implementation.

  1. Go back to the text editor that holds the file Custom-ParameterNaming.test.ps1.

    Note

    If you accidentally closed Visual Studio Code, go to the subdirectory arm-ttk/testcases/deploymentTemplate and open Custom-ParameterNaming.test.ps1.

  2. Replace the file's content with the following code:

    <#
    .Synopsis
     Ensures that all parameters adheres to a naming standard
    .Description
     All parameters should start with the company specific prefix 'tailwind'
    #>
    param(
       # The Template Object
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateObject,
    
       # The Template JSON Text
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateText
    )
    
    foreach ($parameter in $TemplateObject.parameters.psobject.properties) {
      # If the parameter name starts with tailwind, then the parameter is correctly named
      if ($parameter.Name -notmatch 'tailwind*') {
         Write-Error "Parameter '$($parameter.Name)' must start with prefix 'tailwind'" -TargetObject $parameter
      }
    }
    

    The preceding code iterates through all the parameters. It inspects the name attribute and checks whether the name starts with the prefix tailwind. If the inspected parameter doesn't match the naming rule, the code then invokes the Write-Error cmdlet with a suitable error message.

Update the template file

You'll now add a parameter to the template file.

  1. Select the text editor that contains azuredeploy.json and change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The preceding template content defines a parameter named location that doesn't fulfill the naming rule, because it lacks the tailwind prefix.

Rerun the test toolkit

You have a custom test written at this point. However, your template file's naming doesn't fulfill the requirement. So you expect the upcoming test run to fail. Ensure that's the case by taking the following step.

Note

Use the existing Visual Studio Code integrated terminal window where PowerShell has been started and the test toolkit has been imported.

In Visual Studio Code, run Test-AzTemplate from the integrated terminal:

Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming

The preceding command is run with the parameter named -Test, which takes a test name as input. You've provided Custom-ParameterNaming as a parameter, which means only your newly developed test will be run.

Tip

Using this parameter is a good practice when you're developing a test because it limits what's being run and the size of the terminal output.

This command results in the following output:

Validating custom\azuredeploy.json
 deploymentTemplate
   [-] Custom ParameterNaming (2ms)
       Parameter 'location' must start with prefix 'tailwind'

The result indicates that your test works. Let's ensure that's the case by altering the deployment file.

Correct the template file

At this point, you want to verify the correctness of your custom test by changing the template file to adhere to the rules that the custom test laid out.

  1. In the same Visual Studio Code instance that shows the azuredeploy.json file, change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "tailwindLocation": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The parameter named location has been renamed to tailwindLocation. In theory, this parameter should now pass the test. Let's verify.

  2. Continue with the same Visual Studio Code instance and run Test-AzTemplate in the integrated terminal:

    Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming
    

    Your output now looks like the following:

    Validating custom\azuredeploy.json
      deploymentTemplate
        [+] Custom ParameterNaming (2 ms)
    

Success! You've implemented and run a custom test. You've also corrected a deployment template to match the test's condition.

Author and run a custom test

You'll author a custom test and use the test toolkit tool to run it. You'll also correct the deployment template to ensure that the test passes. The custom test will look to verify that all parameters follow a naming rule. This rule is a domain-specific requirement on the product the team where you're working.

We recommend that you have two text editors open for this exercise:

  • Author a custom test. Locate the path of the subdirectory arm-ttk\testcases\deploymentTemplate\ of the test toolkit's installation directory. From here, you'll run Visual Studio Code, where you'll create and edit a custom test.
  • Author a template file and run tests. You select the location that you want for this path. We recommend that you start an instance of Visual Studio Code from this path so you can easily edit the azuredeploy.json file when asked. Start an integrated terminal with this Visual Studio Code instance to make it easy to run tests.

Create the template file

Choose a directory and create a file called azuredeploy.json.

Warning

Ensure that the selected directory is empty with no subdirectories.

Give it the following content:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "resources": []
}

Create the custom test

  1. Open a terminal.

  2. Go to your installation directory for the test toolkit.

  3. Place yourself in the subdirectory arm-ttk\testcases\deploymentTemplate.

  4. Run the following command:

    code .
    

    Note

    Open Visual Studio Code manually and open the directory if Visual Studio Code isn't on the path.

  5. Create a file called Custom-ParameterNaming.test.ps1 and give the file the following content:

    param(
    [Parameter(Mandatory=$false,Position=0)] #not mandatory for case of an empty resource array
    [PSObject]
    $MainTemplateResources
    )
    
    Write-Error "To be implemented"
    

    Leave the text editor open. You'll edit this file later.

Run the custom test

Run the custom test by following these steps:

  1. Open a new terminal window or reuse the old one.

  2. Go to the directory of where you created azuredeploy.json.

  3. Run the following command to start Visual Studio Code:

    code .
    

    Note

    Open Visual Studio Code manually and open the template directory if Visual Studio Code isn't on the path.

  4. From Visual Studio Code, open the integrated terminal. Bring up the command palette, type PowerShell, and select Show integrated terminal.

  5. Run the following command in the terminal:

    Note

    Before you import the module, replace path\to\arm-ttk\arm-ttk.psd1 with the path to the downloaded test toolkit.

    Import-Module path\to\arm-ttk\arm-ttk.psd1
    

    Tip

    If you downloaded or cloned the tool to your Downloads directory, the path would look something like this: C:\Users\<user>\Downloads\arm-ttk\arm-ttk\arm-ttk.psd1.

    You're now ready to use the tool. As long as you're in the same PowerShell session, there's no need to run the import command again.

  6. Run Test-AzTemplate in the terminal:

    Test-AzTemplate -TemplatePath .
    

    Your output resembles the following. Notice the highlighted lines show your test:

    Validating custom\azuredeploy.json
      JSONFiles Should Be Valid
        [+] JSONFiles Should Be Valid (56 ms)
    Pass  : 1
    Fail  : 0
    Total : 1
    
    
    
      adminUsername Should Not Be A Literal
        [+] adminUsername Should Not Be A Literal (68 ms)
      apiVersions Should Be Recent In Reference Functions
        [+] apiVersions Should Be Recent In Reference Functions (203 ms)
      apiVersions Should Be Recent
        [+] apiVersions Should Be Recent (137 ms)
      artifacts parameter
        [+] artifacts parameter (145 ms)
      CommandToExecute Must Use ProtectedSettings For Secrets
        [+] CommandToExecute Must Use ProtectedSettings For Secrets (171 ms)
      deploymentTemplate
        [-] Custom ParameterNaming (354 ms)
            To be implemented
    
      DependsOn Best Practices
        [+] DependsOn Best Practices (152 ms)
      Deployment Resources Must Not Be Debug
        [+] Deployment Resources Must Not Be Debug (152 ms)
      DeploymentTemplate Must Not Contain Hardcoded Uri
        [+] DeploymentTemplate Must Not Contain Hardcoded Uri (185 ms)
      DeploymentTemplate Schema Is Correct
        [+] DeploymentTemplate Schema Is Correct (197 ms)
      Dynamic Variable References Should Not Use Concat
        [+] Dynamic Variable References Should Not Use Concat (157 ms)
      IDs Should Be Derived From ResourceIDs
        [+] IDs Should Be Derived From ResourceIDs (69 ms)
      Location Should Not Be Hardcoded
        [+] Location Should Not Be Hardcoded (260 ms)
      ManagedIdentityExtension must not be used
        [+] ManagedIdentityExtension must not be used (70 ms)
      Min And Max Value Are Numbers
        [+] Min And Max Value Are Numbers (213 ms)
      Outputs Must Not Contain Secrets
        [+] Outputs Must Not Contain Secrets (76 ms)
      Parameter Types Should Be Consistent
        [+] Parameter Types Should Be Consistent (68 ms)
      Parameters Must Be Referenced
        [+] Parameters Must Be Referenced (93 ms)
      Password params must be secure
        [+] Password params must be secure (111 ms)
      providers apiVersions Is Not Permitted
        [+] providers apiVersions Is Not Permitted (68 ms)
      ResourceIds should not contain
        [+] ResourceIds should not contain (210 ms)
      Resources Should Have Location
        [+] Resources Should Have Location (113 ms)
      Resources Should Not Be Ambiguous
        [+] Resources Should Not Be Ambiguous (147 ms)
      Secure Params In Nested Deployments
        [+] Secure Params In Nested Deployments (242 ms)
      Secure String Parameters Cannot Have Default
        [+] Secure String Parameters Cannot Have Default (129 ms)
      Template Should Not Contain Blanks
        [+] Template Should Not Contain Blanks (201 ms)
      URIs Should Be Properly Constructed
        [+] URIs Should Be Properly Constructed (180 ms)
      Variables Must Be Referenced
        [+] Variables Must Be Referenced (132 ms)
      Virtual Machines Should Not Be Preview
        [+] Virtual Machines Should Not Be Preview (91 ms)
      VM Images Should Use Latest Version
        [+] VM Images Should Use Latest Version (114 ms)
      VM Size Should Be A Parameter
        [+] VM Size Should Be A Parameter (130 ms)
    Pass  : 31
    Fail  : 1
    Total : 32
    

    Now that you've found the test, leave this terminal window open. You'll reuse it later.

Refactor the custom test

Now you'll give the custom test a proper implementation.

  1. Go back to the text editor that holds the file Custom-ParameterNaming.test.ps1.

    Note

    If you accidentally closed Visual Studio Code, go to the subdirectory testcases/deploymentTemplate and open the file Custom-ParameterNaming.test.ps1.

  2. Replace the file's content with the following code:

    <#
    .Synopsis
     Ensures that all parameters adheres to a naming standard
    .Description
     All parameters should start with the company specific prefix 'tailwind'
    #>
    param(
       # The Template Object
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateObject,
    
       # The Template JSON Text
       [Parameter(Mandatory = $true, Position = 0)]
       [PSObject]
       $TemplateText
    )
    
    foreach ($parameter in $TemplateObject.parameters.psobject.properties) {
      # If the parameter name starts with tailwind, then the parameter is correctly named
      if ($parameter.Name -notmatch 'tailwind*') {
         Write-Error "Parameter '$($parameter.Name)' must start with prefix 'tailwind'" -TargetObject $parameter
      }
    }
    

    The preceding code iterates through all the parameters. It inspects the name attribute and checks whether the name starts with the prefix tailwind. If the inspected parameter doesn't match the naming rule, the code then invokes the Write-Error cmdlet with a suitable error message.

Update the template file

You'll now add a parameter to the template file.

  1. Select the text editor that contains azuredeploy.json and change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The preceding template content defines a parameter named location that doesn't fulfill the naming rule, because it lacks the tailwind prefix.

Rerun the test toolkit

You have a custom test written at this point. However, your template file's naming doesn't fulfill the requirement. So you expect the upcoming test run to fail. Ensure that's the case by taking the following step.

Use the existing Visual Studio Code integrated terminal window where PowerShell has been started and the test toolkit has been imported.

In Visual Studio Code, run Test-AzTemplate from the integrated terminal:

Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming

The preceding command is run with the parameter -Test, which takes a test name as input. You've provided Custom-ParameterNaming as a parameter, which means only your newly developed test will be run.

Tip

Using this parameter is a good practice when you're developing a test because it limits what's being run and the size of the terminal output.

This command results in the following output:

Validating custom\azuredeploy.json
 deploymentTemplate
   [-] Custom ParameterNaming (2ms)
       Parameter 'location' must start with prefix 'tailwind'

The result indicates that your test works. Let's ensure that's the case by altering the deployment file.

Correct the template file

At this point, you want to verify the correctness of your custom test by changing the template file to adhere to the rules that the custom test laid out.

  1. In the same Visual Studio Code instance that shows the azuredeploy.json file, change the file's content to the following content:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "tailwindLocation": {
          "type": "string",
          "metadata": {
            "description": "a deployment location"
          }
        }
      },
      "resources": []
    }
    

    The parameter named location has been renamed to tailwindLocation. In theory, this parameter should now pass the test. Let's verify.

  2. Continue with the same Visual Studio Code instance and run Test-AzTemplate in the integrated terminal:

    Test-AzTemplate -TemplatePath . -Test Custom-ParameterNaming
    

    Your output now looks like the following:

    Validating custom\azuredeploy.json
      Custom ParameterNaming
        [+] Custom ParameterNaming (9 ms)
    

Success! You've implemented and run a custom test. You've also corrected a deployment template to match the test's condition.