-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFindPSResourceV2Server.Tests.ps1
More file actions
487 lines (419 loc) · 22.2 KB
/
FindPSResourceV2Server.Tests.ps1
File metadata and controls
487 lines (419 loc) · 22.2 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
$psmodulePaths = $env:PSModulePath -split ';'
Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths"
Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
BeforeAll{
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testScriptName = "test_script"
$commandName = "Get-TargetResource"
$dscResourceName = "SystemLocale"
$parentModuleName = "SystemLocaleDsc"
Get-NewPSResourceRepositoryFile
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "find resource given specific Name, Version null" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $PSGalleryName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
}
It "should not find resource given nonexistant Name" {
$res = Find-PSResource -Name NonExistantModule -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
}
It "find resource(s) given wildcard Name" {
# FindNameGlobbing
$foundScript = $False
$res = Find-PSResource -Name "test_*" -Repository $PSGalleryName
$res.Count | Should -BeGreaterThan 1
# should find Module and Script resources
foreach ($item in $res)
{
if ($item.Type -eq "Script")
{
$foundScript = $true
}
}
$foundScript | Should -BeTrue
}
It "find all resources when wildcard only for Name" {
$res = Find-PSResource -Name '*' -Repository $PSGalleryName
$res.Count | Should -BeGreaterThan 0
}
$testCases2 = @{Version="[5.0.0.0]"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match"},
@{Version="5.0.0.0"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("3.0.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,3.0.0.0)"; ExpectedVersions=@("1.0.0.0"); Reason="validate version, maximum version exclusive"},
@{Version="(,3.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
@{Version="(1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, mixed exclusive minimum and inclusive maximum version"}
It "find resource when given Name to <Reason> <Version>" -TestCases $testCases2{
# FindVersionGlobbing()
param($Version, $ExpectedVersions)
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Name | Should -Be $testModuleName
$ExpectedVersions | Should -Contain $item.Version
}
}
It "find all versions of resource when given specific Name, Version not null --> '*'" {
# FindVersionGlobbing()
$res = Find-PSResource -Name $testModuleName -Version "*" -Repository $PSGalleryName
$res | ForEach-Object {
$_.Name | Should -Be $testModuleName
}
$res.Count | Should -BeGreaterOrEqual 1
}
It "find resource with latest (including prerelease) version given Prerelease parameter" {
# FindName()
# test_module resource's latest version is a prerelease version, before that it has a non-prerelease version
$res = Find-PSResource -Name $testModuleName -Repository $PSGalleryName
$res.Version | Should -Be "5.0.0.0"
$resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName
$resPrerelease.Version | Should -Be "5.2.5"
$resPrerelease.Prerelease | Should -Be "alpha001"
}
It "find resources, including Prerelease version resources, when given Prerelease parameter" {
# FindVersionGlobbing()
$resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $PSGalleryName
$resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $PSGalleryName
$resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count
}
It "find resource and its dependency resources with IncludeDependencies parameter" {
# FindName() with deps
$resWithoutDependencies = Find-PSResource -Name "TestModuleWithDependencyE" -Repository $PSGalleryName
$resWithoutDependencies.Name | Should -Be "TestModuleWithDependencyE"
$resWithoutDependencies | Should -HaveCount 1
# TestModuleWithDependencyE has the following dependencies:
# TestModuleWithDependencyC <= 1.0.0.0
# TestModuleWithDependencyB >= 1.0.0.0
# TestModuleWithDependencyD <= 1.0.0.0
$resWithDependencies = Find-PSResource -Name "TestModuleWithDependencyE" -IncludeDependencies -Repository $PSGalleryName
$resWithDependencies | Should -HaveCount 4
$foundParentPkgE = $false
$foundDepB = $false
$foundDepBCorrectVersion = $false
$foundDepC = $false
$foundDepCCorrectVersion = $false
$foundDepD = $false
$foundDepDCorrectVersion = $false
foreach ($pkg in $resWithDependencies)
{
if ($pkg.Name -eq "TestModuleWithDependencyE")
{
$foundParentPkgE = $true
}
elseif ($pkg.Name -eq "TestModuleWithDependencyC")
{
$foundDepC = $true
$foundDepCCorrectVersion = [System.Version]$pkg.Version -le [System.Version]"1.0"
}
elseif ($pkg.Name -eq "TestModuleWithDependencyB")
{
$foundDepB = $true
$foundDepBCorrectVersion = [System.Version]$pkg.Version -ge [System.Version]"1.0"
}
elseif ($pkg.Name -eq "TestModuleWithDependencyD")
{
$foundDepD = $true
$foundDepDCorrectVersion = [System.Version]$pkg.Version -le [System.Version]"1.0"
}
}
$foundParentPkgE | Should -Be $true
$foundDepC | Should -Be $true
$foundDepCCorrectVersion | Should -Be $true
$foundDepB | Should -Be $true
$foundDepBCorrectVersion | Should -Be $true
$foundDepD | Should -Be $true
$foundDepDCorrectVersion | Should -Be $true
}
It "find resource of Type script or module from PSGallery, when no Type parameter provided" {
# FindName() script
$resScript = Find-PSResource -Name $testScriptName -Repository $PSGalleryName
$resScript.Name | Should -Be $testScriptName
$resScriptType = Out-String -InputObject $resScript.Type
$resScriptType.Replace(",", " ").Split() | Should -Contain "Script"
$resModule = Find-PSResource -Name $testModuleName -Repository $PSGalleryName
$resModule.Name | Should -Be $testModuleName
$resModuleType = Out-String -InputObject $resModule.Type
$resModuleType.Replace(",", " ").Split() | Should -Contain "Module"
}
It "find resource of Type Script from PSGallery, when Type Script specified" {
# FindName() Type script
$resScript = Find-PSResource -Name $testScriptName -Repository $PSGalleryName -Type "Script"
$resScript.Name | Should -Be $testScriptName
$resScriptType = Out-String -InputObject $resScript.Type
$resScriptType.Replace(",", " ").Split() | Should -Contain "Script"
}
It "find all resources of Type Module when Type parameter set is used" {
$foundScript = $False
$res = Find-PSResource -Name "test*" -Type Module -Repository $PSGalleryName
$res.Count | Should -BeGreaterThan 1
foreach ($item in $res) {
if ($item.Type -eq "Script")
{
$foundScript = $True
}
}
$foundScript | Should -Be $False
}
# It "find resources only with Tag parameter" {
# $resWithEitherExpectedTag = @("NetworkingDsc", "DSCR_FileContent", "SS.PowerShell")
# $res = Find-PSResource -Name "NetworkingDsc", "HPCMSL", "DSCR_FileContent", "SS.PowerShell", "PSResourceGet" -Tag "Dsc", "json" -Repository $PSGalleryName
# foreach ($item in $res) {
# $resWithEitherExpectedTag | Should -Contain $item.Name
# }
# }
It "find resource that satisfies given Name and Tag property (single tag)" {
# FindNameWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $PSGalleryName
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTag
}
It "should not find resource if Name and Tag are not both satisfied (single tag)" {
# FindNameWithTag
$requiredTag = "Windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
It "find resource that satisfies given Name and Tag property (multiple tags)" {
# FindNameWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $PSGalleryName
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]
}
It "should not find resource if Name and Tag are not both satisfied (multiple tag)" {
# FindNameWithTag
$requiredTags = @("test", "Windows") # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
It "find all resources that satisfy Name pattern and have specified Tag (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "test"
$nameWithWildcard = "test_module*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $PSGalleryName
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain $requiredTag
}
}
It "should not find resources if both Name pattern and Tags are not satisfied (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name "test_module*" -Tag $requiredTag -Repository $PSGalleryName
$res | Should -BeNullOrEmpty
}
It "find all resources that satisfy Name pattern and have specified Tag (multiple tags)" {
# FindNameGlobbingWithTag()
$requiredTags = @("test", "Tag2")
$nameWithWildcard = "test_module*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTags -Repository $PSGalleryName
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain $requiredTags[0]
$pkg.Tags | Should -Contain $requiredTags[1]
}
}
It "should not find resources if both Name pattern and Tags are not satisfied (multiple tags)" {
# FindNameGlobbingWithTag() # tag "windows" is not present for test_module package
$requiredTags = @("test", "windows")
$res = Find-PSResource -Name "test_module*" -Tag $requiredTags -Repository $PSGalleryName
$res | Should -BeNullOrEmpty
}
It "find resource that satisfies given Name, Version and Tag property (single tag)" {
# FindVersionWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $PSGalleryName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
$res.Tags | Should -Contain $requiredTag
}
It "should not find resource if Name, Version and Tag property are not all satisfied (single tag)" {
# FindVersionWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
It "find resource that satisfies given Name, Version and Tag property (multiple tags)" {
# FindVersionWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $PSGalleryName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]
}
It "should not find resource if Name, Version and Tag property are not all satisfied (multiple tags)" {
# FindVersionWithTag()
$requiredTags = @("test", "windows")
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
# It "find all resources with specified tag given Tag property" {
# # FindTag()
# $foundTestModule = $False
# $foundTestScript = $False
# $tagToFind = "Tag2"
# $res = Find-PSResource -Tag $tagToFind -Repository $PSGalleryName
# foreach ($item in $res) {
# $item.Tags -contains $tagToFind | Should -Be $True
# if ($item.Name -eq $testModuleName)
# {
# $foundTestModule = $True
# }
# if ($item.Name -eq $testScriptName)
# {
# $foundTestScript = $True
# }
# }
# $foundTestModule | Should -Be $True
# $foundTestScript | Should -Be $True
# }
It "find all resources with specified tag given Tag property, with and without Prerelease property" {
$tagToFind = "MyPSTag"
$res = Find-PSResource -Tag $tagToFind -Repository $PSGalleryName
$res | Should -HaveCount 1
$res = Find-PSResource -Tag $tagToFind -Repository $PSGalleryName -Prerelease
$res | Should -HaveCount 2
}
It "find resource given CommandName" {
$res = Find-PSResource -CommandName $commandName -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Names | Should -Be $commandName
$item.ParentResource.Includes.Command | Should -Contain $commandName
}
}
It "find resource given DscResourceName" {
$res = Find-PSResource -DscResourceName $dscResourceName -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Names | Should -Be $dscResourceName
$item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName
}
}
It "find resource, but only show listed versions" {
# testmodule99 version 1.0.0-beta1 is unlisted
$res = Find-PSResource -Name "testmodule99" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Version.ToString() + $item.Prerelease | Should -Not -Be "1.0.0-beta1"
}
}
It "find all resources within a version range, including prereleases" {
$res = Find-PSResource -Name "PSReadLine" -Version "(2.0,2.1)" -Prerelease -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Count | Should -BeGreaterOrEqual 7
}
It "find a specific version using NuGet versioning bracket syntax" {
$res = Find-PSResource -Name $testModuleName -Version "[3.0.0,3.0.0]" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "3.0.0.0"
}
It "not find resource and error handle when repository's ApiVersion is ApiVersion.unknown" {
Register-PSResourceRepository -Name "UnknownTypeRepo" -Uri "https://org.MyCompany.com/repository/shared-feed/" -Trusted
$repo = Get-PSResourceRepository -Name "UnknownTypeRepo"
$repo.ApiVersion | Should -Be "unknown"
$res = Find-PSResource -Name "MyPackage" -Repository "UnknownTypeRepo" -ErrorAction SilentlyContinue -ErrorVariable err
$err | Should -Not -BeNullOrEmpty
$err[0].FullyQualifiedErrorId | Should -BeExactly "RepositoryApiVersionUnknown,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
Unregister-PSResourceRepository -Name "UnknownTypeRepo"
}
It "find a module that does not exist in the highest priority repository, but does exist in a lower priority repository" {
$res = Find-PSResource -Name "NewpsGetTestModule" -ErrorVariable err
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "2.1.0"
$err.Count | Should -Be 0
}
It "find should not return a module that has all unlisted versions, given full name and no version (i.e non wildcard name)" {
# FindName() scenario
# 'test_completelyunlisted' only has version 0.0.1, which is unlisted
$res = Find-PSResource -Name "test_completelyunlisted" -Repository $PSGalleryName -ErrorVariable err
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
It "find should return a module version even if all versions are unlisted, given full name and version (i.e non wildcard name)" {
# FindVersion() scenario
# test_completelyunlisted has 1 version, 0.0.1, which is unlisted
$res = Find-PSResource -Name "test_completelyunlisted" -Version "0.0.1" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "0.0.1"
}
It "find should return an unlisted module, where the module has a mix of listed and unlisted versions, given full name and version (i.e non wildcard name)" {
# FindVersion scenario
# 'test_unlisted' version 0.0.3 is unlisted
$res = Find-PSResource -Name "test_unlisted" -Version "0.0.3" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "0.0.3"
}
It "find should not return an unlisted module with it was requested with wildcards in the name" {
# FindNameGlobbing() scenario
# 'test_completelyunlisted' has all unlisted versions -> should not be returned
# whereas 'test_unlisted' has a listed verison and 'test_notunlisted' has all listed versions -> should be returned
$res = Find-PSResource -Name "test_*unlisted" -Repository $PSGalleryName
$res.Count | Should -Be 2
$res.Name | Should -Contain 'test_unlisted'
$res.Name | Should -Contain 'test_notunlisted'
}
}
Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidationOnly' {
BeforeAll{
$PSGalleryName = Get-PSGalleryName
$testModuleName = "MicrosoftPowerBIMgmt"
Get-NewPSResourceRepositoryFile
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "find resource given CommandName" {
$res = Find-PSResource -Name $testModuleName -Repository $PSGalleryName -Type Module
$res.Name | Should -Be $testModuleName
}
It "find should not duplicate dependencies found" {
$res = Find-PSResource -Name "Az" -IncludeDependencies -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$foundPkgs = [System.Collections.Generic.HashSet[String]]::new()
$duplicatePkgsFound = $false
foreach ($item in $res)
{
if ($foundPkgs.Contains($item.Name))
{
$duplicatePkgsFound = $true
break
}
$foundPkgs.Add($item.Name)
}
$duplicatePkgsFound | Should -BeFalse
}
}