|
1 | | -#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} |
2 | | -param( |
3 | | - $ModuleName = "FabricTools", |
4 | | - $expectedParams = @( |
5 | | - "DomainId" |
6 | | - "CapacitiesIds" |
7 | | - "Verbose" |
8 | | - "Debug" |
9 | | - "ErrorAction" |
10 | | - "WarningAction" |
11 | | - "InformationAction" |
12 | | - "ProgressAction" |
13 | | - "ErrorVariable" |
14 | | - "WarningVariable" |
15 | | - "InformationVariable" |
16 | | - "OutVariable" |
17 | | - "OutBuffer" |
18 | | - "PipelineVariable" |
19 | | - |
20 | | - ) |
21 | | -) |
22 | | - |
23 | | -Describe "Add-FabricDomainWorkspaceAssignmentByCapacity" -Tag "UnitTests" { |
24 | | - |
25 | | - BeforeDiscovery { |
26 | | - $command = Get-Command -Name Add-FabricDomainWorkspaceAssignmentByCapacity |
27 | | - $expected = $expectedParams |
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + $script:moduleName = 'FabricTools' |
| 6 | + |
| 7 | + # If the module is not found, run the build task 'noop'. |
| 8 | + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) { |
| 9 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 10 | + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 11 | + } |
| 12 | + |
| 13 | + # Re-import the module using force to get any code changes between runs. |
| 14 | + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' |
| 15 | + |
| 16 | + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName |
| 17 | + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName |
| 18 | + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName |
| 19 | +} |
| 20 | + |
| 21 | +AfterAll { |
| 22 | + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') |
| 23 | + $PSDefaultParameterValues.Remove('Mock:ModuleName') |
| 24 | + $PSDefaultParameterValues.Remove('Should:ModuleName') |
| 25 | + |
| 26 | + # Unload the module being tested so that it doesn't impact any other tests. |
| 27 | + Get-Module -Name $script:moduleName -All | Remove-Module -Force |
| 28 | +} |
| 29 | + |
| 30 | +Describe 'Add-FabricDomainWorkspaceAssignmentByCapacity' -Tag 'Public' { |
| 31 | + It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @( |
| 32 | + @{ |
| 33 | + MockParameterSetName = '__AllParameterSets' |
| 34 | + MockExpectedParameters = '[-DomainId] <guid> [-CapacitiesIds] <guid[]> [<CommonParameters>]' |
| 35 | + } |
| 36 | + ) { |
| 37 | + $result = (Get-Command -Name 'Add-FabricDomainWorkspaceAssignmentByCapacity').ParameterSets | |
| 38 | + Where-Object -FilterScript { |
| 39 | + $_.Name -eq $MockParameterSetName |
| 40 | + } | |
| 41 | + Select-Object -Property @( |
| 42 | + @{ |
| 43 | + Name = 'ParameterSetName' |
| 44 | + Expression = { $_.Name } |
| 45 | + }, |
| 46 | + @{ |
| 47 | + Name = 'ParameterListAsString' |
| 48 | + Expression = { $_.ToString() } |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + $result.ParameterSetName | Should -Be $MockParameterSetName |
| 53 | + $result.ParameterListAsString | Should -Be $MockExpectedParameters |
| 54 | + } |
| 55 | + |
| 56 | + Context 'When assigning workspaces by capacity successfully (201)' { |
| 57 | + BeforeAll { |
| 58 | + Mock -CommandName Confirm-TokenState -MockWith { } |
| 59 | + Mock -CommandName Write-Message -MockWith { } |
| 60 | + Mock -CommandName Invoke-FabricRestMethod -MockWith { |
| 61 | + InModuleScope -ModuleName 'FabricTools' { |
| 62 | + $script:statusCode = 201 |
| 63 | + } |
| 64 | + return [pscustomobject]@{ |
| 65 | + status = 'Succeeded' |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + It 'Should call Invoke-FabricRestMethod with the correct parameters' { |
| 71 | + $mockDomainId = [guid]::NewGuid() |
| 72 | + $mockCapacityIds = @([guid]::NewGuid(), [guid]::NewGuid()) |
| 73 | + |
| 74 | + Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 75 | + |
| 76 | + Should -Invoke -CommandName Invoke-FabricRestMethod -Times 1 -ParameterFilter { |
| 77 | + $Uri -like "*admin/domains/*/assignWorkspacesByCapacities" -and |
| 78 | + $Method -eq 'Post' |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + It 'Should return the response on success' { |
| 83 | + $mockDomainId = [guid]::NewGuid() |
| 84 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 85 | + |
| 86 | + $result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 87 | + |
| 88 | + $result.status | Should -Be 'Succeeded' |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Context 'When assigning workspaces by capacity is in progress (202)' { |
| 93 | + BeforeAll { |
| 94 | + Mock -CommandName Confirm-TokenState -MockWith { } |
| 95 | + Mock -CommandName Write-Message -MockWith { } |
| 96 | + Mock -CommandName Invoke-FabricRestMethod -MockWith { |
| 97 | + InModuleScope -ModuleName 'FabricTools' { |
| 98 | + $script:statusCode = 202 |
| 99 | + $script:responseHeader = @{ |
| 100 | + 'x-ms-operation-id' = 'op-12345' |
| 101 | + 'Location' = 'https://api.fabric.microsoft.com/v1/operations/op-12345' |
| 102 | + 'Retry-After' = '30' |
| 103 | + } |
| 104 | + } |
| 105 | + return [pscustomobject]@{} |
| 106 | + } |
| 107 | + Mock -CommandName Get-FabricLongRunningOperation -MockWith { |
| 108 | + return [pscustomobject]@{ |
| 109 | + status = 'Succeeded' |
| 110 | + operationId = 'op-12345' |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + It 'Should call Get-FabricLongRunningOperation when status is 202' { |
| 116 | + $mockDomainId = [guid]::NewGuid() |
| 117 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 118 | + |
| 119 | + Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 120 | + |
| 121 | + Should -Invoke -CommandName Get-FabricLongRunningOperation -Times 1 |
| 122 | + } |
| 123 | + |
| 124 | + It 'Should return the operation status when long running operation succeeds' { |
| 125 | + $mockDomainId = [guid]::NewGuid() |
| 126 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 127 | + |
| 128 | + $result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 129 | + |
| 130 | + $result.status | Should -Be 'Succeeded' |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Context 'When long running operation fails' { |
| 135 | + BeforeAll { |
| 136 | + Mock -CommandName Confirm-TokenState -MockWith { } |
| 137 | + Mock -CommandName Write-Message -MockWith { } |
| 138 | + Mock -CommandName Invoke-FabricRestMethod -MockWith { |
| 139 | + InModuleScope -ModuleName 'FabricTools' { |
| 140 | + $script:statusCode = 202 |
| 141 | + $script:responseHeader = @{ |
| 142 | + 'x-ms-operation-id' = 'op-failed' |
| 143 | + 'Location' = 'https://api.fabric.microsoft.com/v1/operations/op-failed' |
| 144 | + 'Retry-After' = '30' |
| 145 | + } |
| 146 | + } |
| 147 | + return [pscustomobject]@{} |
| 148 | + } |
| 149 | + Mock -CommandName Get-FabricLongRunningOperation -MockWith { |
| 150 | + return [pscustomobject]@{ |
| 151 | + status = 'Failed' |
| 152 | + operationId = 'op-failed' |
| 153 | + error = @{ |
| 154 | + message = 'Operation failed' |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + It 'Should return the failed operation status' { |
| 161 | + $mockDomainId = [guid]::NewGuid() |
| 162 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 163 | + |
| 164 | + $result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 165 | + |
| 166 | + $result.status | Should -Be 'Failed' |
| 167 | + } |
28 | 168 | } |
29 | 169 |
|
30 | | - Context "Parameter validation" { |
| 170 | + Context 'When an unexpected status code is returned' { |
31 | 171 | BeforeAll { |
32 | | - $command = Get-Command -Name Add-FabricDomainWorkspaceAssignmentByCapacity |
33 | | - $expected = $expectedParams |
| 172 | + Mock -CommandName Confirm-TokenState -MockWith { } |
| 173 | + Mock -CommandName Write-Message -MockWith { } |
| 174 | + Mock -CommandName Invoke-FabricRestMethod -MockWith { |
| 175 | + InModuleScope -ModuleName 'FabricTools' { |
| 176 | + $script:statusCode = 400 |
| 177 | + } |
| 178 | + return [pscustomobject]@{ |
| 179 | + message = 'Bad Request' |
| 180 | + errorCode = 'InvalidRequest' |
| 181 | + } |
| 182 | + } |
34 | 183 | } |
35 | 184 |
|
36 | | - It "Has parameter: <_>" -ForEach $expected { |
37 | | - $command | Should -HaveParameter $PSItem |
| 185 | + It 'Should write an error message for unexpected status codes' { |
| 186 | + $mockDomainId = [guid]::NewGuid() |
| 187 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 188 | + |
| 189 | + Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds |
| 190 | + |
| 191 | + Should -Invoke -CommandName Write-Message -ParameterFilter { |
| 192 | + $Level -eq 'Error' |
| 193 | + } |
38 | 194 | } |
| 195 | + } |
| 196 | + |
| 197 | + Context 'When an exception is thrown' { |
| 198 | + BeforeAll { |
| 199 | + Mock -CommandName Confirm-TokenState -MockWith { } |
| 200 | + Mock -CommandName Write-Message -MockWith { } |
| 201 | + Mock -CommandName Invoke-FabricRestMethod -MockWith { |
| 202 | + throw 'API connection failed' |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + It 'Should handle exceptions gracefully' { |
| 207 | + $mockDomainId = [guid]::NewGuid() |
| 208 | + $mockCapacityIds = @([guid]::NewGuid()) |
| 209 | + |
| 210 | + { Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds } | Should -Not -Throw |
39 | 211 |
|
40 | | - It "Should have exactly the number of expected parameters $($expected.Count)" { |
41 | | - $hasparms = $command.Parameters.Values.Name |
42 | | - #$hasparms.Count | Should -BeExactly $expected.Count |
43 | | - Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty |
| 212 | + Should -Invoke -CommandName Write-Message -ParameterFilter { |
| 213 | + $Level -eq 'Error' -and $Message -like "*Error occurred while assigning workspaces*" |
| 214 | + } |
44 | 215 | } |
45 | 216 | } |
46 | 217 | } |
0 commit comments