-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathInstallPSResourceV3Server.Tests.ps1
More file actions
460 lines (381 loc) · 20.1 KB
/
InstallPSResourceV3Server.Tests.ps1
File metadata and controls
460 lines (381 loc) · 20.1 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
Param()
$ProgressPreference = 'SilentlyContinue'
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
$psmodulePaths = $env:PSModulePath -split ';'
Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths"
Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' {
BeforeAll {
$NuGetGalleryName = Get-NuGetGalleryName
$NuGetGalleryUri = Get-NuGetGalleryLocation
$testModuleName = 'test_module'
$testModuleName2 = 'test_module2'
$testScriptName = 'test_script'
$PackageManagement = 'PackageManagement'
$RequiredResourceJSONFileName = 'TestRequiredResourceFile.json'
$RequiredResourcePSD1FileName = 'TestRequiredResourceFile.psd1'
Get-NewPSResourceRepositoryFile
Register-LocalRepos
}
AfterEach {
Uninstall-PSResource 'test_module', 'test_module2', 'test_script', 'TestModule99', 'test_module_withlicense', 'TestFindModule', 'PackageManagement', `
'TestModuleWithDependencyE', 'TestModuleWithDependencyC', 'TestModuleWithDependencyB', 'TestModuleWithDependencyD' -SkipDependencyCheck -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
$testCases = [hashtable[]](
@{Name='*'; ErrorId='NameContainsWildcard'},
@{Name='Test_Module*'; ErrorId='NameContainsWildcard'},
@{Name='Test?Module','Test[Module'; ErrorId='ErrorFilteringNamesForUnsupportedWildcards'}
)
It 'Should not install resource with wildcard in name' -TestCases $testCases {
param($Name, $ErrorId)
Install-PSResource -Name $Name -Repository $NuGetGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
}
It 'Install specific module resource by name' {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
}
It 'Install specific script resource by name' {
Install-PSResource -Name $testScriptName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testScriptName
$pkg.Name | Should -Be $testScriptName
$pkg.Version | Should -Be '3.5.0'
}
It 'Install multiple resources by name' {
$pkgNames = @($testModuleName, $testModuleName2)
Install-PSResource -Name $pkgNames -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $pkgNames
$pkg.Name | Should -Be $pkgNames
}
It 'Should not install resource given nonexistent name' {
Install-PSResource -Name 'NonExistentModule' -Repository $NuGetGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
$pkg = Get-InstalledPSResource 'NonExistentModule' -ErrorAction SilentlyContinue
$pkg.Name | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly 'InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource'
}
It 'Install module using -WhatIf, should not install the module' {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -WhatIf
$? | Should -BeTrue
$res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
}
# Do some version testing, but Find-PSResource should be doing thorough testing
It 'Should install resource given name and exact version' {
Install-PSResource -Name $testModuleName -Version '1.0.0' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '1.0.0'
}
It 'Should install resource given name and exact version with bracket syntax' {
Install-PSResource -Name $testModuleName -Version '[1.0.0]' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '1.0.0'
}
It 'Should install resource given name and exact range inclusive [1.0.0, 5.0.0]' {
Install-PSResource -Name $testModuleName -Version '[1.0.0, 5.0.0]' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
}
It 'Should install resource given name and exact range exclusive (1.0.0, 5.0.0)' {
Install-PSResource -Name $testModuleName -Version '(1.0.0, 5.0.0)' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '3.0.0'
}
# TODO: Update this test and others like it that use try/catch blocks instead of Should -Throw
It 'Should not install resource with incorrectly formatted version such as exclusive version (1.0.0.0)' {
$Version = '(1.0.0.0)'
try {
Install-PSResource -Name $testModuleName -Version $Version -Repository $NuGetGalleryName -TrustRepository -ErrorAction SilentlyContinue
}
catch {
}
$Error[0].FullyQualifiedErrorId | Should -Be 'IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource'
$res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
}
It "Install resource when given Name, Version '*', should install the latest version" {
Install-PSResource -Name $testModuleName -Version '*' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
}
It 'Install resource with latest (including prerelease) version given Prerelease parameter' {
Install-PSResource -Name $testModuleName -Prerelease -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.2.5'
$pkg.Prerelease | Should -Be 'alpha001'
}
It 'Install resource via InputObject by piping from Find-PSresource' {
Find-PSResource -Name $testModuleName -Repository $NuGetGalleryName | Install-PSResource -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
}
It 'Install resource under specified in PSModulePath' {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
($env:PSModulePath).Contains($pkg.InstalledLocation)
}
It 'Install resource with companyname and repository source location and validate properties' {
Install-PSResource -Name $testModuleName -Version '5.2.5-alpha001' -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Version | Should -Be '5.2.5'
$pkg.Prerelease | Should -Be 'alpha001'
$pkg.CompanyName | Should -Be 'Anam Navied'
# Broken now, tracked in issue
# $pkg.Copyright | Should -Be "(c) Anam Navied. All rights reserved."
$pkg.RepositorySourceLocation | Should -Be $NuGetGalleryUri
}
# Windows only
It 'Install resource under CurrentUser scope - Windows only' -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -Scope CurrentUser
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains('Documents') | Should -Be $true
}
# Windows only
It 'Install resource under AllUsers scope - Windows only' -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
Install-PSResource -Name 'testmodule99' -Repository $NuGetGalleryName -TrustRepository -Scope AllUsers -Verbose
$pkg = Get-Module 'testmodule99' -ListAvailable
$pkg.Name | Should -Be 'testmodule99'
$pkg.Path.ToString().Contains('Program Files')
}
# Windows only
It 'Install resource under no specified scope - Windows only' -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains('Documents') | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It 'Install resource under CurrentUser scope - Unix only' -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -Scope CurrentUser
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It 'Install resource under no specified scope - Unix only' -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
}
It 'Should not install resource that is already installed' {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
}
It 'Reinstall resource that is already installed with -Reinstall parameter' {
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -Reinstall -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be '5.0.0'
}
# It "Restore resource after reinstall fails" {
# Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository
# $pkg = Get-InstalledPSResource $testModuleName
# $pkg.Name | Should -Contain $testModuleName
# $pkg.Version | Should -Contain "5.0.0"
# $resourcePath = Split-Path -Path $pkg.InstalledLocation -Parent
# $resourceFiles = Get-ChildItem -Path $resourcePath -Recurse
# # Lock resource file to prevent reinstall from succeeding.
# $fs = [System.IO.File]::Open($resourceFiles[0].FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
# try
# {
# # Reinstall of resource should fail with one of its files locked.
# Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -TrustRepository -Reinstall -ErrorVariable ev -ErrorAction Silent
# $ev.FullyQualifiedErrorId | Should -BeExactly 'InstallPackageFailed,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource'
# }
# finally
# {
# $fs.Close()
# }
# # Verify that resource module has been restored.
# (Get-ChildItem -Path $resourcePath -Recurse).Count | Should -BeExactly $resourceFiles.Count
# }
It 'Install resource that requires accept license with -AcceptLicense flag' {
Install-PSResource -Name 'test_module_withlicense' -Repository $NuGetGalleryName -AcceptLicense
$pkg = Get-InstalledPSResource 'test_module_withlicense'
$pkg.Name | Should -Be 'test_module_withlicense'
$pkg.Version | Should -Be '1.0.0'
}
It 'Install PSResourceInfo object piped in' {
Find-PSResource -Name $testModuleName -Version '1.0.0.0' -Repository $NuGetGalleryName | Install-PSResource -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be '1.0.0'
}
It 'Install module using -PassThru' {
$res = Install-PSResource -Name $testModuleName -Repository $NuGetGalleryName -PassThru -TrustRepository
$res.Name | Should -Contain $testModuleName
}
It 'Install modules using -RequiredResource with hashtable' {
$rrHash = @{
test_module = @{
version = '[1.0.0,5.0.0)'
repository = $NuGetGalleryName
}
test_module2 = @{
version = '[1.0.0,5.0.0]'
repository = $NuGetGalleryName
prerelease = 'true'
}
TestModule99 = @{
repository = $NuGetGalleryName
}
}
Install-PSResource -RequiredResource $rrHash -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be '3.0.0'
$res2 = Get-InstalledPSResource $testModuleName2
$res2.Name | Should -Be $testModuleName2
$res2.Version | Should -Be '5.0.0'
$res3 = Get-InstalledPSResource 'TestModule99'
$res3.Name | Should -Be 'TestModule99'
$res3.Version | Should -Be '0.0.93'
}
It 'Install module using -RequiredResource with TrustRepository in hashtable' {
# This test verifies that TrustRepository specified in -RequiredResource hashtable is respected
Install-PSResource -RequiredResource @{
'TestModule99' = @{
'repository' = $NuGetGalleryName
'trustrepository' = 'true'
}
}
$res = Get-InstalledPSResource -Name 'TestModule99'
$res.Name | Should -Be 'TestModule99'
$res.Version | Should -Be '0.0.93'
}
It 'Install modules using -RequiredResource with JSON string' {
$rrJSON = "{
'test_module': {
'version': '[1.0.0,5.0.0)',
'repository': 'NuGetGallery'
},
'test_module2': {
'version': '[1.0.0,5.0.0]',
'repository': 'PSGallery',
'prerelease': 'true'
},
'TestModule99': {
'repository': 'NuGetGallery'
}
}"
Install-PSResource -RequiredResource $rrJSON -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be '3.0.0'
$res2 = Get-InstalledPSResource $testModuleName2
$res2.Name | Should -Be $testModuleName2
$res2.Version | Should -Be '5.0.0.0'
$res3 = Get-InstalledPSResource 'testModule99'
$res3.Name | Should -Be 'testModule99'
$res3.Version | Should -Be '0.0.93'
}
It 'Install modules using -RequiredResourceFile with PSD1 file' {
$rrFilePSD1 = "$psscriptroot/../$RequiredResourcePSD1FileName"
Install-PSResource -RequiredResourceFile $rrFilePSD1 -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be '3.0.0.0'
$res2 = Get-InstalledPSResource $testModuleName2 -Version '2.5.0-beta'
$res2.Name | Should -Be $testModuleName2
$res2.Version | Should -Be '2.5.0'
$res2.Prerelease | Should -Be 'beta'
$res3 = Get-InstalledPSResource 'testModule99'
$res3.Name | Should -Be 'testModule99'
$res3.Version | Should -Be '0.0.93'
}
It 'Install modules using -RequiredResourceFile with JSON file' {
$rrFileJSON = "$psscriptroot/../$RequiredResourceJSONFileName"
Install-PSResource -RequiredResourceFile $rrFileJSON -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be '3.0.0.0'
$res2 = Get-InstalledPSResource $testModuleName2 -Version '2.5.0-beta'
$res2.Name | Should -Be $testModuleName2
$res2.Version | Should -Be '2.5.0'
$res2.Prerelease | Should -Be 'beta'
$res3 = Get-InstalledPSResource 'testModule99'
$res3.Name | Should -Be 'testModule99'
$res3.Version | Should -Be '0.0.93'
}
It "Install module and its dependencies" {
$res = Install-PSResource 'TestModuleWithDependencyE' -Repository $NuGetGalleryName -TrustRepository -PassThru
$res.Length | Should -Be 4
}
}
Describe 'Test Install-PSResource for V3Server scenarios - Manual Validation' -tags 'ManualValidationOnly' {
BeforeAll {
$testModuleName = 'TestModule'
$testModuleName2 = 'test_module_withlicense'
Get-NewPSResourceRepositoryFile
Register-LocalRepos
}
AfterEach {
Uninstall-PSResource $testModuleName, $testModuleName2 -SkipDependencyCheck -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
# Unix only manual test
# Expected path should be similar to: '/usr/local/share/powershell/Modules'
It 'Install resource under AllUsers scope - Unix only' -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers
$pkg = Get-Module $testModuleName -ListAvailable
$pkg.Name | Should -Be $testModuleName
$pkg.Path.Contains('/usr/') | Should -Be $true
}
# This needs to be manually tested due to prompt
It 'Install resource that requires accept license without -AcceptLicense flag' {
Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName
$pkg = Get-InstalledPSResource $testModuleName2
$pkg.Name | Should -Be $testModuleName2
$pkg.Version | Should -Be '1.0.0'
}
# This needs to be manually tested due to prompt
It "Install resource should prompt 'trust repository' if repository is not trusted" {
Set-PSResourceRepository PoshTestGallery -Trusted:$false
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Confirm:$false
$pkg = Get-Module $testModuleName -ListAvailable
$pkg.Name | Should -Be $testModuleName
Set-PSResourceRepository PoshTestGallery -Trusted
}
It "Install package from NuGetGallery that has outer and inner 'items' elements only and has outer items element array length greater than 1" {
$res = Install-PSResource -Name 'Microsoft.Extensions.DependencyInjection' -Repository $NuGetGalleryName -TrustRepository -PassThru -ErrorVariable err -ErrorAction SilentlyContinue
$err | Should -HaveCount 0
$res.Name | Should -Be 'Microsoft.Extensions.DependencyInjection'
}
It "Install package from NuGetGallery that has outer 'items', '@id' and inner 'items' elements" {
$res = Install-PSResource -Name 'MsgReader' -Repository $NuGetGalleryName -TrustRepository -PassThru -ErrorVariable err -ErrorAction SilentlyContinue
$err | Should -HaveCount 0
$res.Name | Should -Be 'MsgReader'
}
}