Skip to content

Commit

Permalink
(chocolatey#66) Fixes Remove-CCMStaleDeployment
Browse files Browse the repository at this point in the history
Removes the -All parameter from Get-CCMDeployment, which means this might work.

Adjusts the test to be a little better.
  • Loading branch information
JPRuskin committed May 6, 2023
1 parent e1cd60f commit 4a05bdb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/Public/Remove-CCMStaleDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
33 changes: 20 additions & 13 deletions src/Tests/Public/Remove-CCMStaleDeployment.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,43 @@ 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
}

It "Calls Get-CCMDeployment to find all existing deployments" {
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" {
Expand Down

0 comments on commit 4a05bdb

Please sign in to comment.