Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/code-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Run Pester Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3

- name: Install Required Modules
shell: pwsh
run: |
Install-Module -Name Pester -Force -Scope CurrentUser -MinimumVersion 5.0
Install-Module -Name ImportExcel -Force -Scope CurrentUser

- name: Run Pester Tests
shell: pwsh
run: |
$config = New-PesterConfiguration
$config.Run.Path = @(
'.\1-Collect\*.Tests.ps1',
'.\2-AvailabilityCheck\*.Tests.ps1',
'.\3-CostInformation\*.Tests.ps1',
'.\7-Report\*.Tests.ps1'
)
$config.Output.Verbosity = 'Detailed'
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = 'NUnitXml'
$config.TestResult.OutputPath = './TestResults.xml'
$config.Run.Exit = $true
$config.Run.PassThru = $true

Invoke-Pester -Configuration $config

- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: './TestResults.xml'

# - name: Publish Test Results
# uses: EnricoMi/publish-unit-test-result-action/windows@v2
# if: always()
# with:
# files: './TestResults.xml'
# check_name: 'Pester Test Results'
62 changes: 62 additions & 0 deletions 1-Collect/Get-AzureServices.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
BeforeAll {
$scriptPath = "$PSScriptRoot\Get-AzureServices.ps1"
}

Describe "Get-AzureServices.ps1 Tests" {
Context "Parameter Validation" {
It "Should accept valid scopeType values" {
$validScopes = @('singleSubscription', 'resourceGroup', 'multiSubscription')

# Parse the script to check parameter validation
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'ValidateSet.*singleSubscription.*resourceGroup.*multiSubscription'
}

It "Should have required parameters defined" {
$scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($scriptPath, [ref]$null, [ref]$null)
$params = $scriptAst.FindAll({$args[0] -is [System.Management.Automation.Language.ParameterAst]}, $true)

$paramNames = $params | ForEach-Object { $_.Name.VariablePath.UserPath }
$paramNames | Should -Contain 'scopeType'
$paramNames | Should -Contain 'fullOutputFile'
$paramNames | Should -Contain 'summaryOutputFile'
}

It "Should have default values for output files" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'fullOutputFile.*=.*"resources.json"'
$scriptContent | Should -Match 'summaryOutputFile.*=.*"summary.json"'
}
}

Context "Function Definitions" {
It "Should define Get-Property function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Get-Property'
}

It "Should define Get-SingleData function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Get-SingleData'
}

It "Should define Get-Method function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Get-Method'
}
}

Context "Output File Generation" {
It "Should create resources.json output file" {
# This test would require mocking Azure cmdlets
# Placeholder for integration test
$true | Should -Be $true
}

It "Should create summary.json output file" {
# This test would require mocking Azure cmdlets
# Placeholder for integration test
$true | Should -Be $true
}
}
}
16 changes: 7 additions & 9 deletions 1-Collect/Get-AzureServices.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,20 @@ $baseResult | ForEach-Object {
else {
Get-Method -resourceType $resourceType -flagType "Sku" -object $PSItem
}
# if $sku is a single string and is not N/A then turn it into an object with name and the current sku value
if ($sku -is [string] -and $sku -ne "N/A") {
$tempSku = [PSCustomObject]@{
name = $sku
}
$sku = $tempSku
}
$json = Get-Content -Path .\modules\sku.json | ConvertFrom-Json -depth 100
$excludeList = $json | Where-Object { $_.resourceType -eq $resourceType -and $_.excludeFromReport -ne $null }
if ($excludeList) {
foreach ($excludeProp in $excludeList.excludeFromReport) {
$sku.PSObject.Properties.Remove($excludeProp)
}
}
$str = ""
foreach ($property in $sku.PSObject.Properties) {
$str += "$($property.value.ToString())_"
}
$str = $str.TrimEnd('_')
Add-Member -InputObject $sku -MemberType NoteProperty -Name "skuName" -Value $str -Force



Get-Method -resourceType $resourceType -flagType "resiliencyProperties" -object $PSItem
Get-Method -resourceType $resourceType -flagType "dataSize" -object $PSItem
Get-Method -resourceType $resourceType -flagType "ipConfig" -object $PSItem
Expand Down
84 changes: 84 additions & 0 deletions 1-Collect/Get-RessourcesFromAM.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
BeforeAll {
$scriptPath = "$PSScriptRoot\Get-RessourcesFromAM.ps1"
}

Describe "Get-RessourcesFromAM.ps1 Tests" {
Context "Parameter Validation" {
It "Should require filePath parameter" {
$scriptAst = [System.Management.Automation.Language.Parser]::ParseFile($scriptPath, [ref]$null, [ref]$null)
$params = $scriptAst.FindAll({$args[0] -is [System.Management.Automation.Language.ParameterAst]}, $true)

$filePathParam = $params | Where-Object { $_.Name.VariablePath.UserPath -eq 'filePath' }
$filePathParam | Should -Not -BeNullOrEmpty

# Check if parameter is mandatory
$isMandatory = $filePathParam.Attributes | Where-Object {
$_.TypeName.Name -eq 'Parameter' -and
$_.NamedArguments.ArgumentName -contains 'Mandatory'
}
$isMandatory | Should -Not -BeNullOrEmpty
}

It "Should have default output file" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'outputFile.*=.*".*summary\.json"'
}
}

Context "Excel File Processing" {
It "Should check for Excel file existence" {
Mock Test-Path { return $false }

# Test would validate file existence check
$true | Should -Be $true
}

It "Should validate required worksheets exist" {
# This would test for 'All_Assessed_Machines' and 'All_Assessed_Disks'
$requiredSheets = @('All_Assessed_Machines', 'All_Assessed_Disks')
$requiredSheets.Count | Should -Be 2
}
}

Context "SKU Conversion" {
It "Should convert Premium disk SKU correctly" {
$testSku = "Premium SSD P30"
$expected = "Premium_LRS"

$result = switch -Wildcard ($testSku) {
"PremiumV2*" { "PremiumV2_LRS"; break }
"Premium*" { "Premium_LRS"; break }
"StandardSSD*" { "StandardSSD_LRS"; break }
"Standard*" { "Standard_LRS"; break }
"Ultra*" { "UltraSSD_LRS"; break }
default { "Unknown" }
}

$result | Should -Be $expected
}

It "Should convert StandardSSD disk SKU correctly" {
$testSku = "StandardSSD E10"

$result = switch -Wildcard ($testSku) {
"PremiumV2*" { "PremiumV2_LRS"; break }
"Premium*" { "Premium_LRS"; break }
"StandardSSD*" { "StandardSSD_LRS"; break }
"Standard*" { "Standard_LRS"; break }
"Ultra*" { "UltraSSD_LRS"; break }
default { "Unknown" }
}

$result | Should -Be "StandardSSD_LRS"
}
}

Context "Output Generation" {
It "Should generate correct resource types" {
$expectedTypes = @("microsoft.compute/disks", "microsoft.compute/virtualmachines")
$expectedTypes.Count | Should -Be 2
$expectedTypes | Should -Contain "microsoft.compute/disks"
$expectedTypes | Should -Contain "microsoft.compute/virtualmachines"
}
}
}
7 changes: 7 additions & 0 deletions 1-Collect/modules/sku.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
"description": "Get sku for a SQL Database",
"isContainedInOriginalGraphOutput": true,
"excludeFromReport": ["family"]
},
{
"resourceType": "microsoft.apimanagement/service",
"property": "sku",
"description": "Get sku for an API Management Service",
"isContainedInOriginalGraphOutput": true,
"excludeFromReport": ["capacity"]
}
]

74 changes: 74 additions & 0 deletions 2-AvailabilityCheck/Get-AvailabilityInformation.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
BeforeAll {
$scriptPath = "$PSScriptRoot\Get-AvailabilityInformation.ps1"
}

Describe "Get-AvailabilityInformation.ps1 Tests" {
Context "Function Definitions" {
It "Should define Out-JSONFile function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'function Out-JSONFile'
}

It "Should define Convert-LocationsToRegionCodes function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Convert-LocationsToRegionCodes'
}

It "Should define Import-Provider function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Import-Provider'
}

It "Should define Import-Region function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'function Import-Region'
}

It "Should define Get-Property function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Get-Property'
}

It "Should define Expand-NestedCollection function" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'Function Expand-NestedCollection'
}
}

Context "Logic Validation" {
It "Should have region map creation logic" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'RegionMap'
}

It "Should have SKU availability checking logic" {
$scriptContent = Get-Content $scriptPath -Raw
$scriptContent | Should -Match 'available'
}
}

Context "File Dependencies" {
It "Should check for summary.json from 1-Collect" {
$summaryPath = "$(Get-Location)\..\1-Collect\summary.json"
# Test would validate the file check logic
$summaryPath | Should -Not -BeNullOrEmpty
}

It "Should check for propertyMaps.json" {
$propertyMapPath = ".\propertymaps\propertyMaps.json"
$propertyMapPath | Should -Not -BeNullOrEmpty
}
}

Context "Output Files" {
It "Should generate Availability_Mapping.json" {
$outputFile = "Availability_Mapping.json"
$outputFile | Should -Be "Availability_Mapping.json"
}

It "Should generate Azure_Providers.json" {
$outputFile = "Azure_Providers.json"
$outputFile | Should -Be "Azure_Providers.json"
}
}
}
Loading