-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathInstallPSResourceContainerRegistryServer.Tests.ps1
More file actions
376 lines (321 loc) · 18 KB
/
InstallPSResourceContainerRegistryServer.Tests.ps1
File metadata and controls
376 lines (321 loc) · 18 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
BeforeAll {
$testModuleName = "test-module"
$testModuleName2 = "test-module2"
$testModuleWith2DigitVersion = "test-2DigitPkg"
$testCamelCaseModuleName = "test-camelCaseModule"
$testCamelCaseScriptName = "test-camelCaseScript"
$testModuleParentName = "test_parent_mod"
$testModuleDependencyName = "test_dependency_mod"
$testScriptName = "test-script"
$ACRRepoName = "ACRRepo"
$ACRRepoUri = "https://psresourcegettest.azurecr.io/"
Get-NewPSResourceRepositoryFile
$usingAzAuth = $env:USINGAZAUTH -eq 'true'
if ($usingAzAuth)
{
Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose
}
else
{
$psCredInfo = New-Object Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo ("SecretStore", "$env:TENANTID")
Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -CredentialInfo $psCredInfo -Verbose
}
}
AfterEach {
Uninstall-PSResource $testModuleName, $testModuleName2, $testCamelCaseModuleName, $testScriptName, $testCamelCaseScriptName, $testModuleWith2DigitVersion -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
$testCases = @{Name="*"; ErrorId="NameContainsWildcard"},
@{Name="Test-mod*"; ErrorId="NameContainsWildcard"},
@{Name="Test?modu","Test[module"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"}
It "Should not install resource with wildcard in name" -TestCases $testCases {
param($Name, $ErrorId)
Install-PSResource -Name $Name -Repository $ACRRepoName -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res = Get-InstalledPSResource $testModuleName
$res | Should -BeNullOrEmpty
}
It "Install specific module resource by name" {
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -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 $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testScriptName
$pkg.Name | Should -BeExactly $testScriptName
$pkg.Version | Should -Be "3.0.0"
$pkg.Type.ToString() | Should -Be "Script"
}
It "Install script resource by name and version" {
Install-PSResource -Name $testScriptName -Version "1.0.0" -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testScriptName
$pkg.Name | Should -Be $testScriptName
$pkg.Version | Should -BeExactly "1.0.0"
}
It "Install resource when version contains different number of digits than the normalized version- 1 digit specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}
It "Install resource when version contains different number of digits than the normalized version- 2 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}
It "Install resource when version contains different number of digits than the normalized version- 3 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}
It "Install resource when version contains different number of digits than the normalized version- 4 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}
It "Install resource where version specified is a prerelease version" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.5-alpha" -Prerelease -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.5"
$res.Prerelease | Should -Be "alpha"
}
It "Install multiple resources by name" {
$pkgNames = @($testModuleName, $testModuleName2)
Install-PSResource -Name $pkgNames -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $pkgNames
$pkg.Name | Should -Be $pkgNames
}
It "Should not install resource given nonexistant name" {
Install-PSResource -Name "NonExistantModule" -Repository $ACRRepoName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
$pkg = Get-InstalledPSResource "NonExistantModule"
$pkg | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
# 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 $ACRRepoName -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 $ACRRepoName -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 $ACRRepoName -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 $ACRRepoName -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)"
{ Install-PSResource -Name $testModuleName -Version $Version -Repository $ACRRepoName -TrustRepository -ErrorAction SilentlyContinue } | Should -Throw -ErrorId "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res = Get-InstalledPSResource $testModuleName
$res | Should -BeNullOrEmpty
}
It "Install resource when given Name, Version '*', should install the latest version" {
Install-PSResource -Name $testModuleName -Version "*" -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install resource with a dependency (should install both parent and dependency)" {
Install-PSResource -Name $testModuleParentName -Repository $ACRRepoName -TrustRepository
$parentPkg = Get-InstalledPSResource $testModuleParentName
$parentPkg.Name | Should -Be $testModuleParentName
$parentPkg.Version | Should -Be "1.0.0"
$childPkg = Get-InstalledPSResource $testModuleDependencyName
$childPkg.Name | Should -Be $testModuleDependencyName
$childPkg.Version | Should -Be "1.0.0"
}
It "Install resource with latest (including prerelease) version given Prerelease parameter" {
Install-PSResource -Name $testModuleName -Prerelease -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.2.5"
$pkg.Prerelease | Should -Be "alpha"
}
It "Install resource via InputObject by piping from Find-PSresource" {
Find-PSResource -Name $testModuleName -Repository $ACRRepoName | Install-PSResource -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install resource with copyright, description and repository source location and validate properties" {
Install-PSResource -Name $testModuleName -Version "3.0.0" -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "3.0.0"
$pkg.Copyright | Should -Be "(c) Anam Navied. All rights reserved."
$pkg.Description | Should -Be "This is a test module, for PSGallery team internal testing. Do not take a dependency on this package. This version contains tags for the package."
$pkg.RepositorySourceLocation | Should -Be $ACRRepoUri
}
# Windows only
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -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 $testModuleName -Repository $ACRRepoName -TrustRepository -Scope AllUsers -Verbose
$pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
}
# Windows only
It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -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 $ACRRepoName -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 $ACRRepoName -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 $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
}
It "Reinstall resource that is already installed with -Reinstall parameter" {
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
Install-PSResource -Name $testModuleName -Repository $ACRRepoName -Reinstall -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install PSResourceInfo object piped in" {
Find-PSResource -Name $testModuleName -Version "1.0.0" -Repository $ACRRepoName | 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 $ACRRepoName -PassThru -TrustRepository
$res.Name | Should -Contain $testModuleName
}
It "Install module with varying case sensitivity" {
Install-PSResource -Name $testCamelCaseModuleName -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testCamelCaseModuleName
$res.Name | Should -BeExactly $testCamelCaseModuleName
$res.Version | Should -Be "1.0.0"
$res.Type.ToString() | Should -Be "Module"
}
It "Install script with varying case sensitivity" {
Install-PSResource -Name $testCamelCaseScriptName -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testCamelCaseScriptName
$res.Name | Should -BeExactly $testCamelCaseScriptName
$res.Version | Should -Be "1.0.0"
$res.Type.ToString() | Should -Be "Script"
}
}
Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidationOnly' {
BeforeAll {
$testModuleName = "TestModule"
$testModuleName2 = "testModuleWithlicense"
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 "0.0.1.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
}
}
Describe 'Test Install-PSResource for MAR Repository' -tags 'CI' {
BeforeAll {
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/");
Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry"
}
AfterAll {
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null);
Unregister-PSResourceRepository -Name "MAR"
}
It "Should find resource given specific Name, Version null" {
try {
$pkg = Install-PSResource -Name "Az.Accounts" -Repository "MAR" -PassThru -TrustRepository -Reinstall
$pkg.Name | Should -Be "Az.Accounts"
$pkg.Version | Should -Be "3.0.4"
}
finally {
if ($pkg) {
Uninstall-PSResource -Name "Az.Accounts" -Version "3.0.4"
}
}
}
}