|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Import-Module "$((Get-Item $psscriptroot).parent)\PSGetTestUtils.psm1" -Force |
| 5 | + |
| 6 | +Describe 'Test HTTP Find-PSResource for Module' { |
| 7 | + |
| 8 | + BeforeAll{ |
| 9 | + $localRepo = "psgettestlocal" |
| 10 | + $testModuleName = "test_local_mod" |
| 11 | + $testModuleName2 = "test_local_mod2" |
| 12 | + $commandName = "cmd1" |
| 13 | + $dscResourceName = "dsc1" |
| 14 | + $cmdName = "PSCommand_$commandName" |
| 15 | + $dscName = "PSDscResource_$dscResourceName" |
| 16 | + $prereleaseLabel = "" |
| 17 | + Get-NewPSResourceRepositoryFile |
| 18 | + Register-LocalRepos |
| 19 | + |
| 20 | + $tags = @("Test", "Tag2", $cmdName, $dscName) |
| 21 | + Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "1.0.0" |
| 22 | + Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "3.0.0" |
| 23 | + Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "5.0.0" $prereleaseLabel $tags |
| 24 | + Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName2 $localRepo "5.0.0" $prereleaseLabel $tags |
| 25 | + |
| 26 | + $prereleaseLabel = "alpha001" |
| 27 | + $params = @{ |
| 28 | + moduleName = $testModuleName |
| 29 | + repoName = $localRepo |
| 30 | + packageVersion = "5.2.5" |
| 31 | + prereleaseLabel = $prereleaseLabel |
| 32 | + tags = $tags |
| 33 | + } |
| 34 | + Get-ModuleResourcePublishedToLocalRepoTestDrive @params |
| 35 | + } |
| 36 | + |
| 37 | + AfterAll { |
| 38 | + Get-RevertPSResourceRepositoryFile |
| 39 | + } |
| 40 | + |
| 41 | + It "find resource given specific Name, Version null" { |
| 42 | + # FindName() |
| 43 | + $res = Find-PSResource -Name $testModuleName -Repository $localRepo |
| 44 | + $res.Name | Should -Be $testModuleName |
| 45 | + $res.Version | Should -Be "5.0.0.0" |
| 46 | + } |
| 47 | + |
| 48 | + It "should not find resource given nonexistant Name" { |
| 49 | + $res = Find-PSResource -Name NonExistantModule -Repository $localRepo |
| 50 | + $res | Should -BeNullOrEmpty |
| 51 | + } |
| 52 | + |
| 53 | + # It "find resource(s) given wildcard Name" { |
| 54 | + # # FindNameGlobbing |
| 55 | + # $res = Find-PSResource -Name "test_local_*" -Repository $localRepo |
| 56 | + # $res.Count | Should -BeGreaterThan 1 |
| 57 | + # } |
| 58 | + |
| 59 | + $testCases2 = @{Version="[5.0.0.0]"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match"}, |
| 60 | + @{Version="5.0.0.0"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match without bracket syntax"}, |
| 61 | + @{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"}, |
| 62 | + @{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("3.0.0.0"); Reason="validate version, exact range exclusive"}, |
| 63 | + @{Version="(1.0.0.0,)"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version exclusive"}, |
| 64 | + @{Version="[1.0.0.0,)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version inclusive"}, |
| 65 | + @{Version="(,3.0.0.0)"; ExpectedVersions=@("1.0.0.0"); Reason="validate version, maximum version exclusive"}, |
| 66 | + @{Version="(,3.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, maximum version inclusive"}, |
| 67 | + @{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"} |
| 68 | + @{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"} |
| 69 | + |
| 70 | + It "find resource when given Name to <Reason> <Version>" -TestCases $testCases2{ |
| 71 | + # FindVersionGlobbing() |
| 72 | + param($Version, $ExpectedVersions) |
| 73 | + $res = Find-PSResource -Name $testModuleName -Version $Version -Repository $localRepo |
| 74 | + foreach ($item in $res) { |
| 75 | + $item.Name | Should -Be $testModuleName |
| 76 | + $ExpectedVersions | Should -Contain $item.Version |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + It "find all versions of resource when given specific Name, Version not null --> '*'" { |
| 81 | + # FindVersionGlobbing() |
| 82 | + $res = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo |
| 83 | + $res | ForEach-Object { |
| 84 | + $_.Name | Should -Be $testModuleName |
| 85 | + } |
| 86 | + |
| 87 | + $res.Count | Should -BeGreaterOrEqual 1 |
| 88 | + } |
| 89 | + |
| 90 | + It "find resource with latest (including prerelease) version given Prerelease parameter" { |
| 91 | + # FindName() |
| 92 | + # test_module resource's latest version is a prerelease version, before that it has a non-prerelease version |
| 93 | + $res = Find-PSResource -Name $testModuleName -Repository $localRepo |
| 94 | + $res.Version | Should -Be "5.0.0.0" |
| 95 | + |
| 96 | + $resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $localRepo |
| 97 | + $resPrerelease.Version | Should -Be "5.2.5.0" |
| 98 | + $resPrerelease.Prerelease | Should -Be "alpha001" |
| 99 | + } |
| 100 | + |
| 101 | + It "find resources, including Prerelease version resources, when given Prerelease parameter" { |
| 102 | + # FindVersionGlobbing() |
| 103 | + $resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo |
| 104 | + $resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo |
| 105 | + $resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count |
| 106 | + } |
| 107 | + |
| 108 | + It "find resource that satisfies given Name and Tag property (single tag)" { |
| 109 | + # FindNameWithTag() |
| 110 | + $requiredTag = "test" |
| 111 | + $res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo |
| 112 | + $res.Name | Should -Be $testModuleName |
| 113 | + $res.Tags | Should -Contain $requiredTag |
| 114 | + } |
| 115 | + |
| 116 | + It "should not find resource if Name and Tag are not both satisfied (single tag)" { |
| 117 | + # FindNameWithTag |
| 118 | + $requiredTag = "Windows" # tag "windows" is not present for test_module package |
| 119 | + $res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo |
| 120 | + $res | Should -BeNullOrEmpty |
| 121 | + } |
| 122 | + |
| 123 | + It "find resource that satisfies given Name and Tag property (multiple tags)" { |
| 124 | + # FindNameWithTag() |
| 125 | + $requiredTags = @("test", "Tag2") |
| 126 | + $res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $localRepo |
| 127 | + $res.Name | Should -Be $testModuleName |
| 128 | + $res.Tags | Should -Contain $requiredTags[0] |
| 129 | + $res.Tags | Should -Contain $requiredTags[1] |
| 130 | + } |
| 131 | + |
| 132 | + It "find all resources that satisfy Name pattern and have specified Tag (single tag)" { |
| 133 | + # FindNameGlobbingWithTag() |
| 134 | + $requiredTag = "test" |
| 135 | + $nameWithWildcard = "test_local_mod*" |
| 136 | + $res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $localRepo |
| 137 | + $res.Count | Should -BeGreaterThan 1 |
| 138 | + foreach ($pkg in $res) |
| 139 | + { |
| 140 | + $pkg.Name | Should -BeLike $nameWithWildcard |
| 141 | + $pkg.Tags | Should -Contain $requiredTag |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + It "should not find resources if both Name pattern and Tags are not satisfied (single tag)" { |
| 146 | + # FindNameGlobbingWithTag() |
| 147 | + $requiredTag = "windows" # tag "windows" is not present for test_module package |
| 148 | + $res = Find-PSResource -Name "test_module*" -Tag $requiredTag -Repository $localRepo |
| 149 | + $res | Should -BeNullOrEmpty |
| 150 | + } |
| 151 | + |
| 152 | + It "find all resources that satisfy Name pattern and have specified Tag (multiple tags)" { |
| 153 | + # FindNameGlobbingWithTag() |
| 154 | + $requiredTags = @("test", "Tag2") |
| 155 | + $nameWithWildcard = "test_local_mod*" |
| 156 | + $res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTags -Repository $localRepo |
| 157 | + $res.Count | Should -BeGreaterThan 1 |
| 158 | + foreach ($pkg in $res) |
| 159 | + { |
| 160 | + $pkg.Name | Should -BeLike $nameWithWildcard |
| 161 | + $pkg.Tags | Should -Contain $requiredTags[0] |
| 162 | + $pkg.Tags | Should -Contain $requiredTags[1] |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + It "find resource that satisfies given Name, Version and Tag property (single tag)" { |
| 167 | + # FindVersionWithTag() |
| 168 | + $requiredTag = "test" |
| 169 | + $res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo |
| 170 | + $res.Name | Should -Be $testModuleName |
| 171 | + $res.Version | Should -Be "5.0.0.0" |
| 172 | + $res.Tags | Should -Contain $requiredTag |
| 173 | + } |
| 174 | + |
| 175 | + It "should not find resource if Name, Version and Tag property are not all satisfied (single tag)" { |
| 176 | + # FindVersionWithTag() |
| 177 | + $requiredTag = "windows" # tag "windows" is not present for test_module package |
| 178 | + $res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo |
| 179 | + $res | Should -BeNullOrEmpty |
| 180 | + } |
| 181 | + |
| 182 | + It "find resource that satisfies given Name, Version and Tag property (multiple tags)" { |
| 183 | + # FindVersionWithTag() |
| 184 | + $requiredTags = @("test", "Tag2") |
| 185 | + $res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $localRepo |
| 186 | + $res.Name | Should -Be $testModuleName |
| 187 | + $res.Version | Should -Be "5.0.0.0" |
| 188 | + $res.Tags | Should -Contain $requiredTags[0] |
| 189 | + $res.Tags | Should -Contain $requiredTags[1] |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + It "find resource given CommandName" { |
| 194 | + $res = Find-PSResource -CommandName $commandName -Repository $localRepo |
| 195 | + foreach ($item in $res) { |
| 196 | + $item.Names | Should -Be $commandName |
| 197 | + $item.ParentResource.Includes.Command | Should -Contain $commandName |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + It "find resource given DscResourceName" { |
| 202 | + $res = Find-PSResource -DscResourceName $dscResourceName -Repository $localRepo |
| 203 | + foreach ($item in $res) { |
| 204 | + $item.Names | Should -Be $dscResourceName |
| 205 | + $item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments