From a8eff6250593fd15156a98b1be6ac8e7a21b0bcb Mon Sep 17 00:00:00 2001 From: Mary Sha <15195985+msha1026@users.noreply.github.com> Date: Thu, 30 Mar 2023 13:17:11 -0700 Subject: [PATCH 1/2] Container Application Support (#286) * working container app support * Install Az.App in github actions * Add callout for installation of az.app * Edit tests for inconsistencies * Update to one line test * Remove comments --- .github/workflows/ci.yml | 1 + .github/workflows/pr-powershell.yml | 1 + .../BenchPress.Azure/BenchPress.Azure.psd1 | 1 + .../Classes/ResourceType.psm1 | 1 + .../Public/Confirm-ContainerApp.ps1 | 49 +++++++++++++ .../Public/Get-ResourceByType.ps1 | 3 + .../Public/Confirm-ContainerApp.Tests.ps1 | 23 +++++++ docs/getting_started.md | 3 + examples/ContainerApp/ContainerApp.Tests.ps1 | 69 +++++++++++++++++++ examples/ContainerApp/ContainerApp_Example.md | 47 +++++++++++++ examples/ContainerApp/containerApp.bicep | 69 +++++++++++++++++++ 11 files changed, 267 insertions(+) create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ContainerApp.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ContainerApp.Tests.ps1 create mode 100644 examples/ContainerApp/ContainerApp.Tests.ps1 create mode 100644 examples/ContainerApp/ContainerApp_Example.md create mode 100644 examples/ContainerApp/containerApp.bicep diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a1485b7..0256fbf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,7 @@ jobs: shell: pwsh run: | Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force + Install-Module -Name Az.App -Scope CurrentUser -Repository PSGallery -Force Publish-Module -Path ./BenchPress.Azure -Repository LocalFeedPSRepo -Verbose - name: Install AzBP Module from the repository shell: pwsh diff --git a/.github/workflows/pr-powershell.yml b/.github/workflows/pr-powershell.yml index 0e73c8b..3547378 100644 --- a/.github/workflows/pr-powershell.yml +++ b/.github/workflows/pr-powershell.yml @@ -24,6 +24,7 @@ jobs: run: | Set-PSRepository PSGallery -InstallationPolicy Trusted Install-Module Az -ErrorAction Stop + Install-Module Az.App -ErrorAction Stop Install-Module Pester -ErrorAction Stop - name: Run PowerShell Unit Tests shell: pwsh diff --git a/Modules/BenchPress.Azure/BenchPress.Azure.psd1 b/Modules/BenchPress.Azure/BenchPress.Azure.psd1 index 432de6d..bf0e3c2 100644 --- a/Modules/BenchPress.Azure/BenchPress.Azure.psd1 +++ b/Modules/BenchPress.Azure/BenchPress.Azure.psd1 @@ -16,6 +16,7 @@ "Confirm-AppInsights", "Confirm-AppServicePlan", "Confirm-BicepFile", + "Confirm-ContainerApp", "Confirm-ContainerRegistry", "Confirm-CosmosDBAccount", "Confirm-CosmosDBGremlinDatabase", diff --git a/Modules/BenchPress.Azure/Classes/ResourceType.psm1 b/Modules/BenchPress.Azure/Classes/ResourceType.psm1 index 01504de..2d6b100 100644 --- a/Modules/BenchPress.Azure/Classes/ResourceType.psm1 +++ b/Modules/BenchPress.Azure/Classes/ResourceType.psm1 @@ -3,6 +3,7 @@ enum ResourceType{ AksCluster AppInsights AppServicePlan + ContainerApp CosmosDBAccount CosmosDBGremlinDatabase CosmosDBMongoDBDatabase diff --git a/Modules/BenchPress.Azure/Public/Confirm-ContainerApp.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ContainerApp.ps1 new file mode 100644 index 0000000..5871fc6 --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ContainerApp.ps1 @@ -0,0 +1,49 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ContainerApp { + <# + .SYNOPSIS + Confirms that a Container Application exists. + + .DESCRIPTION + The Confirm-AzBPContainerApp cmdlet gets a Container Application using the specified Container Application and + Resource Group name. + + .PARAMETER Name + The name of the Container Application + + .PARAMETER ResourceGroupName + The name of the Resource Group + + .EXAMPLE + Confirm-AzBPContainerApp -Name "benchpresstest" -ResourceGroupName "rgbenchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$Name, + + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + $Resource = Get-AzContainerApp -ResourceGroupName $ResourceGroupName -Name $Name + + [ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 b/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 index d581e0b..09fc227 100644 --- a/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 +++ b/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 @@ -138,6 +138,9 @@ function Get-ResourceByType { "AppServicePlan" { return Confirm-AppServicePlan -AppServicePlanName $ResourceName -ResourceGroupName $ResourceGroupName } + "ContainerApp" { + return Confirm-ContainerApp -ResourceGroupName $ResourceGroupName -Name $ResourceName + } "ContainerRegistry" { return Confirm-ContainerRegistry -Name $ResourceName -ResourceGroupName $ResourceGroupName } diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ContainerApp.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ContainerApp.Tests.ps1 new file mode 100644 index 0000000..9363d06 --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ContainerApp.Tests.ps1 @@ -0,0 +1,23 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ContainerApp.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ContainerApp" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + } + + It "Calls Get-AzContainerApp" { + Mock Get-AzContainerApp{} + Confirm-ContainerApp -Name "ca" -ResourceGroupName "rgn" + Should -Invoke -CommandName "Get-AzContainerApp" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/docs/getting_started.md b/docs/getting_started.md index bfdb3c0..0e89c97 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -18,6 +18,9 @@ as their testing framework and runner. To use BenchPress, have the following ins - [PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows) (PowerShell 7+ recommended) - [Az PowerShell module](https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-9.3.0) + - [Az.App PowerShell module](https://learn.microsoft.com/en-us/powershell/module/az.app/?view=azps-9.5.0) for any + testing of Container Applications. Az.App is not GA yet which is why it is not included with the main Az + PowerShell module. - [Pester](https://pester.dev/docs/introduction/installation) - [Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install#azure-powershell) - [Service Principal](https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal) diff --git a/examples/ContainerApp/ContainerApp.Tests.ps1 b/examples/ContainerApp/ContainerApp.Tests.ps1 new file mode 100644 index 0000000..1642749 --- /dev/null +++ b/examples/ContainerApp/ContainerApp.Tests.ps1 @@ -0,0 +1,69 @@ +BeforeAll { + Import-Module Az.InfrastructureTesting + + $Script:rgName = 'rg-test' + $Script:conAppName = 'conAppBenchPressTest' + $Script:location = 'westus3' +} + +Describe 'Verify Container Application' { + BeforeAll { + $Script:noContainerAppName = 'nocontainerapp' + } + + It "Should contain a Container Application named $conAppName - Confirm-AzBPResource" { + $params = @{ + ResourceType = "ContainerApp" + ResourceName = $conAppName + ResourceGroupName = $rgName + } + + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain a Container Application with an Ingress Port of 80 - Confirm-AzBPResource" { + $params = @{ + ResourceType = "ContainerApp" + ResourceName = $conAppName + ResourceGroupName = $rgName + PropertyKey = "IngressTargetPort" + PropertyValue = 80 + } + + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain a Container Application named $conAppName" { + (Confirm-AzBPContainerApp -ResourceGroupName $rgName -Name $conAppName).Success | Should -Be $true + } + + It "Should not contain a Container Application named $noContainerAppName" { + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + $params = @{ + ResourceGroupName = $rgName + Name = $noContainerAppName + ErrorAction = 'SilentlyContinue' + } + + (Confirm-AzBPContainerApp @params).Success | Should -Be $false + } + + It "Should contain a Container Application named $conAppName" { + Confirm-AzBPContainerApp -ResourceGroupName $rgName -Name $conAppName | Should -BeDeployed + } + + It "Should contain a Container Application named $conAppName in $location" { + Confirm-AzBPContainerApp -ResourceGroupName $rgName -Name $conAppName | Should -BeInLocation $location + } + + It "Should contain a Container Application named $conAppName in $rgName" { + Confirm-AzBPContainerApp -ResourceGroupName $rgName -Name $conAppName | Should -BeInResourceGroup $rgName + } +} + +AfterAll { + Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module BenchPress.Azure | Remove-Module +} diff --git a/examples/ContainerApp/ContainerApp_Example.md b/examples/ContainerApp/ContainerApp_Example.md new file mode 100644 index 0000000..a40d49c --- /dev/null +++ b/examples/ContainerApp/ContainerApp_Example.md @@ -0,0 +1,47 @@ +# How To Run ContainerApp.Tests.ps1 + +`ContainerApp.Tests.ps1` contains examples of using the `Confirm-AzBPContainerApp` cmdlet. + +## Pre-Requisites + +- Follow the [setup instructions](../README.md) + +## Steps + +1. Navigate to ContainerApp directory: + + ```Powershell + cd examples\ContainerApp\ + ``` + +1. Deploy the Container Registry to your resource group: + + ```Powershell + New-AzResourceGroupDeployment -ResourceGroupName ""` + -TemplateFile ".\containerApp.bicep" + ``` + +1. Update `ContainerApp.Tests.ps1` variables to point to your expected resources: + + - `rg-test` -> `your-resource-group-name` + - `conAppBenchPressTest` -> `your-container-application-name` + - `westus3` -> `your-resource-group-location-name` + +1. If using a local copy of `Az.InfrastructureTesting`, replace `Import-Module Az.InfrastructureTesting` with +`Import-Module "../../bin/BenchPress.Azure.psd1"`. Note that the final `AfterAll` step will properly remove the module +regardless of which method is chosen to load the module. + +1. Run `ContainerApp.Tests.ps1`: + + ```Powershell + Invoke-Pester -Path .\ContainerApp.Tests.ps1 + ``` + +1. Success! + + ```Powershell + Tests completed in 5.02s + Tests Passed: 7, Failed: 0, Skipped: 0 NotRun: 0 + ``` + +1. Don't forget to delete any deployed resources that are no longer needed. diff --git a/examples/ContainerApp/containerApp.bicep b/examples/ContainerApp/containerApp.bicep new file mode 100644 index 0000000..a7dc07a --- /dev/null +++ b/examples/ContainerApp/containerApp.bicep @@ -0,0 +1,69 @@ +param containerAppName string = 'acr${take(uniqueString(resourceGroup().id), 5)}' +param location string = resourceGroup().location +param targetPort int = 80 +param containerImage string = 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest' + +resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2022-10-01' = { + name: 'loganalytics${containerAppName}' + location: location + properties: { + sku: { + name: 'PerGB2018' + } + } +} + +resource containerAppEnv 'Microsoft.App/managedEnvironments@2022-10-01' = { + name: 'env${containerAppName}' + location: location + sku: { + name: 'Consumption' + } + properties: { + appLogsConfiguration: { + destination: 'log-analytics' + logAnalyticsConfiguration: { + customerId: logAnalytics.properties.customerId + sharedKey: logAnalytics.listKeys().primarySharedKey + } + } + } +} + +resource containerApp 'Microsoft.App/containerApps@2022-10-01' = { + name: containerAppName + location: location + properties: { + managedEnvironmentId: containerAppEnv.id + configuration: { + ingress: { + external: true + targetPort: targetPort + allowInsecure: false + traffic: [ + { + latestRevision: true + weight: 100 + } + ] + } + } + template: { + revisionSuffix: 'firstrevision' + containers: [ + { + name: containerAppName + image: containerImage + resources: { + cpu: json('0.5') + memory: '1Gi' + } + } + ] + scale: { + minReplicas: 1 + maxReplicas: 3 + } + } + } +} From 2d406ff60bd52bf4dbbefe205017252142d91cc0 Mon Sep 17 00:00:00 2001 From: marclerwick <109613091+marclerwick@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:40:34 -0700 Subject: [PATCH 2/2] #246 API Management Service support * Added support for API Management Service, API Management API, API Management Diagnostics, API Management Loggers, and API Management Policies * Minor fixes. * Fixing broken unit test. * Escaping the XML * Removing unused parameter per code review. * Fixing examples in the comments per code review. * Added comments for Confirm-ApiManagmentLogger * Making ResourceName optional. * Removing the list of options for ResourceType from Confirm-ResourceByType. It is an enum now, no need to list them. * Updating from Az-InfrastructureTesting to Az.InfrastructureTesting. * Updated example tests to use pipelining. --- .../BenchPress.Azure/BenchPress.Azure.psd1 | 5 + .../Classes/ResourceType.psm1 | 5 + .../Public/Confirm-ApiManagement.ps1 | 49 +++ .../Public/Confirm-ApiManagementApi.ps1 | 56 +++ .../Confirm-ApiManagementDiagnostic.ps1 | 57 +++ .../Public/Confirm-ApiManagementLogger.ps1 | 58 +++ .../Public/Confirm-ApiManagementPolicy.ps1 | 62 +++ .../Public/Confirm-Resource.ps1 | 26 +- .../Public/Get-ResourceByType.ps1 | 73 ++-- .../Public/Confirm-ApiManagement.Tests.ps1 | 23 ++ .../Public/Confirm-ApiManagementApi.Tests.ps1 | 26 ++ .../Confirm-ApiManagementDiagnostic.Tests.ps1 | 26 ++ .../Confirm-ApiManagementLogger.Tests.ps1 | 26 ++ .../Confirm-ApiManagementPolicy.Tests.ps1 | 26 ++ examples/ActionGroup/ActionGroup.Tests.ps1 | 2 +- examples/AksCluster/AKSCluster.Tests.ps1 | 2 +- .../ApiManagement/ApiManagement.Tests.ps1 | 371 ++++++++++++++++++ examples/ApiManagement/ApiManagement.bicep | 85 ++++ .../ApiManagement/ApiManagementPolicy.xml | 63 +++ .../ApiManagement/ApiManagement_Example.md | 51 +++ examples/AppInsights/AppInsights.Tests.ps1 | 2 +- .../AppServicePlan/AppServicePlan.Tests.ps1 | 2 +- examples/BicepHelpers/BicepHelpers.Tests.ps1 | 2 +- examples/Common/Common.Tests.ps1 | 2 +- .../ContainerRegistry.Tests.ps1 | 2 +- examples/CosmosDB/CosmosDB.Tests.ps1 | 2 +- examples/DataFactory/DataFactory.Tests.ps1 | 2 +- examples/EventHub/EventHub.Tests.ps1 | 2 +- examples/KeyVault/KeyVault.Tests.ps1 | 2 +- .../OperationalInsightsWorkspace.Tests.ps1 | 2 +- .../ResourceGroup/ResourceGroup.Tests.ps1 | 2 +- examples/SqlDatabase/SqlDatabase.Tests.ps1 | 2 +- examples/SqlServer/SqlServer.Tests.ps1 | 2 +- examples/Storage/Storage.Tests.ps1 | 2 +- .../StreamAnalytics/StreamAnalytics.Tests.ps1 | 2 +- examples/Synapse/Synapse.Tests.ps1 | 2 +- .../VirtualMachine/VirtualMachine.Tests.ps1 | 2 +- examples/WebApp/WebApp.Tests.ps1 | 2 +- 38 files changed, 1071 insertions(+), 57 deletions(-) create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ApiManagement.ps1 create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ApiManagementApi.ps1 create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ApiManagementDiagnostic.ps1 create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ApiManagementLogger.ps1 create mode 100644 Modules/BenchPress.Azure/Public/Confirm-ApiManagementPolicy.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagement.Tests.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementApi.Tests.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementDiagnostic.Tests.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementLogger.Tests.ps1 create mode 100644 Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementPolicy.Tests.ps1 create mode 100644 examples/ApiManagement/ApiManagement.Tests.ps1 create mode 100644 examples/ApiManagement/ApiManagement.bicep create mode 100644 examples/ApiManagement/ApiManagementPolicy.xml create mode 100644 examples/ApiManagement/ApiManagement_Example.md diff --git a/Modules/BenchPress.Azure/BenchPress.Azure.psd1 b/Modules/BenchPress.Azure/BenchPress.Azure.psd1 index bf0e3c2..0f9a47c 100644 --- a/Modules/BenchPress.Azure/BenchPress.Azure.psd1 +++ b/Modules/BenchPress.Azure/BenchPress.Azure.psd1 @@ -13,6 +13,11 @@ FunctionsToExport = @( "Confirm-ActionGroup", "Confirm-AksCluster", + "Confirm-ApiManagement", + "Confirm-ApiManagementApi", + "Confirm-ApiManagementDiagnostic", + "Confirm-ApiManagementLogger", + "Confirm-ApiManagementPolicy", "Confirm-AppInsights", "Confirm-AppServicePlan", "Confirm-BicepFile", diff --git a/Modules/BenchPress.Azure/Classes/ResourceType.psm1 b/Modules/BenchPress.Azure/Classes/ResourceType.psm1 index 2d6b100..2c1a519 100644 --- a/Modules/BenchPress.Azure/Classes/ResourceType.psm1 +++ b/Modules/BenchPress.Azure/Classes/ResourceType.psm1 @@ -1,6 +1,11 @@ enum ResourceType{ ActionGroup AksCluster + ApiManagement + ApiManagementApi + ApiManagementDiagnostic + ApiManagementLogger + ApiManagementPolicy AppInsights AppServicePlan ContainerApp diff --git a/Modules/BenchPress.Azure/Public/Confirm-ApiManagement.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ApiManagement.ps1 new file mode 100644 index 0000000..1b17b92 --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ApiManagement.ps1 @@ -0,0 +1,49 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ApiManagement { + <# + .SYNOPSIS + Confirms that an API Management Service exists. + + .DESCRIPTION + The Confirm-AzBPApiManagement cmdlet gets an API Management Service using the specified API Management Service + and Resource Group names. + + .PARAMETER ResourceGroupName + Specifies the name of the resource group under in which this cmdlet gets the API Management service. + + .PARAMETER Name + Specifies the name of API Management service. + + .EXAMPLE + Confirm-AzBPApiManagement -ResourceGroupName "rgbenchpresstest" -Name "benchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName, + + [Parameter(Mandatory=$true)] + [string]$Name + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + $Resource = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $Name + + [ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Confirm-ApiManagementApi.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementApi.ps1 new file mode 100644 index 0000000..c93dbbd --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementApi.ps1 @@ -0,0 +1,56 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ApiManagementApi { + <# + .SYNOPSIS + Confirms that an API Management API exists. + + .DESCRIPTION + The Confirm-AzBPApiManagementApi cmdlet gets an API Management API using the specified API, API Management + Service, and Resource Group names. + + .PARAMETER ResourceGroupName + Specifies the name of the resource group under which an API Management service is deployed. + + .PARAMETER ServiceName + Specifies the name of the deployed API Management service. + + .PARAMETER Name + Specifies the name of the API to get. + + .EXAMPLE + Confirm-AzBPApiManagementApi -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" -Name "benchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName, + + [Parameter(Mandatory=$true)] + [string]$ServiceName, + + [Parameter(Mandatory=$true)] + [string]$Name + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + $Resource = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName + | Get-AzApiManagementApi -Name $Name + + [ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Confirm-ApiManagementDiagnostic.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementDiagnostic.ps1 new file mode 100644 index 0000000..bec335f --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementDiagnostic.ps1 @@ -0,0 +1,57 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ApiManagementDiagnostic { + <# + .SYNOPSIS + Confirms that an API Management Diagnostic exists. + + .DESCRIPTION + The Confirm-AzBPApiManagementDiagnostic cmdlet gets an API Management Diagnostic using the specified API + Diagnostic, API, API Management Service, and Resource Group names. + + .PARAMETER ResourceGroupName + Specifies the name of the resource group under which an API Management service is deployed. + + .PARAMETER ServiceName + Specifies the name of the deployed API Management service. + + .PARAMETER Name + Identifier of existing diagnostic. This will return product-scope policy. This parameters is required. + + .EXAMPLE + Confirm-AzBPApiManagementDiagnostic -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" ` + -Name "benchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName, + + [Parameter(Mandatory=$true)] + [string]$ServiceName, + + [Parameter(Mandatory=$true)] + [string]$Name + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + $Resource = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName + | Get-AzApiManagementDiagnostic -DiagnosticId $Name + + [ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Confirm-ApiManagementLogger.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementLogger.ps1 new file mode 100644 index 0000000..6e16993 --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementLogger.ps1 @@ -0,0 +1,58 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ApiManagementLogger { + <# + .SYNOPSIS + Confirms that an API Management Logger exists. + + .DESCRIPTION + The Confirm-AzBPApiManagementLogger cmdlet gets an API Management Logger using the specified Logger, API + Management Service, and Resource Group names. + + .PARAMETER ResourceGroupName + Specifies the name of the resource group under which an API Management service is deployed. + + .PARAMETER ServiceName + Specifies the name of the deployed API Management service. + + .PARAMETER Name + Specifies the ID of the specific logger to get. + + .EXAMPLE + Confirm-AzBPApiManagementLogger -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" ` + -Name "benchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName, + + [Parameter(Mandatory=$true)] + [string]$ServiceName, + + [Parameter(Mandatory=$true)] + [string]$Name + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + # Unlike the other Get-AzApiManagement* cmdlets Get-AzApiManagementLogger does not accept piping of the context + $Context = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName + $Resource = Get-AzApiManagementLogger -Context $Context -LoggerId $Name + + [ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Confirm-ApiManagementPolicy.ps1 b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementPolicy.ps1 new file mode 100644 index 0000000..6077d84 --- /dev/null +++ b/Modules/BenchPress.Azure/Public/Confirm-ApiManagementPolicy.ps1 @@ -0,0 +1,62 @@ +# INLINE_SKIP +using module ./../Classes/ConfirmResult.psm1 + +. $PSScriptRoot/../Private/Connect-Account.ps1 +# end INLINE_SKIP + +function Confirm-ApiManagementPolicy { + <# + .SYNOPSIS + Confirms that an API Management Policy exists. + + .DESCRIPTION + The Confirm-AzBPApiManagementPolicy cmdlet gets an API Management Policy using the specified API, API Management + Service, and Resource Group names. + + .PARAMETER ResourceGroupName + Specifies the name of the resource group under which an API Management service is deployed. + + .PARAMETER ServiceName + Specifies the name of the deployed API Management service. + + .PARAMETER ApiId + Specifies the identifier of the existing API. This cmdlet returns the API-scope policy. + + .EXAMPLE + Confirm-AzBPApiManagementPolicy -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" ` + -ApiId "benchpresstest" + + .INPUTS + System.String + + .OUTPUTS + ConfirmResult + #> + [CmdletBinding()] + [OutputType([ConfirmResult])] + param ( + [Parameter(Mandatory=$true)] + [string]$ResourceGroupName, + + [Parameter(Mandatory=$true)] + [string]$ServiceName, + + [Parameter(Mandatory=$true)] + [string]$ApiId + ) + Begin { + $ConnectResults = Connect-Account + } + Process { + $policy = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName + | Get-AzApiManagementPolicy -ApiId $ApiId + + # Get-AzApiManagementPolicy returns the XML for a policy, not a resource + if ([string]::IsNullOrWhiteSpace($policy)) { + $policy = $null + } + + [ConfirmResult]::new($policy, $ConnectResults.AuthenticationData) + } + End { } +} diff --git a/Modules/BenchPress.Azure/Public/Confirm-Resource.ps1 b/Modules/BenchPress.Azure/Public/Confirm-Resource.ps1 index fddd31d..1b2646b 100644 --- a/Modules/BenchPress.Azure/Public/Confirm-Resource.ps1 +++ b/Modules/BenchPress.Azure/Public/Confirm-Resource.ps1 @@ -78,20 +78,14 @@ function Confirm-Resource { [CmdletBinding()] [OutputType([ConfirmResult])] param ( - [Parameter(Mandatory = $true)] - [ResourceType]$ResourceType, - [Parameter(Mandatory = $true)] [string]$ResourceName, [Parameter(Mandatory = $false)] [string]$ResourceGroupName, - [Parameter(Mandatory = $false)] - [string]$NamespaceName, - - [Parameter(Mandatory = $false)] - [string]$EventHubName, + [Parameter(Mandatory = $true)] + [ResourceType]$ResourceType, [Parameter(Mandatory = $false)] [string]$ServerName, @@ -99,12 +93,21 @@ function Confirm-Resource { [Parameter(Mandatory = $false)] [string]$DataFactoryName, + [Parameter(Mandatory = $false)] + [string]$NamespaceName, + + [Parameter(Mandatory = $false)] + [string]$EventHubName, + [Parameter(Mandatory = $false)] [string]$WorkspaceName, [Parameter(Mandatory = $false)] [string]$AccountName, + [Parameter(Mandatory = $false)] + [string]$ServiceName, + [Parameter(Mandatory = $false)] [string]$PropertyKey, @@ -114,15 +117,16 @@ function Confirm-Resource { Begin { } Process { $ResourceParams = @{ - ResourceType = $ResourceType - NamespaceName = $NamespaceName - EventHubName = $EventHubName ResourceName = $ResourceName ResourceGroupName = $ResourceGroupName + ResourceType = $ResourceType ServerName = $ServerName DataFactoryName = $DataFactoryName + NamespaceName = $NamespaceName + EventHubName = $EventHubName WorkspaceName = $WorkspaceName AccountName = $AccountName + ServiceName = $ServiceName } $ConfirmResult = Get-ResourceByType @ResourceParams diff --git a/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 b/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 index 09fc227..85d756f 100644 --- a/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 +++ b/Modules/BenchPress.Azure/Public/Get-ResourceByType.ps1 @@ -43,29 +43,7 @@ function Get-ResourceByType { The name of the Resource Group .PARAMETER ResourceType - The type of the Resource (currently support the following: - ActionGroup - AksCluster - AppInsights - AppServicePlan - ContainerRegistry - DataFactory - DataFactoryLinkedService - EventHub - EventHubConsumerGroup - EventHubNamespace - KeyVault - ResourceGroup - SqlDatabase - SqlServer - StorageAccount - StorageContainer - StreamAnalyticsCluster - SynapseSparkPool - SynapseSqlPool - SynapseWorkspace - VirtualMachine - WebApp) + The type of the Resource. .PARAMETER ServerName If testing an Azure SQL Database resource, the name of the server to which the database is assigned. @@ -79,7 +57,12 @@ function Get-ResourceByType { the name of the workspace to which the resource is assigned. .PARAMETER AccountName - If the Azure resource has an associated account name (e.g., Cosmos DB SQL Database, Storage Container), + If the Azure resource has an associated account name (e.g., Cosmos DB SQL Database, Storage Container) this is + the parameter to use to pass the account name. + + .PARAMETER ServiceName + If the Azure resource is associated with a service (e.g, API Management Service) this is the parameter to use to + pass the service name. .EXAMPLE Get-AzBPResourceByType -ResourceType ActionGroup -ResourceName "bpactiongroup" -ResourceGroupName "rgbenchpresstest" @@ -96,7 +79,7 @@ function Get-ResourceByType { [CmdletBinding()] [OutputType([ConfirmResult])] param ( - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $false)] [string]$ResourceName, [Parameter(Mandatory = $false)] @@ -121,7 +104,10 @@ function Get-ResourceByType { [string]$WorkspaceName, [Parameter(Mandatory = $false)] - [string]$AccountName + [string]$AccountName, + + [Parameter(Mandatory = $false)] + [string]$ServiceName ) Begin { } Process { @@ -132,6 +118,41 @@ function Get-ResourceByType { "AksCluster" { return Confirm-AksCluster -AKSName $ResourceName -ResourceGroupName $ResourceGroupName } + "ApiManagement" { + return Confirm-ApiManagement -ResourceGroupName $ResourceGroupName -Name $ResourceName + } + "ApiManagementApi" { + $params = @{ + ResourceGroupName = $ResourceGroupName + ServiceName = $ServiceName + Name = $ResourceName + } + return Confirm-ApiManagementApi @params + } + "ApiManagementDiagnostic" { + $params = @{ + ResourceGroupName = $ResourceGroupName + ServiceName = $ServiceName + Name = $ResourceName + } + return Confirm-ApiManagementDiagnostic @params + } + "ApiManagementLogger" { + $params = @{ + ResourceGroupName = $ResourceGroupName + ServiceName = $ServiceName + Name = $ResourceName + } + return Confirm-ApiManagementLogger @params + } + "ApiManagementPolicy" { + $params = @{ + ResourceGroupName = $ResourceGroupName + ServiceName = $ServiceName + ApiId = $ResourceName + } + return Confirm-ApiManagementPolicy @params + } "AppInsights" { return Confirm-AppInsights -ResourceGroupName $ResourceGroupName -Name $ResourceName } diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagement.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagement.Tests.ps1 new file mode 100644 index 0000000..01dc72f --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagement.Tests.ps1 @@ -0,0 +1,23 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ApiManagement.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ApiManagement" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + } + + It "Calls Get-AzApiManagement" { + Mock Get-AzApiManagement{} + Confirm-ApiManagement -Name "apim" -ResourceGroupName "rgn" + Should -Invoke -CommandName "Get-AzApiManagement" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementApi.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementApi.Tests.ps1 new file mode 100644 index 0000000..1f4d1e1 --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementApi.Tests.ps1 @@ -0,0 +1,26 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ApiManagementApi.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ApiManagementApi" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + Mock Get-AzApiManagementApi{} + Mock New-AzApiManagementContext{ + New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext' + } + } + + It "Calls Get-AzApiManagementApi" { + Confirm-ApiManagementApi -ResourceGroupName "rgn" -ServiceName "sn" -Name "api" + Should -Invoke -CommandName "Get-AzApiManagementApi" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementDiagnostic.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementDiagnostic.Tests.ps1 new file mode 100644 index 0000000..aab4019 --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementDiagnostic.Tests.ps1 @@ -0,0 +1,26 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ApiManagementDiagnostic.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ApiManagementDiagnostic" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + Mock Get-AzApiManagementDiagnostic{} + Mock New-AzApiManagementContext{ + New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext' + } + } + + It "Calls Get-AzApiManagementDiagnostic" { + Confirm-ApiManagementDiagnostic -ResourceGroupName "rgn" -ServiceName "sn" -Name "diag" + Should -Invoke -CommandName "Get-AzApiManagementDiagnostic" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementLogger.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementLogger.Tests.ps1 new file mode 100644 index 0000000..f6e3432 --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementLogger.Tests.ps1 @@ -0,0 +1,26 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ApiManagementLogger.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ApiManagementLogger" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + Mock Get-AzApiManagementLogger{} + Mock New-AzApiManagementContext{ + New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext' + } + } + + It "Calls Get-AzApiManagementLogger" { + Confirm-ApiManagementLogger -ResourceGroupName "rgn" -ServiceName "sn" -Name "logger" + Should -Invoke -CommandName "Get-AzApiManagementLogger" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementPolicy.Tests.ps1 b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementPolicy.Tests.ps1 new file mode 100644 index 0000000..33c485a --- /dev/null +++ b/Modules/BenchPress.Azure/Tests/Public/Confirm-ApiManagementPolicy.Tests.ps1 @@ -0,0 +1,26 @@ +BeforeAll { + . $PSScriptRoot/../../Public/Confirm-ApiManagementPolicy.ps1 + . $PSScriptRoot/../../Private/Connect-Account.ps1 + Import-Module Az +} + +Describe "Confirm-ApiManagementPolicy" { + Context "unit tests" -Tag "Unit" { + BeforeEach { + Mock Connect-Account{} + Mock Get-AzApiManagementPolicy{} + Mock New-AzApiManagementContext{ + New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext' + } + } + + It "Calls Get-AzApiManagementPolicy" { + Confirm-ApiManagementPolicy -ResourceGroupName "rgn" -ServiceName "sn" -ApiId "apiid" + Should -Invoke -CommandName "Get-AzApiManagementPolicy" -Times 1 + } + } +} + +AfterAll { + Remove-Module Az +} diff --git a/examples/ActionGroup/ActionGroup.Tests.ps1 b/examples/ActionGroup/ActionGroup.Tests.ps1 index ef04e9f..c895b51 100644 --- a/examples/ActionGroup/ActionGroup.Tests.ps1 +++ b/examples/ActionGroup/ActionGroup.Tests.ps1 @@ -88,6 +88,6 @@ Describe 'Verify Action Group' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/AksCluster/AKSCluster.Tests.ps1 b/examples/AksCluster/AKSCluster.Tests.ps1 index 8b494fa..fd7eb04 100644 --- a/examples/AksCluster/AKSCluster.Tests.ps1 +++ b/examples/AksCluster/AKSCluster.Tests.ps1 @@ -88,6 +88,6 @@ Describe 'Verify AKS Cluster' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/ApiManagement/ApiManagement.Tests.ps1 b/examples/ApiManagement/ApiManagement.Tests.ps1 new file mode 100644 index 0000000..80961fc --- /dev/null +++ b/examples/ApiManagement/ApiManagement.Tests.ps1 @@ -0,0 +1,371 @@ +BeforeAll { + Import-Module Az.InfrastructureTesting + + $Script:rgName = 'rg-test' + $Script:apiServiceName = 'servicetest' + $Script:apiName = 'apitest' + $Script:location = 'westus3' +} + +Describe 'Verify API Management Service' { + BeforeAll { + $Script:noApiServiceName = 'noservice' + } + + It 'Should contain an API Management Service with the given name - Confirm-AzBPResource' { + #arrange + $params = @{ + ResourceType = "ApiManagement" + ResourceGroupName = $rgName + ResourceName = $apiServiceName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + It "Should contain an API Management Service named $apiServiceName - ConfirmAzBPResource" { + #arrange + $params = @{ + ResourceType = "ApiManagement" + ResourceGroupName = $rgName + ResourceName = $apiServiceName + PropertyKey = 'Name' + PropertyValue = $apiServiceName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It 'Should contain an API Management Service with the given name' { + (Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName).Success | Should -Be $true + } + + It 'Should not contain an API Management Service with the given name' { + #arrange + $params = @{ + ResourceGroupName = $rgName + Name = $noApiServiceName + ErrorAction = "SilentlyContinue" + } + + #act and assert + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + (Confirm-AzBPApiManagement @params).Success | Should -Be $false + } + + It "Should contain an API Management Service named $apiServiceName" { + Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeDeployed + } + + It "Should contain an API Management Service named $apiServiceName in $location" { + Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeInLocation $location + } + + It "Should be an API Management Service in a resource group named $rgName" { + Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeInResourceGroup $rgName + } +} + +Describe 'Verify API Management API' { + BeforeAll { + $Script:noApiName = 'noapi' + } + + It 'Should contain an API Management API with the given name - Confirm-AzBPResource' { + #arrange + $params = @{ + ResourceType = "ApiManagementApi" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $apiName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain an API Management API named $apiName - ConfirmAzBPResource" { + #arrange + $params = @{ + ResourceType = "ApiManagementApi" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $apiName + PropertyKey = 'Name' + PropertyValue = $apiName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It 'Should contain an API Management API with the given name' { + (Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName).Success + | Should -Be $true + } + + It 'Should not contain an API Management API with the given name' { + # arrange + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + $params = @{ + ResourceGroupName = $rgName + Name = $noApiName + ServiceName = $apiServiceName + ErrorAction = "SilentlyContinue" + } + + #act and assert + (Confirm-AzBPApiManagementApi @params).Success | Should -Be $false + } + + It "Should contain an API Management API named $apiName" { + Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName + | Should -BeDeployed + } + + It "Should be an API Management API in a resource group named $rgName" { + Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName + | Should -BeInResourceGroup $rgName + } +} + +Describe 'Verify API Management Diagnostic' { + BeforeAll { + $Script:diagnosticName = 'diagtest' + $Script:noDiagnosticName = 'nodiag' + } + + It 'Should contain an API Management Diagnostic with the given name - Confirm-AzBPResource' { + #arrange + $params = @{ + ResourceType = "ApiManagementDiagnostic" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $diagnosticName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain an API Management Diagnostic named $diagnosticName - ConfirmAzBPResource" { + #arrange + $params = @{ + ResourceType = "ApiManagementDiagnostic" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $diagnosticName + PropertyKey = 'DiagnosticId' + PropertyValue = $diagnosticName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It 'Should contain an API Management Diagnostic with the given name' { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $diagnosticName + } + + #act and assert + (Confirm-AzBPApiManagementDiagnostic @params).Success | Should -Be $true + } + + It 'Should not contain an API Management Diagnostic with the given name' { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $noDiagnosticName + ErrorAction = "SilentlyContinue" + } + + #act and assert + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + (Confirm-AzBPApiManagementDiagnostic @params).Success | Should -Be $false + } + + It "Should contain an API Management Diagnostic named $diagnosticName" { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $diagnosticName + } + + #act and assert + Confirm-AzBPApiManagementDiagnostic @params | Should -BeDeployed + } + + It "Should be an API Management Diagnostic in a resource group named $rgName" { + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $diagnosticName + } + + #act and assert + Confirm-AzBPApiManagementDiagnostic @params | Should -BeInResourceGroup $rgName + } +} + +Describe 'Verify API Management Logger' { + BeforeAll { + $Script:loggerName = 'loggertest' + $Script:noLoggerName = 'nologger' + } + + It 'Should contain an API Management Logger with the given name - Confirm-AzBPResource' { + #arrange + $params = @{ + ResourceType = "ApiManagementLogger" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $loggerName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain an API Management Logger named $loggerName - ConfirmAzBPResource" { + #arrange + $params = @{ + ResourceType = "ApiManagementLogger" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $loggerName + PropertyKey = 'LoggerId' + PropertyValue = $loggerName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It 'Should contain an API Management Logger with the given name' { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $loggerName + } + + #act and assert + (Confirm-AzBPApiManagementLogger @params).Success | Should -Be $true + } + + It 'Should not contain an API Management Logger with the given name' { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $noLoggerName + ErrorAction = "SilentlyContinue" + } + + #act and assert + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + (Confirm-AzBPApiManagementLogger @params).Success | Should -Be $false + } + + It "Should contain an API Management Logger named $loggerName" { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $loggerName + } + + #act and assert + Confirm-AzBPApiManagementLogger @params | Should -BeDeployed + } + + It "Should be an API Management Logger in a resource group named $rgName" { + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + Name = $loggerName + } + + #act and assert + Confirm-AzBPApiManagementLogger @params | Should -BeInResourceGroup $rgName + } +} + +Describe 'Verify API Management Policy' { + BeforeAll { + $Script:noApiId = 'nopolicy' + } + + It "Should contain an API Management Policy for the API ID $apiName - Confirm-AzBPResource" { + #arrange + $params = @{ + ResourceType = "ApiManagementPolicy" + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ResourceName = $apiName + } + + #act and assert + (Confirm-AzBPResource @params).Success | Should -Be $true + } + + It "Should contain an API Management Policy for the API ID $apiName" { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ApiId = $apiName + } + + #act and assert + (Confirm-AzBPApiManagementPolicy @params).Success | Should -Be $true + } + + It "Should not contain an API Management Policy for the API ID $apiName" { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ApiId = $noApiId + ErrorAction = "SilentlyContinue" + } + + #act and assert + # The '-ErrorAction SilentlyContinue' command suppresses all errors. + # In this test, it will suppress the error message when a resource cannot be found. + # Remove this field to see all errors. + (Confirm-AzBPApiManagementPolicy @params).Success | Should -Be $false + } + + It "Should contain an API Management Policy for the API ID $apiName" { + #arrange + $params = @{ + ResourceGroupName = $rgName + ServiceName = $apiServiceName + ApiId = $apiName + } + + #act and assert + Confirm-AzBPApiManagementPolicy @params | Should -BeDeployed + } +} + +AfterAll { + Get-Module Az.InfrastructureTesting | Remove-Module + Get-Module BenchPress.Azure | Remove-Module +} diff --git a/examples/ApiManagement/ApiManagement.bicep b/examples/ApiManagement/ApiManagement.bicep new file mode 100644 index 0000000..4055aaa --- /dev/null +++ b/examples/ApiManagement/ApiManagement.bicep @@ -0,0 +1,85 @@ +param serviceName string = 'apim${take(uniqueString(resourceGroup().id), 5)}' +param location string = resourceGroup().location + +resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = { + name: serviceName + location: location + sku: { + capacity: 1 + name: 'Developer' + } + properties: { + publisherEmail: 'email@contoso.com' + publisherName: 'Contoso' + } +} + +param apiName string = 'api${take(uniqueString(resourceGroup().id), 5)}' + +resource api 'Microsoft.ApiManagement/service/apis@2022-08-01' = { + name: apiName + parent: apiManagementService + properties: { + displayName: 'api' + path: '/' + protocols: ['https'] + } +} + +param workspaceName string = 'logworkspace${take(uniqueString(resourceGroup().id), 5)}' + +resource workspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = { + name: workspaceName + location: location + properties: { + sku: { + name: 'PerGB2018' + } + } +} + +param appInsightsName string = 'appinsights${take(uniqueString(resourceGroup().id), 5)}' + +resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = { + name: appInsightsName + location: location + kind: 'web' + properties: { + Application_Type: 'web' + WorkspaceResourceId: workspace.id + } +} + +param loggerName string = 'log${take(uniqueString(resourceGroup().id), 5)}' + +resource logger 'Microsoft.ApiManagement/service/loggers@2022-08-01' = { + name: loggerName + parent: apiManagementService + properties: { + credentials: { + instrumentationKey: applicationInsights.properties.InstrumentationKey + } + loggerType: 'applicationInsights' + } +} + +param diagnosticName string = 'applicationinsights' + +resource diagnostic 'Microsoft.ApiManagement/service/diagnostics@2022-08-01' = { + name: diagnosticName + parent: apiManagementService + properties: { + loggerId: logger.id + } +} + +param policyName string = 'policy' + +resource apiPolicy 'Microsoft.ApiManagement/service/apis/policies@2022-08-01' = { + name: policyName + parent: api + properties: { + format: 'rawxml' + value: loadTextContent('./ApiManagementPolicy.xml') + } +} diff --git a/examples/ApiManagement/ApiManagementPolicy.xml b/examples/ApiManagement/ApiManagementPolicy.xml new file mode 100644 index 0000000..21c7fc3 --- /dev/null +++ b/examples/ApiManagement/ApiManagementPolicy.xml @@ -0,0 +1,63 @@ + + + + + + + + * + + + PUT + GET + POST + DELETE + PATCH + + +
*
+
+ +
*
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/examples/ApiManagement/ApiManagement_Example.md b/examples/ApiManagement/ApiManagement_Example.md new file mode 100644 index 0000000..b35bdf5 --- /dev/null +++ b/examples/ApiManagement/ApiManagement_Example.md @@ -0,0 +1,51 @@ +# How To Run ApiManagement.Tests.ps1 + +`ApiManagement.Tests.ps1` contains examples of using the `Confirm-AzBPApiManagement`, `Confirm-AzBPApiManagementApi`, +`Confirm-AzBPApiManagementDiagnostic`, `Confirm-AzBPApiManagementLogger`, and `Confirm-AzBPApiManagementPolicy` +cmdlets. + +## Pre-Requisites + +- Follow the [setup instructions](../README.md) + +## Steps + +1. Navigate to ApiManagement directory: + + ```Powershell + cd examples\ApiManagement\ + ``` + +1. Deploy the Api Management Service to your resource group: + + ```Powershell + New-AzResourceGroupDeployment -ResourceGroupName ""` + -TemplateFile ".\ApiManagement.bicep" + ``` + +1. Update `ApiManagement.Tests.ps1` variables to point to your expected resources: + + - `rg-test` -> `your-resource-group-name` + - `servicetest` -> `your-api-management-service-name` + - `apitest` -> `your-api-management-api-name` + - `diagtest` -> `your-api-management-diagnostic-name` + - `loggertest` -> `your-api-managemetn-logger-name` + - `westus` -> `your-resource-group-location` + +1. If using a local copy of `Az.InfrastructureTesting`, replace `Import-Module Az.InfrastructureTesting` with +`Import-Module "../../bin/BenchPress.Azure.psd1"`. + +1. Run `ApiManagement.Tests.ps1`: + + ```Powershell + Invoke-Pester -Path .\ApiManagement.Tests.ps1 + ``` + +1. Success! + + ```Powershell + Tests completed in 20.02s + Tests Passed: 29, Failed: 0, Skipped: 0 NotRun: 0 + ``` + +1. Don't forget to delete any deployed resources that are no longer needed. diff --git a/examples/AppInsights/AppInsights.Tests.ps1 b/examples/AppInsights/AppInsights.Tests.ps1 index df777d2..1ee94a2 100644 --- a/examples/AppInsights/AppInsights.Tests.ps1 +++ b/examples/AppInsights/AppInsights.Tests.ps1 @@ -88,6 +88,6 @@ Describe 'Verify App Insights' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/AppServicePlan/AppServicePlan.Tests.ps1 b/examples/AppServicePlan/AppServicePlan.Tests.ps1 index 4ae596c..2b1f1d6 100644 --- a/examples/AppServicePlan/AppServicePlan.Tests.ps1 +++ b/examples/AppServicePlan/AppServicePlan.Tests.ps1 @@ -87,6 +87,6 @@ Describe 'Verify App Service Plan' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/BicepHelpers/BicepHelpers.Tests.ps1 b/examples/BicepHelpers/BicepHelpers.Tests.ps1 index 12ba618..eeb1d4e 100644 --- a/examples/BicepHelpers/BicepHelpers.Tests.ps1 +++ b/examples/BicepHelpers/BicepHelpers.Tests.ps1 @@ -23,6 +23,6 @@ Describe 'Spin up , Tear down Action Group' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/Common/Common.Tests.ps1 b/examples/Common/Common.Tests.ps1 index ee392c0..33ea275 100644 --- a/examples/Common/Common.Tests.ps1 +++ b/examples/Common/Common.Tests.ps1 @@ -32,7 +32,7 @@ Describe 'Verify Resource Exists' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/ContainerRegistry/ContainerRegistry.Tests.ps1 b/examples/ContainerRegistry/ContainerRegistry.Tests.ps1 index 32d3b72..38ae25c 100644 --- a/examples/ContainerRegistry/ContainerRegistry.Tests.ps1 +++ b/examples/ContainerRegistry/ContainerRegistry.Tests.ps1 @@ -88,6 +88,6 @@ Describe 'Verify Container Registry' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/CosmosDB/CosmosDB.Tests.ps1 b/examples/CosmosDB/CosmosDB.Tests.ps1 index c3375b8..4a26ade 100644 --- a/examples/CosmosDB/CosmosDB.Tests.ps1 +++ b/examples/CosmosDB/CosmosDB.Tests.ps1 @@ -631,6 +631,6 @@ Describe 'Comsos DB SQL Database' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/DataFactory/DataFactory.Tests.ps1 b/examples/DataFactory/DataFactory.Tests.ps1 index 34bf238..7ff9582 100644 --- a/examples/DataFactory/DataFactory.Tests.ps1 +++ b/examples/DataFactory/DataFactory.Tests.ps1 @@ -159,6 +159,6 @@ Describe 'Verify Data Factory Linked Service' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/EventHub/EventHub.Tests.ps1 b/examples/EventHub/EventHub.Tests.ps1 index 88dcace..7edd815 100644 --- a/examples/EventHub/EventHub.Tests.ps1 +++ b/examples/EventHub/EventHub.Tests.ps1 @@ -333,6 +333,6 @@ Describe 'Verify EventHub Consumer Group' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/KeyVault/KeyVault.Tests.ps1 b/examples/KeyVault/KeyVault.Tests.ps1 index 9a40bd1..fa67150 100644 --- a/examples/KeyVault/KeyVault.Tests.ps1 +++ b/examples/KeyVault/KeyVault.Tests.ps1 @@ -123,6 +123,6 @@ Describe 'Verify KeyVault' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/OperationalInsightsWorkspace/OperationalInsightsWorkspace.Tests.ps1 b/examples/OperationalInsightsWorkspace/OperationalInsightsWorkspace.Tests.ps1 index 738d585..1e33beb 100644 --- a/examples/OperationalInsightsWorkspace/OperationalInsightsWorkspace.Tests.ps1 +++ b/examples/OperationalInsightsWorkspace/OperationalInsightsWorkspace.Tests.ps1 @@ -93,6 +93,6 @@ Describe 'Verify Operational Insights Workspace Exists' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/ResourceGroup/ResourceGroup.Tests.ps1 b/examples/ResourceGroup/ResourceGroup.Tests.ps1 index 2cf8800..e975a2a 100644 --- a/examples/ResourceGroup/ResourceGroup.Tests.ps1 +++ b/examples/ResourceGroup/ResourceGroup.Tests.ps1 @@ -78,6 +78,6 @@ Describe 'Verify Resource Group Exists' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/SqlDatabase/SqlDatabase.Tests.ps1 b/examples/SqlDatabase/SqlDatabase.Tests.ps1 index 83146a1..87ab245 100644 --- a/examples/SqlDatabase/SqlDatabase.Tests.ps1 +++ b/examples/SqlDatabase/SqlDatabase.Tests.ps1 @@ -92,6 +92,6 @@ Describe 'Verify Sql Database' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/SqlServer/SqlServer.Tests.ps1 b/examples/SqlServer/SqlServer.Tests.ps1 index 1c1d1d7..88a349b 100644 --- a/examples/SqlServer/SqlServer.Tests.ps1 +++ b/examples/SqlServer/SqlServer.Tests.ps1 @@ -89,6 +89,6 @@ Describe 'Verify Sql Server' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/Storage/Storage.Tests.ps1 b/examples/Storage/Storage.Tests.ps1 index a897c96..c9fb353 100644 --- a/examples/Storage/Storage.Tests.ps1 +++ b/examples/Storage/Storage.Tests.ps1 @@ -178,6 +178,6 @@ Describe 'Verify Storage Container' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/StreamAnalytics/StreamAnalytics.Tests.ps1 b/examples/StreamAnalytics/StreamAnalytics.Tests.ps1 index 35b20b7..a51552e 100644 --- a/examples/StreamAnalytics/StreamAnalytics.Tests.ps1 +++ b/examples/StreamAnalytics/StreamAnalytics.Tests.ps1 @@ -409,6 +409,6 @@ Describe 'Stream Analytics Jobs' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/Synapse/Synapse.Tests.ps1 b/examples/Synapse/Synapse.Tests.ps1 index f258aa0..4f0ccef 100644 --- a/examples/Synapse/Synapse.Tests.ps1 +++ b/examples/Synapse/Synapse.Tests.ps1 @@ -288,6 +288,6 @@ Describe 'Verify Synapse Spark/SQL Pool' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/VirtualMachine/VirtualMachine.Tests.ps1 b/examples/VirtualMachine/VirtualMachine.Tests.ps1 index 4dcf839..e7df48c 100644 --- a/examples/VirtualMachine/VirtualMachine.Tests.ps1 +++ b/examples/VirtualMachine/VirtualMachine.Tests.ps1 @@ -95,6 +95,6 @@ Describe 'Verify Virtual Machine' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module } diff --git a/examples/WebApp/WebApp.Tests.ps1 b/examples/WebApp/WebApp.Tests.ps1 index 78010d3..8e3db36 100644 --- a/examples/WebApp/WebApp.Tests.ps1 +++ b/examples/WebApp/WebApp.Tests.ps1 @@ -88,6 +88,6 @@ Describe 'Verify Web App Exists' { } AfterAll { - Get-Module Az-InfrastructureTesting | Remove-Module + Get-Module Az.InfrastructureTesting | Remove-Module Get-Module BenchPress.Azure | Remove-Module }