1
- # Requires -Module @ { ModuleName = " Pester" ; ModuleVersion = " 5.0" }
2
- param (
3
- $ModuleName = " dbatools" ,
4
- $CommandName = " Get-DbaBuild" ,
5
- $PSDefaultParameterValues = $TestConfig.Defaults
6
- )
7
-
1
+ $CommandName = $MyInvocation.MyCommand.Name.Replace (" .Tests.ps1" , " " )
8
2
Write-Host - Object " Running $PSCommandPath " - ForegroundColor Cyan
9
3
$global :TestConfig = Get-TestConfig
10
4
11
- Describe $CommandName - Tag UnitTests {
12
- Context " Parameter validation" {
13
- BeforeAll {
14
- $hasParameters = (Get-Command $CommandName ).Parameters.Values.Name | Where-Object { $PSItem -notin (" WhatIf" , " Confirm" ) }
15
- $expectedParameters = $TestConfig.CommonParameters
16
- $expectedParameters += @ (
17
- " Build" ,
18
- " Kb" ,
19
- " MajorVersion" ,
20
- " ServicePack" ,
21
- " CumulativeUpdate" ,
22
- " SqlInstance" ,
23
- " SqlCredential" ,
24
- " Update" ,
25
- " EnableException"
26
- )
27
- }
28
-
29
- It " Should have the expected parameters" {
30
- Compare-Object - ReferenceObject $expectedParameters - DifferenceObject $hasParameters | Should - BeNullOrEmpty
5
+ Describe " $CommandName Unit Tests" - Tag ' UnitTests' {
6
+ Context " Validate parameters" {
7
+ [object []]$params = (Get-Command $CommandName ).Parameters.Keys | Where-Object { $_ -notin (' whatif' , ' confirm' ) }
8
+ [object []]$knownParameters = ' Build' , ' Kb' , ' MajorVersion' , ' ServicePack' , ' CumulativeUpdate' , ' SqlInstance' , ' SqlCredential' , ' Update' , ' EnableException'
9
+ $knownParameters += [System.Management.Automation.PSCmdlet ]::CommonParameters
10
+ It " Should only contain our specific parameters" {
11
+ (@ (Compare-Object - ReferenceObject ($knownParameters | Where-Object { $_ }) - DifferenceObject $params ).Count ) | Should Be 0
31
12
}
32
13
}
33
14
}
34
15
35
- Describe $CommandName - Tag UnitTests {
16
+ Describe " $CommandName Unit Test " - Tags Unittest {
36
17
BeforeAll {
37
18
$ModuleBase = (Get-Module - Name dbatools | Where-Object ModuleBase -NotMatch net).ModuleBase
38
19
$idxfile = " $ModuleBase \bin\dbatools-buildref-index.json"
39
20
}
40
21
41
- Context " Validate data in json is correct" {
22
+ Context ' Validate data in json is correct' {
42
23
It " the json file is there" {
43
24
$result = Test-Path $idxfile
44
25
$result | Should - Be $true
@@ -48,58 +29,50 @@ Describe $CommandName -Tag UnitTests {
48
29
$IdxRef | Should - BeOfType System.Object
49
30
}
50
31
}
51
-
52
- Context " Validate LastUpdated property" {
32
+ Context ' Validate LastUpdated property' {
53
33
BeforeAll {
54
34
$IdxRef = Get-Content $idxfile - Raw | ConvertFrom-Json
55
35
}
56
-
57
36
It " Has a proper LastUpdated property" {
58
37
$lastupdate = Get-Date - Date $IdxRef.LastUpdated
59
38
$lastupdate | Should - BeOfType System.DateTime
60
39
}
61
-
62
40
It " LastUpdated is updated regularly (keeps everybody on their toes)" {
63
41
$lastupdate = Get-Date - Date $IdxRef.LastUpdated
64
42
$lastupdate | Should - BeGreaterThan (Get-Date ).AddDays(-45 )
65
43
}
66
-
67
44
It " LastUpdated is not in the future" {
68
45
$lastupdate = Get-Date - Date $IdxRef.LastUpdated
69
46
$lastupdate | Should - BeLessThan (Get-Date )
70
47
}
71
48
}
72
-
73
- Context " Validate Data property" {
49
+ Context ' Validate Data property' {
74
50
BeforeAll {
75
51
$IdxRef = Get-Content $idxfile - Raw | ConvertFrom-Json
76
52
$Groups = @ { }
77
53
$OrderedKeys = @ ()
78
54
foreach ($el in $IdxRef.Data ) {
79
- $ver = $el.Version.Split (" . " )[0 .. 1 ] -join " . "
55
+ $ver = $el.Version.Split (' . ' )[0 .. 1 ] -join ' . '
80
56
if (! ($Groups.ContainsKey ($ver ))) {
81
57
$Groups [$ver ] = New-Object System.Collections.ArrayList
82
58
$OrderedKeys += $ver
83
59
}
84
60
$null = $Groups [$ver ].Add($el )
85
61
}
86
62
}
87
-
88
63
It " Data is a proper array" {
89
64
$IdxRef.Data.Length | Should - BeGreaterThan 100
90
65
}
91
-
92
66
It " Each Datum has a Version property" {
93
67
$DataLength = $IdxRef.Data.Length
94
- $DataWithVersion = @ ($IdxRef.Data.Version | Where-Object { $PSItem }).Count
68
+ $DataWithVersion = ($IdxRef.Data.Version | Where-Object { $_ }).Length
95
69
$DataLength | Should - Be $DataWithVersion
96
70
}
97
-
98
71
It " Each version is correctly parsable" {
99
- $Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
72
+ $Versions = $IdxRef.Data.Version | Where-Object { $_ }
100
73
foreach ($ver in $Versions ) {
101
- $splitted = $ver.split (" . " )
102
- $dots = $ver.split (" . " ).Length - 1
74
+ $splitted = $ver.split (' . ' )
75
+ $dots = $ver.split (' . ' ).Length - 1
103
76
if ($dots -ne 2 ) {
104
77
if ($dots [0 ] -le 15 ) {
105
78
$dots | Should - Be 3
@@ -108,171 +81,143 @@ Describe $CommandName -Tag UnitTests {
108
81
}
109
82
}
110
83
try {
111
- $splitted | ForEach-Object { [convert ]::ToInt32($PSItem ) }
84
+ $splitted | ForEach-Object { [convert ]::ToInt32($_ ) }
112
85
} catch {
113
86
# I know. But someone can find a method to output a custom message ?
114
- $splitted -join " . " | Should - Be " Composed by integers"
87
+ $splitted -join ' . ' | Should - Be " Composed by integers"
115
88
}
116
89
}
117
90
}
118
-
119
91
It " Versions are ordered, the way versions are ordered" {
120
- $Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
92
+ $Versions = $IdxRef.Data.Version | Where-Object { $_ }
121
93
$Naturalized = $Versions | ForEach-Object {
122
- $splitted = $PSItem .split (" . " ) | ForEach-Object { [convert ]::ToInt32($PSItem ) }
123
- " $ ( $splitted [0 ].toString(" 00 " )) $ ( $splitted [1 ].toString(" 00 " )) $ ( $splitted [2 ].toString(" 0000" )) "
94
+ $splitted = $_ .split (' . ' ) | ForEach-Object { [convert ]::ToInt32($_ ) }
95
+ " $ ( $splitted [0 ].toString(' 00 ' )) $ ( $splitted [1 ].toString(' 00 ' )) $ ( $splitted [2 ].toString(' 0000' )) "
124
96
}
125
97
$SortedVersions = $Naturalized | Sort-Object
126
98
($SortedVersions -join " ," ) | Should - Be ($Naturalized -join " ," )
127
99
}
128
-
129
100
It " Names are at least 8" {
130
- $Names = $IdxRef.Data.Name | Where-Object { $PSItem }
131
- $Names.Count | Should - BeGreaterThan 7
101
+ $Names = $IdxRef.Data.Name | Where-Object { $_ }
102
+ $Names.Length | Should - BeGreaterThan 7
132
103
}
133
104
}
134
-
135
105
Context " Params mutual exclusion" {
136
106
It " Doesn't accept 'Build', 'Kb', 'SqlInstance" {
137
- { Get-DbaBuild - Build " 10.0.1600" - Kb " 4052908" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
107
+ { Get-DbaBuild - Build ' 10.0.1600' - Kb ' 4052908' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
138
108
}
139
109
It " Doesn't accept 'Build', 'Kb'" {
140
- { Get-DbaBuild - Build " 10.0.1600" - Kb " 4052908" - EnableException - ErrorAction Stop } | Should - Throw
110
+ { Get-DbaBuild - Build ' 10.0.1600' - Kb ' 4052908' - EnableException - ErrorAction Stop } | Should - Throw
141
111
}
142
112
It " Doesn't accept 'Build', 'SqlInstance'" {
143
- { Get-DbaBuild - Build " 10.0.1600" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
113
+ { Get-DbaBuild - Build ' 10.0.1600' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
144
114
}
145
115
It " Doesn't accept 'Build', 'SqlInstance'" {
146
- { Get-DbaBuild - Build " 10.0.1600" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
116
+ { Get-DbaBuild - Build ' 10.0.1600' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
147
117
}
148
118
It " Doesn't accept 'Build', 'MajorVersion'" {
149
- { Get-DbaBuild - Build " 10.0.1600" - MajorVersion " 2016" - EnableException - ErrorAction Stop } | Should - Throw
119
+ { Get-DbaBuild - Build ' 10.0.1600' - MajorVersion ' 2016' - EnableException - ErrorAction Stop } | Should - Throw
150
120
}
151
121
It " Doesn't accept 'Build', 'ServicePack'" {
152
- { Get-DbaBuild - Build " 10.0.1600" - ServicePack " SP2" - EnableException - ErrorAction Stop } | Should - Throw
122
+ { Get-DbaBuild - Build ' 10.0.1600' - ServicePack ' SP2' - EnableException - ErrorAction Stop } | Should - Throw
153
123
}
154
124
It " Doesn't accept 'Build', 'CumulativeUpdate'" {
155
- { Get-DbaBuild - Build " 10.0.1600" - CumulativeUpdate " CU2" - EnableException - ErrorAction Stop } | Should - Throw
125
+ { Get-DbaBuild - Build ' 10.0.1600' - CumulativeUpdate ' CU2' - EnableException - ErrorAction Stop } | Should - Throw
156
126
}
157
127
It " Doesn't accept 'ServicePack' without 'MajorVersion'" {
158
- { Get-DbaBuild - ServicePack " SP2" - EnableException - ErrorAction Stop } | Should - Throw
128
+ { Get-DbaBuild - ServicePack ' SP2' - EnableException - ErrorAction Stop } | Should - Throw
159
129
}
160
130
It " Doesn't accept 'CumulativeUpdate' without 'MajorVersion'" {
161
- { Get-DbaBuild - CumulativeUpdate " CU2" - EnableException - ErrorAction Stop } | Should - Throw
131
+ { Get-DbaBuild - CumulativeUpdate ' CU2' - EnableException - ErrorAction Stop } | Should - Throw
162
132
}
163
133
}
164
-
165
134
Context " Passing just -Update works, see #6823" {
166
- It " works with -Update" {
135
+ It ' works with -Update' {
167
136
function Get-DbaBuildReferenceIndexOnline { }
168
137
Mock Get-DbaBuildReferenceIndexOnline - MockWith { } - ModuleName dbatools
169
138
Get-DbaBuild - Update - WarningVariable warnings 3> $null
170
139
$warnings | Should - BeNullOrEmpty
171
140
}
172
141
}
173
-
174
142
Context " Retired KBs" {
175
- It " Handles retired KBs" {
176
- $result = Get-DbaBuild - Build " 13.0.5479"
177
- $result.Warning | Should - Be " This version has been officially retired by Microsoft"
143
+ It ' Handles retired KBs' {
144
+ $result = Get-DbaBuild - Build ' 13.0.5479'
145
+ $result.Warning | Should - Be ' This version has been officially retired by Microsoft'
178
146
}
179
147
}
180
148
181
149
Context " Recognizes version 'aliases', see #8915" {
182
- It " works with versions with the minor being either not 0 or 50" {
183
- $result2016 = Get-DbaBuild - Build " 13.3.6300"
184
- $result2016.Build | Should - Be " 13.3.6300"
185
- $result2016.BuildLevel | Should - Be " 13.0.6300"
186
- $result2016.MatchType | Should - Be " Exact"
150
+ It ' works with versions with the minor being either not 0 or 50' {
151
+ $result2016 = Get-DbaBuild - Build ' 13.3.6300'
152
+ $result2016.Build | Should - Be ' 13.3.6300'
153
+ $result2016.BuildLevel | Should - Be ' 13.0.6300'
154
+ $result2016.MatchType | Should - Be ' Exact'
187
155
188
- $result2008R2 = Get-DbaBuild - Build " 10.53.6220"
189
- $result2008R2.Build | Should - Be " 10.53.6220"
190
- $result2008R2.BuildLevel | Should - Be " 10.50.6220"
191
- $result2008R2.MatchType | Should - Be " Exact"
156
+ $result2008R2 = Get-DbaBuild - Build ' 10.53.6220'
157
+ $result2008R2.Build | Should - Be ' 10.53.6220'
158
+ $result2008R2.BuildLevel | Should - Be ' 10.50.6220'
159
+ $result2008R2.MatchType | Should - Be ' Exact'
192
160
}
193
161
}
194
-
195
162
# These are groups by major release (aka "Name")
196
- Context " Properties Check" {
197
- BeforeAll {
198
- $IdxRef = Get-Content $idxfile - Raw | ConvertFrom-Json
199
- $Groups = @ { }
200
- $OrderedKeys = @ ()
201
- foreach ($el in $IdxRef.Data ) {
202
- $ver = $el.Version.Split (" ." )[0 .. 1 ] -join " ."
203
- if (! ($Groups.ContainsKey ($ver ))) {
204
- $Groups [$ver ] = New-Object System.Collections.ArrayList
205
- $OrderedKeys += $ver
206
- }
207
- $null = $Groups [$ver ].Add($el )
163
+ foreach ($g in $OrderedKeys ) {
164
+ $Versions = $Groups [$g ]
165
+ Context " Properties Check, for major release $g " {
166
+ It " has the first element with a Name" {
167
+ $Versions [0 ].Name | Should - BeLike " 20*"
208
168
}
209
- }
210
-
211
- foreach ($g in $OrderedKeys ) {
212
- $Versions = $Groups [$g ]
213
- Context " Properties Check, for major release $g " {
214
- It " has the first element with a Name" {
215
- $Versions [0 ].Name | Should - BeLike " 20*"
216
- }
217
- It " No multiple Names around" {
218
- @ ($Versions.Name | Where-Object { $PSItem }).Count | Should - Be 1
219
- }
220
- # Skip for now bc a prerelease has been added
221
- It " has one version tagged as RTM" - Skip:$true {
222
- @ ($Versions.SP -eq " RTM" ).Count | Should - Be 1
223
- }
224
- It " SP Property is formatted correctly" {
225
- $Versions.SP | Where-Object { $PSItem } | Should -Match " ^RTM$|^SP[\d]+$|^RC"
226
- }
227
- It " CU Property is formatted correctly" {
228
- $CUMatch = $Versions.CU | Where-Object { $PSItem }
229
- if ($CUMatch ) {
230
- $CUMatch | Should -Match " ^CU[\d]+$"
231
- }
232
- }
233
- It " SPs are ordered correctly" {
234
- $SPs = $Versions.SP | Where-Object { $PSItem }
235
- ($SPs | Select-Object - First 1 ) | Should - BeIn " RTM" , " RC"
236
- $ActualSPs = $SPs | Where-Object { $PSItem -match " ^SP[\d]+$" }
237
- $OrderedActualSPs = $ActualSPs | Sort-Object
238
- ($ActualSPs -join " ," ) | Should - Be ($OrderedActualSPs -join " ," )
169
+ It " No multiple Names around" {
170
+ ($Versions.Name | Where-Object { $_ }).Count | Should - Be 1
171
+ }
172
+ # Skip for now bc a prerelease has been added
173
+ It - Skip " has one version tagged as RTM" {
174
+ ($Versions.SP -eq ' RTM' ).Count | Should - Be 1
175
+ }
176
+ It " SP Property is formatted correctly" {
177
+ $Versions.SP | Where-Object { $_ } | Should -Match ' ^RTM$|^SP[\d]+$|^RC'
178
+ }
179
+ It " CU Property is formatted correctly" {
180
+ $CUMatch = $Versions.CU | Where-Object { $_ }
181
+ if ($CUMatch ) {
182
+ $CUMatch | Should -Match ' ^CU[\d]+$'
239
183
}
240
- # see https://github.com/dataplat/dbatools/pull/2466
241
- It " KBList has only numbers on it" {
242
- $NotNumbers = $Versions.KBList | Where-Object { $PSItem } | Where-Object { $PSItem -notmatch " ^[\d]+$" }
243
- if ($NotNumbers.Count -ne 0 ) {
244
- foreach ($Nn in $NotNumbers ) {
245
- $Nn | Should - Be " Composed by integers"
246
- }
184
+ }
185
+ It " SPs are ordered correctly" {
186
+ $SPs = $Versions.SP | Where-Object { $_ }
187
+ ($SPs | Select-Object - First 1 ) | Should - BeIn ' RTM' , ' RC'
188
+ $ActualSPs = $SPs | Where-Object { $_ -match ' ^SP[\d]+$' }
189
+ $OrderedActualSPs = $ActualSPs | Sort-Object
190
+ ($ActualSPs -join ' ,' ) | Should - Be ($OrderedActualSPs -join ' ,' )
191
+ }
192
+ # see https://github.com/dataplat/dbatools/pull/2466
193
+ It " KBList has only numbers on it" {
194
+ $NotNumbers = $Versions.KBList | Where-Object { $_ } | Where-Object { $_ -notmatch ' ^[\d]+$' }
195
+ if ($NotNumbers.Count -ne 0 ) {
196
+ foreach ($Nn in $NotNumbers ) {
197
+ $Nn | Should - Be " Composed by integers"
247
198
}
248
199
}
249
200
}
250
201
}
251
202
}
252
203
}
253
204
254
- Describe $CommandName - Tag IntegrationTests {
205
+ Describe " $commandname Integration Tests " - Tags ' IntegrationTests' {
255
206
Context " piping and params" {
256
207
BeforeAll {
257
208
$server1 = Connect-DbaInstance - SqlInstance $TestConfig.instance1
258
209
$server2 = Connect-DbaInstance - SqlInstance $TestConfig.instance2
259
210
}
260
-
261
211
It " works when instances are piped" {
262
212
$res = @ ($server1 , $server2 ) | Get-DbaBuild
263
213
$res.Count | Should - Be 2
264
214
}
265
-
266
215
It " doesn't work when passed both piped instances and, e.g., -Kb (params mutual exclusion)" {
267
216
{ @ ($server1 , $server2 ) | Get-DbaBuild - Kb - EnableException } | Should - Throw
268
217
}
269
218
}
270
-
271
219
Context " Test retrieving version from instances" {
272
- BeforeAll {
273
- $results = Get-DbaBuild - SqlInstance $TestConfig.instance1 , $TestConfig.instance2
274
- }
275
-
220
+ $results = Get-DbaBuild - SqlInstance $TestConfig.instance1 , $TestConfig.instance2
276
221
It " Should return an exact match" {
277
222
$results | Should -Not - BeNullOrEmpty
278
223
foreach ($r in $results ) {
@@ -292,7 +237,7 @@ Describe $CommandName -Tag IntegrationTests {
292
237
$m.KBLevel | Should - BeIn $r.KBLevel
293
238
}
294
239
}
295
- $spLevel = $r.SPLevel | Where-Object { $PSItem -ne " LATEST" }
240
+ $spLevel = $r.SPLevel | Where-Object { $_ -ne ' LATEST' }
296
241
$versionMatch = Get-DbaBuild - MajorVersion $r.NameLevel - ServicePack $spLevel - CumulativeUpdate $r.CULevel
297
242
$versionMatch | Should -Not - BeNullOrEmpty
298
243
foreach ($v in $versionMatch ) {
@@ -304,4 +249,4 @@ Describe $CommandName -Tag IntegrationTests {
304
249
}
305
250
}
306
251
}
307
- }
252
+ }
0 commit comments