-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathUpdatePSResourceV2.Tests.ps1
More file actions
435 lines (366 loc) · 19.7 KB
/
Copy pathUpdatePSResourceV2.Tests.ps1
File metadata and controls
435 lines (366 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
Describe 'Test HTTP Update-PSResource for V2 Server Protocol' -tags 'CI' {
BeforeAll {
$PSGalleryName = Get-PSGalleryName
$NuGetGalleryName = Get-NuGetGalleryName
$testModuleName = "test_module"
$testModuleName2 = "test_module2"
$testModuleName3 = "TestModule99"
$PackageManagement = "PackageManagement"
Get-NewPSResourceRepositoryFile
Get-PSResourceRepository
}
AfterEach {
Uninstall-PSResource "test_module", "TestModule99", "TestModuleWithLicense", "test_module2", "test_script" -Version "*" -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "Update resource installed given Name parameter" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
It "Update resources installed given Name (with wildcard) parameter" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Install-PSResource -Name $testModuleName2 -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name "test_mod*" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name "test_mod*" -Version "5.0.0.0"
$res | Should -Not -BeNullOrEmpty
$inputHashtable = @{test_module = "1.0.0.0"; test_module2 = "1.0.0.0"}
$isTest_ModuleUpdated = $false
$isTest_Module2Updated = $false
foreach ($item in $res)
{
if ([System.Version]$item.Version -gt [System.Version]$inputHashtable[$item.Name])
{
if ($item.Name -like $testModuleName)
{
$isTest_ModuleUpdated = $true
}
elseif ($item.Name -like $testModuleName2)
{
$isTest_Module2Updated = $true
}
}
}
$isTest_ModuleUpdated | Should -BeTrue
$isTest_Module2Updated | Should -BeTrue
}
It "Update resource installed given Name and Version (specific) parameters" {
$v1000 = Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Reinstall -PassThru
$v1000.Version | Should -Be "1.0.0.0"
$v5000 = Update-PSResource -Name $testModuleName -Version "5.0.0.0" -Repository $PSGalleryName -TrustRepository -PassThru -Force
$v5000.Version | Should -Contain "5.0.0.0"
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -eq [System.Version]"5.0.0.0")
{
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -BeTrue
}
$testCases2 = @{Version="[3.0.0.0]"; UpdatedVersion="3.0.0.0"; Reason="validate version, exact match"},
@{Version="3.0.0.0"; UpdatedVersion="3.0.0.0"; Reason="validate version, exact match without bracket syntax"},
@{Version="[3.0.0.0, 5.0.0.0]"; UpdatedVersion="5.0.0.0"; Reason="validate version, exact range inclusive"},
@{Version="(3.0.0.0, 6.0.0.0)"; UpdatedVersion="5.0.0.0"; Reason="validate version, exact range exclusive"},
@{Version="(3.0.0.0,)"; UpdatedVersion="5.0.0.0"; Reason="validate version, minimum version exclusive"},
@{Version="[3.0.0.0,)"; UpdatedVersion="5.0.0.0"; Reason="validate version, minimum version inclusive"},
@{Version="(,5.0.0.0)"; UpdatedVersion="3.0.0.0"; Reason="validate version, maximum version exclusive"},
@{Version="(,5.0.0.0]"; UpdatedVersion="5.0.0.0"; Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 5.0.0.0)"; UpdatedVersion="3.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
@{Version="(1.0.0.0, 3.0.0.0]"; UpdatedVersion="3.0.0.0"; Reason="validate version, mixed exclusive minimum and inclusive maximum version"}
It "Update resource when given Name to <Reason> <Version>" -TestCases $testCases2{
param($Version, $UpdatedVersion)
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
$res = Update-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -TrustRepository -PassThru -SkipDependencyCheck
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be $UpdatedVersion
}
$testCases = @(
@{Version='(3.0.0.0)'; Description="exclusive version (3.0.0.0)"},
@{Version='[3-0-0-0]'; Description="version formatted with invalid delimiter [3-0-0-0]"}
)
It "Should not update resource with incorrectly formatted version such as <Description>" -TestCases $testCases{
param($Version, $Description)
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -TrustRepository 2>$null
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $false
}
It "Update resource installed given Name and Version '3.*' parameters" {
Install-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -eq [System.Version]"3.0.0.0")
{
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -BeTrue
}
It "Update resource with latest (including prerelease) version given Prerelease parameter" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -ge [System.Version]"5.2.5")
{
$pkg.Prerelease | Should -Be "alpha001"
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
It "Update resource to explicit prerelease version using NuGet syntax" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Prerelease -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -eq [System.Version]"5.2.5")
{
$pkg.Prerelease | Should -Be "alpha001"
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
It "Update prerelease version to a higher prerelease version not using -Prerelease parameter" {
Install-PSResource -Name $testModuleName3 -Version "0.0.99-beta1" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName3 -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName3
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -ge [System.Version]"1.0.0")
{
$pkg.Prerelease | Should -Be "beta2"
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# Windows only
It "update resource under CurrentUser scope" -skip:(!($IsWindows -and (Test-IsAdmin))) {
# TODO: perhaps also install TestModule with the highest version (the one above 1.2.0.0) to the AllUsers path too
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope AllUsers
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Update-PSResource -Name $testModuleName -Version "3.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$pkg.InstalledLocation.Contains("Documents") | Should -Be $true
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# Windows only
It "update resource under AllUsers scope" -skip:(!($IsWindows -and (Test-IsAdmin))) {
Install-PSResource -Name "testmodule99" -Version "0.0.91" -Repository $PSGalleryName -TrustRepository -Scope AllUsers
Install-PSResource -Name "testmodule99" -Version "0.0.91" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Update-PSResource -Name "testmodule99" -Version "0.0.93" -Repository $PSGalleryName -TrustRepository -Scope AllUsers
$res = Get-Module -Name "testmodule99" -ListAvailable
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Contain "0.0.93"
}
# Windows only
It "Update resource under no specified scope" -skip:(!$IsWindows) {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -Version "3.0.0.0" -Repository $PSGalleryName -TrustRepository -verbose
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$pkg.InstalledLocation.Contains("Documents") | Should -Be $true
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Update resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) {
# this line is commented out because AllUsers scope requires sudo and that isn't supported in CI yet
# Install-PSResource -Name "TestModule" -Version "1.1.0.0" -Repository $TestGalleryName -Scope AllUsers
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Update-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$pkg.InstalledLocation.Contains("$env:HOME/.local") | Should -Be $true
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/usr/local/share/powershell/Modules'
# this test is skipped because it requires sudo to run and has yet to be resolved in CI
It "Update resource under AllUsers scope - Unix only" -Skip:($true) {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope AllUsers
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Update-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$pkg.InstalledLocation.Contains("usr") | Should -Be $true
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Update resource under no specified scope - Unix only" -Skip:(Get-IsWindows) {
# this is commented out because it requires sudo to run with AllUsers scope and this hasn't been resolved in CI yet
# Install-PSResource -Name "TestModule" -Version "1.1.0.0" -Repository $TestGalleryName -Scope AllUsers
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
Update-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$pkg.InstalledLocation.Contains("$env:HOME/.local") | Should -Be $true
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $true
}
# It "update resource that requires accept license with -AcceptLicense flag" {
# Install-PSResource -Name "TestModuleWithLicense" -Version "0.0.1.0" -Repository $TestGalleryName -AcceptLicense
# Update-PSResource -Name "TestModuleWithLicense" -Repository $TestGalleryName -AcceptLicense
# $res = Get-InstalledPSResource "TestModuleWithLicense"
# $isPkgUpdated = $false
# foreach ($pkg in $res)
# {
# if ([System.Version]$pkg.Version -gt [System.Version]"0.0.1.0")
# {
# $isPkgUpdated = $true
# }
# }
# $isPkgUpdated | Should -Be $true
# }
It "Update module using -WhatIf, should not update the module" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $testModuleName -WhatIf -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res | Should -Not -BeNullOrEmpty
$isPkgUpdated = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0")
{
$isPkgUpdated = $true
}
}
$isPkgUpdated | Should -Be $false
}
It "Update resource installed given -Name and -PassThru parameters" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository
$res = Update-PSResource -Name $testModuleName -Version "3.0.0.0" -Repository $PSGalleryName -TrustRepository -PassThru
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Contain "3.0.0.0"
}
## TODO update this -- find module with valid catalog file
# Update to module 1.4.3 (is authenticode signed and has catalog file)
# Should update successfully
It "Update module with catalog file using publisher validation" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository -verbose
Update-PSResource -Name $PackageManagement -Version "1.4.3" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource $PackageManagement -Version "1.4.3"
$res1.Name | Should -Be $PackageManagement
$res1.Version | Should -Be "1.4.3"
}
# # Update to module 1.4.4.1 (with incorrect catalog file)
# # Should FAIL to update the module
# It "Update module with incorrect catalog file" -Skip:(!(Get-IsWindows)) {
# Install-PSResource -Name $PackageManagement -Version "1.4.2" -Reinstall -Repository $PSGalleryName -TrustRepository
# Update-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
# $err.Count | Should -Not -Be 0
# $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.UpdatePSResource"
# }
# Update to module 1.4.7 (is authenticode signed and has NO catalog file)
# Should update successfully
It "Install module with no catalog file" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name $PackageManagement -Version "1.4.7" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource $PackageManagement -Version "1.4.7"
$res1.Name | Should -Be $PackageManagement
$res1.Version | Should -Be "1.4.7"
}
# Update script that is signed
# Should update successfully
It "Update script that is authenticode signed" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "Install-VSCode" -Version "1.4.1" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name "Install-VSCode" -Version "1.4.2" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource "Install-VSCode" -Version "1.4.2"
$res1.Name | Should -Be "Install-VSCode"
$res1.Version | Should -Be "1.4.2"
}
# Update script that is not signed
# Should throw
It "Update script that is not signed" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "TestTestScript" -Version "1.0" -Repository $PSGalleryName -TrustRepository
Update-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -Contain "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.UpdatePSResource"
$err[1].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.UpdatePSResource"
}
}