Skip to content

Commit

Permalink
Update azmodule test for version (#68)
Browse files Browse the repository at this point in the history
# Pull Request

## Description

Description of changes: Updated Az module tests to check for a specific
version of at least 10.0.0.

## License

By submitting this pull request, I confirm that my contribution is made
under the terms of the projects associated license.
  • Loading branch information
oZakari authored Aug 16, 2023
1 parent d12c1b6 commit 10e95d3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Test-ALZRequirement

Currently this tests for:

* Supported minimum PowerShell version
* Azure PowerShell Module
* Supported minimum PowerShell version (7.1)
* Supported minimum Az PowerShell module version (10.0.0)
* Git
* Azure CLI
* Bicep
Expand Down
5 changes: 4 additions & 1 deletion src/ALZ/Private/Test-Utility-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
function Get-PSVersion { $PSVersionTable }

#Unable to mock $PSScriptRoot variable
function Get-ScriptRoot { $PSScriptRoot }
function Get-ScriptRoot { $PSScriptRoot }

# Used to allow mocking of the Get-Module AZ
function Get-AZVersion { Get-Module -Name Az -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 }
12 changes: 6 additions & 6 deletions src/ALZ/Public/Test-ALZRequirement.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ function Test-ALZRequirement {
Write-Error "Bicep is not installed. Please install Bicep."
$result = $false
}
# Check if Azure PowerShell module is installed
$azModule = Get-Module -Name Az -ListAvailable
if ($azModule) {
Write-Verbose "Azure PowerShell module is installed."
} else {
Write-Error "Azure PowerShell module is not installed. Please install the Azure PowerShell module."
# Check if AZ PowerShell module is the correct version
$azModule = Get-AZVersion
if ($azModule.Version.Major -lt 10) {
Write-Error "Az module version $($azModule.Version) is not supported. Please Upgrade to AZ module version 10.0.0 or higher."
$result = $false
} else {
Write-Verbose "Az module version is supported."
}
if ($result) {
return "ALZ requirements are met."
Expand Down
32 changes: 19 additions & 13 deletions src/Tests/Unit/Private/Test-ALZRequirement.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ Import-Module $PathToManifest -Force
#-------------------------------------------------------------------------

InModuleScope 'ALZ' {
Describe 'Request-ConfigurationValue Public Function Tests' -Tag Unit {
Describe 'Test-ALZRequirement Public Function Tests' -Tag Unit {
BeforeAll {
$WarningPreference = 'SilentlyContinue'
$ErrorActionPreference = 'SilentlyContinue'
}
Context 'Non Az Module' {
Context 'Incompatible Az module version lower than 10.0.0' {
BeforeEach {
Mock -CommandName Get-Module -MockWith {
$null
Mock -CommandName Get-AZVersion -MockWith {
[PSCustomObject]@{
Version = [PSCustomObject]@{
Major = 9
Minor = 7
}
}
}
}
It 'should return the not met for non AZ module' {
It 'should return the not met for non compatible az module versions' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Context 'Incompatible Powershell version lower them 7' {
Context 'Incompatible Powershell version lower than 7' {
BeforeEach {
Mock -CommandName Get-PSVersion -MockWith {
[PSCustomObject]@{
Expand Down Expand Up @@ -63,7 +68,7 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no git instalation' {
It 'should return the not met for no git installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Expand All @@ -73,7 +78,7 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no Visual Studio Code instalation' {
It 'should return the not met for no Visual Studio Code installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Expand All @@ -83,16 +88,18 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no bicep instalation' {
It 'should return the not met for no bicep installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Context 'Success' {

BeforeEach {
Mock -CommandName Get-Module -MockWith {
Mock -CommandName Get-AZVersion -MockWith {
[PSCustomObject]@{
Name = 'Az'
Version = [PSCustomObject]@{
Major = 10
Minor = 0
}
}
}
Mock -CommandName Get-PSVersion -MockWith {
Expand Down Expand Up @@ -123,7 +130,6 @@ InModuleScope 'ALZ' {
It 'should return the expected results' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are met."
}

}
}
}
28 changes: 18 additions & 10 deletions src/Tests/Unit/Public/Test-ALZRequirement.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ InModuleScope 'ALZ' {
$WarningPreference = 'SilentlyContinue'
$ErrorActionPreference = 'SilentlyContinue'
}
Context 'Non Az Module' {
Context 'Incompatible Az module version lower than 10.0.0' {
BeforeEach {
Mock -CommandName Get-Module -MockWith {
$null
Mock -CommandName Get-AZVersion -MockWith {
[PSCustomObject]@{
Version = [PSCustomObject]@{
Major = 9
Minor = 7
}
}
}
}
It 'should return the not met for non AZ module' {
It 'should return the not met for non compatible az module versions' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Context 'Incompatible Powershell version lower them 7' {
Context 'Incompatible Powershell version lower than 7' {
BeforeEach {
Mock -CommandName Get-PSVersion -MockWith {
[PSCustomObject]@{
Expand Down Expand Up @@ -63,7 +68,7 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no git instalation' {
It 'should return the not met for no git installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Expand All @@ -73,7 +78,7 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no Visual Studio Code instalation' {
It 'should return the not met for no Visual Studio Code installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Expand All @@ -83,15 +88,18 @@ InModuleScope 'ALZ' {
$null
}
}
It 'should return the not met for no bicep instalation' {
It 'should return the not met for no bicep installation' {
Test-ALZRequirement | Should -BeExactly "ALZ requirements are not met."
}
}
Context 'Success' {
BeforeEach {
Mock -CommandName Get-Module -MockWith {
Mock -CommandName Get-AZVersion -MockWith {
[PSCustomObject]@{
Name = 'Az'
Version = [PSCustomObject]@{
Major = 10
Minor = 0
}
}
}
Mock -CommandName Get-PSVersion -MockWith {
Expand Down

0 comments on commit 10e95d3

Please sign in to comment.