diff --git a/src/Public/Remove-CCMStaleDeployment.ps1 b/src/Public/Remove-CCMStaleDeployment.ps1 index 5232136..62e1150 100644 --- a/src/Public/Remove-CCMStaleDeployment.ps1 +++ b/src/Public/Remove-CCMStaleDeployment.ps1 @@ -29,7 +29,11 @@ function Remove-CCMStaleDeployment { #> $badStates = @(0, 2, 8) if ($PSCmdlet.ShouldProcess("$Deployment", "DELETE")) { - Get-CCMDeployment -All | Where-Object { $_.CreationDate -ge (Get-Date).AddDays(-$Age) -and $null -eq $_.StartDateTimeUtc -and $_.Result -in $badStates } | Remove-CCMDeployment + Get-CCMDeployment | Where-Object { + $_.CreationDate -ge (Get-Date).AddDays(-$Age) -and + $null -eq $_.StartDateTimeUtc -and + $_.Result -in $badStates + } | Remove-CCMDeployment } } } diff --git a/src/Tests/Public/Remove-CCMStaleDeployment.Tests.ps1 b/src/Tests/Public/Remove-CCMStaleDeployment.Tests.ps1 index 7887fee..85c678b 100644 --- a/src/Tests/Public/Remove-CCMStaleDeployment.Tests.ps1 +++ b/src/Tests/Public/Remove-CCMStaleDeployment.Tests.ps1 @@ -8,25 +8,34 @@ Describe "Remove-CCMStaleDeployment" { $script:Protocol = "http" } Mock Invoke-RestMethod -ModuleName ChocoCCM + Mock Get-Date -ModuleName ChocoCCM { + $script:TestDate + } Mock Get-CCMDeployment -ModuleName ChocoCCM { - 1..60 | ForEach-Object { - @{ - Deployment = $_ - CreationDate = (Get-Date).AddDays(-$_) - Result = 0 # Is 0 really a bad state? - } - } + $script:TestData } Mock Remove-CCMDeployment -ModuleName ChocoCCM } - # This function is broken. It tries to use the -All parameter of Get-CCMDeployment, but that parameter doesn't exist. - Context "Removing Stale Deployments" -Skip { + Context "Removing Stale Deployments" { BeforeAll { $TestParams = @{ Age = "30" } + $script:TestData = 1..100 | ForEach-Object { + @{ + Deployment = $_ + CreationDate = (Get-Date).AddDays((Get-Random -Minimum (- $TestParams.Age * 2) -Maximum 0)) + Result = 0, 1, 2, 8 | Get-Random + } + } + $script:TestDate = Get-Date + $StaleDeployments = $script:TestData.Where{ + $_.CreationDate -ge $script:TestDate.AddDays(-$TestParams.Age) -and + $_.Result -ne 1 + } + $Result = Remove-CCMStaleDeployment @TestParams -Confirm:$false } @@ -34,10 +43,8 @@ Describe "Remove-CCMStaleDeployment" { Should -Invoke Get-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly } - It "Calls Remove-CCMDeployment to remove deployments with age greater than 30 days" { - Should -Invoke Remove-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 30 -Exactly -ParameterFilter { - $Deployment -ge 30 # This test could be improved, but as the function is broken it is what it is. - } + It "Calls Remove-CCMDeployment to remove stale deployments from the last $($TestParams.Age) days" { + Should -Invoke Remove-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times $StaleDeployments.Count -Exactly } It "Returns Nothing" {