Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Dependencies #650

Merged
merged 5 commits into from
Jun 18, 2022
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
14 changes: 13 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ jobs:
run: |
./src/tests/Pester.ps1 -TestFunctions $true

# Run Environment Primer
- name: "Run Environment Primer"
shell: pwsh
run: |
./src/tests/Pester.ps1 -CleanupEnvironment $true

# Run Functional Tests
- name: "Run Functional Tests"
shell: pwsh
Expand All @@ -80,4 +86,10 @@ jobs:
if: always()
shell: pwsh
run: |
./src/tests/Pester.ps1 -TestIntegration $true
./src/tests/Pester.ps1 -TestIntegration $true

# Run Cleanup Environment
- name: "Run Environment Cleanup"
shell: pwsh
run: |
./src/tests/Pester.ps1 -CleanupEnvironment $true
2 changes: 1 addition & 1 deletion scripts/New-AzOpsTestsDeploymentHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
'ResourceGroup' {
try {
Write-PSFMessage -Level Verbose -Message "Deployment of $script:resourceType starting at $Scope scope."
$script:resourceGroupName = $script:resourceType + "-" + (Get-Date -UFormat "%Y%m%d%R" | ForEach-Object {$_ -replace ":", ""}) + "-rg"
$script:resourceGroupName = $script:resourceType + "-" + (Get-Date -UFormat "%Y%m%d%R" | ForEach-Object {$_ -replace ":", ""}) + "-azopsrg"
$script:scope = New-AzResourceGroup -Name $script:resourceGroupName -Location $Location -Confirm:$false -Force
if ($script:templateParametersFile) {
$script:functionalTestDeploy = New-AzResourceGroupDeployment -Name ($script:resourceType + 'testdeploy') -ResourceGroupName $script:resourceGroupName -TemplateFile $script:templateFile -TemplateParameterFile $script:templateParametersFile -Confirm:$false -Force
Expand Down
98 changes: 98 additions & 0 deletions scripts/Remove-AzOpsTestsDeployment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function Remove-AzOpsTestsDeployment {

<#
.SYNOPSIS
Assist in removal of AzOps Test Deployments, destructive command removes resources in the context executed.
.DESCRIPTION
Assist in removal of AzOps Test Deployments, destructive command removes resources in the context executed.
.EXAMPLE
> Remove-AzOpsTestsDeployment -CleanupEnvironment:$true
#>

[CmdletBinding()]
param (
$cleanupEnvironment = $false
)

process {
if ($CleanupEnvironment) {
function Remove-ManagementGroups {

param (
[Parameter()]
[string]
$DisplayName,

[Parameter()]
[string]
$Name,

[Parameter()]
[string]
$RootName
)

process {
# Retrieve list of children within the provided Management Group Id
$children = (Get-AzManagementGroup -GroupId $Name -Expand -Recurse -WarningAction SilentlyContinue).Children
if ($children) {
$children | ForEach-Object {
if ($_.Type -eq "Microsoft.Management/managementGroups") {
# Invoke function again with Child resources
Write-PSFMessage -Level Verbose -Message "Nested Management Group: $($DisplayName)" -FunctionName "Remove-AzOpsTestsDeployment"
Remove-ManagementGroups -DisplayName $_.DisplayName -Name $_.Name -RootName $RootName
}
if ($_.Type -eq '/subscriptions') {
# Move Subscription resource to Tenant Root Group
Write-PSFMessage -Level Verbose -Message "Moving Subscription: $($_.Name)" -FunctionName "Remove-AzOpsTestsDeployment"
$null = New-AzManagementGroupSubscription -GroupId $RootName -SubscriptionId $_.Name -WarningAction SilentlyContinue
}
}
}
Write-PSFMessage -Level Verbose -Message "Removing Management Group: $($DisplayName)" -FunctionName "Remove-AzOpsTestsDeployment"
Remove-AzManagementGroup -GroupId $Name -Confirm:$false -WarningAction SilentlyContinue
}

}
#region cleanupEnvironment
try {
Write-PSFMessage -Level Verbose -Message "Executing test cleanup" -FunctionName "Remove-AzOpsTestsDeployment"
# Cleanup managementGroups
$script:managementGroups = Get-AzManagementGroup | Where-Object {$_.DisplayName -eq "Test" -or $_.DisplayName -eq "AzOpsMGMTName"}
foreach ($script:mgclean in $script:managementGroups) {
Remove-ManagementGroups -DisplayName $script:mgclean.DisplayName -Name $script:mgclean.Name -RootName (Get-AzTenant).TenantId
}
# Collect resources to cleanup
$script:resourceGroups = Get-AzResourceGroup | Where-Object {$_.ResourceGroupName -like "*-azopsrg"}
$script:roleAssignments = Get-AzRoleAssignment | Where-Object {$_.Scope -ne "/"}
$script:policyAssignments = Get-AzPolicyAssignment
$script:policyExemptions = Get-AzPolicyExemption -ErrorAction SilentlyContinue
# Cleanup resourceGroups
$script:resourceGroups | ForEach-Object -ThrottleLimit 20 -Parallel {
Write-PSFMessage -Level Verbose -Message "Executing test resourceGroups cleanup thread of $($_.ResourceGroupName)" -FunctionName "Remove-AzOpsTestsDeployment"
$script:run = $_ | Remove-AzResourceGroup -Confirm:$false -Force
}
# Cleanup roleAssignments and policyAssignments
$script:roleAssignments | Remove-AzRoleAssignment -Confirm:$false -ErrorAction SilentlyContinue
$script:policyExemptions | Remove-AzPolicyExemption -Force -Confirm:$false
$script:policyAssignments | Remove-AzPolicyAssignment -Confirm:$false
# Collect and cleanup deployment jobs
$azTenantDeploymentJobs = Get-AzTenantDeployment
$azTenantDeploymentJobs | ForEach-Object -ThrottleLimit 20 -Parallel {
Write-PSFMessage -Level Verbose -Message "Executing test AzDeployment cleanup thread of $($_.DeploymentName)" -FunctionName "Remove-AzOpsTestsDeployment"
$_ | Remove-AzTenantDeployment -Confirm:$false
}
Get-AzManagementGroupDeployment -ManagementGroupId "cd35e23c-537f-4553-a280-f5a60033a446" | Remove-AzManagementGroupDeployment -Confirm:$false
$azDeploymentJobs = Get-AzDeployment
$azDeploymentJobs | ForEach-Object -ThrottleLimit 20 -Parallel {
Write-PSFMessage -Level Verbose -Message "Executing test AzDeployment cleanup thread of $($_.DeploymentName)" -FunctionName "Remove-AzOpsTestsDeployment"
$_ | Remove-AzDeployment -Confirm:$false
}
}
catch {
Write-PSFMessage -Level Warning -Message $_ -FunctionName "Remove-AzOpsTestsDeployment"
}
#endregion cleanupEnvironment
}
}
}
4 changes: 2 additions & 2 deletions src/AzOps.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Customer Architecture Team (CAT)
#
# Generated on: 05/24/2022
# Generated on: 06/18/2022
#

@{
Expand Down Expand Up @@ -51,7 +51,7 @@ PowerShellVersion = '7.2'
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(@{ModuleName = 'PSFramework'; RequiredVersion = '1.7.227'; },
RequiredModules = @(@{ModuleName = 'PSFramework'; RequiredVersion = '1.7.237'; },
@{ModuleName = 'Az.Accounts'; RequiredVersion = '2.8.0'; },
@{ModuleName = 'Az.Billing'; RequiredVersion = '2.0.0'; },
@{ModuleName = 'Az.Resources'; RequiredVersion = '6.0.0'; })
Expand Down
28 changes: 17 additions & 11 deletions src/tests/Pester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

$Include = "*",

$Exclude = @("Help.Tests.ps1", "PSScriptAnalyzer.Tests.ps1")
$Exclude = @("Help.Tests.ps1", "PSScriptAnalyzer.Tests.ps1"),

$CleanupEnvironment = $false
)

Set-PSFConfig -FullName PSFramework.Message.Info.Maximum -Value 3
Expand Down Expand Up @@ -109,12 +111,10 @@ if ($TestFunctional) {
Write-PSFMessage -Level Important -Message "Proceeding with functional tests"
try {
$functionalTestsScript = (Get-ChildItem "$PSScriptRoot\functional" | Where-Object Name -like "*.Tests.ps1")
& $functionalTestsScript.VersionInfo.FileName -cleanupEnvironment $true
$functionalTestDeploymentOutput = & $functionalTestsScript.VersionInfo.FileName -setupEnvironment $true
$functionalTestDeploymentOutput = & $functionalTestsScript.VersionInfo.FileName
}
catch {
Write-PSFMessage -Level Critical -Message "Functional tests initialize failed" -Exception $_.Exception
throw
}
foreach ($file in (Get-ChildItem "$PSScriptRoot\functional" -Recurse | Where-Object Name -eq "scenario.ps1")) {
if ($file.Name -notlike $Include) { continue }
Expand Down Expand Up @@ -145,13 +145,6 @@ if ($TestFunctional) {
}
}
}
try {
& $functionalTestsScript.VersionInfo.FileName -cleanupEnvironment $true
}
catch {
Write-PSFMessage -Level Critical -Message "Functional tests cleanup failed" -Exception $_.Exception
throw
}
}
#endregion Run Functional Tests

Expand Down Expand Up @@ -196,3 +189,16 @@ else {
if ($totalFailed -gt 0) {
throw "$totalFailed / $totalRun tests failed!"
}

#region cleanupEnvironment
if ($CleanupEnvironment) {
Write-PSFMessage -Level Verbose -Message "Cleanup test environment"
try {
. "$global:testroot/../../scripts/Remove-AzOpsTestsDeployment.ps1"
Remove-AzOpsTestsDeployment -CleanupEnvironment $true
}
catch {
Write-PSFMessage -Level Critical -Message "Test environment is not clean" -Exception $_.Exception
}
}
#endregion cleanupEnvironment
Loading