Skip to content

Commit 109b902

Browse files
authored
Add tests for ADO v2 server (#1539)
1 parent 4b41e62 commit 109b902

File tree

2 files changed

+294
-5
lines changed

2 files changed

+294
-5
lines changed

src/code/V2ServerAPICalls.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ private string FindAllFromTypeEndPoint(bool includePrerelease, bool isSearchingM
809809
{
810810
_cmdletPassedIn.WriteDebug("In V2ServerAPICalls::FindAllFromTypeEndPoint()");
811811
string typeEndpoint = _isPSGalleryRepo && !isSearchingModule ? "/items/psscript" : String.Empty;
812-
string paginationParam = $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000";
812+
string paginationParam = _isPSGalleryRepo ? $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000" : $"&$inlinecount=allpages&$skip={skip}&$top=6000";
813813
// JFrog/Artifactory requires an empty search term to enumerate all packages in the feed
814814
string searchTerm = _isJFrogRepo ? "&searchTerm=''" : "";
815815
var prereleaseFilter = includePrerelease ? "IsAbsoluteLatestVersion&includePrerelease=true" : "IsLatestVersion";
@@ -831,7 +831,7 @@ private string FindTagFromEndpoint(string[] tags, bool includePrerelease, bool i
831831
// type: DSCResource -> just search Modules
832832
// type: Command -> just search Modules
833833
string typeEndpoint = _isPSGalleryRepo && !isSearchingModule ? "/items/psscript" : String.Empty;
834-
string paginationParam = $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000";
834+
string paginationParam = _isPSGalleryRepo ? $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000" : $"&$inlinecount=allpages&$skip={skip}&$top=6000";
835835
// JFrog/Artifactory requires an empty search term to enumerate all packages in the feed
836836
string searchTerm = _isJFrogRepo ? "&searchTerm=''" : "";
837837
var prereleaseFilter = includePrerelease ? "includePrerelease=true&$filter=IsAbsoluteLatestVersion" : "$filter=IsLatestVersion";
@@ -855,7 +855,7 @@ private string FindCommandOrDscResource(string[] tags, bool includePrerelease, b
855855
{
856856
_cmdletPassedIn.WriteDebug("In V2ServerAPICalls::FindCommandOrDscResource()");
857857
// can only find from Modules endpoint
858-
string paginationParam = $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000";
858+
string paginationParam = _isPSGalleryRepo ? $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=6000" : $"&$inlinecount=allpages&$skip={skip}&$top=6000";
859859
var prereleaseFilter = includePrerelease ? "$filter=IsAbsoluteLatestVersion&includePrerelease=true" : "$filter=IsLatestVersion";
860860

861861
var tagPrefix = isSearchingForCommands ? "PSCommand_" : "PSDscResource_";
@@ -884,7 +884,7 @@ private string FindNameGlobbing(string packageName, ResourceType type, bool incl
884884
// https://www.powershellgallery.com/api/v2/Search()?$filter=endswith(Id, 'Get') and startswith(Id, 'PowerShell') and IsLatestVersion (stable)
885885
// https://www.powershellgallery.com/api/v2/Search()?$filter=endswith(Id, 'Get') and IsAbsoluteLatestVersion&includePrerelease=true
886886

887-
string extraParam = $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=100";
887+
string extraParam = _isPSGalleryRepo ? $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=100" : $"&$inlinecount=allpages&$skip={skip}&$top=100";
888888
var prerelease = includePrerelease ? "IsAbsoluteLatestVersion&includePrerelease=true" : "IsLatestVersion";
889889
string nameFilter;
890890

@@ -937,6 +937,16 @@ private string FindNameGlobbing(string packageName, ResourceType type, bool incl
937937
return string.Empty;
938938
}
939939

940+
if (!_isPSGalleryRepo && type != ResourceType.None)
941+
{
942+
errRecord = new ErrorRecord(
943+
new ArgumentException("-Name with wildcards with -Type is not supported for this repository."),
944+
"FindNameGlobbingNotSupportedForRepo",
945+
ErrorCategory.InvalidArgument,
946+
this);
947+
948+
return string.Empty;
949+
}
940950
string typeFilterPart = GetTypeFilterForRequest(type);
941951
var requestUrlV2 = $"{Repository.Uri}/Search()?$filter={nameFilter}{typeFilterPart} and {prerelease}{extraParam}";
942952

@@ -952,12 +962,22 @@ private string FindNameGlobbingWithTag(string packageName, string[] tags, Resour
952962
// https://www.powershellgallery.com/api/v2/Search()?$filter=endswith(Id, 'Get') and startswith(Id, 'PowerShell') and IsLatestVersion (stable)
953963
// https://www.powershellgallery.com/api/v2/Search()?$filter=endswith(Id, 'Get') and IsAbsoluteLatestVersion&includePrerelease=true
954964

955-
string extraParam = $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=100";
965+
string extraParam = _isPSGalleryRepo ? $"&$orderby=Id desc&$inlinecount=allpages&$skip={skip}&$top=100" : $"&$inlinecount=allpages&$skip={skip}&$top=100";
956966
var prerelease = includePrerelease ? "IsAbsoluteLatestVersion&includePrerelease=true" : "IsLatestVersion";
957967
string nameFilter;
958968

959969
var names = packageName.Split(new char[] {'*'}, StringSplitOptions.RemoveEmptyEntries);
960970

971+
if (!_isPSGalleryRepo)
972+
{
973+
errRecord = new ErrorRecord(
974+
new ArgumentException("Name globbing with tags is not supported for V2 server protocol repositories."),
975+
"FindNameGlobbingAndTagFailure",
976+
ErrorCategory.InvalidArgument,
977+
this);
978+
979+
return string.Empty;
980+
}
961981
if (names.Length == 0)
962982
{
963983
errRecord = new ErrorRecord(
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
5+
Import-Module $modPath -Force -Verbose
6+
7+
$psmodulePaths = $env:PSModulePath -split ';'
8+
Write-Verbose -Verbose "Current module search paths: $psmodulePaths"
9+
10+
Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
11+
12+
BeforeAll{
13+
$testModuleName = "test_local_mod"
14+
$ADOV2RepoName = "PSGetTestingPublicFeed"
15+
$ADOV2RepoUri = "https://pkgs.dev.azure.com/powershell/PowerShell/_packaging/powershell-public-test/nuget/v2"
16+
Get-NewPSResourceRepositoryFile
17+
Register-PSResourceRepository -Name $ADOV2RepoName -Uri $ADOV2RepoUri
18+
}
19+
20+
AfterAll {
21+
Get-RevertPSResourceRepositoryFile
22+
}
23+
24+
It "Find resource given specific Name, Version null" {
25+
$res = Find-PSResource -Name $testModuleName -Repository $ADOV2RepoName
26+
$res.Name | Should -Be $testModuleName
27+
$res.Version | Should -Be "5.0.0"
28+
}
29+
30+
It "Should not find resource given nonexistant Name" {
31+
$res = Find-PSResource -Name NonExistantModule -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
32+
$res | Should -BeNullOrEmpty
33+
$err.Count | Should -BeGreaterThan 0
34+
$err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
35+
$res | Should -BeNullOrEmpty
36+
}
37+
38+
It "Find resource(s) given wildcard Name" {
39+
# FindNameGlobbing
40+
$foundScript = $False
41+
$res = Find-PSResource -Name "test_*" -Repository $ADOV2RepoName
42+
$res.Count | Should -BeGreaterThan 1
43+
}
44+
45+
$testCases2 = @{Version="[5.0.0]"; ExpectedVersions=@("5.0.0"); Reason="validate version, exact match"},
46+
@{Version="5.0.0"; ExpectedVersions=@("5.0.0"); Reason="validate version, exact match without bracket syntax"},
47+
@{Version="[1.0.0, 5.0.0]"; ExpectedVersions=@("1.0.0", "3.0.0", "5.0.0"); Reason="validate version, exact range inclusive"},
48+
@{Version="(1.0.0, 5.0.0)"; ExpectedVersions=@("3.0.0"); Reason="validate version, exact range exclusive"},
49+
@{Version="(1.0.0,)"; ExpectedVersions=@("3.0.0", "5.0.0"); Reason="validate version, minimum version exclusive"},
50+
@{Version="[1.0.0,)"; ExpectedVersions=@("1.0.0", "3.0.0", "5.0.0"); Reason="validate version, minimum version inclusive"},
51+
@{Version="(,3.0.0)"; ExpectedVersions=@("1.0.0"); Reason="validate version, maximum version exclusive"},
52+
@{Version="(,3.0.0]"; ExpectedVersions=@("1.0.0", "3.0.0"); Reason="validate version, maximum version inclusive"},
53+
@{Version="[1.0.0, 5.0.0)"; ExpectedVersions=@("1.0.0", "3.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
54+
@{Version="(1.0.0, 5.0.0]"; ExpectedVersions=@("3.0.0", "5.0.0"); Reason="validate version, mixed exclusive minimum and inclusive maximum version"}
55+
56+
It "Find resource when given Name to <Reason> <Version>" -TestCases $testCases2{
57+
# FindVersionGlobbing()
58+
param($Version, $ExpectedVersions)
59+
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $ADOV2RepoName
60+
$res | Should -Not -BeNullOrEmpty
61+
foreach ($item in $res) {
62+
$item.Name | Should -Be $testModuleName
63+
$ExpectedVersions | Should -Contain $item.Version
64+
}
65+
}
66+
67+
It "Find all versions of resource when given specific Name, Version not null --> '*'" {
68+
# FindVersionGlobbing()
69+
$res = Find-PSResource -Name $testModuleName -Version "*" -Repository $ADOV2RepoName
70+
$res | ForEach-Object {
71+
$_.Name | Should -Be $testModuleName
72+
}
73+
74+
$res.Count | Should -BeGreaterOrEqual 1
75+
}
76+
77+
It "Find resource with latest (including prerelease) version given Prerelease parameter" {
78+
# FindName()
79+
# test_module resource's latest version is a prerelease version, before that it has a non-prerelease version
80+
$res = Find-PSResource -Name $testModuleName -Repository $ADOV2RepoName
81+
$res.Version | Should -Be "5.0.0"
82+
83+
$resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $ADOV2RepoName
84+
$resPrerelease.Version | Should -Be "5.2.5"
85+
$resPrerelease.Prerelease | Should -Be "alpha001"
86+
}
87+
88+
It "Find resources, including Prerelease version resources, when given Prerelease parameter" {
89+
# FindVersionGlobbing()
90+
$resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $ADOV2RepoName
91+
$resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $ADOV2RepoName
92+
$resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count
93+
}
94+
95+
<# LATER
96+
It "Find resource and its dependency resources with IncludeDependencies parameter" {
97+
# FindName() with deps
98+
$resWithoutDependencies = Find-PSResource -Name "TestModuleWithDependencyE" -Repository $ADOV2RepoName
99+
$resWithoutDependencies.Name | Should -Be "TestModuleWithDependencyE"
100+
$resWithoutDependencies | Should -HaveCount 1
101+
102+
# TestModuleWithDependencyE has the following dependencies:
103+
# TestModuleWithDependencyC <= 1.0.0.0
104+
# TestModuleWithDependencyB >= 1.0.0.0
105+
# TestModuleWithDependencyD <= 1.0.0.0
106+
107+
$resWithDependencies = Find-PSResource -Name "TestModuleWithDependencyE" -IncludeDependencies -Repository $ADOV2RepoName
108+
$resWithDependencies | Should -HaveCount 4
109+
110+
$foundParentPkgE = $false
111+
$foundDepB = $false
112+
$foundDepBCorrectVersion = $false
113+
$foundDepC = $false
114+
$foundDepCCorrectVersion = $false
115+
$foundDepD = $false
116+
$foundDepDCorrectVersion = $false
117+
foreach ($pkg in $resWithDependencies)
118+
{
119+
if ($pkg.Name -eq "TestModuleWithDependencyE")
120+
{
121+
$foundParentPkgE = $true
122+
}
123+
elseif ($pkg.Name -eq "TestModuleWithDependencyC")
124+
{
125+
$foundDepC = $true
126+
$foundDepCCorrectVersion = [System.Version]$pkg.Version -le [System.Version]"1.0"
127+
}
128+
elseif ($pkg.Name -eq "TestModuleWithDependencyB")
129+
{
130+
$foundDepB = $true
131+
$foundDepBCorrectVersion = [System.Version]$pkg.Version -ge [System.Version]"1.0"
132+
}
133+
elseif ($pkg.Name -eq "TestModuleWithDependencyD")
134+
{
135+
$foundDepD = $true
136+
$foundDepDCorrectVersion = [System.Version]$pkg.Version -le [System.Version]"1.0"
137+
}
138+
}
139+
140+
$foundParentPkgE | Should -Be $true
141+
$foundDepC | Should -Be $true
142+
$foundDepCCorrectVersion | Should -Be $true
143+
$foundDepB | Should -Be $true
144+
$foundDepBCorrectVersion | Should -Be $true
145+
$foundDepD | Should -Be $true
146+
$foundDepDCorrectVersion | Should -Be $true
147+
}
148+
149+
It "find resource of Type script or module from PSGallery, when no Type parameter provided" {
150+
# FindName() script
151+
$resScript = Find-PSResource -Name $testScriptName -Repository $ADOV2RepoName
152+
$resScript.Name | Should -Be $testScriptName
153+
$resScriptType = Out-String -InputObject $resScript.Type
154+
$resScriptType.Replace(",", " ").Split() | Should -Contain "Script"
155+
156+
$resModule = Find-PSResource -Name $testModuleName -Repository $ADOV2RepoName
157+
$resModule.Name | Should -Be $testModuleName
158+
$resModuleType = Out-String -InputObject $resModule.Type
159+
$resModuleType.Replace(",", " ").Split() | Should -Contain "Module"
160+
}
161+
162+
It "find resource of Type Script from PSGallery, when Type Script specified" {
163+
# FindName() Type script
164+
$resScript = Find-PSResource -Name $testScriptName -Repository $ADOV2RepoName -Type "Script"
165+
$resScript.Name | Should -Be $testScriptName
166+
$resScriptType = Out-String -InputObject $resScript.Type
167+
$resScriptType.Replace(",", " ").Split() | Should -Contain "Script"
168+
}
169+
#>
170+
171+
172+
It "Find all resources of Type Module when Type parameter set is used" {
173+
$res = Find-PSResource -Name "test*" -Type Module -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
174+
$res | Should -BeNullOrEmpty
175+
$err.Count | Should -BeGreaterThan 0
176+
$err[0].FullyQualifiedErrorId | Should -BeExactly "FindNameGlobbingNotSupportedForRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
177+
}
178+
179+
It "Find resource that satisfies given Name and Tag property (single tag)" {
180+
# FindNameWithTag()
181+
$requiredTag = "Test"
182+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $ADOV2RepoName
183+
$res.Name | Should -Be $testModuleName
184+
$res.Tags | Should -Contain $requiredTag
185+
}
186+
187+
It "Should not find resource if Name and Tag are not both satisfied (single tag)" {
188+
# FindNameWithTag
189+
$requiredTag = "Windows" # tag "windows" is not present for test_module package
190+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
191+
$res | Should -BeNullOrEmpty
192+
$err.Count | Should -BeGreaterThan 0
193+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
194+
}
195+
196+
It "Find resource that satisfies given Name and Tag property (multiple tags)" {
197+
# FindNameWithTag()
198+
$requiredTags = @("Test", "Tag2")
199+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $ADOV2RepoName
200+
$res.Name | Should -Be $testModuleName
201+
$res.Tags | Should -Contain $requiredTags[0]
202+
$res.Tags | Should -Contain $requiredTags[1]
203+
}
204+
205+
It "Should not find resource if Name and Tag are not both satisfied (multiple tag)" {
206+
# FindNameWithTag
207+
$requiredTags = @("test", "Windows") # tag "windows" is not present for test_module package
208+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
209+
$res | Should -BeNullOrEmpty
210+
$err.Count | Should -BeGreaterThan 0
211+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
212+
}
213+
214+
It "Find all resources that satisfy Name pattern and have specified Tag (single tag)" {
215+
# FindNameGlobbingWithTag()
216+
$requiredTag = "test"
217+
$nameWithWildcard = "test_module*"
218+
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
219+
$res | Should -BeNullOrEmpty
220+
$err.Count | Should -BeGreaterThan 0
221+
$err[0].FullyQualifiedErrorId | Should -BeExactly "FindNameGlobbingAndTagFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
222+
223+
}
224+
225+
It "Should not find resources if both Name pattern and Tags are not satisfied (multiple tags)" {
226+
# FindNameGlobbingWithTag() # tag "windows" is not present for test_module package
227+
$requiredTags = @("Test", "windows")
228+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $ADOV2RepoName -ErrorAction SilentlyContinue
229+
$res | Should -BeNullOrEmpty
230+
}
231+
232+
It "Find resource that satisfies given Name, Version and Tag property (single tag)" {
233+
# FindVersionWithTag()
234+
$requiredTag = "Test"
235+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTag -Repository $ADOV2RepoName
236+
$res.Name | Should -Be $testModuleName
237+
$res.Version | Should -Be "5.0.0"
238+
$res.Tags | Should -Contain $requiredTag
239+
}
240+
241+
It "Should not find resource if Name, Version and Tag property are not all satisfied (single tag)" {
242+
# FindVersionWithTag()
243+
$requiredTag = "windows" # tag "windows" is not present for test_module package
244+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTag -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
245+
$res | Should -BeNullOrEmpty
246+
$err.Count | Should -BeGreaterThan 0
247+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
248+
}
249+
250+
It "Find resource that satisfies given Name, Version and Tag property (multiple tags)" {
251+
# FindVersionWithTag()
252+
$requiredTags = @("Test", "Tag2")
253+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTags -Repository $ADOV2RepoName
254+
$res.Name | Should -Be $testModuleName
255+
$res.Version | Should -Be "5.0.0"
256+
$res.Tags | Should -Contain $requiredTags[0]
257+
$res.Tags | Should -Contain $requiredTags[1]
258+
259+
}
260+
261+
It "Should not find resource if Name, Version and Tag property are not all satisfied (multiple tags)" {
262+
# FindVersionWithTag()
263+
$requiredTags = @("test", "windows")
264+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTags -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
265+
$res | Should -BeNullOrEmpty
266+
$err.Count | Should -BeGreaterThan 0
267+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
268+
}
269+
}

0 commit comments

Comments
 (0)